Commit 6f4fe5e6 authored by Guillaume's avatar Guillaume

resolves #245 ignore impractical break-inside: avoid rules

parent 16bafad5
<!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>
Break inside avoid
</title>
<script src="../../../../../dist/paged.polyfill.js"></script>
<script src="../../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 6in 8in;
margin: 20mm 20mm;
......@@ -21,14 +17,11 @@
}
}
h1, p {
break-inside: avoid;
}
/* style and interface */
* { font-family: Times New Roman, sans-serif; }
:root { font-size: 18px; }
......@@ -59,22 +52,10 @@
margin-top: 10mm;
margin-left: 10mm;
}
}
</style>
</head>
<body>
<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>
<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 @@
<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>
</section>
</body>
</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 @@
<title>
Long table
</title>
<script src="../../../dist/paged.polyfill.js"></script>
<script src="../../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 140mm 150mm;
size: 140mm 120mm;
margin: 0px;
padding: 0px;
}
table {
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>
......@@ -126,22 +141,6 @@
<td>1</td>
<td>v</td>
</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>
</table>
</section>
......
......@@ -3,7 +3,7 @@ const TIMEOUT = 10000;
describe("long table", () => {
let page;
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;
}, TIMEOUT);
......@@ -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
// this issue can be reproduced on v0.1.40
it.skip("should ignore break-inside:avoid when the element (table) does not fit on a page", async () => {
// it should ignore the rule "break-inside: avoid" because the table does not fit on the next page
it("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);
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";
import EventEmitter from "event-emitter";
import Hook from "../utils/hook";
import Queue from "../utils/queue";
import {
requestIdleCallback
} from "../utils/utils";
import {requestIdleCallback} from "../utils/utils";
import {breakInsideAvoidNode, breakInsideAvoidParentNode} from "../utils/dom";
const MAX_PAGES = false;
const MAX_LAYOUTS = false;
......@@ -173,7 +172,6 @@ class Chunker {
this.emit("rendered", this.pages);
return this;
}
......@@ -213,9 +211,11 @@ class Chunker {
let loops = 0;
while (!done) {
result = await this.q.enqueue(() => { return this.renderAsync(renderer); });
result = await this.q.enqueue(() => {
return this.renderAsync(renderer);
});
done = result.done;
if(MAX_LAYOUTS) {
if (MAX_LAYOUTS) {
loops += 1;
if (loops >= MAX_LAYOUTS) {
this.stop();
......@@ -241,11 +241,11 @@ class Chunker {
return new Promise(resolve => {
requestIdleCallback(async () => {
if (this.stopped) {
return resolve({ done: true, canceled: true });
return resolve({done: true, canceled: true});
}
let result = await renderer.next();
if (this.stopped) {
resolve({ done: true, canceled: true });
resolve({done: true, canceled: true});
} else {
resolve(result);
}
......@@ -255,16 +255,37 @@ class Chunker {
async renderAsync(renderer) {
if (this.stopped) {
return { done: true, canceled: true };
return {done: true, canceled: true};
}
let result = await renderer.next();
if (this.stopped) {
return { done: true, canceled: true };
return {done: true, canceled: true};
} else {
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) {
let currentPage = this.total + 1;
let currentPosition = currentPage % 2 === 0 ? "left" : "right";
......@@ -279,32 +300,32 @@ class Chunker {
}
if (node &&
typeof node.dataset !== "undefined" &&
typeof node.dataset.previousBreakAfter !== "undefined") {
typeof node.dataset !== "undefined" &&
typeof node.dataset.previousBreakAfter !== "undefined") {
previousBreakAfter = node.dataset.previousBreakAfter;
}
if (node &&
typeof node.dataset !== "undefined" &&
typeof node.dataset.breakBefore !== "undefined") {
typeof node.dataset !== "undefined" &&
typeof node.dataset.breakBefore !== "undefined") {
breakBefore = node.dataset.breakBefore;
}
if( previousBreakAfter &&
(previousBreakAfter === "left" || previousBreakAfter === "right") &&
previousBreakAfter !== currentPosition) {
if (previousBreakAfter &&
(previousBreakAfter === "left" || previousBreakAfter === "right") &&
previousBreakAfter !== currentPosition) {
page = this.addPage(true);
} else if( previousBreakAfter &&
(previousBreakAfter === "verso" || previousBreakAfter === "recto") &&
previousBreakAfter !== currentSide) {
} else if (previousBreakAfter &&
(previousBreakAfter === "verso" || previousBreakAfter === "recto") &&
previousBreakAfter !== currentSide) {
page = this.addPage(true);
} else if( breakBefore &&
(breakBefore === "left" || breakBefore === "right") &&
breakBefore !== currentPosition) {
} else if (breakBefore &&
(breakBefore === "left" || breakBefore === "right") &&
breakBefore !== currentPosition) {
page = this.addPage(true);
} else if( breakBefore &&
(breakBefore === "verso" || breakBefore === "recto") &&
breakBefore !== currentSide) {
} else if (breakBefore &&
(breakBefore === "verso" || breakBefore === "recto") &&
breakBefore !== currentSide) {
page = this.addPage(true);
}
......@@ -317,18 +338,29 @@ class Chunker {
}
}
async *layout(content, startAt) {
async* layout(content, startAt) {
let breakToken = startAt || false;
let page;
while (breakToken !== undefined && (MAX_PAGES ? this.total < MAX_PAGES : true)) {
let reflowCurrentPage = false;
if (breakToken && 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 {
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);
this.emit("page", page);
......@@ -364,7 +396,7 @@ class Chunker {
this.maxChars = this.charsPerBreak.reduce((a, b) => a + b, 0) / (this.charsPerBreak.length);
}
removePages(fromIndex=0) {
removePages(fromIndex = 0) {
if (fromIndex >= this.pages.length) {
return;
......@@ -446,6 +478,7 @@ class Chunker {
return page;
}
/*
insertPage(index, blank) {
let lastPage = this.pages[index];
......@@ -486,7 +519,6 @@ class Chunker {
*/
loadFonts() {
let fontPromises = [];
(document.fonts || []).forEach((fontFace) => {
......
......@@ -295,29 +295,6 @@ class Layout {
});
}
avoidBreakInside(node, limiter) {
let breakNode;
if (node === limiter) {