import { describe, it, expect } from "vitest";
import { marked } from "marked";
import {
autoCacheSize,
buildCodeBlockHtml,
COPY_ICON_TOKEN,
WRAP_ICON_TOKEN,
escapeHtml,
highlightAutoCached,
isClosedFence,
restoreCodeBlockIcons,
sanitizeLang,
splitHljsHtmlLines,
} from "./codeBlock";
describe("codeBlock helpers (1.3.7 single-pass markdown)", () => {
it("escapeHtml encodes markup characters", () => {
expect(escapeHtml(`
& "`)).toBe(
"<img onerror="alert(1)"> & "",
);
});
it("sanitizeLang keeps safe language ids only", () => {
expect(sanitizeLang("ts")).toBe("ts");
expect(sanitizeLang("c++")).toBe("c++");
expect(sanitizeLang("js;alert(1)")).toBe("js");
expect(sanitizeLang("")).toBe("");
expect(sanitizeLang(undefined)).toBe("");
});
it("splitHljsHtmlLines re-opens spans across newlines", () => {
const hl = `const x;\nfn()`;
expect(splitHljsHtmlLines(hl)).toEqual([
`const x;`,
`fn()`,
]);
// span that wraps a newline
const cross = `"a\nb"`;
expect(splitHljsHtmlLines(cross)).toEqual([
`"a`,
`b"`,
]);
});
it("buildCodeBlockHtml emits .cb chrome with numbered lines and no bare script", () => {
const html = buildCodeBlockHtml("const x = 1;\nconsole.log(x);", "js", 3, {
wrap: "Wrap lines",
copy: "Copy",
});
expect(html).toContain('class="cb numbered"');
expect(html).toContain('class="cb-lang">js<');
expect(html).toContain('data-i="3"');
expect(html).toContain('class="cb-l"');
expect(html).toContain("hljs-keyword");
expect(html).not.toContain("`, "notalang", 0, {
wrap: "w",
copy: "c",
});
expect(html).toContain("<img onerror=alert(1)>");
expect(html).toContain("<script>x</script>");
expect(html).not.toMatch(/
/i);
});
it("buildCodeBlockHtml bakes a wrapped block's .wrap and aria-pressed into the HTML", () => {
const off = buildCodeBlockHtml("a\nb", "ts", 0, { wrap: "w", copy: "c" });
expect(off).toMatch(/^
/);
expect(off).toContain('aria-pressed="false"');
const on = buildCodeBlockHtml("a\nb", "ts", 0, { wrap: "w", copy: "c" }, true);
expect(on).toMatch(/^
/);
expect(on).toContain('class="cb-btn cb-wrap" title="w" aria-label="w" aria-pressed="true"');
});
it("buildCodeBlockHtml emits icon placeholders that restoreCodeBlockIcons turns into the trusted