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
......@@ -31,6 +31,7 @@
"rollup": "^2.38.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-license": "^2.1.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-resolve": "^5.2.0",
......@@ -2153,6 +2154,15 @@
"node": ">=0.10.0"
}
},
"node_modules/array-find-index": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-find-index/-/array-find-index-1.0.2.tgz",
"integrity": "sha1-3wEKoSh+Fku9pvlyOwqWoexBh6E=",
"dev": true,
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/array-flatten": {
"version": "1.1.1",
"dev": true,
......@@ -3146,6 +3156,12 @@
"node": ">= 6"
}
},
"node_modules/commenting": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/commenting/-/commenting-1.1.0.tgz",
"integrity": "sha512-YeNK4tavZwtH7jEgK1ZINXzLKm6DZdEMfsaaieOsCAN0S8vsY7UeuO3Q7d/M018EFgE+IeUAuBOKkFccBZsUZA==",
"dev": true
},
"node_modules/component-emitter": {
"version": "1.3.0",
"dev": true,
......@@ -7123,9 +7139,10 @@
}
},
"node_modules/lodash": {
"version": "4.17.20",
"dev": true,
"license": "MIT"
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
},
"node_modules/lru-cache": {
"version": "6.0.0",
......@@ -7144,9 +7161,10 @@
"license": "MIT"
},
"node_modules/magic-string": {
"version": "0.25.4",
"version": "0.25.7",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz",
"integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==",
"dev": true,
"license": "MIT",
"dependencies": {
"sourcemap-codec": "^1.4.4"
}
......@@ -7382,6 +7400,15 @@
"dev": true,
"license": "MIT"
},
"node_modules/moment": {
"version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==",
"dev": true,
"engines": {
"node": "*"
}
},
"node_modules/ms": {
"version": "2.0.0",
"dev": true,
......@@ -7760,6 +7787,12 @@
"node": ">=6"
}
},
"node_modules/package-name-regex": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/package-name-regex/-/package-name-regex-1.0.9.tgz",
"integrity": "sha512-+U2oQCfEz2IlGqws8gmfKzdMDbSd6+RZp6UIFdKo+GAw3+o+kfnsgXkWtJ1JMoKhpP2kEvuYyTy1lXOEQEe0ZA==",
"dev": true
},
"node_modules/parent-module": {
"version": "1.0.1",
"dev": true,
......@@ -8657,6 +8690,41 @@
"rollup-pluginutils": "^2.5.0"
}
},
"node_modules/rollup-plugin-license": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/rollup-plugin-license/-/rollup-plugin-license-2.3.0.tgz",
"integrity": "sha512-oi8pL59fVTwXCkLUsZ8dVGVJjO7Hcc5UT0chJvKd0MktPgeYHSadkaicAYUemdYHHpjb0D3DyvedZAEPt+2r8w==",
"dev": true,
"dependencies": {
"commenting": "1.1.0",
"glob": "7.1.6",
"lodash": "4.17.21",
"magic-string": "0.25.7",
"mkdirp": "1.0.4",
"moment": "2.29.1",
"package-name-regex": "1.0.9",
"spdx-expression-validate": "2.0.0",
"spdx-satisfies": "5.0.0"
},
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"rollup": "^1.0.0 || ^2.0.0"
}
},
"node_modules/rollup-plugin-license/node_modules/mkdirp": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
"integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==",
"dev": true,
"bin": {
"mkdirp": "bin/cmd.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/rollup-plugin-livereload": {
"version": "2.0.0",
"dev": true,
......@@ -9258,6 +9326,17 @@
"dev": true,
"license": "MIT"
},
"node_modules/spdx-compare": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/spdx-compare/-/spdx-compare-1.0.0.tgz",
"integrity": "sha512-C1mDZOX0hnu0ep9dfmuoi03+eOdDoz2yvK79RxbcrVEG1NO1Ph35yW102DHWKN4pk80nwCgeMmSY5L25VE4D9A==",
"dev": true,
"dependencies": {
"array-find-index": "^1.0.2",
"spdx-expression-parse": "^3.0.0",
"spdx-ranges": "^2.0.0"
}
},
"node_modules/spdx-correct": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.1.tgz",
......@@ -9284,12 +9363,38 @@
"spdx-license-ids": "^3.0.0"
}
},
"node_modules/spdx-expression-validate": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/spdx-expression-validate/-/spdx-expression-validate-2.0.0.tgz",
"integrity": "sha512-b3wydZLM+Tc6CFvaRDBOF9d76oGIHNCLYFeHbftFXUWjnfZWganmDmvtM5sm1cRwJc/VDBMLyGGrsLFd1vOxbg==",
"dev": true,
"dependencies": {
"spdx-expression-parse": "^3.0.0"
}
},
"node_modules/spdx-license-ids": {
"version": "3.0.7",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.7.tgz",
"integrity": "sha512-U+MTEOO0AiDzxwFvoa4JVnMV6mZlJKk2sBLt90s7G0Gd0Mlknc7kxEn3nuDPNZRta7O2uy8oLcZLVT+4sqNZHQ==",
"dev": true
},
"node_modules/spdx-ranges": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/spdx-ranges/-/spdx-ranges-2.1.1.tgz",
"integrity": "sha512-mcdpQFV7UDAgLpXEE/jOMqvK4LBoO0uTQg0uvXUewmEFhpiZx5yJSZITHB8w1ZahKdhfZqP5GPEOKLyEq5p8XA==",
"dev": true
},
"node_modules/spdx-satisfies": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-5.0.0.tgz",
"integrity": "sha512-/hGhwh20BeGmkA+P/lm06RvXD94JduwNxtx/oX3B5ClPt1/u/m5MCaDNo1tV3Y9laLkQr/NRde63b9lLMhlNfw==",
"dev": true,
"dependencies": {
"spdx-compare": "^1.0.0",
"spdx-expression-parse": "^3.0.0",
"spdx-ranges": "^2.0.0"
}
},
"node_modules/split-string": {
"version": "3.1.0",
"dev": true,
......@@ -11958,6 +12063,12 @@
"version": "3.1.0",
"dev": true
},
"array-find-index": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-find-index/-/array-find-index-1.0.2.tgz",
"integrity": "sha1-3wEKoSh+Fku9pvlyOwqWoexBh6E=",
"dev": true
},
"array-flatten": {
"version": "1.1.1",
"dev": true
......@@ -12681,6 +12792,12 @@
"version": "4.1.1",
"dev": true
},
"commenting": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/commenting/-/commenting-1.1.0.tgz",
"integrity": "sha512-YeNK4tavZwtH7jEgK1ZINXzLKm6DZdEMfsaaieOsCAN0S8vsY7UeuO3Q7d/M018EFgE+IeUAuBOKkFccBZsUZA==",
"dev": true
},
"component-emitter": {
"version": "1.3.0",
"dev": true
......@@ -15615,7 +15732,9 @@
}
},
"lodash": {
"version": "4.17.20",
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
},
"lru-cache": {
......@@ -15630,7 +15749,9 @@
"dev": true
},
"magic-string": {
"version": "0.25.4",
"version": "0.25.7",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz",
"integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==",
"dev": true,
"requires": {
"sourcemap-codec": "^1.4.4"
......@@ -15798,6 +15919,12 @@
"version": "0.5.3",
"dev": true
},
"moment": {
"version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==",
"dev": true
},
"ms": {
"version": "2.0.0",
"dev": true
......@@ -16073,6 +16200,12 @@
"integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
"dev": true
},
"package-name-regex": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/package-name-regex/-/package-name-regex-1.0.9.tgz",
"integrity": "sha512-+U2oQCfEz2IlGqws8gmfKzdMDbSd6+RZp6UIFdKo+GAw3+o+kfnsgXkWtJ1JMoKhpP2kEvuYyTy1lXOEQEe0ZA==",
"dev": true
},
"parent-module": {
"version": "1.0.1",
"dev": true,
......@@ -16706,6 +16839,31 @@
"rollup-pluginutils": "^2.5.0"
}
},
"rollup-plugin-license": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/rollup-plugin-license/-/rollup-plugin-license-2.3.0.tgz",
"integrity": "sha512-oi8pL59fVTwXCkLUsZ8dVGVJjO7Hcc5UT0chJvKd0MktPgeYHSadkaicAYUemdYHHpjb0D3DyvedZAEPt+2r8w==",
"dev": true,
"requires": {
"commenting": "1.1.0",
"glob": "7.1.6",
"lodash": "4.17.21",
"magic-string": "0.25.7",
"mkdirp": "1.0.4",
"moment": "2.29.1",
"package-name-regex": "1.0.9",
"spdx-expression-validate": "2.0.0",
"spdx-satisfies": "5.0.0"
},
"dependencies": {
"mkdirp": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
"integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==",
"dev": true
}
}
},
"rollup-plugin-livereload": {
"version": "2.0.0",
"dev": true,
......@@ -17159,6 +17317,17 @@
"version": "1.4.6",
"dev": true
},
"spdx-compare": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/spdx-compare/-/spdx-compare-1.0.0.tgz",
"integrity": "sha512-C1mDZOX0hnu0ep9dfmuoi03+eOdDoz2yvK79RxbcrVEG1NO1Ph35yW102DHWKN4pk80nwCgeMmSY5L25VE4D9A==",
"dev": true,
"requires": {
"array-find-index": "^1.0.2",
"spdx-expression-parse": "^3.0.0",
"spdx-ranges": "^2.0.0"
}
},
"spdx-correct": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.1.tgz",
......@@ -17185,12 +17354,38 @@
"spdx-license-ids": "^3.0.0"
}
},
"spdx-expression-validate": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/spdx-expression-validate/-/spdx-expression-validate-2.0.0.tgz",
"integrity": "sha512-b3wydZLM+Tc6CFvaRDBOF9d76oGIHNCLYFeHbftFXUWjnfZWganmDmvtM5sm1cRwJc/VDBMLyGGrsLFd1vOxbg==",
"dev": true,
"requires": {
"spdx-expression-parse": "^3.0.0"
}
},
"spdx-license-ids": {
"version": "3.0.7",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.7.tgz",
"integrity": "sha512-U+MTEOO0AiDzxwFvoa4JVnMV6mZlJKk2sBLt90s7G0Gd0Mlknc7kxEn3nuDPNZRta7O2uy8oLcZLVT+4sqNZHQ==",
"dev": true
},
"spdx-ranges": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/spdx-ranges/-/spdx-ranges-2.1.1.tgz",
"integrity": "sha512-mcdpQFV7UDAgLpXEE/jOMqvK4LBoO0uTQg0uvXUewmEFhpiZx5yJSZITHB8w1ZahKdhfZqP5GPEOKLyEq5p8XA==",
"dev": true
},
"spdx-satisfies": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-5.0.0.tgz",
"integrity": "sha512-/hGhwh20BeGmkA+P/lm06RvXD94JduwNxtx/oX3B5ClPt1/u/m5MCaDNo1tV3Y9laLkQr/NRde63b9lLMhlNfw==",
"dev": true,
"requires": {
"spdx-compare": "^1.0.0",
"spdx-expression-parse": "^3.0.0",
"spdx-ranges": "^2.0.0"
}
},
"split-string": {
"version": "3.1.0",
"dev": true,
......
......@@ -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