Skip to content

Commit 6f6c824

Browse files
committed
fix: #218
1 parent cdadb13 commit 6f6c824

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/nodes/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default class HTMLElement extends Node {
158158
return 'null';
159159
}
160160

161-
return JSON.stringify(attr.replace(/"/g, '"')).replace(/\\t/g, '\t').replace(/\\n/g, '\n').replace(/\\r/g, '\r');
161+
return JSON.stringify(attr.replace(/"/g, '"')).replace(/\\t/g, '\t').replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\/g, '');
162162
}
163163

164164
/**
@@ -713,7 +713,7 @@ export default class HTMLElement extends Node {
713713
// Update rawString
714714
this.rawAttrs = Object.keys(attrs)
715715
.map((name) => {
716-
const val = JSON.stringify(attrs[name]);
716+
const val = this.quoteAttribute(attrs[name]);
717717
if (val === undefined || val === 'null') {
718718
return name;
719719
}

test/tests/issues/218.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { parse } = require('@test/test-target');
2+
3+
describe('issue 218', function () {
4+
it('attribute value contains quote should be parsed correct', function () {
5+
const html = `<html>
6+
<div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
7+
<div id="e" title='"world"' color="red">expected</div>
8+
<div id="a" title='"world"' onClick='alert("hello")' color="red">actual</div>
9+
</html>`;
10+
const root = parse(html);
11+
root.toString().should.eql(`<html>
12+
<div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
13+
<div id="e" title='"world"' color="red">expected</div>
14+
<div id="a" title='"world"' onClick='alert("hello")' color="red">actual</div>
15+
</html>`);
16+
root.querySelector('#e').setAttribute('onClick', "alert('hello')");
17+
18+
root.toString().should.eql(`<html>
19+
<div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
20+
<div id="e" title="&quot;world&quot;" color="red" onClick="alert('hello')">expected</div>
21+
<div id="a" title='"world"' onClick='alert("hello")' color="red">actual</div>
22+
</html>`);
23+
24+
// root.querySelector('#a').setAttribute('title', '"replaced"');
25+
root.querySelector('#a').removeAttribute('color'); // FIXME
26+
27+
root.toString().should.eql(`<html>
28+
<div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
29+
<div id="e" title="&quot;world&quot;" color="red" onClick="alert('hello')">expected</div>
30+
<div id="a" title="&quot;world&quot;" onClick="alert(&quot;hello&quot;)">actual</div>
31+
</html>`);
32+
root.querySelector('#a').setAttribute('title', '"replaced"');
33+
root.toString().should.eql(`<html>
34+
<div id="_" title='"world"' onClick='alert("hello")' color="red">nochange</div>
35+
<div id="e" title="&quot;world&quot;" color="red" onClick="alert('hello')">expected</div>
36+
<div id="a" title="&quot;replaced&quot;" onClick="alert(&quot;hello&quot;)">actual</div>
37+
</html>`);
38+
});
39+
it('should escape newlines to html entities', function () {
40+
const root = parse('<p></p>');
41+
const p = root.firstChild;
42+
p.setAttribute('a', '1\n2');
43+
p.getAttribute('a').should.eql('1\n2');
44+
p.toString().should.eql('<p a="1\n2"></p>');
45+
});
46+
});

0 commit comments

Comments
 (0)