Commit 77aaeb27 authored by Fred Chasen's avatar Fred Chasen

spaces -> tabs

parent f7ba5416
This diff is collapsed.
......@@ -24,6 +24,7 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"chalk": "^2.4.1",
"eslint": "^5.9.0",
"express": "^4.16.3",
"ghostscript4js": "^3.0.0",
"jest": "^23.4.1",
......@@ -48,7 +49,8 @@
"compile": "./node_modules/.bin/babel src/ -d lib/",
"pretest": "npm run build",
"prepublishOnly": "npm run build && npm run compile && npm run legacy",
"watch": "./node_modules/.bin/rollup -w -c"
"watch": "./node_modules/.bin/rollup -w -c",
"lint": "./node_modules/.bin/eslint -c .eslintrc.js src; exit 0"
},
"repository": {
"type": "git",
......
This diff is collapsed.
This diff is collapsed.
......@@ -6,22 +6,22 @@ import htmlparser2 from 'htmlparser2';
*/
class Parser {
constructor(content, cb) {
// this.text = text;
// this.nodes = [];
constructor(content, cb) {
// this.text = text;
// this.nodes = [];
this.refs = {};
this.sections = [];
if (content && content.nodeType) {
// handle dom
} else if(typeof content === "object") {
this.add(content);
} else if (typeof content === "string") {
this.dom = this.parse(content);
}
}
parse(data) {
this.sections = [];
if (content && content.nodeType) {
// handle dom
} else if(typeof content === "object") {
this.add(content);
} else if (typeof content === "string") {
this.dom = this.parse(content);
}
}
parse(data) {
let options = {};
let process = (node) => {
let uuid = this.uuid();
......@@ -36,30 +36,30 @@ class Parser {
node.dataset.text = node.data.length;
}
if (node.name === "section") {
this.sections.push(node);
}
if (node.name === "section") {
this.sections.push(node);
}
}
let handler = new htmlparser2.DomHandler(undefined, options, process.bind(this));
new htmlparser2.Parser(handler, options).end(data);
return handler.dom;
}
}
add(contents) {
this.dom = [contents];
add(contents) {
this.dom = [contents];
let walker = this.walk(this.dom[0]);
let next, done, node;
while (!done) {
next = walker.next();
node = next.value;
done = next.done;
let walker = this.walk(this.dom[0]);
let next, done, node;
while (!done) {
next = walker.next();
node = next.value;
done = next.done;
if (node && node.dataset && node.dataset.ref) {
this.refs[node.dataset.ref] = node;
}
}
}
if (node && node.dataset && node.dataset.ref) {
this.refs[node.dataset.ref] = node;
}
}
}
find(ref) {
return this.refs[ref];
......@@ -87,60 +87,60 @@ class Parser {
}
}
// isWrapper(element) {
// return wrappersRegex.test(element.nodeName);
// }
isText(node) {
return node.name === "tag";
}
isElement(node) {
return node.name === "text";
}
hasChildren(node) {
return node.children && node.children.length;
}
after(node) {
let after = node;
if (after.next) {
after = after.next;
} else {
while (after) {
after = after.parent;
if (after && after.next) {
after = after.next;
break;
}
}
}
return after;
}
/**
// isWrapper(element) {
// return wrappersRegex.test(element.nodeName);
// }
isText(node) {
return node.name === "tag";
}
isElement(node) {
return node.name === "text";
}
hasChildren(node) {
return node.children && node.children.length;
}
after(node) {
let after = node;
if (after.next) {
after = after.next;
} else {
while (after) {
after = after.parent;
if (after && after.next) {
after = after.next;
break;
}
}
}
return after;
}
/**
* Generates a UUID
* based on: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
* @returns {string} uuid
*/
uuid() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
destroy() {
this.refs = undefined;
uuid() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
destroy() {
this.refs = undefined;
this.dom = undefined;
}
}
}
export default Parser;
......@@ -49,8 +49,8 @@ class ContentParser {
var treeWalker = document.createTreeWalker(
content,
NodeFilter.SHOW_ELEMENT,
{ acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
false
{ acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
false
);
let node;
......
......@@ -5,10 +5,10 @@ import Handler from './modules/handler';
import { registerHandlers, initializeHandlers } from './utils/handlers';
export {
Chunker,
Polisher,
Previewer,
Handler,
registerHandlers,
initializeHandlers
Chunker,
Polisher,
Previewer,
Handler,
registerHandlers,
initializeHandlers
};
......@@ -74,7 +74,7 @@ class TargetCounters extends Handler {
}
afterPageLayout(fragment, page, breakToken, chunker) {
Object.keys(this.counterTargets).forEach((name) => {
Object.keys(this.counterTargets).forEach((name) => {
let target = this.counterTargets[name];
let split = target.selector.split("::");
let query = split[0];
......
......@@ -130,7 +130,7 @@ class AtPage extends Handler {
let orientation = this.pages["*"].orientation;
if ((width && height) &&
(this.width !== width || this.height !== height)) {
(this.width !== width || this.height !== height)) {
this.width = width;
this.height = height;
this.format = format;
......
......@@ -5,19 +5,19 @@ class Lists extends Handler {
super(chunker, polisher, caller);
}
afterParsed(content) {
const orderedLists = content.querySelectorAll('ol');
const orderedLists = content.querySelectorAll('ol');
for (var list of orderedLists) {
this.addDataNumbers(list);
}
}
for (var list of orderedLists) {
this.addDataNumbers(list);
}
}
afterPageLayout(pageElement, page, breakToken, chunker) {
var orderedLists = pageElement.getElementsByTagName('ol');
for (var list of orderedLists) {
list.start = list.firstElementChild.dataset.itemNum;
}
}
var orderedLists = pageElement.getElementsByTagName('ol');
for (var list of orderedLists) {
list.start = list.firstElementChild.dataset.itemNum;
}
}
addDataNumbers(list) {
let items = list.children;
......
......@@ -34,7 +34,7 @@ class Splits extends Handler {
});
}
handleAlignment(node) {
handleAlignment(node) {
let styles = window.getComputedStyle(node);
let align = styles["text-align"];
let alignLast = styles["text-align-last"];
......
This diff is collapsed.
......@@ -336,87 +336,87 @@ export function *letters(wordRange) {
}
export function isContainer(node) {
let container;
if (typeof node.tagName === "undefined") {
return true;
}
if (node.style.display === "none") {
return false;
}
switch (node.tagName) {
// Inline
case "A":
case "ABBR":
case "ACRONYM":
case "B":
case "BDO":
case "BIG":
case "BR":
case "BUTTON":
case "CITE":
case "CODE":
case "DFN":
case "EM":
case "I":
case "IMG":
case "INPUT":
case "KBD":
case "LABEL":
case "MAP":
case "OBJECT":
case "Q":
case "SAMP":
case "SCRIPT":
case "SELECT":
case "SMALL":
case "SPAN":
case "STRONG":
case "SUB":
case "SUP":
case "TEXTAREA":
case "TIME":
case "TT":
case "VAR":
// Content
case "P":
case "H1":
case "H2":
case "H3":
case "H4":
case "H5":
case "H6":
case "FIGCAPTION":
case "BLOCKQUOTE":
case "PRE":
case "LI":
case "TR":
case "DT":
case "DD":
case "VIDEO":
case "CANVAS":
container = false;
break;
default:
container = true;
}
return container;
let container;
if (typeof node.tagName === "undefined") {
return true;
}
if (node.style.display === "none") {
return false;
}
switch (node.tagName) {
// Inline
case "A":
case "ABBR":
case "ACRONYM":
case "B":
case "BDO":
case "BIG":
case "BR":
case "BUTTON":
case "CITE":
case "CODE":
case "DFN":
case "EM":
case "I":
case "IMG":
case "INPUT":
case "KBD":
case "LABEL":
case "MAP":
case "OBJECT":
case "Q":
case "SAMP":
case "SCRIPT":
case "SELECT":
case "SMALL":
case "SPAN":
case "STRONG":
case "SUB":
case "SUP":
case "TEXTAREA":
case "TIME":
case "TT":
case "VAR":
// Content
case "P":
case "H1":
case "H2":
case "H3":
case "H4":
case "H5":
case "H6":
case "FIGCAPTION":
case "BLOCKQUOTE":
case "PRE":
case "LI":
case "TR":
case "DT":
case "DD":
case "VIDEO":
case "CANVAS":
container = false;
break;
default:
container = true;
}
return container;
}
export function cloneNode(n, deep=false) {
return n.cloneNode(deep);
return n.cloneNode(deep);
}
export function findElement(node, doc) {
const ref = node.getAttribute("data-ref");
return findRef(ref, doc);
const ref = node.getAttribute("data-ref");
return findRef(ref, doc);
}
export function findRef(ref, doc) {
return doc.querySelector(`[data-ref='${ref}']`);
return doc.querySelector(`[data-ref='${ref}']`);
}
export function validNode(node) {
......@@ -465,15 +465,15 @@ export function nextValidNode(node) {
export function indexOf(node) {
let parent = node.parentNode;
if (!parent) {
return 0;
}
return Array.prototype.indexOf.call(parent.childNodes, node);
let parent = node.parentNode;
if (!parent) {
return 0;
}
return Array.prototype.indexOf.call(parent.childNodes, node);
}
export function child(node, index) {
return node.childNodes[index];
return node.childNodes[index];
}
export function isVisible(node) {
......
......@@ -4,39 +4,39 @@ import ePub from 'epubjs';
// window.JSZip = JSZip;
class Epub {
constructor(bookData) {
// this.epub = ePub({
// worker: false,
// replacements: true
// });
}
constructor(bookData) {
// this.epub = ePub({
// worker: false,
// replacements: true
// });
}
open(bookData){
return ePub(bookData, {replacements: true} ).then((book) => {
this.book = book;
return this.sections(this.book.spine);
})
}
open(bookData){
return ePub(bookData, {replacements: true} ).then((book) => {
this.book = book;
return this.sections(this.book.spine);
})
}
async sections(spine) {
let text = "";
let pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
async sections(spine) {
let text = "";
let pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
for (let section of spine) {
let href = section.href;
let html = await fetch(href)
.then((response) => {
return response.text();
}).then((t) => {
let matches = pattern.exec(t);
return matches && matches.length && matches[1];
});
text += html;
// let body = html.querySelector("body");
// text += body.innerHTML;
}
return text;
}
for (let section of spine) {
let href = section.href;
let html = await fetch(href)
.then((response) => {
return response.text();
}).then((t) => {
let matches = pattern.exec(t);
return matches && matches.length && matches[1];
});
text += html;
// let body = html.querySelector("body");
// text += body.innerHTML;
}
return text;
}
}
......
......@@ -52,12 +52,12 @@ export function UUID() {
* @returns an index of the match, or -1 if there is no match
*/
export function positionInNodeList(element, nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (element === nodeList[i]) {
return i;
}
}
return -1;
for (let i = 0; i < nodeList.length; i++) {
if (element === nodeList[i]) {
return i;
}
}
return -1;
}
/**
......@@ -66,67 +66,67 @@ export function positionInNodeList(element, nodeList) {
* and ele.ownerDocument.querySelectorAll(reply).length === 1
*/
export function findCssSelector(ele) {
let document = ele.ownerDocument;
let document = ele.ownerDocument;
// Fred: commented out to allow for parsing in fragments
// if (!document || !document.contains(ele)) {
// throw new Error("findCssSelector received element not inside document");
// }
let cssEscape = window.CSS.escape;
// document.querySelectorAll("#id") returns multiple if elements share an ID
if (ele.id &&
document.querySelectorAll("#" + cssEscape(ele.id)).length === 1) {
return "#" + cssEscape(ele.id);
}
// Inherently unique by tag name
let tagName = ele.localName;
if (tagName === "html") {
return "html";
}
if (tagName === "head") {
return "head";
}
if (tagName === "body") {
return "body";
}
// We might be able to find a unique class name
let selector, index, matches;
if (ele.classList.length > 0) {
for (let i = 0; i < ele.classList.length; i++) {
// Is this className unique by itself?
selector = "." + cssEscape(ele.classList.item(i));
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique with a tag name?
selector = cssEscape(tagName) + selector;
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique using a tag name and nth-child
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = selector + ":nth-child(" + index + ")";
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
}
}
// Not unique enough yet. As long as it's not a child of the document,
// continue recursing up until it is unique enough.
if (ele.parentNode !== document && ele.parentNode.nodeType === 1) {
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = findCssSelector(ele.parentNode) + " > " +
cssEscape(tagName) + ":nth-child(" + index + ")";
}
return selector;
// if (!document || !document.contains(ele)) {
// throw new Error("findCssSelector received element not inside document");
// }
let cssEscape = window.CSS.escape;
// document.querySelectorAll("#id") returns multiple if elements share an ID
if (ele.id &&
document.querySelectorAll("#" + cssEscape(ele.id)).length === 1) {
return "#" + cssEscape(ele.id);
}
// Inherently unique by tag name
let tagName = ele.localName;
if (tagName === "html") {
return "html";
}
if (tagName === "head") {
return "head";
}
if (tagName === "body") {
return "body";
}
// We might be able to find a unique class name
let selector, index, matches;
if (ele.classList.length > 0) {
for (let i = 0; i < ele.classList.length; i++) {
// Is this className unique by itself?
selector = "." + cssEscape(ele.classList.item(i));
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique with a tag name?
selector = cssEscape(tagName) + selector;
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique using a tag name and nth-child
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = selector + ":nth-child(" + index + ")";
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
}
}
// Not unique enough yet. As long as it's not a child of the document,
// continue recursing up until it is unique enough.
if (ele.parentNode !== document && ele.parentNode.nodeType === 1) {
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = findCssSelector(ele.parentNode) + " > " +
cssEscape(tagName) + ":nth-child(" + index + ")";
}
return selector;
};
export function attr(element, attributes) {
......
Markdown is supported
0% or