Commit abeda839 authored by Fred Chasen's avatar Fred Chasen

Update counters to allow page and footnote resets

parent 1431319b
Pipeline #725 failed with stage
in 6 minutes and 27 seconds
This diff is collapsed.
......@@ -5,7 +5,7 @@ import json from 'rollup-plugin-json';
// import builtins from 'rollup-plugin-node-builtins';
// import globals from 'rollup-plugin-node-globals';
import pkg from './package.json';
// import pkg from './package.json';
import serve from 'rollup-plugin-serve'
import livereload from 'rollup-plugin-livereload'
......
......@@ -14,17 +14,28 @@ class Counters extends Handler {
let property = declaration.property;
if (property === "counter-increment") {
let inc = this.handleIncrement(declaration, rule);
if (inc) {
this.handleIncrement(declaration, rule);
// clean up empty declaration
let hasProperities = false;
declaration.value.children.forEach((data) => {
if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
}
});
if (!hasProperities) {
dList.remove(dItem);
}
} else if (property === "counter-reset") {
let reset = this.handleReset(declaration, rule);
for (const r of reset) {
if (r.selector !== ".pagedjs_page") {
dList.remove(dItem);
break;
this.handleReset(declaration, rule);
// clean up empty declaration
let hasProperities = false;
declaration.value.children.forEach((data) => {
if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
}
});
if (!hasProperities) {
dList.remove(dItem);
}
}
}
......@@ -49,82 +60,112 @@ class Counters extends Handler {
}
handleIncrement(declaration, rule) {
let names = [];
let values = [];
let increments = [];
declaration.value.children.forEach((child) => {
if (child.type && child.type === "Identifier") {
names.push(child.name);
}
if (child.type && child.type === "Number") {
values.push(child.value);
}
});
for (const name of names) {
let number = values.shift();
let children = declaration.value.children;
if (name === "page" || name.indexOf("target-counter-") === 0) {
continue;
}
children.forEach((data, item) => {
if (data.type && data.type === "Identifier") {
let name = data.name;
let selector = csstree.generate(rule.ruleNode.prelude);
if (name === "page" || name.indexOf("target-counter-") === 0) {
return;
}
let counter;
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
let whitespace, number, value;
if (item.next && item.next.data.type === "WhiteSpace") {
whitespace = item.next;
}
if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
number = whitespace.next;
value = parseInt(number.data.value);
}
let selector = csstree.generate(rule.ruleNode.prelude);
let counter;
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
}
let increment = {
selector: selector,
number: value || 1
};
counter.increments[selector] = increment;
increments.push(increment);
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
}
let increment = {
selector: selector,
number: number || 1
};
counter.increments[selector] = increment;
increments.push(increment);
}
});
return increments;
}
handleReset(declaration, rule) {
let names = [];
let values = [];
let resets = [];
declaration.value.children.forEach((child) => {
if (child.type && child.type === "Identifier") {
names.push(child.name);
}
if (child.type && child.type === "Number") {
values.push(child.value);
}
});
for (const name of names) {
let counter;
let selector;
let prelude = rule.ruleNode.prelude;
let number = values.shift();
if (rule.ruleNode.type === "Atrule" && rule.ruleNode.name === "page") {
selector = ".pagedjs_page";
} else {
selector = csstree.generate(prelude || rule.ruleNode);
}
let children = declaration.value.children;
children.forEach((data, item) => {
if (data.type && data.type === "Identifier") {
let name = data.name;
let whitespace, number, value;
if (item.next && item.next.data.type === "WhiteSpace") {
whitespace = item.next;
}
if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
number = whitespace.next;
value = parseInt(number.data.value);
}
if (name === "footnote") {
this.addFootnoteMarkerCounter(declaration.value.children);
}
let counter;
let selector;
let prelude = rule.ruleNode.prelude;
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
if (rule.ruleNode.type === "Atrule" && rule.ruleNode.name === "page") {
selector = ".pagedjs_page";
} else {
selector = csstree.generate(prelude || rule.ruleNode);
}
if (name === "footnote") {
this.addFootnoteMarkerCounter(declaration.value.children);
}
if (!(name in this.counters)) {
counter = this.addCounter(name);
} else {
counter = this.counters[name];
}
let reset = {
selector: selector,
number: value || 0
};
counter.resets[selector] = reset;
resets.push(reset);
if (selector !== ".pagedjs_page") {
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
}
}
});
let reset = {
selector: selector,
number: number || 0
};
counter.resets[selector] = reset;
resets.push(reset);
}
return resets;
}
......@@ -322,7 +363,9 @@ class Counters extends Handler {
resets.push(`footnote-marker ${value}`);
});
this.styleSheet.insertRule(`[data-page-number="${pageElement.dataset.pageNumber}"] { counter-increment: none; counter-reset: ${resets.join(" ")} }`, this.styleSheet.cssRules.length);
if (resets.length) {
this.styleSheet.insertRule(`[data-page-number="${pageElement.dataset.pageNumber}"] { counter-increment: none; counter-reset: ${resets.join(" ")} }`, this.styleSheet.cssRules.length);
}
}
}
......
......@@ -36,7 +36,7 @@ class PageCounterIncrement extends Handler {
const number = declaration.value.children.getSize() > 1 ? declaration.value.children.last().value : 1;
const name = identifier && identifier.name;
if (name.indexOf("target-counter-") === 0) {
if (name && name.indexOf("target-counter-") === 0) {
return;
}
// A counter named page is automatically created and incremented by 1 on every page of the document,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment