Skip to content

Commit 8cda134

Browse files
committed
Add tests for entity escaping
1 parent 26b8442 commit 8cda134

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

test/interface/attr.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ assert(attributes.removeNamedItem('test'), void 0, 'removeNamedItem');
2020
assert(attributes.item(0), null, 'attributes.item()');
2121
assert(attributes.setNamedItem(attr), void 0, 'setNamedItem');
2222

23+
const {document: htmlDoc} = parseHTML('<element attr="&lt;a&quot;b&quot;c&gt;\t\n\r&#160;"></element>');
24+
25+
assert(htmlDoc.toString(), '<element attr="&lt;a&quot;b&quot;c&gt;\t\n\r&nbsp;"></element>');
26+
assert(htmlDoc.firstElementChild.toString(), '<element attr="&lt;a&quot;b&quot;c&gt;\t\n\r&nbsp;"></element>');
27+
assert(htmlDoc.firstElementChild.outerHTML, '<element attr="&lt;a&quot;b&quot;c&gt;\t\n\r&nbsp;"></element>');
28+
assert(htmlDoc.firstElementChild.attributes.attr.value, '<a"b"c>\t\n\r\xA0');
29+
2330
/** @type {(str: string) => Document} */
2431
const parseXML = xmlStr => new DOMParser().parseFromString(xmlStr, 'text/xml');
25-
const xmlDoc = parseXML('<element attr="a&quot;b&quot;c"></element>');
32+
const xmlDoc = parseXML('<element attr="&lt;a&quot;b&quot;c&gt;\t\n\r&#160;"></element>');
2633

27-
assert(xmlDoc.toString(), '<?xml version="1.0" encoding="utf-8"?><element attr="a&quot;b&quot;c" />');
28-
assert(xmlDoc.firstElementChild.toString(), '<element attr="a&quot;b&quot;c" />');
29-
assert(xmlDoc.firstElementChild.outerHTML, '<element attr="a&quot;b&quot;c" />');
30-
assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c');
34+
assert(xmlDoc.toString(), '<?xml version="1.0" encoding="utf-8"?><element attr="&lt;a&quot;b&quot;c&gt;&#x9;&#xA;&#xD;\xA0" />');
35+
assert(xmlDoc.firstElementChild.toString(), '<element attr="&lt;a&quot;b&quot;c&gt;&#x9;&#xA;&#xD;\xA0" />');
36+
assert(xmlDoc.firstElementChild.outerHTML, '<element attr="&lt;a&quot;b&quot;c&gt;&#x9;&#xA;&#xD;\xA0" />');
37+
assert(xmlDoc.firstElementChild.attributes.attr.value, '<a"b"c>\t\n\r\xA0');

test/shared/text-escaper.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
const BODY = '<body>Foo&#160;&quot;&#160;&quot;&#160;Bar</body>';
2-
const REBODY = '<body>Foo&nbsp;"&nbsp;"&nbsp;Bar</body>';
2+
const REBODY_IN_HTML = '<body>Foo&nbsp;"&nbsp;"&nbsp;Bar</body>';
3+
const REBODY_IN_XML = '<body>Foo\xA0"\xA0"\xA0Bar</body>';
34
const HTML = `<html id="html" class="live">${BODY}</html>`;
4-
const REHTML = `<html id="html" class="live">${REBODY}</html>`;
5+
const REHTML_IN_HTML = `<html id="html" class="live">${REBODY_IN_HTML}</html>`;
6+
const REHTML_IN_XML = `<html id="html" class="live">${REBODY_IN_XML}</html>`;
57

68
const assert = require('../assert.js').for('Text Escaper');
79

8-
const {parseHTML} = global[Symbol.for('linkedom')];
10+
const {parseHTML, DOMParser} = global[Symbol.for('linkedom')];
911

10-
const {document} = parseHTML('<!DOCTYPE html>' + HTML);
12+
let {document} = parseHTML('<!DOCTYPE html>' + HTML);
1113

12-
assert(document.documentElement.toString(), REHTML);
14+
assert(document.documentElement.toString(), REHTML_IN_HTML);
1315

1416
document.documentElement.innerHTML = BODY;
1517

16-
assert(document.documentElement.toString(), REHTML);
18+
assert(document.documentElement.toString(), REHTML_IN_HTML);
19+
20+
document.documentElement.innerHTML = '<body>&amp;amp;</body>';
21+
assert(document.documentElement.toString(), `<html id="html" class="live"><body>&amp;amp;</body></html>`);
22+
23+
document = (new DOMParser).parseFromString('<!DOCTYPE html>' + HTML, 'text/xml');
24+
25+
assert(document.documentElement.toString(), REHTML_IN_XML);
26+
27+
document.documentElement.innerHTML = BODY;
28+
29+
assert(document.documentElement.toString(), REHTML_IN_XML);
1730

1831
document.documentElement.innerHTML = '<body>&amp;amp;</body>';
1932
assert(document.documentElement.toString(), `<html id="html" class="live"><body>&amp;amp;</body></html>`);

0 commit comments

Comments
 (0)