Element-matches.js (4836B)
1 /* 2 * Check that the matches() method exists on the given Node 3 */ 4 function interfaceCheckMatches(method, type, obj) { 5 if (obj.nodeType === obj.ELEMENT_NODE) { 6 test(function() { 7 assert_idl_attribute(obj, method, type + " supports " + method); 8 }, type + " supports " + method) 9 } else { 10 test(function() { 11 assert_false(method in obj, type + " supports " + method); 12 }, type + " should not support " + method) 13 } 14 } 15 16 function runSpecialMatchesTests(method, type, element) { 17 test(function() { // 1 18 if (element.tagName.toLowerCase() === "null") { 19 assert_true(element[method](null), "An element with the tag name '" + element.tagName.toLowerCase() + "' should match."); 20 } else { 21 assert_false(element[method](null), "An element with the tag name '" + element.tagName.toLowerCase() + "' should not match."); 22 } 23 }, type + "." + method + "(null)") 24 25 test(function() { // 2 26 if (element.tagName.toLowerCase() === "undefined") { 27 assert_true(element[method](undefined), "An element with the tag name '" + element.tagName.toLowerCase() + "' should match."); 28 } else { 29 assert_false(element[method](undefined), "An element with the tag name '" + element.tagName.toLowerCase() + "' should not match."); 30 } 31 }, type + "." + method + "(undefined)") 32 33 test(function() { // 3 34 assert_throws_js(element.ownerDocument.defaultView.TypeError, function() { 35 element[method](); 36 }, "This should throw a TypeError.") 37 }, type + "." + method + " no parameter") 38 } 39 40 /* 41 * Execute queries with the specified invalid selectors for matches() 42 * Only run these tests when errors are expected. Don't run for valid selector tests. 43 */ 44 function runInvalidSelectorTestMatches(method, type, root, selectors) { 45 if (root.nodeType === root.ELEMENT_NODE) { 46 for (var i = 0; i < selectors.length; i++) { 47 var s = selectors[i]; 48 var n = s["name"]; 49 var q = s["selector"]; 50 51 test(function() { 52 assert_throws_dom( 53 "SyntaxError", 54 root.ownerDocument.defaultView.DOMException, 55 function() { 56 root[method](q) 57 } 58 ); 59 }, type + "." + method + ": " + n + ": " + q); 60 } 61 } 62 } 63 64 function runMatchesTest(method, type, root, selectors, docType) { 65 var nodeType = getNodeType(root); 66 67 for (var i = 0; i < selectors.length; i++) { 68 var s = selectors[i]; 69 var n = s["name"]; 70 var q = s["selector"]; 71 var e = s["expect"]; 72 var u = s["unexpected"]; 73 74 var ctx = s["ctx"]; 75 var ref = s["ref"]; 76 77 if ((!s["exclude"] || (s["exclude"].indexOf(nodeType) === -1 && s["exclude"].indexOf(docType) === -1)) 78 && (s["testType"] & TEST_MATCH) ) { 79 80 if (ctx && !ref) { 81 test(function() { 82 var j, element, refNode; 83 for (j = 0; j < e.length; j++) { 84 element = root.querySelector("#" + e[j]); 85 refNode = root.querySelector(ctx); 86 assert_true(element[method](q, refNode), "The element #" + e[j] + " should match the selector.") 87 } 88 89 if (u) { 90 for (j = 0; j < u.length; j++) { 91 element = root.querySelector("#" + u[j]); 92 refNode = root.querySelector(ctx); 93 assert_false(element[method](q, refNode), "The element #" + u[j] + " should not match the selector.") 94 } 95 } 96 }, type + " Element." + method + ": " + n + " (with refNode Element): " + q); 97 } 98 99 if (ref) { 100 test(function() { 101 var j, element, refNodes; 102 for (j = 0; j < e.length; j++) { 103 element = root.querySelector("#" + e[j]); 104 refNodes = root.querySelectorAll(ref); 105 assert_true(element[method](q, refNodes), "The element #" + e[j] + " should match the selector.") 106 } 107 108 if (u) { 109 for (j = 0; j < u.length; j++) { 110 element = root.querySelector("#" + u[j]); 111 refNodes = root.querySelectorAll(ref); 112 assert_false(element[method](q, refNodes), "The element #" + u[j] + " should not match the selector.") 113 } 114 } 115 }, type + " Element." + method + ": " + n + " (with refNodes NodeList): " + q); 116 } 117 118 if (!ctx && !ref) { 119 test(function() { 120 for (var j = 0; j < e.length; j++) { 121 var element = root.querySelector("#" + e[j]); 122 assert_true(element[method](q), "The element #" + e[j] + " should match the selector.") 123 } 124 125 if (u) { 126 for (j = 0; j < u.length; j++) { 127 element = root.querySelector("#" + u[j]); 128 assert_false(element[method](q), "The element #" + u[j] + " should not match the selector.") 129 } 130 } 131 }, type + " Element." + method + ": " + n + " (with no refNodes): " + q); 132 } 133 } 134 } 135 }