File: escaping.html

package info (click to toggle)
firefox-esr 78.15.0esr-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,301,296 kB
  • sloc: cpp: 5,665,905; javascript: 4,798,386; ansic: 2,878,233; python: 977,004; asm: 270,347; xml: 181,456; java: 111,756; sh: 72,926; makefile: 21,819; perl: 13,380; cs: 4,725; yacc: 4,565; objc: 3,026; pascal: 1,787; lex: 1,720; ada: 1,681; exp: 505; php: 436; lisp: 260; awk: 152; ruby: 103; csh: 80; sed: 53; sql: 45
file content (89 lines) | stat: -rw-r--r-- 3,228 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<meta charset="utf-8">
<title>Serialization of script-disabled documents should follow escaping rules</title>
<link rel="author" title="Mason Freed" href="mailto:masonfreed@chromium.org">
<link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>

<script>
const html_escaped = '&amp;&nbsp;&lt;&gt;';
const html_unescaped = '& <>';
function getHtml(deEscapeParse) {
  return `<noscript>${deEscapeParse ? html_escaped : html_unescaped}</noscript>`;
}
function testDoc(escapeSerialize, parsedNode) {
  const node = parsedNode.firstChild;
  const innerText = node.textContent;
  assert_equals(innerText, html_unescaped, 'Content should be unescaped');
  const serialized = node.innerHTML;
  const expectation = escapeSerialize ? html_escaped : html_unescaped;
  assert_equals(serialized, expectation, `Serialization ${escapeSerialize ? 'should' : 'should NOT'} re-escape <noscript> contents`);
}

test(() => {
  const div = document.createElement('div');
  document.body.appendChild(div);
  div.innerHTML = getHtml(false);
  testDoc(false, div);
}, 'div.innerHTML');

test(() => {
  const div = document.createElement('div');
  div.insertAdjacentHTML('afterbegin',getHtml(false));
  testDoc(false, div);
}, 'div.insertAdjacentHTML');

test(() => {
  const id = 'doc-write-1';
  document.write(`<div id=${id} style="display:none">${getHtml(false)}</div>`);
  testDoc(false, document.getElementById(id));
}, 'document.write on main document');

test(() => {
  const doc = (new DOMParser()).parseFromString(`<body>${getHtml(true)}</body>`, 'text/html');
  testDoc(true, doc.body);
}, 'DOMParser.parseFromString');

test(() => {
  const template = document.createElement('template');
  document.body.appendChild(template);
  template.innerHTML = getHtml(true);
  testDoc(true, template.content);
}, 'template.innerHTML');

test(() => {
  const doc = document.implementation.createHTMLDocument('');
  doc.body.innerHTML=`<pre>${getHtml(true)}</pre>`;
  testDoc(true, doc.body.firstChild);
}, 'document.implementation.createHTMLDocument and innerHTML');

test(() => {
  const doc = document.implementation.createHTMLDocument('');
  let range = doc.createRange();
  range.selectNode(doc.body);
  const frag = range.createContextualFragment(getHtml(true));
  testDoc(true, frag);
}, 'document.implementation.createHTMLDocument and createContextualFragment');

test(() => {
  const id = 'doc-write-2';
  const doc = document.implementation.createHTMLDocument('');
  doc.write(`<div id=${id} style="display:none">${getHtml(false)}</div>`);
  testDoc(true, doc.getElementById(id));
}, 'document.implementation.createHTMLDocument and document.write');

async_test((t) => {
  let client = new XMLHttpRequest();
  client.addEventListener('load', t.step_func_done(() => {
    assert_true(client.status == 200 && client.responseXML != null);
    testDoc(true, client.responseXML.body.firstChild);
    t.done();
  }));
  client.open("GET", `data:text/html,<pre>${getHtml(true)}</pre>`);
  client.responseType = 'document';
  client.send();
}, 'XMLHttpRequest');
</script>