tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug522601-shadow.xhtml (13644B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <!--
      3 https://bugzilla.mozilla.org/show_bug.cgi?id=522601
      4 -->
      5 <head>
      6  <title>Test for Bug 522601</title>
      7  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      9 </head>
     10 <body>
     11 <template id="template"><div><slot/></div><slot name="foo"/></template>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=522601">Mozilla Bug 522601</a>
     13 <custom-element id="display">
     14  <span slot="foo" id="s">This is some text</span>
     15  More text
     16  <b id="b">Even more <i id="i1">Italic</i>text<i id="i2">And more italic</i></b></custom-element>
     17 <div id="content" style="display: none">
     18 </div>
     19 <div id="subdoc">
     20  <iframe id="frame1" src="file_bug522601.html">frame text</iframe>
     21 </div>
     22 <pre id="test">
     23 <script>
     24 <![CDATA[
     25 
     26 /** Test for Bug 522601 */
     27 SimpleTest.waitForExplicitFinish();
     28 
     29 customElements.define("custom-element", class extends HTMLElement {
     30  constructor() {
     31    super();
     32    const template = document.getElementById("template");
     33    const shadowRoot = this.attachShadow({mode: "open"})
     34      .appendChild(template.content.cloneNode(true));
     35  }
     36 });
     37 
     38 function testFunc(walker, func, expectedNode, str) {
     39  var oldCurrent = SpecialPowers.unwrap(walker.currentNode);
     40  var newNode = SpecialPowers.unwrap(walker[func]());
     41  is(newNode, expectedNode, "Unexpected node after " + str);
     42  is(SpecialPowers.unwrap(walker.currentNode), newNode ? newNode : oldCurrent,
     43     "Unexpected current node after " + str);
     44 }
     45 
     46 addLoadEvent(function() {
     47 var walkerSubDocument =
     48    SpecialPowers.Cc["@mozilla.org/inspector/deep-tree-walker;1"]
     49              .createInstance(SpecialPowers.Ci.inIDeepTreeWalker);
     50  walkerSubDocument.showAnonymousContent = false;
     51  walkerSubDocument.showSubDocuments = true;
     52  walkerSubDocument.init($("frame1"));
     53 
     54  is(SpecialPowers.unwrap(walkerSubDocument.currentNode), $("frame1"), "Unexpected sub-doc root");
     55  testFunc(walkerSubDocument, "firstChild", $("frame1").contentDocument.doctype,
     56           "step to sub documents doctype");
     57  testFunc(walkerSubDocument, "nextSibling", $("frame1").contentDocument.documentElement,
     58           "step to sub documents documentElement");
     59 
     60  walkerSubDocument =
     61    SpecialPowers.Cc["@mozilla.org/inspector/deep-tree-walker;1"]
     62              .createInstance(SpecialPowers.Ci.inIDeepTreeWalker);
     63  walkerSubDocument.showAnonymousContent = false;
     64  walkerSubDocument.showSubDocuments = true;
     65  walkerSubDocument.showDocumentsAsNodes = true;
     66  walkerSubDocument.init($("frame1"));
     67 
     68  is(SpecialPowers.unwrap(walkerSubDocument.currentNode), $("frame1"), "Unexpected sub-doc root");
     69  testFunc(walkerSubDocument, "firstChild", $("frame1").contentDocument,
     70           "step to sub document");
     71  testFunc(walkerSubDocument, "firstChild", $("frame1").contentDocument.doctype,
     72           "step to sub documents doctype");
     73  testFunc(walkerSubDocument, "nextSibling", $("frame1").contentDocument.documentElement,
     74           "step to sub documents documentElement");
     75 
     76  walkerSubDocument.currentNode = $("frame1").contentDocument;
     77  is(SpecialPowers.unwrap(walkerSubDocument.currentNode), $("frame1").contentDocument,
     78     "setting currentNode to sub document");
     79  testFunc(walkerSubDocument, "nextSibling", null,
     80           "nextSibling for sub document is null");
     81 
     82  var walkerFrameChild =
     83    SpecialPowers.Cc["@mozilla.org/inspector/deep-tree-walker;1"]
     84              .createInstance(SpecialPowers.Ci.inIDeepTreeWalker);
     85  walkerFrameChild.showAnonymousContent = false;
     86  walkerFrameChild.showSubDocuments = false;
     87  walkerFrameChild.init($("frame1"));
     88 
     89  is(SpecialPowers.unwrap(walkerFrameChild.currentNode), $("frame1"), "Unexpected sub-doc root");
     90  testFunc(walkerFrameChild, "firstChild", $("frame1").firstChild,
     91           "step to frames child");
     92 
     93  var walkerNonAnon =
     94    SpecialPowers.Cc["@mozilla.org/inspector/deep-tree-walker;1"]
     95              .createInstance(SpecialPowers.Ci.inIDeepTreeWalker);
     96  walkerNonAnon.init($("display"));
     97  walkerNonAnon.showAnonymousContent = false;
     98 
     99  is(SpecialPowers.unwrap(walkerNonAnon.currentNode), $("display"), "Unexpected non-anon root");
    100  testFunc(walkerNonAnon, "nextNode", $("s").previousSibling,
    101           "step to some text");
    102  testFunc(walkerNonAnon, "nextNode", $("s"), "step to span");
    103  testFunc(walkerNonAnon, "nextNode", $("s").firstChild, "step to span text");
    104  testFunc(walkerNonAnon, "nextNode", $("s").nextSibling, "step to more text");
    105  testFunc(walkerNonAnon, "nextNode", $("b"), "step to bold");
    106  testFunc(walkerNonAnon, "nextNode", $("b").firstChild, "step to bold text");
    107  testFunc(walkerNonAnon, "nextNode", $("i1"), "step to first italic");
    108  testFunc(walkerNonAnon, "nextNode", $("i1").firstChild,
    109           "step to first italic text");
    110  testFunc(walkerNonAnon, "nextNode", $("i1").nextSibling,
    111           "step to more bold text");
    112  testFunc(walkerNonAnon, "nextNode", $("i2"), "step to second italic");
    113  testFunc(walkerNonAnon, "nextNode", $("i2").firstChild,
    114           "step to second italic text");
    115  testFunc(walkerNonAnon, "nextNode", null, "step past end");
    116  testFunc(walkerNonAnon, "parentNode", $("i2"), "step up to second italic");
    117  testFunc(walkerNonAnon, "parentNode", $("b"), "step up to bold");
    118  testFunc(walkerNonAnon, "nextNode", $("b").firstChild, "step to bold text again");
    119  testFunc(walkerNonAnon, "parentNode", $("b"), "step up to bold again");
    120  testFunc(walkerNonAnon, "parentNode", $("display"), "step up to display");
    121  testFunc(walkerNonAnon, "parentNode", null, "step up past root");
    122  testFunc(walkerNonAnon, "firstChild", $("s").previousSibling,
    123           "step firstChild to display first child");
    124  testFunc(walkerNonAnon, "nextSibling", $("s"),
    125           "step nextSibling to span");
    126  testFunc(walkerNonAnon, "nextSibling", $("s").nextSibling,
    127           "step nextSibling to more text");
    128  testFunc(walkerNonAnon, "nextSibling", $("b"), "step nextSibling to bold");
    129  testFunc(walkerNonAnon, "nextSibling", null, "step nextSibling past end");
    130  testFunc(walkerNonAnon, "previousSibling", $("s").nextSibling,
    131           "step previousSibling to more text");
    132  testFunc(walkerNonAnon, "previousSibling", $("s"),
    133           "step previousSibling to span");
    134  testFunc(walkerNonAnon, "previousSibling", $("s").previousSibling,
    135           "step previousSibling to display first child");
    136  testFunc(walkerNonAnon, "previousSibling", null,
    137           "step previousSibling past end");
    138 
    139  // Move the walker over to the end
    140  while (walkerNonAnon.nextNode()) { /* do nothing */ }
    141 
    142  is(SpecialPowers.unwrap(walkerNonAnon.currentNode), $("i2").firstChild, "unexpected last node");
    143  testFunc(walkerNonAnon, "previousNode", $("i2"), "step back to second italic");
    144  testFunc(walkerNonAnon, "previousNode", $("i1").nextSibling,
    145           "step back to more bold text");
    146  testFunc(walkerNonAnon, "previousNode", $("i1").firstChild,
    147           "step back to first italic text");
    148  testFunc(walkerNonAnon, "previousNode", $("i1"), "step back to first italic");
    149  testFunc(walkerNonAnon, "previousNode", $("b").firstChild,
    150           "step back to bold text");
    151  testFunc(walkerNonAnon, "previousNode", $("b"), "step back to bold");
    152  testFunc(walkerNonAnon, "previousNode", $("s").nextSibling, "step back to more text");
    153  testFunc(walkerNonAnon, "previousNode", $("s").firstChild, "step back to span text");
    154  testFunc(walkerNonAnon, "previousNode", $("s"), "step back to span");
    155  testFunc(walkerNonAnon, "previousNode", $("s").previousSibling,
    156           "step back to some text");
    157  testFunc(walkerNonAnon, "previousNode", $("display"),
    158           "step back to root");
    159  testFunc(walkerNonAnon, "previousNode", null,
    160           "step back past root");
    161 
    162  walkerNonAnon.currentNode =  $("s");
    163  is(SpecialPowers.unwrap(walkerNonAnon.currentNode), SpecialPowers.unwrap($("s")), 
    164     "Setting currentNode to span");
    165 
    166  var anonDiv = $("display").shadowRoot.children[0];
    167 
    168  try {
    169    walkerNonAnon.currentNode = anonDiv;
    170    // See bug 1586916.
    171    todo(false, "Setting current node to a node that is otherwise unreachable," +
    172              " with the current visibility settings should throw");
    173  } catch(e) {
    174    ok(e.toString().indexOf("NS_ERROR_ILLEGAL_VALUE") > -1, "Setting current node to an anon node should throw" +
    175       " NS_ERROR_ILLEGAL_VALUE if showAnonymousContent is set to false");
    176    is(SpecialPowers.unwrap(walkerNonAnon.currentNode), SpecialPowers.unwrap($("s")), 
    177       "An unsuccessfull set currentNode should leave behind the old state");
    178    testFunc(walkerNonAnon, "nextSibling", $("s").nextSibling, "nextSibling after set currentNode");
    179  }
    180 
    181  var slot = $("display").shadowRoot.querySelectorAll("slot")[0];
    182  var namedSlot = $("display").shadowRoot.querySelectorAll("slot")[1];
    183 
    184  var walkerAnon =
    185    SpecialPowers.Cc["@mozilla.org/inspector/deep-tree-walker;1"]
    186              .createInstance(SpecialPowers.Ci.inIDeepTreeWalker);
    187  walkerAnon.showAnonymousContent = true;
    188  walkerAnon.init($("display"));
    189 
    190  is(SpecialPowers.unwrap(walkerAnon.currentNode), $("display"), "Unexpected anon root");
    191  testFunc(walkerAnon, "nextNode", $("display").shadowRoot,
    192           "step to shadow root");
    193  testFunc(walkerAnon, "nextNode", anonDiv,
    194           "step to anonymous div");
    195  testFunc(walkerAnon, "nextNode", slot, "step into slot");
    196  testFunc(walkerAnon, "nextNode", namedSlot, "step into named slot");
    197  testFunc(walkerAnon, "nextNode", $("s").previousSibling, "step to light dom text (out of shadow tree)");
    198  testFunc(walkerAnon, "nextNode", $("s"), "step to span (anon)");
    199  testFunc(walkerAnon, "nextNode", $("s").firstChild, "step to span text (anon)");
    200  testFunc(walkerAnon, "nextNode", $("s").nextSibling, "step to more text (anon)");
    201  testFunc(walkerAnon, "nextNode", $("b"), "step to bold (anon)");
    202  testFunc(walkerAnon, "nextNode", $("b").firstChild, "step to bold text (anon)");
    203  testFunc(walkerAnon, "nextNode", $("i1"), "step to first italic (anon)");
    204  testFunc(walkerAnon, "nextNode", $("i1").firstChild,
    205           "step to first italic text (anon)");
    206  testFunc(walkerAnon, "nextNode", $("i1").nextSibling,
    207           "step to more bold text (anon)");
    208  testFunc(walkerAnon, "nextNode", $("i2"), "step to second italic (anon)");
    209  testFunc(walkerAnon, "nextNode", $("i2").firstChild,
    210           "step to second italic text (anon)");
    211  testFunc(walkerAnon, "nextNode", null, "step past end (anon)");
    212  testFunc(walkerAnon, "parentNode", $("i2"), "step up to italic (anon)");
    213  testFunc(walkerAnon, "parentNode", $("b"), "step up to bold (anon)");
    214  testFunc(walkerAnon, "parentNode", $("display"), "step up to display (anon)");
    215  testFunc(walkerAnon, "nextNode", $("display").shadowRoot, "step to shadow root again");
    216  testFunc(walkerAnon, "parentNode", $("display"),
    217           "step up to display again (anon)")
    218  testFunc(walkerAnon, "parentNode", null, "step up past root (anon)");
    219  testFunc(walkerAnon, "firstChild", $("display").shadowRoot,
    220           "step firstChild to display first child (anon)");
    221  testFunc(walkerAnon, "nextSibling", $("s").previousSibling,
    222           "step nextSibling text (anon)");
    223  testFunc(walkerAnon, "nextSibling", $("s"),
    224           "step nextSibling span (anon)");
    225  testFunc(walkerAnon, "nextSibling", $("s").nextSibling,
    226           "step nextSibling more text (anon)");
    227  testFunc(walkerAnon, "nextSibling", $("b"),
    228           "step nextSibling bold (anon)");
    229  testFunc(walkerAnon, "nextSibling", null, "step nextSibling past end (anon)");
    230  testFunc(walkerAnon, "previousSibling", $("s").nextSibling,
    231           "step previousSibling to more text");
    232  testFunc(walkerAnon, "previousSibling", $("s"),
    233           "step previousSibling to span");
    234  testFunc(walkerAnon, "previousSibling", $("s").previousSibling,
    235           "step previousSibling to prev text");
    236  testFunc(walkerAnon, "previousSibling", $("display").shadowRoot,
    237           "step shadowRoot");
    238  testFunc(walkerAnon, "previousSibling", null, "step previousSibling past end (anon)");
    239 
    240  // Move the walker over to the end
    241  while (walkerAnon.nextNode()) { /* do nothing */ }
    242 
    243  testFunc(walkerAnon, "previousNode", $("i2"), "step back to second italic (anon)");
    244  testFunc(walkerAnon, "previousNode", $("i1").nextSibling,
    245           "step back to more bold text (anon)");
    246  testFunc(walkerAnon, "previousNode", $("i1").firstChild,
    247           "step back to first italic text (anon)");
    248  testFunc(walkerAnon, "previousNode", $("i1"), "step back to first italic (anon)");
    249  testFunc(walkerAnon, "previousNode", $("b").firstChild, "step back to bold text (anon)");
    250  testFunc(walkerAnon, "previousNode", $("b"), "step back to bold (anon)");
    251  testFunc(walkerAnon, "previousNode", $("s").nextSibling, "step back to more text (anon)");
    252  testFunc(walkerAnon, "previousNode", $("s").firstChild,
    253           "step back to span text (anon)");
    254  testFunc(walkerAnon, "previousNode", $("s"),
    255           "step back to span (anon)");
    256  testFunc(walkerAnon, "previousNode", $("s").previousSibling,
    257           "step back to some text (anon)");
    258  testFunc(walkerAnon, "previousNode", namedSlot, "step back to named slot");
    259  testFunc(walkerAnon, "previousNode", slot, "step back to slot");
    260  testFunc(walkerAnon, "previousNode", anonDiv,
    261           "step back to anonymous div");
    262  testFunc(walkerAnon, "previousNode", $("display").shadowRoot, "step back to shadow root (anon)");
    263  testFunc(walkerAnon, "previousNode", $("display"), "step back to root (anon)");
    264  testFunc(walkerAnon, "previousNode", null, "step back past root (anon)");
    265 
    266  SimpleTest.finish();
    267 });
    268 
    269 ]]>
    270 </script>
    271 </pre>
    272 </body>
    273 </html>