Commit db0b5388 authored by Nellie McKesson's avatar Nellie McKesson

adding better handling of following selector

parent d794dcd8
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 = 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 dataCssRule = elements[i].getAttribute("data-css-rule");
if (dataCssRule && dataCssRule != "") {
dataCssRule = `${dataCssRule},${selectors[s][0]}`;
elements[i].setAttribute("data-css-rule", dataCssRule);
} else {
elements[i].setAttribute("data-css-rule", selectors[s][0]);
}
}
let rule = `*[data-css-rule*='${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