Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dom): Escape ]]> when serializing CharData #181

Merged
merged 3 commits into from Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/dom.js
Expand Up @@ -1066,7 +1066,25 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
case ATTRIBUTE_NODE:
return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
case TEXT_NODE:
return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
/**
* The ampersand character (&) and the left angle bracket (<) must not appear in their literal form,
* except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section.
* If they are needed elsewhere, they must be escaped using either numeric character references or the strings
* `&amp;` and `&lt;` respectively.
* The right angle bracket (>) may be represented using the string " &gt; ", and must, for compatibility,
* be escaped using either `&gt;` or a character reference when it appears in the string `]]>` in content,
* when that string is not marking the end of a CDATA section.
*
* In the content of elements, character data is any string of characters
* which does not contain the start-delimiter of any markup
* and does not include the CDATA-section-close delimiter, `]]>`.
*
* @see https://www.w3.org/TR/xml/#NT-CharData
*/
return buf.push(node.data
.replace(/[<&]/g,_xmlEncoder)
karfau marked this conversation as resolved.
Show resolved Hide resolved
.replace(/]]>/g, ']]&gt;')
);
case CDATA_SECTION_NODE:
return buf.push( '<![CDATA[',node.data,']]>');
case COMMENT_NODE:
Expand Down
2 changes: 1 addition & 1 deletion test/dom/serializer.test.js
Expand Up @@ -6,7 +6,7 @@ describe('XML Serializer', () => {
it('supports text node containing "]]>"', () => {
const doc = new DOMParser().parseFromString('<test/>', 'text/xml')
doc.documentElement.appendChild(doc.createTextNode('hello ]]> there'))
expect(doc.documentElement.firstChild.toString()).toBe('hello ]]> there')
expect(doc.documentElement.firstChild.toString()).toBe('hello ]]&gt; there')
})

it('supports <script> element with no children', () => {
Expand Down
14 changes: 7 additions & 7 deletions test/xmltest/__snapshots__/not-wf.test.js.snap
Expand Up @@ -144,7 +144,7 @@ Object {

exports[`xmltest/not-wellformed standalone should match 018.xml with snapshot 1`] = `
Object {
"actual": "<doc>&lt;![CDATA [ stuff]]></doc>",
"actual": "<doc>&lt;![CDATA [ stuff]]&gt;</doc>",
}
`;

Expand Down Expand Up @@ -205,13 +205,13 @@ Object {

exports[`xmltest/not-wellformed standalone should match 025.xml with snapshot 1`] = `
Object {
"actual": "<doc>]]></doc>",
"actual": "<doc>]]&gt;</doc>",
}
`;

exports[`xmltest/not-wellformed standalone should match 026.xml with snapshot 1`] = `
Object {
"actual": "<doc>]]]></doc>",
"actual": "<doc>]]]&gt;</doc>",
}
`;

Expand All @@ -237,7 +237,7 @@ Object {

exports[`xmltest/not-wellformed standalone should match 029.xml with snapshot 1`] = `
Object {
"actual": "<doc>abc]]]>def</doc>",
"actual": "<doc>abc]]]&gt;def</doc>",
}
`;

Expand Down Expand Up @@ -831,7 +831,7 @@ Object {
exports[`xmltest/not-wellformed standalone should match 108.xml with snapshot 1`] = `
Object {
"actual": "<doc>
&lt;![CDATA [ ]]>
&lt;![CDATA [ ]]&gt;
</doc>",
}
`;
Expand Down Expand Up @@ -870,7 +870,7 @@ Object {
exports[`xmltest/not-wellformed standalone should match 112.xml with snapshot 1`] = `
Object {
"actual": "<doc>
&lt;![cdata[data]]>
&lt;![cdata[data]]&gt;
</doc>",
}
`;
Expand Down Expand Up @@ -1354,7 +1354,7 @@ Object {

exports[`xmltest/not-wellformed standalone should match 181.xml with snapshot 1`] = `
Object {
"actual": "<doc>&amp;e;]]></doc>",
"actual": "<doc>&amp;e;]]&gt;</doc>",
"error": Array [
"[xmldom error] entity not found:&e;
@#[line:1,col:1]",
Expand Down