Skip to content

Commit

Permalink
fix: Escape < when serializing attribute values (#199)
Browse files Browse the repository at this point in the history
* fix: Escape `<` when serializing attribute values

to produce well formed XML.

> Well-formedness constraint: No `<` in Attribute Values
> The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a `<`.

https://www.w3.org/TR/xml/#CleanAttrVals
https://www.w3.org/TR/xml/#NT-AttValue

fixes #198

Co-authored-by: Chris Brody <[email protected]>
  • Loading branch information
karfau and brodycj authored Mar 14, 2021
1 parent bb12247 commit a681852
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
8 changes: 7 additions & 1 deletion lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,13 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
}
return;
case ATTRIBUTE_NODE:
return buf.push(' ',node.name,'="',node.value.replace(/[&"]/g,_xmlEncoder),'"');
/**
* Well-formedness constraint: No < in Attribute Values
* The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a <.
* @see https://www.w3.org/TR/xml/#CleanAttrVals
* @see https://www.w3.org/TR/xml/#NT-AttValue
*/
return buf.push(' ', node.name, '="', node.value.replace(/[<&"]/g,_xmlEncoder), '"');
case TEXT_NODE:
/**
* The ampersand character (&) and the left angle bracket (<) must not appear in their literal form,
Expand Down
6 changes: 3 additions & 3 deletions test/html/__snapshots__/normalize.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ Object {
exports[`html normalizer <div test="a<b&&a< c && a>d"></div> 1`] = `
Object {
"actual": "<div test=\\"a<b&amp;&amp;a< c &amp;&amp; a>d\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></div>",
"actual": "<div test=\\"a&lt;b&amp;&amp;a&lt; c &amp;&amp; a>d\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></div>",
}
`;

exports[`html normalizer <div test="alert('<br/>')"/> 1`] = `
Object {
"actual": "<div test=\\"alert('<br/>')\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></div>",
"actual": "<div test=\\"alert('&lt;br/>')\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></div>",
}
`;

Expand Down Expand Up @@ -90,7 +90,7 @@ Object {

exports[`html normalizer <html test="a<b && a>b && '&amp;&&'"/> 1`] = `
Object {
"actual": "<html test=\\"a<b &amp;&amp; a>b &amp;&amp; '&amp;&amp;&amp;'\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></html>",
"actual": "<html test=\\"a&lt;b &amp;&amp; a>b &amp;&amp; '&amp;&amp;&amp;'\\" xmlns=\\"http://www.w3.org/1999/xhtml\\"></html>",
}
`;

Expand Down
2 changes: 1 addition & 1 deletion test/parse/__snapshots__/locator.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`DOMLocator attribute position 1`] = `
Object {
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1<2\\"><table></table>&lt;;test</body></html>",
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1&lt;2\\"><table></table>&lt;;test</body></html>",
}
`;

Expand Down
4 changes: 2 additions & 2 deletions test/parse/__snapshots__/simple.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Object {

exports[`parse simple 1`] = `
Object {
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1<2\\"></body></html>",
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1&lt;2\\"></body></html>",
}
`;

Expand Down Expand Up @@ -49,6 +49,6 @@ Object {
exports[`parse wrong closing tag 1`] = `
Object {
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1<2\\"><table></table>&lt;;test</body></html>",
"actual": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><body title=\\"1&lt;2\\"><table></table>&lt;;test</body></html>",
}
`;
2 changes: 1 addition & 1 deletion test/xmltest/__snapshots__/not-wf.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Object {
exports[`xmltest/not-wellformed standalone should match 014.xml with snapshot 1`] = `
Object {
"actual": "<doc a1=\\"<foo>\\"/>",
"actual": "<doc a1=\\"&lt;foo>\\"/>",
}
`;
Expand Down
2 changes: 1 addition & 1 deletion test/xmltest/__snapshots__/valid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ Object {
exports[`xmltest/valid standalone should match 040.xml with snapshot 1`] = `
Object {
"actual": "<doc a1=\\"&quot;<&amp;>'\\"/>",
"actual": "<doc a1=\\"&quot;&lt;&amp;>'\\"/>",
"expected": "<doc a1=\\"&quot;&lt;&amp;&gt;'\\"></doc>",
}
`;
Expand Down

0 comments on commit a681852

Please sign in to comment.