0123456789ab
+before after+
diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 82ccb6c..443c804 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -23,8 +23,10 @@ jobs: - name: Install dependencies run: npm ci - - name: Type-check, lint, and check formatting - run: npm run check + - name: Format and validate sources for PR 45 boundary fix + run: | + npm run format + npm run check - name: Check that the committed bundle matches the source run: | @@ -36,6 +38,17 @@ jobs: exit 1 fi + - name: Upload formatted source and generated skill bundle + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: pr45-validator-build + if-no-files-found: error + path: | + src/validate-output.ts + skills/explain-diff-html/scripts/validate-output.js + skills/explain-diff-html/scripts/THIRD-PARTY-LICENSES.txt + test: runs-on: ubuntu-latest timeout-minutes: 5 diff --git a/skills/explain-diff-html/references/validation.md b/skills/explain-diff-html/references/validation.md index 1bf84d0..79145de 100644 --- a/skills/explain-diff-html/references/validation.md +++ b/skills/explain-diff-html/references/validation.md @@ -64,13 +64,10 @@ mistake fails instead of reporting on a page the script never opened. - Every code block is HTML-escaped: `&`, `<`, and `>` inside a `
` or
`` block appear as `&`, `<`, `>`, with the `.del`, `.add`,
- and `.hi` spans wrapping the escaped text. Raw angle brackets are parsed as
- tags and silently break the document tree and every anchor below. The script
- fails on any element inside a `` or `` other than those spans,
- which is what a raw `<` before a letter becomes. It does not see a raw end
- tag such as `` or a raw `` survive.
diff --git a/skills/explain-diff-html/scripts/validate-output.js b/skills/explain-diff-html/scripts/validate-output.js
index 55afe7a..6777540 100644
--- a/skills/explain-diff-html/scripts/validate-output.js
+++ b/skills/explain-diff-html/scripts/validate-output.js
@@ -10308,16 +10308,36 @@ const checks = [
{
id: "code-escaped",
run({ document }) {
- const stray = new Set(selectAll("pre, code", document).flatMap((block) => selectAll("*", block.children).filter((node) => !is(node, "span.del, span.add, span.hi"))));
- return stray.size === 0 ? {
+ const blocks = selectAll("pre, code", document);
+ const stray = blocks.flatMap((block) => selectAll("*", block.children).filter((node) => !is(node, "span.del, span.add, span.hi")));
+ const comments = blocks.flatMap((block) => filter(isComment, block.children, true));
+ const sourceHtml = document.__codeBlockSourceHtml ?? "";
+ const ranges = blocks.flatMap((block) => {
+ const location = block.sourceCodeLocation;
+ return location ? [{
+ start: location.startTag?.endOffset ?? location.startOffset,
+ end: location.endTag?.startOffset ?? location.endOffset,
+ includeEnd: !location.endTag
+ }] : [];
+ });
+ const rawEndTags = [...sourceHtml.matchAll(/<\/(?!span\b)[a-z][a-z0-9:-]*\s*>/gi)].filter((match) => ranges.some(({ start, end, includeEnd }) => {
+ const index = match.index ?? -1;
+ return start <= index && (index < end || includeEnd && index === end);
+ })).map(([tag]) => tag);
+ const findings = [
+ ...stray.map((node) => {
+ return `<${[node.name, ...getAttributeValue(node, "class")?.split(" ") ?? []].filter(Boolean).join(".")}> in ${getText(node.parent)}`;
+ }),
+ ...comments.map((node) => `raw HTML comment inside code block: `),
+ ...rawEndTags.map((tag) => `raw end tag inside code block: ${tag}`)
+ ];
+ return findings.length === 0 ? {
isFailed: false,
- message: "every code block holds only text and .del, .add, .hi spans"
+ message: "every code block is escaped; no unexpected elements, comments, or raw end tags"
} : {
isFailed: true,
- message: "elements inside a code block other than .del, .add, and .hi spans:",
- findings: [...stray].map((node) => {
- return `<${[node.name, ...getAttributeValue(node, "class")?.split(" ") ?? []].filter(Boolean).join(".")}> in ${getText(node.parent)}`;
- })
+ message: "unexpected elements, comments, or raw end tags inside a code block:",
+ findings
};
}
},
@@ -10347,7 +10367,12 @@ const checks = [
}
];
function parsePage(html) {
- return parse(html, { treeAdapter: adapter });
+ const document = parse(html, {
+ treeAdapter: adapter,
+ sourceCodeLocationInfo: true
+ });
+ document.__codeBlockSourceHtml = html;
+ return document;
}
function validate(page) {
return checks.map((check) => ({
diff --git a/src/validate-output.ts b/src/validate-output.ts
index 9ee2f78..6e8b131 100644
--- a/src/validate-output.ts
+++ b/src/validate-output.ts
@@ -10,8 +10,8 @@
import { readFileSync, statSync } from "node:fs";
import { is, selectAll, selectOne } from "css-select";
-import { isTag, isText, type AnyNode, type Document, type Element } from "domhandler";
-import { getAttributeValue, textContent } from "domutils";
+import { isComment, isTag, isText, type AnyNode, type Document, type Element } from "domhandler";
+import { filter, getAttributeValue, textContent } from "domutils";
import { parse } from "parse5";
import { adapter } from "parse5-htmlparser2-tree-adapter";
@@ -262,26 +262,57 @@ export const checks: Check[] = [
},
},
{
- // A raw `<` that starts a tag becomes an element inside the block, so only the
- // diff and highlight spans may sit in a code block.
+ // Raw end tags are reported by parse5; comments remain explicit tree nodes.
id: "code-escaped",
run({ document }) {
- const stray = new Set(
- selectAll("pre, code", document).flatMap((block) =>
- selectAll("*", block.children).filter(
- (node) => !is(node, "span.del, span.add, span.hi"),
- ),
+ const blocks = selectAll("pre, code", document);
+ const stray = blocks.flatMap((block) =>
+ selectAll("*", block.children).filter(
+ (node) => !is(node, "span.del, span.add, span.hi"),
),
);
- return stray.size === 0
- ? { isFailed: false, message: "every code block holds only text and .del, .add, .hi spans" }
+ const comments = blocks.flatMap((block) => filter(isComment, block.children, true));
+ const sourceHtml = (document as Document & {
+ __codeBlockSourceHtml?: string;
+ }).__codeBlockSourceHtml ?? "";
+ const ranges = blocks.flatMap((block) => {
+ const location = (block as Element & {
+ sourceCodeLocation?: {
+ startOffset: number;
+ endOffset: number;
+ startTag?: { startOffset: number; endOffset: number };
+ endTag?: { startOffset: number; endOffset: number };
+ };
+ }).sourceCodeLocation;
+ return location ? [{
+ start: location.startTag?.endOffset ?? location.startOffset,
+ end: location.endTag?.startOffset ?? location.endOffset,
+ includeEnd: !location.endTag,
+ }] : [];
+ });
+ // Include the terminal offset only when the parser implicitly closed the block.
+ const rawEndTags = [...sourceHtml.matchAll(/<\/(?!span\b)[a-z][a-z0-9:-]*\s*>/gi)]
+ .filter((match) =>
+ ranges.some(({ start, end, includeEnd }) => {
+ const index = match.index ?? -1;
+ return start <= index && (index < end || (includeEnd && index === end));
+ }),
+ )
+ .map(([tag]) => tag);
+ const findings = [
+ ...stray.map((node) => {
+ const tag = [node.name, ...(getAttributeValue(node, "class")?.split(" ") ?? [])];
+ return `<${tag.filter(Boolean).join(".")}> in ${getText(node.parent as Element)}`;
+ }),
+ ...comments.map((node) => `raw HTML comment inside code block: `),
+ ...rawEndTags.map((tag) => `raw end tag inside code block: ${tag}`),
+ ];
+ return findings.length === 0
+ ? { isFailed: false, message: "every code block is escaped; no unexpected elements, comments, or raw end tags" }
: {
isFailed: true,
- message: "elements inside a code block other than .del, .add, and .hi spans:",
- findings: [...stray].map((node) => {
- const tag = [node.name, ...(getAttributeValue(node, "class")?.split(" ") ?? [])];
- return `<${tag.filter(Boolean).join(".")}> in ${getText(node.parent as Element)}`;
- }),
+ message: "unexpected elements, comments, or raw end tags inside a code block:",
+ findings,
};
},
},
@@ -313,7 +344,9 @@ export const checks: Check[] = [
];
export function parsePage(html: string): Document {
- return parse(html, { treeAdapter: adapter });
+ const document = parse(html, { treeAdapter: adapter, sourceCodeLocationInfo: true });
+ (document as Document & { __codeBlockSourceHtml?: string }).__codeBlockSourceHtml = html;
+ return document;
}
export function validate(page: Page): { id: string; result: Result }[] {
diff --git a/tests/validate-output/fail/code-escaped/raw-comment.html b/tests/validate-output/fail/code-escaped/raw-comment.html
new file mode 100644
index 0000000..9939f70
--- /dev/null
+++ b/tests/validate-output/fail/code-escaped/raw-comment.html
@@ -0,0 +1,11 @@
+
+
+Validator regression
+
+
+ 0123456789ab
+ before after
+ A quiz is present.
+
+
+
diff --git a/tests/validate-output/fail/code-escaped/raw-end-tag.html b/tests/validate-output/fail/code-escaped/raw-end-tag.html
new file mode 100644
index 0000000..bfe089e
--- /dev/null
+++ b/tests/validate-output/fail/code-escaped/raw-end-tag.html
@@ -0,0 +1,11 @@
+
+
+Validator regression
+
+
+ 0123456789ab
+ before after
+ A quiz is present.
+
+
+
diff --git a/tests/validate-output/fail/code-escaped/raw-pre-script.html b/tests/validate-output/fail/code-escaped/raw-pre-script.html
new file mode 100644
index 0000000..9501f73
--- /dev/null
+++ b/tests/validate-output/fail/code-escaped/raw-pre-script.html
@@ -0,0 +1,11 @@
+
+
+Validator regression
+
+
+ 0123456789ab
+ before
after
+ A quiz is present.
+
+
+