1 description('Test JS objects as NodeFilters.'); 2 3 var walker; 4 var testElement = document.createElement("div"); 5 testElement.id = 'root'; 6 testElement.innerHTML='<div id="A1"><div id="B1"></div><div id="B2"></div></div>'; 7 8 function filter(node) 9 { 10 debug(" filtering node " + node.id); 11 if (node.id == "B1") 12 return NodeFilter.FILTER_SKIP; 13 return NodeFilter.FILTER_ACCEPT; 14 } 15 16 debug("Testing with raw function filter"); 17 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, filter, false); 18 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 19 shouldBe("walker.nextNode(); walker.currentNode.id", "'B2'"); 20 21 debug("<br>Testing with object filter"); 22 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, { 23 acceptNode : function(node) { 24 debug(" filtering node " + node.id); 25 if (node.id == "B1") 26 return NodeFilter.FILTER_SKIP; 27 return NodeFilter.FILTER_ACCEPT; 28 } 29 }, false); 30 31 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 32 shouldBe("walker.nextNode(); walker.currentNode.id", "'B2'"); 33 34 debug("<br>Testing with null filter"); 35 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, null, false); 36 37 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 38 shouldBe("walker.nextNode(); walker.currentNode.id", "'B1'"); 39 40 debug("<br>Testing with undefined filter"); 41 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, undefined, false); 42 43 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 44 shouldBe("walker.nextNode(); walker.currentNode.id", "'B1'"); 45 46 debug("<br>Testing with object lacking acceptNode property"); 47 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, {}, false); 48 49 shouldThrow("walker.firstChild();"); 50 shouldBe("walker.currentNode.id;", "'root'"); 51 shouldThrow("walker.nextNode();"); 52 shouldBe("walker.currentNode.id;", "'root'"); 53 54 debug("<br>Testing with object with non-function acceptNode property"); 55 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, { acceptNode: "foo" }, false); 56 57 shouldThrow("walker.firstChild();"); 58 shouldBe("walker.currentNode.id;", "'root'"); 59 shouldThrow("walker.nextNode();"); 60 shouldBe("walker.currentNode.id;", "'root'"); 61 62 debug("<br>Testing with function having acceptNode function"); 63 var filter = function() { return NodeFilter.FILTER_ACCEPT; }; 64 filter.acceptNode = function(node) { return NodeFilter.FILTER_SKIP; }; 65 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, filter, false); 66 67 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 68 shouldBe("walker.nextNode(); walker.currentNode.id", "'B1'"); 69 70 debug("<br>Testing acceptNode callee"); 71 var filter = { 72 acceptNode: function(node) { 73 debug('Callee: ' + arguments.callee); 74 return NodeFilter.FILTER_ACCEPT; 75 } 76 }; 77 walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, filter, false); 78 79 shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'"); 80 81 var successfullyParsed = true; 82