test_bug319374.html (3541B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=319374 5 --> 6 <head> 7 <title>Test for Bug 319374</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=319374">Mozilla Bug 319374</a> 13 <p id="display"></p> 14 <div id="content"><custom-el></custom-el><custom-el></custom-el><custom-el></custom-el></div> 15 <pre id="test"> 16 <script class="testbody" type="text/javascript"> 17 18 customElements.define("custom-el", class extends HTMLElement { 19 constructor() { 20 super(); 21 this.attachShadow({ mode: "open" }); 22 this.shadowRoot.innerHTML = 23 `<span attr="attribute"><span></span></span><span> anon text </span><br>`; 24 } 25 }); 26 27 function testChangesInShadowDOM() { 28 // Test 1: Make sure that modifying anonymous content doesn't 29 // cause non-anonymous XPath result to throw exceptions.. 30 var counter = 0; 31 var error = null; 32 try { 33 var xp = new XPathEvaluator(); 34 var result = xp.evaluate("*", 35 document.getElementById('content'), 36 null, 37 XPathResult.UNORDERED_NODE_ITERATOR_TYPE, 38 null); 39 var res = null; 40 while ((res = result.iterateNext())) { 41 ++counter; 42 let anon = res.shadowRoot.childNodes; 43 anon[0].firstChild.remove(); // Removing a child node 44 anon[0].removeAttribute("attr1"); // Removing an attribute 45 anon[1].firstChild.data = "anon text changed" // Modifying text data 46 } 47 } catch (e) { 48 error = e; 49 } 50 ok(!error, error); 51 ok(counter == 3, "XPathEvaluator should have found 3 elements.") 52 53 // Test 2: If the context node is in anonymous content, changing some 54 // other anonymous tree shouldn't cause XPath result to throw. 55 let shadowAttr1 = document.getElementById("content").firstChild. 56 shadowRoot.firstChild.getAttributeNode("attr"); 57 let shadowAttr2 = document.getElementById("content").lastChild. 58 shadowRoot.firstChild.getAttributeNode("attr"); 59 var resultAttr = null; 60 try { 61 var xp2 = xp.evaluate(".", 62 shadowAttr1, 63 null, 64 XPathResult.UNORDERED_NODE_ITERATOR_TYPE, 65 null); 66 // Attribute changing in a different anonymous tree. 67 shadowAttr2.value = "foo"; 68 resultAttr = xp2.iterateNext(); 69 is(resultAttr, shadowAttr1, "XPathEvaluator returned wrong attribute!"); 70 } catch (e) { 71 ok(false, e); 72 } 73 74 // Test 3: If the anonymous tree in which context node is in is modified, 75 // XPath result should throw when iterateNext() is called. 76 resultAttr = null; 77 try { 78 var xp3 = xp.evaluate(".", 79 shadowAttr1, 80 null, 81 XPathResult.UNORDERED_NODE_ITERATOR_TYPE, 82 null); 83 // Attribute changing in the same anonymous tree. 84 shadowAttr1.ownerElement.setAttribute("foo", "bar"); 85 resultAttr = xp3.iterateNext(); 86 ok(resultAttr == shadowAttr1, 87 "XPathEvaluator should have thrown an exception!") 88 } catch (e) { 89 ok(true, e); 90 } 91 92 SimpleTest.finish(); 93 } 94 95 SimpleTest.waitForExplicitFinish(); 96 addLoadEvent(testChangesInShadowDOM); 97 </script> 98 </pre> 99 </body> 100 </html>