Commit c3533fbc authored by Fred Chasen's avatar Fred Chasen

Merge branch 'nellie/following' into 'master'

Adding better handling for following (+) selectors

See merge request !125
parents d794dcd8 57133c23
Pipeline #550 passed with stage
in 4 minutes and 13 seconds
This diff is collapsed.
const TIMEOUT = 10000; // Some book might take longer than this to renderer
describe("following-selector", () => {
let page;
beforeAll(async () => {
page = await loadPage("following-selector/following-selector.html");
return page.rendered;
}, TIMEOUT);
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
});
it("should render text", async () => {
let text = await page.evaluate(() => document.body.textContent);
expect(text).toContain("Chapter 1. Loomings.");
});
it("should render 14 pages", async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => r.length);
expect(pages).toBe(14);
});
if (!DEBUG) {
it("should create a pdf", async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
expect(pdf).toMatchPDFSnapshot(2);
});
}
}
);
import Handler from "../handler";
import csstree from "css-tree";
import {UUID} from "../../utils/utils";
class Following extends Handler {
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
this.styleSheet = polisher.styleSheet;
this.selectors = {};
}
onRule(ruleNode, ruleItem, rulelist) {
let selector = csstree.generate(ruleNode.prelude);
if (selector.match(/\+/)) {
let declarations = csstree.generate(ruleNode.block);
declarations = declarations.replace(/[{}]/g,"");
let uuid = "following-" + UUID();
selector.split(",").forEach((s) => {
if (!this.selectors[s]) {
this.selectors[s] = [uuid, declarations];
} else {
this.selectors[s][1] = `${this.selectors[s][1]};${declarations}` ;
}
});
rulelist.remove(ruleItem);
}
}
afterParsed(parsed) {
this.processSelectors(parsed, this.selectors);
}
processSelectors(parsed, selectors) {
// add the new attributes to matching elements
for (let s in selectors) {
let elements = parsed.querySelectorAll(s);
for (var i = 0; i < elements.length; i++) {
let dataFollowing = elements[i].getAttribute("data-following");
if (dataFollowing && dataFollowing != "") {
dataFollowing = `${dataFollowing},${selectors[s][0]}`;
elements[i].setAttribute("data-following", dataFollowing);
} else {
elements[i].setAttribute("data-following", selectors[s][0]);
}
}
let rule = `*[data-following*='${selectors[s][0]}'] { ${selectors[s][1]}; }`;
this.styleSheet.insertRule(rule, this.styleSheet.cssRules.length);
}
}
}
export default Following;
......@@ -6,6 +6,7 @@ import Counters from "./counters";
import Lists from "./lists";
import PositionFixed from "./position-fixed";
import NthOfType from "./nth-of-type";
import Following from "./following";
export default [
AtPage,
......@@ -15,5 +16,6 @@ export default [
Counters,
Lists,
PositionFixed,
NthOfType
NthOfType,
Following
];
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