...
 
Commits (4)
This diff is collapsed.
{ {
"name": "pagedjs", "name": "@ggrossetie/pagedjs",
"version": "0.2.0", "version": "0.2.0-next.1623590414",
"description": "Chunks up a document into paged media flows and applies print styles", "description": "Chunks up a document into paged media flows and applies print styles",
"author": "Fred Chasen", "author": "Fred Chasen",
"license": "MIT", "license": "MIT",
...@@ -59,5 +59,8 @@ ...@@ -59,5 +59,8 @@
"files": [ "files": [
"dist", "dist",
"lib" "lib"
] ],
"publishConfig": {
"access": "public"
}
} }
<!DOCTYPE html PUBLIC> <!DOCTYPE html PUBLIC>
<html lang="en" lang="en"> <html lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Style-Type" content="text/css" />
<title> <title>
Break inside avoid Break inside avoid
</title> </title>
<script src="../../../../dist/paged.polyfill.js"></script>
<script src="../../../../../dist/paged.polyfill.js"></script>
<style> <style>
@page { @page {
size: 6in 8in; size: 6in 8in;
margin: 20mm 20mm; margin: 20mm 20mm;
...@@ -21,14 +17,11 @@ ...@@ -21,14 +17,11 @@
} }
} }
h1, p { h1, p {
break-inside: avoid; break-inside: avoid;
} }
/* style and interface */ /* style and interface */
* { font-family: Times New Roman, sans-serif; } * { font-family: Times New Roman, sans-serif; }
:root { font-size: 18px; } :root { font-size: 18px; }
...@@ -59,22 +52,10 @@ ...@@ -59,22 +52,10 @@
margin-top: 10mm; margin-top: 10mm;
margin-left: 10mm; margin-left: 10mm;
} }
} }
</style> </style>
</head> </head>
<body> <body>
<section> <section>
<p>Nam eros tellus, hendrerit ut rhoncus sed, aliquet et felis. Cras a ex malesuada, fringilla magna elementum, cursus purus. Etiam fringilla leo non diam congue tempor. Ut laoreet, est eget blandit congue, mauris magna cursus sapien, dictum tempus sapien diam et dolor. Vestibulum vel egestas velit. Pellentesque vel consectetur urna, eu consequat odio. Sed ac pretium magna, ut ultrices tortor.</p> <p>Nam eros tellus, hendrerit ut rhoncus sed, aliquet et felis. Cras a ex malesuada, fringilla magna elementum, cursus purus. Etiam fringilla leo non diam congue tempor. Ut laoreet, est eget blandit congue, mauris magna cursus sapien, dictum tempus sapien diam et dolor. Vestibulum vel egestas velit. Pellentesque vel consectetur urna, eu consequat odio. Sed ac pretium magna, ut ultrices tortor.</p>
<h1>Cras ut augue condimentum, egestas nisi in, dictum erat. Nullam tincidunt tincidunt tempor. Sed in eleifend nibh, sit amet feugiat nisi. Cras at ante ut urna sagittis dictum ut nec elit. In feugiat euismod massa sagittis dictum. Nullam eu nisl eu elit laoreet tincidunt id sed ligula. Praesent vulputate faucibus nibh, ut ultrices nunc aliquam nec. Mauris et condimentum ligula. Vestibulum nec tortor quis urna dictum luctus. Cras quis suscipit metus. Ut dignissim ullamcorper aliquam. Donec condimentum eu tellus at interdum.</h1> <h1>Cras ut augue condimentum, egestas nisi in, dictum erat. Nullam tincidunt tincidunt tempor. Sed in eleifend nibh, sit amet feugiat nisi. Cras at ante ut urna sagittis dictum ut nec elit. In feugiat euismod massa sagittis dictum. Nullam eu nisl eu elit laoreet tincidunt id sed ligula. Praesent vulputate faucibus nibh, ut ultrices nunc aliquam nec. Mauris et condimentum ligula. Vestibulum nec tortor quis urna dictum luctus. Cras quis suscipit metus. Ut dignissim ullamcorper aliquam. Donec condimentum eu tellus at interdum.</h1>
...@@ -83,8 +64,5 @@ ...@@ -83,8 +64,5 @@
<p>Nam eros tellus, hendrerit ut rhoncus sed, aliquet et felis. Cras a ex malesuada, fringilla magna elementum, cursus purus. Etiam fringilla leo non diam congue tempor. Ut laoreet, est eget blandit congue, mauris magna cursus sapien, dictum tempus sapien diam et dolor. Vestibulum vel egestas velit. Pellentesque vel consectetur urna, eu consequat odio. Sed ac pretium magna, ut ultrices tortor.</p> <p>Nam eros tellus, hendrerit ut rhoncus sed, aliquet et felis. Cras a ex malesuada, fringilla magna elementum, cursus purus. Etiam fringilla leo non diam congue tempor. Ut laoreet, est eget blandit congue, mauris magna cursus sapien, dictum tempus sapien diam et dolor. Vestibulum vel egestas velit. Pellentesque vel consectetur urna, eu consequat odio. Sed ac pretium magna, ut ultrices tortor.</p>
<p>Nulla dignissim pellentesque magna ac maximus. Integer id tincidunt erat. Sed elementum posuere augue, quis pharetra mi vehicula in. Nullam rhoncus mi quis lectus gravida dignissim. Pellentesque a tortor ut leo pretium auctor non in massa. Nunc efficitur vestibulum mi, id mattis quam aliquet id. Ut semper tortor sit amet molestie mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec laoreet eleifend purus ut sagittis. Nunc consequat vel sapien at convallis. Maecenas sollicitudin quis justo non varius.</p> <p>Nulla dignissim pellentesque magna ac maximus. Integer id tincidunt erat. Sed elementum posuere augue, quis pharetra mi vehicula in. Nullam rhoncus mi quis lectus gravida dignissim. Pellentesque a tortor ut leo pretium auctor non in massa. Nunc efficitur vestibulum mi, id mattis quam aliquet id. Ut semper tortor sit amet molestie mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec laoreet eleifend purus ut sagittis. Nunc consequat vel sapien at convallis. Maecenas sollicitudin quis justo non varius.</p>
</section> </section>
</body> </body>
</html> </html>
<!DOCTYPE html PUBLIC>
<html lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title>
Long nested section
</title>
<script src="../../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 140mm 150mm;
margin: 0px;
padding: 0px;
}
.related {
break-inside: avoid;
}
@media screen {
body {
background-color: whitesmoke;
}
.pagedjs_page {
background-color: white;
box-shadow: 0 0 0 1px #bfbfbf;
margin-top: 10mm;
margin-left: 10mm;
}
}
</style>
</head>
<body>
<section>
<h3>Section title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fermentum vel tortor at suscipit. Donec augue leo, viverra sit amet mauris eget, blandit vestibulum lacus. Quisque
a nibh ut ligula elementum efficitur et molestie mauris. Suspendisse potenti. Nam dapibus orci nec vestibulum volutpat. Nunc magna nibh, tristique eget ornare vel, convallis
eget quam. Donec risus neque, mattis ac finibus at, ullamcorper vitae tellus. Aenean faucibus turpis quis ligula tincidunt consectetur. Ut sed tortor urna. Vivamus volutpat
turpis vitae tortor tempor, in luctus arcu fringilla. Quisque hendrerit ligula vitae nunc porta elementum.</p>
<p>Etiam euismod interdum tellus, vitae semper nisi semper in. Curabitur tempus enim vel lectus laoreet ornare. Quisque venenatis ante in maximus scelerisque. Quisque auctor
volutpat nibh, ac finibus dui. Integer et nisi sed odio rhoncus rhoncus. Nunc auctor mi a erat pretium, sit amet placerat felis congue. Integer id mattis mauris. Etiam suscipit
pulvinar enim, sed finibus tortor. Nulla purus nisi, malesuada at orci in, fringilla aliquet mi.</p>
</section>
<section>
<div class="related">
<div class="first">
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
</div>
<div class="second">
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
</div>
<div class="third">
<p>13</p>
<p>14</p>
<p>15</p>
<p>16</p>
<p>17</p>
<p>18</p>
</div>
</div>
</section>
</body>
</html>
const TIMEOUT = 10000;
describe("long nested section", () => {
let page;
beforeAll(async () => {
page = await loadPage("breaks/break-inside/break-inside-avoid/long-nested-section.html");
return page.rendered;
}, TIMEOUT);
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
});
// it should ignore the rule "break-inside: avoid" because the section does not fit on the next page
it("should ignore break-inside:avoid when the element (section) does not fit on a page", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => r.length);
expect(pages).toBe(2);
});
if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
});
}
});
<!DOCTYPE html PUBLIC>
<html lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title>
Long section break before
</title>
<script src="../../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 140mm 150mm;
margin: 0px;
padding: 0px;
}
section {
break-before: page;
break-inside: avoid;
}
@media screen {
body {
background-color: whitesmoke;
}
.pagedjs_page {
background-color: white;
box-shadow: 0 0 0 1px #bfbfbf;
margin-top: 10mm;
margin-left: 10mm;
}
}
</style>
</head>
<body>
<section>
<h3>Section title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fermentum vel tortor at suscipit. Donec augue leo, viverra sit amet mauris eget, blandit vestibulum lacus. Quisque
a nibh ut ligula elementum efficitur et molestie mauris. Suspendisse potenti. Nam dapibus orci nec vestibulum volutpat. Nunc magna nibh, tristique eget ornare vel, convallis
eget quam. Donec risus neque, mattis ac finibus at, ullamcorper vitae tellus. Aenean faucibus turpis quis ligula tincidunt consectetur. Ut sed tortor urna. Vivamus volutpat
turpis vitae tortor tempor, in luctus arcu fringilla. Quisque hendrerit ligula vitae nunc porta elementum.</p>
<p>Etiam euismod interdum tellus, vitae semper nisi semper in. Curabitur tempus enim vel lectus laoreet ornare. Quisque venenatis ante in maximus scelerisque. Quisque auctor
volutpat nibh, ac finibus dui. Integer et nisi sed odio rhoncus rhoncus. Nunc auctor mi a erat pretium, sit amet placerat felis congue. Integer id mattis mauris. Etiam suscipit
pulvinar enim, sed finibus tortor. Nulla purus nisi, malesuada at orci in, fringilla aliquet mi.</p>
</section>
<section>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>15</p>
<p>16</p>
<p>17</p>
<p>18</p>
</section>
</body>
</html>
const TIMEOUT = 10000;
describe("long section break before", () => {
let page;
beforeAll(async () => {
page = await loadPage("breaks/break-inside/break-inside-avoid/long-section-break-before.html");
return page.rendered;
}, TIMEOUT);
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
});
it("should ignore comply with break-before:page (precedence over break-inside:avoid)", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => r.length);
expect(pages).toBe(3);
});
if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
expect(pdf).toMatchPDFSnapshot(3);
});
}
});
<!DOCTYPE html PUBLIC>
<html lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title>
Long section
</title>
<script src="../../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 140mm 150mm;
margin: 0px;
padding: 0px;
}
section {
break-inside: avoid;
}
@media screen {
body {
background-color: whitesmoke;
}
.pagedjs_page {
background-color: white;
box-shadow: 0 0 0 1px #bfbfbf;
margin-top: 10mm;
margin-left: 10mm;
}
}
</style>
</head>
<body>
<section>
<h3>Section title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fermentum vel tortor at suscipit. Donec augue leo, viverra sit amet mauris eget, blandit vestibulum lacus. Quisque
a nibh ut ligula elementum efficitur et molestie mauris. Suspendisse potenti. Nam dapibus orci nec vestibulum volutpat. Nunc magna nibh, tristique eget ornare vel, convallis
eget quam. Donec risus neque, mattis ac finibus at, ullamcorper vitae tellus. Aenean faucibus turpis quis ligula tincidunt consectetur. Ut sed tortor urna. Vivamus volutpat
turpis vitae tortor tempor, in luctus arcu fringilla. Quisque hendrerit ligula vitae nunc porta elementum.</p>
<p>Etiam euismod interdum tellus, vitae semper nisi semper in. Curabitur tempus enim vel lectus laoreet ornare. Quisque venenatis ante in maximus scelerisque. Quisque auctor
volutpat nibh, ac finibus dui. Integer et nisi sed odio rhoncus rhoncus. Nunc auctor mi a erat pretium, sit amet placerat felis congue. Integer id mattis mauris. Etiam suscipit
pulvinar enim, sed finibus tortor. Nulla purus nisi, malesuada at orci in, fringilla aliquet mi.</p>
</section>
<section>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p>15</p>
<p>16</p>
<p>17</p>
<p>18</p>
</section>
</body>
</html>
const TIMEOUT = 10000;
describe("long section", () => {
let page;
beforeAll(async () => {
page = await loadPage("breaks/break-inside/break-inside-avoid/long-section.html");
return page.rendered;
}, TIMEOUT);
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
});
// it should ignore the rule "break-inside: avoid" because the section does not fit on the next page
it("should ignore break-inside:avoid when the element (section) does not fit on a page", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => r.length);
expect(pages).toBe(2);
});
if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
});
}
});
...@@ -6,16 +6,31 @@ ...@@ -6,16 +6,31 @@
<title> <title>
Long table Long table
</title> </title>
<script src="../../../dist/paged.polyfill.js"></script> <script src="../../../../dist/paged.polyfill.js"></script>
<style> <style>
@page { @page {
size: 140mm 150mm; size: 140mm 120mm;
margin: 0px; margin: 0px;
padding: 0px; padding: 0px;
} }
table { table {
break-inside: avoid; break-inside: avoid;
} }
@media screen {
body {
background-color: whitesmoke;
}
.pagedjs_page {
background-color: white;
box-shadow: 0 0 0 1px #bfbfbf;
margin-top: 10mm;
margin-left: 10mm;
}
}
</style> </style>
</head> </head>
<body> <body>
...@@ -126,22 +141,6 @@ ...@@ -126,22 +141,6 @@
<td>1</td> <td>1</td>
<td>v</td> <td>v</td>
</tr> </tr>
<tr>
<td>1</td>
<td>w</td>
</tr>
<tr>
<td>1</td>
<td>x</td>
</tr>
<tr>
<td>1</td>
<td>y</td>
</tr>
<tr>
<td>1</td>
<td>z</td>
</tr>
</tbody> </tbody>
</table> </table>
</section> </section>
......
...@@ -3,7 +3,7 @@ const TIMEOUT = 10000; ...@@ -3,7 +3,7 @@ const TIMEOUT = 10000;
describe("long table", () => { describe("long table", () => {
let page; let page;
beforeAll(async () => { beforeAll(async () => {
page = await loadPage("splits/tables/long-table.html"); page = await loadPage("breaks/break-inside/break-inside-avoid/long-table.html");
return page.rendered; return page.rendered;
}, TIMEOUT); }, TIMEOUT);
...@@ -13,11 +13,17 @@ describe("long table", () => { ...@@ -13,11 +13,17 @@ describe("long table", () => {
} }
}); });
// TODO: the following test will put the table on the next page but it should ignore the rule break-inside: avoid because the table does not fit on the next page // it should ignore the rule "break-inside: avoid" because the table does not fit on the next page
// this issue can be reproduced on v0.1.40 it("should ignore break-inside:avoid when the element (table) does not fit on a page", async () => {
it.skip("should ignore break-inside:avoid when the element (table) does not fit on a page", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => r.length); let pages = await page.$$eval(".pagedjs_page", (r) => r.length);
expect(pages).toBe(2); expect(pages).toBe(2);
}); });
}
); if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
});
}
});
...@@ -3,9 +3,8 @@ import ContentParser from "./parser"; ...@@ -3,9 +3,8 @@ import ContentParser from "./parser";
import EventEmitter from "event-emitter"; import EventEmitter from "event-emitter";
import Hook from "../utils/hook"; import Hook from "../utils/hook";
import Queue from "../utils/queue"; import Queue from "../utils/queue";
import { import {requestIdleCallback} from "../utils/utils";
requestIdleCallback import {breakInsideAvoidNode, breakInsideAvoidParentNode} from "../utils/dom";
} from "../utils/utils";
const MAX_PAGES = false; const MAX_PAGES = false;
const MAX_LAYOUTS = false; const MAX_LAYOUTS = false;
...@@ -179,7 +178,6 @@ class Chunker { ...@@ -179,7 +178,6 @@ class Chunker {
this.emit("rendered", this.pages); this.emit("rendered", this.pages);
return this; return this;
} }
...@@ -219,9 +217,11 @@ class Chunker { ...@@ -219,9 +217,11 @@ class Chunker {
let loops = 0; let loops = 0;
while (!done) { while (!done) {
result = await this.q.enqueue(() => { return this.renderAsync(renderer); }); result = await this.q.enqueue(() => {
return this.renderAsync(renderer);
});
done = result.done; done = result.done;
if(MAX_LAYOUTS) { if (MAX_LAYOUTS) {
loops += 1; loops += 1;
if (loops >= MAX_LAYOUTS) { if (loops >= MAX_LAYOUTS) {
this.stop(); this.stop();
...@@ -247,11 +247,11 @@ class Chunker { ...@@ -247,11 +247,11 @@ class Chunker {
return new Promise(resolve => { return new Promise(resolve => {
requestIdleCallback(async () => { requestIdleCallback(async () => {
if (this.stopped) { if (this.stopped) {
return resolve({ done: true, canceled: true }); return resolve({done: true, canceled: true});
} }
let result = await renderer.next(); let result = await renderer.next();
if (this.stopped) { if (this.stopped) {
resolve({ done: true, canceled: true }); resolve({done: true, canceled: true});
} else { } else {
resolve(result); resolve(result);
} }
...@@ -261,16 +261,37 @@ class Chunker { ...@@ -261,16 +261,37 @@ class Chunker {
async renderAsync(renderer) { async renderAsync(renderer) {
if (this.stopped) { if (this.stopped) {
return { done: true, canceled: true }; return {done: true, canceled: true};
} }
let result = await renderer.next(); let result = await renderer.next();
if (this.stopped) { if (this.stopped) {
return { done: true, canceled: true }; return {done: true, canceled: true};
} else { } else {
return result; return result;
} }
} }
async handleOverruledBreakInside(node) {
const breakInsideNode = breakInsideAvoidNode(node);
if (breakInsideNode) {
const page = this.addPage();
// QUESTION: should we emit the temporary page?
//this.emit("page", page);
const newBreakToken = await page.layout(breakInsideNode, null, this.maxChars);
const breakInsideAvoidParent = newBreakToken && breakInsideAvoidParentNode(newBreakToken.node);
// we are breaking inside a "break-inside: avoid" node which mean that we cannot comply with this rule!
const overruleBreakInside = breakInsideAvoidParent && breakInsideAvoidParent.dataset && breakInsideNode.dataset && breakInsideNode.dataset.ref === breakInsideAvoidParent.dataset.ref;
if (overruleBreakInside) {
// we need to overrule this definition, remove "break-inside: avoid" directive and reflow the previous page
delete breakInsideNode.dataset.breakInside;
breakInsideNode.style = breakInsideNode.style + ";break-inside: auto;";
}
// remove the temporary page
this.removePages(this.pages.length - 1);
return typeof overruleBreakInside !== "undefined";
}
}
async handleBreaks(node, force) { async handleBreaks(node, force) {
let currentPage = this.total + 1; let currentPage = this.total + 1;
let currentPosition = currentPage % 2 === 0 ? "left" : "right"; let currentPosition = currentPage % 2 === 0 ? "left" : "right";
...@@ -285,34 +306,34 @@ class Chunker { ...@@ -285,34 +306,34 @@ class Chunker {
} }
if (node && if (node &&
typeof node.dataset !== "undefined" && typeof node.dataset !== "undefined" &&
typeof node.dataset.previousBreakAfter !== "undefined") { typeof node.dataset.previousBreakAfter !== "undefined") {
previousBreakAfter = node.dataset.previousBreakAfter; previousBreakAfter = node.dataset.previousBreakAfter;
} }
if (node && if (node &&
typeof node.dataset !== "undefined" && typeof node.dataset !== "undefined" &&
typeof node.dataset.breakBefore !== "undefined") { typeof node.dataset.breakBefore !== "undefined") {
breakBefore = node.dataset.breakBefore; breakBefore = node.dataset.breakBefore;
} }
if (force) { if (force) {
page = this.addPage(true); page = this.addPage(true);
} else if( previousBreakAfter && } else if( previousBreakAfter &&
(previousBreakAfter === "left" || previousBreakAfter === "right") && (previousBreakAfter === "left" || previousBreakAfter === "right") &&
previousBreakAfter !== currentPosition) { previousBreakAfter !== currentPosition) {
page = this.addPage(true); page = this.addPage(true);
} else if( previousBreakAfter && } else if (previousBreakAfter &&
(previousBreakAfter === "verso" || previousBreakAfter === "recto") && (previousBreakAfter === "verso" || previousBreakAfter === "recto") &&
previousBreakAfter !== currentSide) { previousBreakAfter !== currentSide) {
page = this.addPage(true); page = this.addPage(true);
} else if( breakBefore && } else if (breakBefore &&
(breakBefore === "left" || breakBefore === "right") && (breakBefore === "left" || breakBefore === "right") &&
breakBefore !== currentPosition) { breakBefore !== currentPosition) {
page = this.addPage(true); page = this.addPage(true);
} else if( breakBefore && } else if (breakBefore &&
(breakBefore === "verso" || breakBefore === "recto") && (breakBefore === "verso" || breakBefore === "recto") &&
breakBefore !== currentSide) { breakBefore !== currentSide) {
page = this.addPage(true); page = this.addPage(true);
} }
...@@ -325,18 +346,29 @@ class Chunker { ...@@ -325,18 +346,29 @@ class Chunker {
} }
} }
async *layout(content, startAt) { async* layout(content, startAt) {
let breakToken = startAt || false; let breakToken = startAt || false;
let page;
while (breakToken !== undefined && (MAX_PAGES ? this.total < MAX_PAGES : true)) { while (breakToken !== undefined && (MAX_PAGES ? this.total < MAX_PAGES : true)) {
let reflowCurrentPage = false;
if (breakToken && breakToken.node) { if (breakToken && breakToken.node) {
await this.handleBreaks(breakToken.node); await this.handleBreaks(breakToken.node);
const overruleBreakInside = await this.handleOverruledBreakInside(breakToken.node);
if (overruleBreakInside) {
reflowCurrentPage = typeof page !== "undefined";
breakToken = page && page.startToken;
}
} else { } else {
await this.handleBreaks(content.firstChild); await this.handleBreaks(content.firstChild);
} }
let page = this.addPage(); if (reflowCurrentPage) {
// reflow the current page because a "break" definition was overruled!
} else {
page = this.addPage();
}
await this.hooks.beforePageLayout.trigger(page, content, breakToken, this); await this.hooks.beforePageLayout.trigger(page, content, breakToken, this);
this.emit("page", page); this.emit("page", page);
...@@ -372,7 +404,7 @@ class Chunker { ...@@ -372,7 +404,7 @@ class Chunker {
this.maxChars = this.charsPerBreak.reduce((a, b) => a + b, 0) / (this.charsPerBreak.length); this.maxChars = this.charsPerBreak.reduce((a, b) => a + b, 0) / (this.charsPerBreak.length);
} }
removePages(fromIndex=0) { removePages(fromIndex = 0) {
if (fromIndex >= this.pages.length) { if (fromIndex >= this.pages.length) {
return; return;
...@@ -454,6 +486,7 @@ class Chunker { ...@@ -454,6 +486,7 @@ class Chunker {
return page; return page;
} }
/* /*
insertPage(index, blank) { insertPage(index, blank) {
let lastPage = this.pages[index]; let lastPage = this.pages[index];
......
...@@ -297,29 +297,6 @@ class Layout { ...@@ -297,29 +297,6 @@ class Layout {
}); });
} }
avoidBreakInside(node, limiter) {
let breakNode;
if (node === limiter) {
return;
}
while (node.parentNode) {
node = node.parentNode;
if (node === limiter) {
break;
}
if (window.getComputedStyle(node)["break-inside"] === "avoid") {
breakNode = node;
break;
}
}
return breakNode;
}
createBreakToken(overflow, rendered, source) { createBreakToken(overflow, rendered, source) {
let container = overflow.startContainer; let container = overflow.startContainer;
let offset = overflow.startOffset; let offset = overflow.startOffset;
...@@ -484,12 +461,16 @@ class Layout { ...@@ -484,12 +461,16 @@ class Layout {
if (!range && left >= end) { if (!range && left >= end) {
// Check if it is a float // Check if it is a float
let isFloat = false; let isFloat = false;
// Check if the node is inside a break-inside: avoid table cell // Check if the node is inside a break-inside: avoid table cell
const insideTableCell = parentOf(node, "TD", rendered); const insideTableCell = parentOf(node, "TD", rendered);
if (insideTableCell && window.getComputedStyle(insideTableCell)["break-inside"] === "avoid") { if (insideTableCell && window.getComputedStyle(insideTableCell)["break-inside"] === "avoid") {
// breaking inside a table cell produces unexpected result, as a workaround, we forcibly avoid break inside in a cell. // breaking inside a table cell produces unexpected result, as a workaround, we forcibly avoid break inside in a cell.
prev = insideTableCell; prev = insideTableCell;
}
// Check if the node is the first and only row in the thead
else if (node.parentNode.nodeName === "THEAD" && node.parentNode.childElementCount === 1) {
// break on the table otherwise it will produce an empty table
prev = node.parentNode.parentNode;
} else if (isElement(node)) { } else if (isElement(node)) {
let styles = window.getComputedStyle(node); let styles = window.getComputedStyle(node);
isFloat = styles.getPropertyValue("float") !== "none"; isFloat = styles.getPropertyValue("float") !== "none";
......
...@@ -39,7 +39,9 @@ class Breaks extends Handler { ...@@ -39,7 +39,9 @@ class Breaks extends Handler {
if (property === "break-before" || if (property === "break-before" ||
property === "break-after" || property === "break-after" ||
property === "page-break-before" || property === "page-break-before" ||
property === "page-break-after" property === "page-break-after" ||
property === "page-break-inside" ||
property === "break-inside"
) { ) {
let child = declaration.value.children.first(); let child = declaration.value.children.first();
let value = child.name; let value = child.name;
...@@ -49,6 +51,8 @@ class Breaks extends Handler { ...@@ -49,6 +51,8 @@ class Breaks extends Handler {
property = "break-before"; property = "break-before";
} else if (property === "page-break-after") { } else if (property === "page-break-after") {
property = "break-after"; property = "break-after";
} else if (property === "page-break-inside") {
property = "break-inside";
} }
let breaker = { let breaker = {
...@@ -66,7 +70,9 @@ class Breaks extends Handler { ...@@ -66,7 +70,9 @@ class Breaks extends Handler {
}); });
// Remove from CSS -- handle right / left in module // Remove from CSS -- handle right / left in module
dList.remove(dItem); if (property !== "break-inside") {
dList.remove(dItem);
}
} }
} }
......
...@@ -645,6 +645,22 @@ export function breakInsideAvoidParentNode(node) { ...@@ -645,6 +645,22 @@ export function breakInsideAvoidParentNode(node) {
return null; return null;
} }
export function breakInsideAvoidNode(node) {
if (node && node.dataset && node.dataset.breakInside === "avoid") {
return node;
}
let children = node.children;
if (children) {
for (const child of children) {
if (child && child.dataset && child.dataset.breakInside === "avoid") {
return child;
}
return breakInsideAvoidNode(child);
}
}
return null;
}
/** /**
* Find a parent with a given node name. * Find a parent with a given node name.
* @param {Node} node - initial Node * @param {Node} node - initial Node
......