Home | History | Annotate | Download | only in script-tests
      1 description('Test TreeWalker with skipping');
      2 
      3 var walker;
      4 var testElement = document.createElement("div");
      5 testElement.innerHTML='<div id="A1">  <div id="B1">  <div id="C1"></div>  </div>  <div id="B2"></div><div id="B3"></div>  </div>';
      6 
      7 var skipB1Filter = {
      8   acceptNode: function(node) {
      9     if (node.id == 'B1')
     10       return NodeFilter.FILTER_SKIP;
     11 
     12     return NodeFilter.FILTER_ACCEPT;
     13   }
     14 }
     15 
     16 var skipB2Filter = {
     17   acceptNode: function(node) {
     18     if (node.id == 'B2')
     19       return NodeFilter.FILTER_SKIP;
     20 
     21     return NodeFilter.FILTER_ACCEPT;
     22   }
     23 }
     24 
     25 debug("<br>Testing nextNode")
     26 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);
     27 shouldBe("walker.nextNode(); walker.currentNode.id", "'A1'");
     28 shouldBe("walker.nextNode(); walker.currentNode.id", "'C1'");
     29 shouldBe("walker.nextNode(); walker.currentNode.id", "'B2'");
     30 shouldBe("walker.nextNode(); walker.currentNode.id", "'B3'");
     31 
     32 debug("<br>Testing firstChild")
     33 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);
     34 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'");
     35 shouldBe("walker.firstChild(); walker.currentNode.id", "'C1'");
     36 
     37 debug("<br>Testing nextSibling")
     38 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB2Filter, false);
     39 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'");
     40 shouldBe("walker.firstChild(); walker.currentNode.id", "'B1'");
     41 shouldBe("walker.nextSibling(); walker.currentNode.id", "'B3'");
     42 
     43 debug("<br>Testing parentNode")
     44 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);
     45 walker.currentNode = testElement.querySelectorAll('#C1')[0];
     46 shouldBe("walker.parentNode(); walker.currentNode.id", "'A1'");
     47 
     48 debug("<br>Testing previousSibling")
     49 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB2Filter, false);
     50 walker.currentNode = testElement.querySelectorAll('#B3')[0];
     51 shouldBe("walker.previousSibling(); walker.currentNode.id", "'B1'");
     52 
     53 debug("<br>Testing previousNode")
     54 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);
     55 walker.currentNode = testElement.querySelectorAll('#B3')[0];
     56 shouldBe("walker.previousNode(); walker.currentNode.id", "'B2'");
     57 shouldBe("walker.previousNode(); walker.currentNode.id", "'C1'");
     58 shouldBe("walker.previousNode(); walker.currentNode.id", "'A1'");
     59 
     60 var successfullyParsed = true;
     61