escaping.html (3204B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Serialization of script-disabled documents should follow escaping rules</title> 4 <link rel="author" href="mailto:masonf@chromium.org"> 5 <link rel="help" href="https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 9 <body> 10 11 <script> 12 const html_escaped = '& <>'; 13 const html_unescaped = '& <>'; 14 function getHtml(deEscapeParse) { 15 return `<noscript>${deEscapeParse ? html_escaped : html_unescaped}</noscript>`; 16 } 17 function testDoc(escapeSerialize, parsedNode) { 18 const node = parsedNode.firstChild; 19 const innerText = node.textContent; 20 assert_equals(innerText, html_unescaped, 'Content should be unescaped'); 21 const serialized = node.innerHTML; 22 const expectation = escapeSerialize ? html_escaped : html_unescaped; 23 assert_equals(serialized, expectation, `Serialization ${escapeSerialize ? 'should' : 'should NOT'} re-escape <noscript> contents`); 24 } 25 26 test(() => { 27 const div = document.createElement('div'); 28 document.body.appendChild(div); 29 div.innerHTML = getHtml(false); 30 testDoc(false, div); 31 }, 'div.innerHTML'); 32 33 test(() => { 34 const div = document.createElement('div'); 35 div.insertAdjacentHTML('afterbegin',getHtml(false)); 36 testDoc(false, div); 37 }, 'div.insertAdjacentHTML'); 38 39 test(() => { 40 const id = 'doc-write-1'; 41 document.write(`<div id=${id} style="display:none">${getHtml(false)}</div>`); 42 testDoc(false, document.getElementById(id)); 43 }, 'document.write on main document'); 44 45 test(() => { 46 const doc = (new DOMParser()).parseFromString(`<body>${getHtml(true)}</body>`, 'text/html'); 47 testDoc(true, doc.body); 48 }, 'DOMParser.parseFromString'); 49 50 test(() => { 51 const template = document.createElement('template'); 52 document.body.appendChild(template); 53 template.innerHTML = getHtml(true); 54 testDoc(true, template.content); 55 }, 'template.innerHTML'); 56 57 test(() => { 58 const doc = document.implementation.createHTMLDocument(''); 59 doc.body.innerHTML=`<pre>${getHtml(true)}</pre>`; 60 testDoc(true, doc.body.firstChild); 61 }, 'document.implementation.createHTMLDocument and innerHTML'); 62 63 test(() => { 64 const doc = document.implementation.createHTMLDocument(''); 65 let range = doc.createRange(); 66 range.selectNode(doc.body); 67 const frag = range.createContextualFragment(getHtml(true)); 68 testDoc(true, frag); 69 }, 'document.implementation.createHTMLDocument and createContextualFragment'); 70 71 test(() => { 72 const id = 'doc-write-2'; 73 const doc = document.implementation.createHTMLDocument(''); 74 doc.write(`<div id=${id} style="display:none">${getHtml(false)}</div>`); 75 testDoc(true, doc.getElementById(id)); 76 }, 'document.implementation.createHTMLDocument and document.write'); 77 78 async_test((t) => { 79 let client = new XMLHttpRequest(); 80 client.addEventListener('load', t.step_func_done(() => { 81 assert_true(client.status == 200 && client.responseXML != null); 82 testDoc(true, client.responseXML.body.firstChild); 83 t.done(); 84 })); 85 client.open("GET", `data:text/html,<pre>${getHtml(true)}</pre>`); 86 client.responseType = 'document'; 87 client.send(); 88 }, 'XMLHttpRequest'); 89 </script>