Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

### Bug Fixes

* Keep `<![CDATA[...]]>` markers inside SVG in HTML mode. With htmlparser2 12 the markers were dropped, so `<svg><style><![CDATA[a{}]]></style></svg>` rendered as `<svg><style>a{}</style></svg>` and CDATA in SVG `<text>` 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
Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> content already
// includes the markers; xmlMode and recognizeCDATA keep exposing plain text.
if (
!options.xmlMode &&
!options.recognizeCDATA &&
html.startsWith("<![CDATA[", parser.startIndex) &&
!text.startsWith("<![CDATA[")
) {
text = `<![CDATA[${text}]]>`;
}

const last: Node = bufferArrayLast();

if (last === undefined) {
Expand Down
49 changes: 49 additions & 0 deletions test/core.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,55 @@ test("should be parse CDATA", () => {
assert.deepEqual(tree, expected);
});

test("should keep CDATA markers inside SVG style and script", () => {
const tree = parser(
"<svg><style><![CDATA[a{color:red}]]></style><script>/*<![CDATA[*/x=\"<>\"/*]]>*/</script></svg>",
);
const expected = [
{
tag: "svg",
content: [
{ tag: "style", content: ["<![CDATA[a{color:red}]]>"] },
{ tag: "script", content: ['/*<![CDATA[*/x="<>"/*]]>*/'] },
],
},
];
assert.deepEqual(tree, expected);
});

test("should keep CDATA markers inside SVG text content", () => {
const tree = parser("<svg><text>a <![CDATA[b<c]]> d</text></svg>");
const expected = [{ tag: "svg", content: [{ tag: "text", content: ["a <![CDATA[b<c]]> d"] }] }];
assert.deepEqual(tree, expected);
});

test("should not duplicate CDATA markers in raw text elements", () => {
const tree = parser("<script><![CDATA[a<b]]></script><svg><style><![CDATA[c]]></style></svg>");
const expected = [
{ tag: "script", content: ["<![CDATA[a<b]]>"] },
{ tag: "svg", content: [{ tag: "style", content: ["<![CDATA[c]]>"] }] },
];
assert.deepEqual(tree, expected);
});

test("should parse CDATA outside foreign content as a comment", () => {
assert.deepEqual(parser("<div><![CDATA[a<b]]></div>"), [{ tag: "div", content: ["<!--[CDATA[a<b]]-->"] }]);
assert.deepEqual(parser("<math><mi><![CDATA[x<y]]></mi></math>"), [
{ tag: "math", content: [{ tag: "mi", content: ["<!--[CDATA[x<y]]-->"] }] },
]);
});

test("should expose CDATA text without markers with recognizeCDATA", () => {
const tree = parser("<div><![CDATA[a<b]]></div><svg><style><![CDATA[c]]></style></svg>", {
recognizeCDATA: true,
});
const expected = [
{ tag: "div", content: ["a<b"] },
{ tag: "svg", content: [{ tag: "style", content: ["c"] }] },
];
assert.deepEqual(tree, expected);
});

test("should be parse tag with escape object in attribute", () => {
const html =
'<button data-bem="{&quot;button&quot;:{&quot;checkedView&quot;:&quot;extra&quot;}}"' + ' type="submit"></button>';
Expand Down