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

backslash-escaped doublequotes in attributes after removeAttribute #218

Closed
milahu opened this issue Sep 19, 2022 · 2 comments
Closed

backslash-escaped doublequotes in attributes after removeAttribute #218

milahu opened this issue Sep 19, 2022 · 2 comments
Labels

Comments

@milahu
Copy link

milahu commented Sep 19, 2022

const { parse } = require('node-html-parser')

var insrc = `\
<html>
  <div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
  <div id="e" title='"world"' color="red">expected</div>
  <div id="a" title='"world"' onClick='alert("hello")' color="red">actual</div>
</html>
`;

var root = parse(insrc);

root.querySelectorAll('#e').forEach(node => {
  node.setAttribute('onClick', "alert('hello')");
})

root.querySelectorAll('#a').forEach(node => {
  //node.setAttribute('title', '"replaced"');
  node.removeAttribute('color'); // FIXME
})

console.log(root.toString());

result

<html>
  <div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
  <div id="e" title="&quot;world&quot;" color="red" onClick="alert('hello')">expected</div>
  <div id="a" title="\"world\"" onClick="alert(\"hello\")">actual</div>
</html>

title="\"world\"" is invalid html

should be either title='"world"' (pretty, minimal diff)
or title="&quot;world&quot;" (ugly, invasive)

@milahu
Copy link
Author

milahu commented Sep 29, 2022

similar problem:
newlines in attribute values are escaped as \n but should be &#10;

failing test

diff --git a/test/tests/html.js b/test/tests/html.js
index 3bcf636..8d85725 100644
--- a/test/tests/html.js
+++ b/test/tests/html.js
@@ -391,6 +391,13 @@ describe('HTML Parser', function () {
                                p.getAttribute('c').should.eql('undefined');
                                p.toString().should.eql('<p a="12" b="null" c="undefined"></p>');
                        });
+                       it('should escape newlines to html entities', function () {
+                               const root = parseHTML('<p></p>');
+                               const p = root.firstChild;
+                               p.setAttribute('a', '1\n2');
+                               p.getAttribute('a').should.eql('1\n2');
+                               p.toString().should.eql('<p a="1&#10;2"></p>');
+                       });
                        it('should throw type Error', function () {
                                const root = parseHTML('<p a=12 b=13 c=14></p>');
                                const p = root.firstChild;

@kasvith
Copy link

kasvith commented Oct 16, 2022

this happens when using setAttributes also

@taoqf taoqf added the bug label Nov 14, 2022
@taoqf taoqf closed this as completed in 6f6c824 Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants