text-html-elements.html (2660B)
1 <!doctype html> 2 <meta charset="utf8"> 3 <title>XPath in text/html: elements</title> 4 <link rel="help" href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#Interfaces"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <body> 8 <div id="log"><span></span></div> 9 <div><span></span></div> 10 <dØdd></dØdd> 11 <svg> 12 <path /> 13 <path /> 14 </svg> 15 16 <script> 17 function test_xpath_succeeds(path, expected, resolver) { 18 resolver = resolver ? resolver : null; 19 var res = document.evaluate(path, document, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 20 actual = []; 21 for (var i=0;;i++) { 22 var node = res.snapshotItem(i); 23 if (node === null) { 24 break; 25 } 26 actual.push(node); 27 } 28 assert_array_equals(actual, expected); 29 } 30 31 function test_xpath_throws(path, error_code, resolver) { 32 resolver = resolver ? resolver : null; 33 assert_throws_dom(error_code, function() {document.evaluate(path, document, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)}) 34 } 35 36 function ns_resolver(x) { 37 map = {"html":"http://www.w3.org/1999/xhtml", 38 "svg":"http://www.w3.org/2000/svg", 39 "math":"http://www.w3.org/1998/Math/MathML"}; 40 var rv = map.hasOwnProperty(x) ? map[x] : null; 41 return rv; 42 } 43 44 test(function() { 45 test_xpath_succeeds("//div", document.getElementsByTagName("div")); 46 }, "HTML elements no namespace prefix"); 47 48 test(function() { 49 test_xpath_succeeds("//html:div", document.getElementsByTagName("div"), ns_resolver); 50 }, "HTML elements namespace prefix"); 51 52 test(function() { 53 test_xpath_succeeds("//html:div/span", document.getElementsByTagName("span"), ns_resolver); 54 }, "HTML elements mixed use of prefix"); 55 56 test(function() { 57 test_xpath_succeeds("//path", []); 58 }, "SVG elements no namespace prefix"); 59 60 test(function() { 61 test_xpath_succeeds("//svg:path", document.getElementsByTagName("path"), ns_resolver); 62 }, "SVG elements namespace prefix"); 63 64 test(function() { 65 test_xpath_succeeds("//DiV", document.getElementsByTagName("div")); 66 }, "HTML elements mixed case"); 67 68 test(function() { 69 test_xpath_succeeds("//svg:PatH", [], ns_resolver); 70 }, "SVG elements mixed case selector"); 71 72 test(function() { 73 test_xpath_succeeds("//dØdd", document.getElementsByTagName("dØdd"), ns_resolver); 74 }, "Non-ascii HTML element"); 75 76 test(function() { 77 test_xpath_succeeds("//dødd", [], ns_resolver); 78 }, "Non-ascii HTML element2"); 79 80 test(function() { 81 test_xpath_succeeds("//DØDD", document.getElementsByTagName("dØdd"), ns_resolver); 82 }, "Non-ascii HTML element3"); 83 84 test(function() { 85 test_xpath_throws("//invalid:path", "NAMESPACE_ERR"); 86 }, "Throw with invalid prefix"); 87 </script>