8a09940b1e79a3b01b69532852c5c67a6a06d2f9
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / dom / traversal / TreeWalker-currentNode.html
1 <!DOCTYPE html>
2 <html>
3 <!--
4 Test adapted from https://dxr.mozilla.org/chromium/source/src/third_party/WebKit/LayoutTests/fast/dom/TreeWalker/resources/TreeWalker-currentNode.js
5 -->
6 <head>
7 <title>TreeWalker: currentNode</title>
8 <script src="/resources/testharness.js"></script>
9 <script src="/resources/testharnessreport.js"></script>
10 <script src="traversal-support.js"></script>
11 <div id=log></div>
12 </head>
13 <body>
14 <div id='parent'>
15 <div id='subTree'><p>Lorem ipsum <span>dolor <b>sit</b> amet</span>, consectetur <i>adipisicing</i> elit, sed do eiusmod <tt>tempor <b><i>incididunt ut</i> labore</b> et dolore magna</tt> aliqua.</p></div>
16 </div>
17 <p>Test TreeWalker currentNode functionality</p>
18 <script>
19 // var subTree = document.createElement('div');
20 // subTree.innerHTML = "<p>Lorem ipsum <span>dolor <b>sit</b> amet</span>, consectetur <i>adipisicing</i> elit, sed do eiusmod <tt>tempor <b><i>incididunt ut</i> labore</b> et dolore magna</tt> aliqua.</p>"
21 // document.body.appendChild(subTree);
22 var subTree = document.getElementById("subTree");
23
24 var all = function(node) { return true; }
25
26 test(function()
27 {
28     var w = document.createTreeWalker(subTree, NodeFilter.SHOW_ELEMENT, all);
29     assert_node(w.currentNode, { type: Element, id: 'subTree' });
30     assert_equals(w.parentNode(), null);
31     assert_node(w.currentNode, { type: Element, id: 'subTree' });
32 }, "Test that TreeWalker.parent() doesn't set the currentNode to a node not under the root.");
33
34 test(function()
35 {
36     var w = document.createTreeWalker(subTree,
37                                       NodeFilter.SHOW_ELEMENT
38                                       | NodeFilter.SHOW_COMMENT,
39                                       all);
40     w.currentNode = document.documentElement;
41     assert_equals(w.parentNode(), null);
42     assert_equals(w.currentNode, document.documentElement);
43     w.currentNode = document.documentElement;
44     assert_equals(w.nextNode(), document.documentElement.firstChild);
45     assert_equals(w.currentNode, document.documentElement.firstChild);
46     w.currentNode = document.documentElement;
47     assert_equals(w.previousNode(), null);
48     assert_equals(w.currentNode, document.documentElement);
49     w.currentNode = document.documentElement;
50     assert_equals(w.firstChild(), document.documentElement.firstChild);
51     assert_equals(w.currentNode, document.documentElement.firstChild);
52     w.currentNode = document.documentElement;
53     assert_equals(w.lastChild(), document.documentElement.lastChild);
54     assert_equals(w.currentNode, document.documentElement.lastChild);
55     w.currentNode = document.documentElement;
56     assert_equals(w.nextSibling(), null);
57     assert_equals(w.currentNode, document.documentElement);
58     w.currentNode = document.documentElement;
59     assert_equals(w.previousSibling(), null);
60     assert_equals(w.currentNode, document.documentElement);
61 }, "Test that we handle setting the currentNode to arbitrary nodes not under the root element.");
62
63 test(function()
64 {
65     var w = document.createTreeWalker(subTree, NodeFilter.SHOW_ELEMENT, all);
66     w.currentNode = subTree.previousSibling;
67     assert_equals(w.nextNode(), subTree);
68     w.currentNode = document.getElementById("parent");
69     assert_equals(w.firstChild(), subTree);
70 }, "Test how we handle the case when the traversed to node is within the root, but the currentElement is not.");
71 </script>
72 </body>
73 </html>