Skip to content

Commit 99f48b0

Browse files
build(tooling): find an EOF processing instruction by position and keep postcss declarations after a stray semicolon
1 parent 808a9c8 commit 99f48b0

4 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎test/unitCases/tooling/compare-css.unittest.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe("compare-css-tools — agreeing with css-tree", () => {
378378

379379
it("drops an IE hack the spec drops, and reads postcss's as written", async () => {
380380
const source =
381-
".a{*zoom:1;_display:inline;-webkit-box:2;--v:3;-1x:4;\\61:5;-\\62:6}";
381+
".a{*zoom:1;;_display:inline;-webkit-box:2;--v:3;-1x:4;\\61:5;-\\62:6}";
382382
const loc = within(source);
383383
/**
384384
* @param {string} property a declaration's name, as written

‎test/unitCases/tooling/compare-html.unittest.js‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ describe("compare-html-tools — agreeing with parse5", () => {
308308
expect(await cell("htmlparser2")).toBe("own dialect");
309309
expect(await cell("webpack")).toBe("yes");
310310
// The byte order mark is the decoder's, gone before tree construction.
311-
expect(mockParse5.parse).toHaveBeenLastCalledWith(html.slice(1));
311+
expect(mockParse5.parse).toHaveBeenLastCalledWith(html.slice(1), {
312+
sourceCodeLocationInfo: true
313+
});
312314
});
313315

314316
// Each a `<?…` the input runs out in, which webpack's processing instruction
@@ -319,23 +321,58 @@ describe("compare-html-tools — agreeing with parse5", () => {
319321
["a cut instruction", "?x y"],
320322
["a target no name starts", "?1x"],
321323
["a target spelled out of name characters", "?x$y"],
322-
["a target the XML reserves", "?xml v"]
324+
["a target the XML reserves", "?xml v"],
325+
["a target the XML reserves, before a line break", "?xml\nv"]
323326
])("agrees about %s at the end of the input", async (_name, data) => {
327+
const source = `<p>a</p><${data}`;
324328
mockParse5.parse.mockReturnValue({
325329
nodeName: "#document",
326330
childNodes: [
327331
element("html", HTML_NAMESPACE, [
328332
element("head", HTML_NAMESPACE, []),
329333
element("body", HTML_NAMESPACE, [
330334
element("p", HTML_NAMESPACE, [{ nodeName: "#text", value: "a" }]),
331-
{ nodeName: "#comment", data }
335+
{
336+
nodeName: "#comment",
337+
data,
338+
sourceCodeLocation: {
339+
startOffset: source.indexOf("<?"),
340+
// Where parse5 ends a token the input ran out in.
341+
endOffset: source.length + 1
342+
}
343+
}
332344
])
333345
])
334346
]
335347
});
336-
expect(
337-
await agreement("parse5", TREE_READERS, `<p>a</p><${data}`)("webpack")
338-
).toBe("yes");
348+
expect(await agreement("parse5", TREE_READERS, source)("webpack")).toBe(
349+
"yes"
350+
);
351+
});
352+
353+
it("keeps a closed `<?…>` whose text the input happens to end with", async () => {
354+
const source = "<?x y><script><?x y";
355+
mockParse5.parse.mockReturnValue({
356+
nodeName: "#document",
357+
childNodes: [
358+
{
359+
nodeName: "#comment",
360+
data: "?x y",
361+
sourceCodeLocation: { startOffset: 0, endOffset: 6 }
362+
},
363+
element("html", HTML_NAMESPACE, [
364+
element("head", HTML_NAMESPACE, [
365+
element("script", HTML_NAMESPACE, [
366+
{ nodeName: "#text", value: "<?x y" }
367+
])
368+
]),
369+
element("body", HTML_NAMESPACE, [])
370+
])
371+
]
372+
});
373+
expect(await agreement("parse5", TREE_READERS, source)("webpack")).toBe(
374+
"yes"
375+
);
339376
});
340377

341378
it("names the first node the two trees differ at", async () => {

‎tooling/compare-css-tools.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,9 @@ const postcssTree = (css) => {
10001000
} else if (node.type === "decl") {
10011001
declaration(
10021002
events,
1003-
node.raws.before.trim() + node.prop,
1003+
/** @type {RegExpExecArray} */ (
1004+
/[^\s;]*$/.exec(node.raws.before)
1005+
)[0] + node.prop,
10041006
node.raws.value ? node.raws.value.raw : node.value,
10051007
Boolean(node.important)
10061008
);

‎tooling/compare-html-tools.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ const setup = () => installPackages(CACHE_NAME);
100100
* @property {{ name: string, value: string, prefix?: string }[]=} attrs
101101
* @property {Parse5Node[]=} childNodes
102102
* @property {Parse5Node=} content a `<template>`'s document fragment
103+
* @property {{ startOffset: number, endOffset: number }=} sourceCodeLocation where it was written, when asked for
103104
*/
104-
/** @typedef {{ parse: (html: string) => Parse5Node }} Parse5 */
105+
/** @typedef {{ parse: (html: string, options?: { sourceCodeLocationInfo: boolean }) => Parse5Node }} Parse5 */
105106
/** @typedef {import("../lib/html/syntax-parser").HtmlPath} HtmlPath */
106107

107108
// An app shell, whose weight is inline critical CSS and form markup: without
@@ -934,15 +935,15 @@ const startOf = (namespace, name, attributes) =>
934935
* Whether webpack's processing instruction rules drop the bogus comment `<?…`
935936
* the input ran out in: cut inside its target, or cut before its `>` with a
936937
* target other than `xml` or `xml-stylesheet`, which stay comments.
937-
* @param {string} data the comment's text, from its `?`
938+
* @param {string} data the source from the comment's `?` to the end of input
938939
* @returns {boolean} true where webpack's tree holds no node for it
939940
*/
940941
const droppedAtEnd = (data) => {
941-
const target = /^\?([A-Za-z_][\w-]*)?(.?)/.exec(data);
942+
const target = /^\?([A-Za-z_][\w-]*)?([^]?)/.exec(data);
942943
const name = /** @type {RegExpExecArray} */ (target)[1];
943944
const after = /** @type {RegExpExecArray} */ (target)[2];
944945
if (data === "?" || (name !== undefined && after === "")) return true;
945-
if (name === undefined || !/[\s?]/.test(after)) return false;
946+
if (name === undefined || !/[\t\n\f\r ?]/.test(after)) return false;
946947
return !/^xml(?:-stylesheet)?$/i.test(name);
947948
};
948949

@@ -967,15 +968,18 @@ const parse5Tree = (html) => {
967968
if (child.nodeName === "#text") {
968969
events.text(/** @type {string} */ (child.value));
969970
} else if (child.nodeName === "#comment") {
970-
const data = /** @type {string} */ (child.data);
971+
const at = child.sourceCodeLocation;
972+
// parse5 ends a token the input ran out in one past its end, and a
973+
// closed one at its `>`.
971974
if (
972-
data[0] === "?" &&
973-
source.endsWith(`<${data}`) &&
974-
droppedAtEnd(data)
975+
at !== undefined &&
976+
at.endOffset > source.length &&
977+
source.charCodeAt(at.startOffset + 1) === 0x3f &&
978+
droppedAtEnd(source.slice(at.startOffset + 1))
975979
) {
976980
continue;
977981
}
978-
events.leaf(`comment ${JSON.stringify(data)}`);
982+
events.leaf(`comment ${JSON.stringify(child.data)}`);
979983
} else if (child.nodeName === "#documentType") {
980984
events.leaf(`doctype ${child.name}`);
981985
} else {
@@ -1004,7 +1008,7 @@ const parse5Tree = (html) => {
10041008
}
10051009
}
10061010
};
1007-
walk(parse5.parse(source));
1011+
walk(parse5.parse(source, { sourceCodeLocationInfo: true }));
10081012
return events;
10091013
};
10101014

0 commit comments

Comments
 (0)