insert_adjacent_html-xhtml.xhtml (4017B)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>insertAdjacentHTML in HTML</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="insert_adjacent_html.js"></script> 7 </head> 8 <body> 9 <p id="display"></p><div id="content" style="display: none"></div><div id="content2" style="display: none"></div> 10 <script><![CDATA[ 11 var script_ran = false; 12 13 function testPositions(node, testDesc) { 14 test(function() { 15 script_ran = false; 16 node.insertAdjacentHTML("beforeBegin", "\u003Cscript>script_ran = true;\u003C/script><i></i>"); 17 assert_equals(node.previousSibling.localName, "i", "Should have had <i> as previous sibling"); 18 assert_equals(node.previousSibling.previousSibling.localName, "script", "Should have had <script> as second previous child"); 19 assert_false(script_ran, "script should not have run"); 20 }, "beforeBegin " + node.id + " " + testDesc) 21 22 test(function() { 23 script_ran = false; 24 node.insertAdjacentHTML("Afterbegin", "<b></b>\u003Cscript>script_ran = true;\u003C/script>"); 25 assert_equals(node.firstChild.localName, "b", "Should have had <b> as first child"); 26 assert_equals(node.firstChild.nextSibling.localName, "script", "Should have had <script> as second child"); 27 assert_false(script_ran, "script should not have run"); 28 }, "Afterbegin " + node.id + " " + testDesc); 29 30 test(function() { 31 script_ran = false; 32 node.insertAdjacentHTML("BeforeEnd", "\u003Cscript>script_ran = true;\u003C/script><u></u>"); 33 assert_equals(node.lastChild.localName, "u", "Should have had <u> as last child"); 34 assert_equals(node.lastChild.previousSibling.localName, "script", "Should have had <script> as penultimate child"); 35 assert_false(script_ran, "script should not have run"); 36 }, "BeforeEnd " + node.id + " " + testDesc) 37 38 test(function() { 39 script_ran = false; 40 node.insertAdjacentHTML("afterend", "<a></a>\u003Cscript>script_ran = true;\u003C/script>"); 41 assert_equals(node.nextSibling.localName, "a", "Should have had <a> as next sibling"); 42 assert_equals(node.nextSibling.nextSibling.localName, "script", "Should have had <script> as second next sibling"); 43 assert_false(script_ran, "script should not have run"); 44 }, "afterend " + node.id + " " + testDesc) 45 } 46 47 var content = document.getElementById("content"); 48 testPositions(content, "without next sibling"); 49 testPositions(content, "again, with next sibling"); 50 51 test(function() { 52 assert_throws_dom("SYNTAX_ERR", function() {content.insertAdjacentHTML("bar", "foo")}); 53 assert_throws_dom("SYNTAX_ERR", function() {content.insertAdjacentHTML("beforebegİn", "foo")}); 54 assert_throws_dom("SYNTAX_ERR", function() {content.insertAdjacentHTML("beforebegın", "foo")}); 55 }, "Should throw when inserting with invalid position string"); 56 57 var parentElement = document.createElement("div"); 58 var child = document.createElement("div"); 59 child.id = "child"; 60 61 testThrowingNoParent(child, "null"); 62 testThrowingNoParent(document.documentElement, "a document"); 63 64 test(function() { 65 child.insertAdjacentHTML("afterBegin", "foo"); 66 child.insertAdjacentHTML("beforeend", "bar"); 67 assert_equals(child.textContent, "foobar"); 68 parentElement.appendChild(child); 69 }, "Inserting after being and before end should order things correctly"); 70 71 testPositions(child, "node not in tree but has parent"); 72 73 test(function() { 74 script_ran = false; 75 content.appendChild(parentElement); // must not run scripts 76 assert_false(script_ran, "script should not have run"); 77 }, "Should not run script when appending things which have descendant <script> inserted via insertAdjacentHTML"); 78 79 var content2 = document.getElementById("content2"); 80 testPositions(content2, "without next sibling"); 81 testPositions(content2, "test again, now that there's a next sibling"); 82 83 // XML-only: 84 test(function() { 85 assert_throws_dom("SYNTAX_ERR", function() {content.insertAdjacentHTML("beforeend", "<p>")}); 86 }); 87 88 ]]></script> 89 <div id="log"></div> 90 </body> 91 </html>