Skip to content

Commit

Permalink
fix: Do not skip encoding entities for tags when in xmlMode (#282)
Browse files Browse the repository at this point in the history
Fixes #281

Add test for tag in script tag
  • Loading branch information
fb55 authored Dec 22, 2020
1 parent f214f3c commit 5590940
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ describe("render DOM parsed with htmlparser2", () => {
const str = '<svg viewBox="0 0 8 8"><radialGradient/></svg>';
expect(xml(str)).toStrictEqual(str);
});

it("should encode entities in otherwise special tags", () => {
expect(xml('<script>"<br/>"</script>')).toStrictEqual(
"<script>&quot;<br/>&quot;</script>"
);
});

it("should not encode entities if disabled", () => {
const str = '<script>"<br/>"</script>';
expect(xml(str, { decodeEntities: false })).toStrictEqual(str);
});
});
});

Expand Down Expand Up @@ -192,6 +203,11 @@ function testBody(html: (input: string, opts?: CheerioOptions) => string) {
expect(html(str)).toStrictEqual(str);
});

it("should not encode tags in script tag", () => {
const str = '<script>"<br>"</script>';
expect(html(str)).toStrictEqual(str);
});

it("should not encode json data", () => {
const str =
'<script>var json = {"simple_value": "value", "value_with_tokens": "&quot;here & \'there\'&quot;"};</script>';
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ function renderText(elem: DataNode, opts: DomSerializerOptions) {
// If entities weren't decoded, no need to encode them back
if (
opts.decodeEntities !== false &&
!(elem.parent && unencodedElements.has((elem.parent as Element).name))
!(
!opts.xmlMode &&
elem.parent &&
unencodedElements.has((elem.parent as Element).name)
)
) {
data = encodeXML(data);
}
Expand Down

0 comments on commit 5590940

Please sign in to comment.