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: Escape < when serializing attribute values #199

Merged
merged 2 commits into from Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion lib/dom.js
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
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
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
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
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
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