diff --git a/changelog.md b/changelog.md index ec0998b..564e74c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ ## Unreleased +### Bug Fixes + +* Keep `` markers inside SVG in HTML mode. With htmlparser2 12 the markers were dropped, so `` rendered as `` and CDATA in SVG `` lost its wrapper. CDATA outside foreign content is still a comment, and `xmlMode`/`recognizeCDATA` still expose plain text. + ## 0.13.0 (2026-10-28) ### Breaking Changes diff --git a/src/index.ts b/src/index.ts index e26b2ee..f96f118 100644 --- a/src/index.ts +++ b/src/index.ts @@ -226,6 +226,19 @@ export const parser = (html: string, options: Options = {}): Node[] => { } function ontext(text: string) { + // In HTML mode htmlparser2 reports a CDATA section inside foreign content + // (SVG) as plain text without its markers. Restore them so the section + // renders back as written. Raw text such as ", + ); + const expected = [ + { + tag: "svg", + content: [ + { tag: "style", content: [""] }, + { tag: "script", content: ['/*"/*]]>*/'] }, + ], + }, + ]; + assert.deepEqual(tree, expected); +}); + +test("should keep CDATA markers inside SVG text content", () => { + const tree = parser("a b<c d"); + const expected = [{ tag: "svg", content: [{ tag: "text", content: ["a d"] }] }]; + assert.deepEqual(tree, expected); +}); + +test("should not duplicate CDATA markers in raw text elements", () => { + const tree = parser(""); + const expected = [ + { tag: "script", content: [""] }, + { tag: "svg", content: [{ tag: "style", content: [""] }] }, + ]; + assert.deepEqual(tree, expected); +}); + +test("should parse CDATA outside foreign content as a comment", () => { + assert.deepEqual(parser("
"), [{ tag: "div", content: [""] }]); + assert.deepEqual(parser("x<y"), [ + { tag: "math", content: [{ tag: "mi", content: [""] }] }, + ]); +}); + +test("should expose CDATA text without markers with recognizeCDATA", () => { + const tree = parser("
", { + recognizeCDATA: true, + }); + const expected = [ + { tag: "div", content: ["a { const html = '';