/**
* The renderer's linear scanners (AGENTS.md rule 13, CodeQL alerts 12, 13, 16, 17 and 41): each
* gives the regex it replaced's answers on ordinary input and stays fast on 100k adversarial ones.
*/
import { describe, expect, it } from "vitest";
import { usesEmbeds } from "../src/embed-client.ts";
import { tableOfContents } from "../src/kit.ts";
import { inlineMarkdownText, markdownText, renderMarkdown } from "../src/markdown.ts";
import {
hasEmbedLine,
htmlText,
leadingHeading,
renderedHeadings,
stripTags,
} from "../src/text-scan.ts";
const N = 100_000;
/** Runs `f` and fails when it takes longer than a linear scan of 100k characters ever should. */
function fast(f: () => T): T {
const start = performance.now();
const out = f();
expect(performance.now() - start).toBeLessThan(1500);
return out;
}
// The regex versions, as they were.
const oldStrip = (html: string, gap: string) => html.replace(/<[^>]*>/g, gap);
const ENTITIES: Record = {
"&": "&",
"<": "<",
">": ">",
""": '"',
"'": "'",
};
const oldHtmlText = (html: string) =>
html
.replace(/<[^>]*>/g, " ")
.replace(/&(?:amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m] ?? m)
.replace(/(\d{1,7});/g, (m, n: string) =>
Number(n) <= 0x10ffff ? String.fromCodePoint(Number(n)) : m,
)
.replace(/\s+/g, " ")
.trim();
const oldH1 = (md: string) => {
const m = md.match(/^\s*#\s+(.+?)\s*#*\s*(?:\r?\n|$)/);
return m ? { text: (m[1] as string).trim(), length: m[0].length } : undefined;
};
const oldHeadings = (html: string) =>
[...html.matchAll(/([\s\S]*?)<\/h\1>/g)].map((m) => ({
depth: Number(m[1]),
id: m[2],
inner: m[3],
}));
const oldEmbed = (md: string) => /(^|\n)\s*(?:>\s*)*::embed\b/.test(md);
describe("stripTags", () => {
it("drops tags as /<[^>]*>/g did", () => {
for (const html of [
"",
"plain",
"a b
",
"a < b > c",
"c",
"x < y",
"one
two",
"<<>>",
'
',
])
for (const gap of [" ", ""]) expect(stripTags(html, gap)).toBe(oldStrip(html, gap));
});
it("is linear on a long run of < without >", () => {
expect(fast(() => stripTags("<".repeat(N), " "))).toBe("<".repeat(N));
expect(fast(() => stripTags(`${"`, ""))).toBe("");
});
});
describe("htmlText", () => {
it("gives the regex version's text on ordinary HTML", () => {
for (const html of [
"Hello world
\nTwo & three
",
""Quoted" and it's ’fine’
",
'A < B > C
',
"x < 5 and 3 > 2
",
" spaced\n\t out ",
" stays",
"Using Array<string> with <dialog>
",
"<script>alert(1)</script> <!-- c --> when x<y
",
"",
])
expect(htmlText(html)).toBe(oldHtmlText(html));
});
it("keeps the decoded text verbatim, `<` included (the sinks escape it)", () => {
expect(htmlText("Using Array<string> with <dialog>
")).toBe(
"Using Array with
")).toBe(
"",
);
// decoded once: `<` is the text `<`, `<` the text `<`
expect(htmlText("<b> <i")).toBe("<b> <i");
expect(oldHtmlText("<i")).toBe(" {
expect(fast(() => htmlText("<".repeat(N)))).toBe("<".repeat(N));
expect(fast(() => htmlText("<a".repeat(N / 5))).length).toBe((N / 5) * 2);
expect(fast(() => htmlText(`a${" \t\n".repeat(N / 3)}b`))).toBe("a b");
});
});
describe("markdown and inline markdown as text", () => {
it("decode entities after rendering, as before (the sinks: test/html-text-sinks.test.ts)", () => {
expect(markdownText("Intro <b> and **bold**")).toBe("Intro and bold");
expect(inlineMarkdownText("**537**, <script>")).toBe("537,