summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/syntax/serializing-html-fragments/escaping.html
blob: 7bbc5566fc67964c0b07d24869ab7166ec611ebf (plain)
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" href="mailto:masonf@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>