Commit 7b14274b authored by Guillaume's avatar Guillaume

resolves #311 honor named pages even when they are nested/wrapped

parent 2f2f4c62
Pipeline #791 passed with stage
in 4 minutes and 50 seconds
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<title>named-page-nested-first-child</title>
<meta charset="UTF-8">
<script src="../../../dist/paged.polyfill.js"></script>
<style>
.preamble {
page: preamble;
}
@page preamble {
background: orange;
}
/* interface */
@media screen {
body {
background-color: whitesmoke;
}
.pagedjs_page {
margin-bottom: 20px;
flex: none;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2);
background-color: white;
}
.pagedjs_pages {
width: calc(var(--pagedjs-width) * 2);
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
transform-origin: 0 0;
margin: 0 auto;
}
.pagedjs_first_page {
margin-left: 50%;
}
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Introduction.</p>
<div class="content">
<div class="preamble">
<p>This is a preamble.</p>
</div>
</div>
</body>
</html>
const TIMEOUT = 10000;
describe("named-page-nested", () => {
let page;
beforeAll(async () => {
page = await loadPage("named-page/nested/named-page-nested-first-child.html");
return page.rendered;
}, TIMEOUT);
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
});
it("should put preamble on its own page with an orange background (2 pages)", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => {
return r.length;
});
expect(pages).toEqual(2);
});
if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
});
}
});
......@@ -17,7 +17,6 @@ import {
nodeAfter,
nodeBefore,
parentOf,
previousSignificantNode,
prevValidNode,
rebuildAncestors,
validNode,
......@@ -103,7 +102,7 @@ class Layout {
this.hooks && this.hooks.layoutNode.trigger(node);
// Check if the rendered element has a break set
if (hasRenderedContent && this.shouldBreak(node)) {
if (hasRenderedContent && this.shouldBreak(node, start)) {
this.hooks && this.hooks.layout.trigger(wrapper, this);
let imgs = wrapper.querySelectorAll("img");
......@@ -201,17 +200,17 @@ class Layout {
return newBreakToken;
}
shouldBreak(node) {
let previousSibling = previousSignificantNode(node);
shouldBreak(node, limiter) {
let previousNode = nodeBefore(node, limiter);
let parentNode = node.parentNode;
let parentBreakBefore = needsBreakBefore(node) && parentNode && !previousSibling && needsBreakBefore(parentNode);
let parentBreakBefore = needsBreakBefore(node) && parentNode && !previousNode && needsBreakBefore(parentNode);
let doubleBreakBefore;
if (parentBreakBefore) {
doubleBreakBefore = node.dataset.breakBefore === parentNode.dataset.breakBefore;
}
return !doubleBreakBefore && needsBreakBefore(node) || needsPreviousBreakAfter(node) || needsPageBreak(node, previousSibling);
return !doubleBreakBefore && needsBreakBefore(node) || needsPreviousBreakAfter(node) || needsPageBreak(node, previousNode);
}
forceBreak() {
......
......@@ -6,7 +6,7 @@ export function isText(node) {
return node && node.nodeType === 3;
}
export function *walk(start, limiter) {
export function* walk(start, limiter) {
let node = start;
while (node) {
......@@ -325,7 +325,13 @@ export function needsPageBreak(node, previousSignificantNode) {
return false;
}
const previousSignificantNodePage = previousSignificantNode.dataset ? previousSignificantNode.dataset.page : undefined;
const currentNodePage = node.dataset ? node.dataset.page : undefined;
let currentNodePage = node.dataset ? node.dataset.page : undefined;
if (typeof previousSignificantNodePage !== "undefined" && typeof currentNodePage === "undefined") {
const nodeWithNamedPage = getNodeWithNamedPage(node);
if (nodeWithNamedPage) {
currentNodePage = nodeWithNamedPage.dataset.page;
}
}
return currentNodePage !== previousSignificantNodePage;
}
......@@ -336,7 +342,7 @@ export function *words(node) {
let currentLetter;
let range;
const significantWhitespaces = node.parentElement && node.parentElement.nodeName === 'PRE';
const significantWhitespaces = node.parentElement && node.parentElement.nodeName === "PRE";
while (currentOffset < max) {
currentLetter = currentText[currentOffset];
......@@ -636,6 +642,23 @@ export function previousSignificantNode(sib) {
return null;
}
function getNodeWithNamedPage(node, limiter) {
if (node && node.dataset && node.dataset.page) {
return node;
}
if (node.parentNode) {
while ((node = node.parentNode)) {
if (limiter && node === limiter) {
return;
}
if (node.dataset && node.dataset.page) {
return node;
}
}
}
return null;
}
export function breakInsideAvoidParentNode(node) {
while ((node = node.parentNode)) {
if (node && node.dataset && node.dataset.breakInside === "avoid") {
......
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