Commit 5d2606dd authored by Fred Chasen's avatar Fred Chasen

Order running headers, merge @page rules, remove specs pdfs

parent 3f93a729
......@@ -3,3 +3,4 @@ node_modules
dist
lib
__diff_output__
specs/**/*.pdf
This diff is collapsed.
const TIMEOUT = 10000;
describe('duplicate-headers', async () => {
let page;
let rendered;
beforeAll(async () => {
page = await loadPage('issues/duplicate-headers/duplicate-headers.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
})
it('should render 6 pages', async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => {
return r.length;
});
expect(pages).toEqual(6);
})
it('page 1 header should be "chapter"', async () => {
let text = await page.$eval("[data-page-number='1'] .pagedjs_margin-top-left > .pagedjs_margin-content", (r) => r.textContent);
expect(text).toContain('chapter');
})
it('page 3 header should be "chapter nth"', async () => {
let text = await page.$eval("[data-page-number='3'] .pagedjs_margin-top-left > .pagedjs_margin-content", (r) => r.textContent);
expect(text).toContain('chapter nth');
})
it('page 4 header should be "chapter left"', async () => {
let text = await page.$eval("[data-page-number='4'] .pagedjs_margin-top-left > .pagedjs_margin-content", (r) => r.textContent);
expect(text).toContain('chapter left');
})
if (!DEBUG) {
it('should create a pdf', async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(3);
expect(pdf).toMatchPDFSnapshot(4);
})
}
}
)
This diff is collapsed.
const TIMEOUT = 10000;
describe('issue', async () => {
let page;
let rendered;
beforeAll(async () => {
page = await loadPage('issues/template/template.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
})
xit('should render 6 pages', async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => {
return r.length;
});
expect(pages).toEqual(6);
})
if (!DEBUG) {
xit('should create a pdf', async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
})
}
}
)
......@@ -17,7 +17,6 @@
@page {
size: A5;
margin: 20mm 20mm;
/* border: solid 1px black; */
@bottom-left{
content: counter(page);
}
......
import Handler from "../handler";
import csstree from 'css-tree';
import { getTypeSelector, getPsuedoSelector, getNthSelector } from "../../utils/csstree";
class RunningHeaders extends Handler {
constructor(chunker, polisher, caller) {
......@@ -12,7 +13,6 @@ 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
if (identifier === "running") {
......@@ -109,13 +109,20 @@ class RunningHeaders extends Handler {
}
// move elements
for (let selector of Object.keys(this.elements)) {
if (!this.orderedSelectors) {
this.orderedSelectors = this.orderSelectors(this.elements);
}
for (let selector of this.orderedSelectors) {
if (selector) {
let el = this.elements[selector];
let selected = fragment.querySelector(selector);
if (selected) {
let running = this.runningSelectors[el.args[0]];
if (running.first) {
if (running && running.first) {
selected.innerHTML = ""; // Clear node
selected.classList.add("pagedjs_clear-after"); // Clear ::after
let clone = running.first.cloneNode(true);
clone.style.display = null;
selected.appendChild(clone);
......@@ -124,6 +131,92 @@ class RunningHeaders extends Handler {
}
}
}
/**
* Assign a weight to @page selector classes
* 1) page
* 2) left & right
* 3) blank
* 4) first & nth
* 5) named page
* 6) named left & right
* 7) named first & nth
*/
pageWeight(s) {
let weight = 1;
let selector = s.split(" ");
let parts = selector.length && selector[0].split(".");
parts.shift() // remove empty first part
switch (parts.length) {
case 4:
if (parts[3] === "pagedjs_first_page") {
weight = 7;
} else if (parts[3] === "pagedjs_left_page" || parts[3] === "pagedjs_right_page") {
weight = 6;
}
break;
case 3:
if (parts[1] === "pagedjs_named_page") {
if (parts[2].indexOf(":nth-of-type") > -1) {
weight = 7;
} else {
weight = 5;
}
}
break;
case 2:
if (parts[1] === "pagedjs_first_page") {
weight = 4;
} else if (parts[1] === "pagedjs_blank_page") {
weight = 3;
} else if (parts[1] === "pagedjs_left_page" || parts[1] === "pagedjs_right_page") {
weight = 2;
}
break;
default:
if (parts[0].indexOf(":nth-of-type") > -1) {
weight = 4;
} else {
weight = 1;
}
}
return weight;
}
/**
* Orders the selectors based on weight
*
* Does not try to deduplicate base on specifity of the selector
* Previous matched selector will just be overwritten
*/
orderSelectors(obj) {
let selectors = Object.keys(obj);
let weighted = {
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: []
};
let orderedSelectors = [];
for (let s of selectors) {
let w = this.pageWeight(s);
weighted[w].unshift(s);
}
for (var i = 1; i <= 7; i++) {
orderedSelectors = orderedSelectors.concat(weighted[i]);
}
return orderedSelectors;
}
}
export default RunningHeaders;
......@@ -37,9 +37,11 @@ class AtPage extends Handler {
// Find and Remove @page rules
onAtPage(node, item, list) {
let page, marginalia;
let selector = "";
let name = "";
let named, psuedo, nth;
let needsMerge = false;
if (node.prelude) {
named = this.getTypeSelector(node);
......@@ -50,22 +52,30 @@ class AtPage extends Handler {
selector = "*";
}
if (selector in this.pages) {
// this.pages[selector] = Object.assign(this.pages[selector], page);
// console.log("after", selector, this.pages[selector]);
let page = this.pageModel(selector);
// this.pages[selector].added = false;
page = this.pages[selector];
marginalia = this.replaceMarginalia(node);
needsMerge = true;
} else {
page = this.pageModel(selector);
marginalia = this.replaceMarginalia(node);
this.pages[selector] = page;
}
page.name = named;
page.psuedo = psuedo;
page.nth = nth;
if (name in this.pages) {
this.pages[selector] = Object.assign(this.pages[selector], page);
this.pages[selector].added = false;
if (needsMerge) {
page.marginalia = Object.assign(page.marginalia, marginalia);
} else {
this.pages[selector] = page;
page.marginalia = marginalia;
}
page.marginalia = this.replaceMarginalia(node);
let declarations = this.replaceDeclartations(node);
if (declarations.size) {
......@@ -82,7 +92,11 @@ class AtPage extends Handler {
page.marks = declarations.marks;
}
if (needsMerge) {
page.block.children.appendList(node.block.children);
} else {
page.block = node.block;
}
// Remove the rule
list.remove(item);
......@@ -376,6 +390,11 @@ class AtPage extends Handler {
// Named page
if (page.name) {
selectors.insert(selectors.createItem({
type: 'ClassSelector',
name: "pagedjs_named_page"
}));
selectors.insert(selectors.createItem({
type: 'ClassSelector',
name: "pagedjs_" + page.name + "_page"
......@@ -554,6 +573,11 @@ class AtPage extends Handler {
// Named page
if (page.name) {
selectors.insert(selectors.createItem({
type: 'ClassSelector',
name: "pagedjs_named_page"
}));
name = page.name + "_page";
selectors.insert(selectors.createItem({
type: 'ClassSelector',
......@@ -651,6 +675,11 @@ class AtPage extends Handler {
// Named page
if (page.name) {
selectors.insert(selectors.createItem({
type: 'ClassSelector',
name: "pagedjs_named_page"
}));
name = page.name + "_page";
selectors.insert(selectors.createItem({
type: 'ClassSelector',
......@@ -892,6 +921,7 @@ class AtPage extends Handler {
if (named) {
page.name = named;
page.element.classList.add("pagedjs_named_page");
page.element.classList.add("pagedjs_" + named + "_page");
if (!start.dataset.splitFrom) {
......
......@@ -193,6 +193,10 @@ export default `
break-after: column;
}
.pagedjs_clear-after::after {
content: none !important;
}
@media print {
body {
width: unset;
......
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