Commit 6c883e30 authored by Fred Chasen's avatar Fred Chasen
Browse files

Fixed eslint issues

parent 0f4581a2
......@@ -16,7 +16,10 @@ module.exports = {
"indent": [
"error",
"tab",
{ "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }
{
"VariableDeclarator": { "var": 2, "let": 2, "const": 3 },
"SwitchCase" : 1
}
],
"linebreak-style": [
"error",
......
......@@ -63,7 +63,6 @@ class Layout {
let hasRenderedContent = false;
let newBreakToken;
let check = 0;
let length = 0;
while (!done && !newBreakToken) {
......@@ -149,7 +148,7 @@ class Layout {
return {
node,
offset
}
};
}
shouldBreak(node) {
......@@ -265,7 +264,7 @@ class Layout {
createBreakToken(overflow, rendered, source) {
let container = overflow.startContainer;
let offset = overflow.startOffset;
let node, renderedNode, ref, parent, index, temp, startOffset;
let node, renderedNode, parent, index, temp;
if (isElement(container)) {
temp = child(container, offset);
......@@ -348,7 +347,6 @@ class Layout {
let walker = walk(rendered.firstChild, rendered);
// Find Start
let startContainer, startOffset;
let next, done, node, offset, skip, breakAvoid, prev, br;
while (!done) {
next = walker.next();
......@@ -512,7 +510,7 @@ class Layout {
if (right > end) {
let letterwalker = letters(word);
let letter, nextLetter, doneLetter, posLetter;
let letter, nextLetter, doneLetter;
while (!doneLetter) {
nextLetter = letterwalker.next();
......
......@@ -145,7 +145,6 @@ class Page {
e = entries[i];
if(e.dataset.ref === ref) {
return e;
break;
}
}
}
......
import htmlparser2 from 'htmlparser2';
/**
* Render a flow of text offscreen
* @class
*/
class Parser {
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) {
let options = {};
let process = (node) => {
let uuid = this.uuid();
this.refs[uuid] = node;
node.dataset.ref = uuid; // Refs for all nodes
node.dataset.ref = uuid;
node.dataset.children = node.children.length;
if (node.data) {
node.dataset.text = node.data.length;
}
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];
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;
}
}
}
find(ref) {
return this.refs[ref];
}
*walk(start) {
let node = start || this.dom[0];
while (node) {
yield node;
if (node.children && node.children.length) {
node = node.children[0];
} else if (node.next) {
node = node.next;
} else {
while (node) {
node = node.parent;
if (node && node.next) {
node = node.next;
break;
}
}
}
}
}
// 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;
this.dom = undefined;
}
}
export default Parser;
......@@ -19,8 +19,6 @@ class ContentParser {
}
parse(markup, mime) {
let parser = new DOMParser();
let range = document.createRange();
let fragment = range.createContextualFragment(markup);
......@@ -53,8 +51,8 @@ class ContentParser {
false
);
let node;
while(node = treeWalker.nextNode()) {
let node = treeWalker.nextNode();
while(node) {
if (!node.hasAttribute("data-ref")) {
let uuid = UUID();
......@@ -68,6 +66,7 @@ class ContentParser {
// node.setAttribute("data-children", node.childNodes.length);
// node.setAttribute("data-text", node.textContent.trim().length);
node = treeWalker.nextNode();
}
}
......
import Chunker from './chunker/chunker';
import Polisher from './polisher/polisher';
import Previewer from './polyfill/previewer';
import Handler from './modules/handler';
import { registerHandlers, initializeHandlers } from './utils/handlers';
import Chunker from "./chunker/chunker";
import Polisher from "./polisher/polisher";
import Previewer from "./polyfill/previewer";
import Handler from "./modules/handler";
import { registerHandlers, initializeHandlers } from "./utils/handlers";
export {
Chunker,
......
import RunningHeaders from './running-headers';
import StringSets from './string-sets';
import TargetCounters from './target-counters';
import TargetText from './target-text';
import RunningHeaders from "./running-headers";
import StringSets from "./string-sets";
import TargetCounters from "./target-counters";
import TargetText from "./target-text";
export default [
RunningHeaders,
StringSets,
TargetCounters,
TargetText
]
];
import Handler from "../handler";
import csstree from 'css-tree';
import csstree from "css-tree";
class RunningHeaders extends Handler {
constructor(chunker, polisher, caller) {
......@@ -12,12 +12,12 @@ class RunningHeaders extends Handler {
onDeclaration(declaration, dItem, dList, rule) {
if (declaration.property === "position") {
let selector = csstree.generate(rule.ruleNode.prelude);
let identifier = declaration.value.children.first().name
let identifier = declaration.value.children.first().name;
if (identifier === "running") {
let value;
csstree.walk(declaration, {
visit: 'Function',
visit: "Function",
enter: (node, item, list) => {
value = node.children.first().name;
}
......@@ -27,14 +27,14 @@ class RunningHeaders extends Handler {
identifier: identifier,
value: value,
selector: selector
}
};
}
}
if (declaration.property === "content") {
csstree.walk(declaration, {
visit: 'Function',
visit: "Function",
enter: (funcNode, fItem, fList) => {
if (funcNode.name.indexOf("element") > -1) {
......@@ -61,7 +61,7 @@ class RunningHeaders extends Handler {
style: style || "first",
selector: s,
fullSelector: selector
}
};
});
}
......@@ -89,14 +89,14 @@ class RunningHeaders extends Handler {
let set = this.runningSelectors[name];
let selected = fragment.querySelector(set.selector);
if (selected) {
let cssVar;
// let cssVar;
if (set.identifier === "running") {
// cssVar = selected.textContent.replace(/\\([\s\S])|(["|'])/g,"\\$1$2");
// this.styleSheet.insertRule(`:root { --string-${name}: "${cssVar}"; }`, this.styleSheet.cssRules.length);
// fragment.style.setProperty(`--string-${name}`, `"${cssVar}"`);
set.first = selected;
} else {
console.log(set.value + "needs css replacement");
console.warn(set.value + "needs css replacement");
}
}
}
......@@ -134,13 +134,15 @@ class RunningHeaders extends Handler {
* 5) named page
* 6) named left & right
* 7) named first & nth
* @param {string} [s] selector string
* @return {int} weight
*/
pageWeight(s) {
let weight = 1;
let selector = s.split(" ");
let parts = selector.length && selector[0].split(".");
parts.shift() // remove empty first part
parts.shift(); // remove empty first part
switch (parts.length) {
case 4:
......@@ -184,6 +186,8 @@ class RunningHeaders extends Handler {
*
* Does not try to deduplicate base on specifity of the selector
* Previous matched selector will just be overwritten
* @param {obj} [obj] selectors object
* @return {Array} orderedSelectors
*/
orderSelectors(obj) {
let selectors = Object.keys(obj);
......@@ -213,7 +217,7 @@ class RunningHeaders extends Handler {
beforeTreeParse(text, sheet) {
// element(x) is parsed as image element selector, so update element to element-ident
sheet.text = text.replace(/element[\s]*\(([^\|^#)]*)\)/g, "element-ident($1)");
sheet.text = text.replace(/element[\s]*\(([^|^#)]*)\)/g, "element-ident($1)");
}
}
......
import Handler from "../handler";
import csstree from 'css-tree';
import csstree from "css-tree";
class StringSets extends Handler {
constructor(chunker, polisher, caller) {
......@@ -12,11 +12,11 @@ class StringSets extends Handler {
if (declaration.property === "string-set") {
let selector = csstree.generate(rule.ruleNode.prelude);
let identifier = declaration.value.children.first().name
let identifier = declaration.value.children.first().name;
let value;
csstree.walk(declaration, {
visit: 'Function',
visit: "Function",
enter: (node, item, list) => {
value = csstree.generate(node);
}
......@@ -26,7 +26,7 @@ class StringSets extends Handler {
identifier: identifier,
value: value,
selector: selector
}
};
}
}
......@@ -57,7 +57,7 @@ class StringSets extends Handler {
set.first = cssVar;
fragment.style.setProperty(`--string-${name}`, `"${set.first}"`);
} else {
console.log(set.value + "needs css replacement");
console.warn(set.value + "needs css replacement");
}
} else {
// Use the previous values
......
import Handler from "../handler";
import { UUID, attr, querySelectorEscape } from "../../utils/utils";
import csstree from 'css-tree';
import csstree from "css-tree";
class TargetCounters extends Handler {
constructor(chunker, polisher, caller) {
......@@ -15,12 +15,11 @@ class TargetCounters extends Handler {
if (funcNode.name === "target-counter") {
let selector = csstree.generate(rule.ruleNode.prelude);
let first = funcNode.children.first();
let last = funcNode.children.last();
let func = first.name;
let value = csstree.generate(funcNode);
let args = []
let args = [];
first.children.forEach((child) => {
if (child.type === "Identifier") {
......@@ -41,7 +40,7 @@ class TargetCounters extends Handler {
style = child.name;
}
}
})
});
let variable = "target-counter-" + UUID();
......@@ -55,12 +54,12 @@ class TargetCounters extends Handler {
selector: s,
fullSelector: selector,
variable: variable
}
};
});
// Replace with counter
funcNode.name = "counter";
funcNode.children = new csstree.List()
funcNode.children = new csstree.List();
funcNode.children.appendData({
type: "Identifier",
loc: 0,
......@@ -110,10 +109,10 @@ class TargetCounters extends Handler {
}
}
let psuedo = "";
if (split.length > 1) {
psuedo += "::" + split[1];
}
// let psuedo = "";
// if (split.length > 1) {
// psuedo += "::" + split[1];
// }
this.styleSheet.insertRule(`[data-${target.variable}="${selector}"] { counter-increment: ${target.variable} ${pg}; }`, this.styleSheet.cssRules.length);
} else {
......
import Handler from "../handler";
import { UUID, attr, querySelectorEscape } from "../../utils/utils";
import csstree from 'css-tree';
import csstree from "css-tree";
class TargetText extends Handler {
constructor(chunker, polisher, caller) {
......@@ -19,7 +19,7 @@ class TargetText extends Handler {
let value = csstree.generate(funcNode);
let args = []
let args = [];
first.children.forEach((child) => {
if (child.type === "Identifier") {
......@@ -43,12 +43,12 @@ class TargetText extends Handler {
selector: s,
fullSelector: selector,
variable: variable
}
};
});
// Replace with variable
funcNode.name = "var";
funcNode.children = new csstree.List()
funcNode.children = new csstree.List();
funcNode.children.appendData({
type: "Identifier",
loc: 0,
......@@ -68,7 +68,6 @@ class TargetText extends Handler {
let element = fragment.querySelector(querySelectorEscape(val));
if (element) {
if (target.style === "content") {
let text = element.textContent;
let selector = UUID();
selected.setAttribute("data-target-text", selector);
......@@ -77,7 +76,7 @@ class TargetText extends Handler {
psuedo += "::" + split[1];
}
let textContent = element.textContent.trim().replace(/[\"\']/g, (match) => {
let textContent = element.textContent.trim().replace(/["']/g, (match) => {
return "\\" + match;
}).replace(/[\n]/g, (match) => {
return "\\00000A";
......
import Handler from "../handler";
import csstree from 'css-tree';
import pageSizes from '../../polisher/sizes';
import { rebuildAncestors, elementAfter } from "../../utils/dom";
import csstree from "css-tree";
import pageSizes from "../../polisher/sizes";
import { rebuildAncestors } from "../../utils/dom";
class AtPage extends Handler {
constructor(chunker, polisher, caller) {
......@@ -34,14 +34,13 @@ class AtPage extends Handler {
},
block: {},
marks: undefined
}
};
}
// Find and Remove @page rules
onAtPage(node, item, list) {
let page, marginalia;
let selector = "";
let name = "";
let named, psuedo, nth;
let needsMerge = false;
......@@ -152,7 +151,7 @@ class AtPage extends Handler {
csstree.walk(ast, {
visit: "TypeSelector",
enter: (node, item, list) => {
name = node.name
name = node.name;
}
});
......@@ -194,7 +193,7 @@ class AtPage extends Handler {
let parsed = {};
csstree.walk(ast.block, {
visit: 'Atrule',
visit: "Atrule",
enter: (node, item, list) => {
let name = node.name;
if (name === "top") {
......@@ -221,7 +220,7 @@ class AtPage extends Handler {
let parsed = {};
csstree.walk(ast.block, {
visit: 'Declaration',
visit: "Declaration",
enter: (declaration, dItem, dList) => {
let prop = csstree.property(declaration.property).name;
let value = declaration.value;
......@@ -249,7 +248,7 @@ class AtPage extends Handler {
}
}
})
});
return parsed;
}
......@@ -259,7 +258,7 @@ class AtPage extends Handler {
// Get size: Xmm Ymm
csstree.walk(declaration, {
visit: 'Dimension',
visit: "Dimension",
enter: (node, item, list) => {
let {value, unit} = node;
if (typeof width === "undefined") {
......@@ -270,11 +269,11 @@ class AtPage extends Handler {
}
});
// Get size: 'A4'
// Get size: "A4"
csstree.walk(declaration, {
visit: 'String',
visit: "String",
enter: (node, item, list) => {
let name = node.value.replace(/["|']/g, '');
let name = node.value.replace(/["|']/g, "");
let s = pageSizes[name];
if (s) {
width = s.width;
......@@ -306,7 +305,7 @@ class AtPage extends Handler {
height,
orientation,
format
}
};
}
getMargins(declaration) {
......@@ -319,7 +318,7 @@ class AtPage extends Handler {
};
csstree.walk(declaration, {
visit: 'Dimension',
visit: "Dimension",
enter: (node, item, list) => {
margins.push(node);