text-html-attributes.html (3545B)
1 <!doctype html> 2 <meta charset="utf8"> 3 <title>XPath in text/html: attributes</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" nonÄsciiAttribute><span></span></div> 9 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 10 <path id="a" refx /> 11 <path id="b" nonÄscii xlink:href /> 12 </svg> 13 14 <script> 15 function test_xpath_succeeds(path, expected, resolver) { 16 resolver = resolver ? resolver : null; 17 var res = document.evaluate(path, document, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 18 actual = []; 19 for (var i=0;;i++) { 20 var node = res.snapshotItem(i); 21 if (node === null) { 22 break; 23 } 24 actual.push(node); 25 } 26 assert_array_equals(actual, expected); 27 } 28 29 function test_xpath_throws(path, error_code, resolver) { 30 resolver = resolver ? resolver : null; 31 assert_throws_dom(error_code, function() {document.evaluate(path, document, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)}) 32 } 33 34 function ns_resolver(x) { 35 map = {"html":"http://www.w3.org/1999/xhtml", 36 "svg":"http://www.w3.org/2000/svg", 37 "math":"http://www.w3.org/1998/Math/MathML", 38 "xlink":"http://www.w3.org/1999/xlink"}; 39 var rv = map.hasOwnProperty(x) ? map[x] : null; 40 return rv; 41 } 42 43 test(function() { 44 test_xpath_succeeds("//div[@id='log']", [document.getElementById("log")]); 45 }, "Select html element based on attribute"); 46 47 test(function() { 48 test_xpath_succeeds("//div[@Id='log']", [document.getElementById("log")]); 49 }, "Select html element based on attribute mixed case"); 50 51 test(function() { 52 test_xpath_succeeds("//*[@id]", [document.getElementById("log")].concat(Array.prototype.slice.call(document.getElementsByTagName("path")))); 53 }, "Select both HTML and SVG elements based on attribute"); 54 55 test(function() { 56 test_xpath_succeeds("//*[@nonÄsciiattribute]", [document.getElementById("log")]); 57 }, "Select HTML element with non-ascii attribute 1"); 58 59 test(function() { 60 test_xpath_succeeds("//*[@nonäsciiattribute]", []); 61 }, "Select HTML element with non-ascii attribute 2"); 62 63 test(function() { 64 test_xpath_succeeds("//*[@nonÄsciiAttribute]", [document.getElementById("log")]); 65 }, "Select HTML element with non-ascii attribute 3"); 66 67 test(function() { 68 test_xpath_succeeds("//svg:path[@Id]", [], ns_resolver); 69 }, "Select SVG element based on mixed case attribute"); 70 71 test(function() { 72 test_xpath_succeeds("//*[@Id]", [document.getElementById("log")]); 73 }, "Select both HTML and SVG elements based on mixed case attribute"); 74 75 test(function() { 76 test_xpath_succeeds("//*[@refX]", [document.getElementById("a")]); 77 }, "Select SVG elements with refX attribute"); 78 79 test(function() { 80 test_xpath_succeeds("//*[@Refx]", []); 81 }, "Select SVG elements with refX attribute incorrect case"); 82 83 test(function() { 84 test_xpath_succeeds("//*[@refx]", []); 85 }, "Select SVG elements with refX attribute lowercase"); 86 87 test(function() { 88 test_xpath_succeeds("//*[@nonÄscii]", [document.getElementById("b")]); 89 }, "Select SVG element with non-ascii attribute 1"); 90 91 test(function() { 92 test_xpath_succeeds("//*[@nonäscii]", []); 93 }, "Select SVG element with non-ascii attribute 2"); 94 95 test(function() { 96 test_xpath_succeeds("//*[@xmlns]", []); 97 }, "xmlns attribute"); 98 99 test(function() { 100 test_xpath_succeeds("//*[@xlink:href]", [document.getElementById("b")], ns_resolver); 101 }, "svg element with XLink attribute"); 102 </script>