index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. var jsdom = require('../../lib/jsdom');
  2. var fs = require('fs');
  3. var jqueryString = fs.readFileSync(__dirname + '/support/jquery-1.6.2.js', 'utf-8');
  4. var testFile = fs.readFileSync(__dirname + '/files/index.html', 'utf-8');
  5. var qunit = require('./support/qunit').QUnit;
  6. function test(fn) {
  7. // return a nodeunit compatible test case
  8. return function(test) {
  9. jsdom.env({
  10. html : testFile,
  11. src : jqueryString,
  12. done : function(e, window) {
  13. var jQuery = window.jQuery;
  14. var document = window.document;
  15. /**
  16. * Returns an array of elements with the given IDs, eg.
  17. * @example q("main", "foo", "bar")
  18. * @result [<div id="main">, <span id="foo">, <input id="bar">]
  19. */
  20. function q() {
  21. var r = [];
  22. for ( var i = 0; i < arguments.length; i++ ) {
  23. r.push( document.getElementById( arguments[i] ) );
  24. }
  25. return r;
  26. }
  27. /**
  28. * Asserts that a select matches the given IDs * @example t(test,"Check for something", "//[a]", ["foo", "baar"]);
  29. * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
  30. */
  31. function t(a,b,c) {
  32. var f = jQuery(b).get(), s = "";
  33. for ( var i = 0; i < f.length; i++ ) {
  34. s += (s && ",") + '"' + f[i].id + '"';
  35. }
  36. var e = qunit.equiv(f, q.apply(q,c));
  37. test.strictEqual(e, true, a + " (" + b + ")");
  38. }
  39. fn(test, window, jQuery, document, q, t);
  40. }
  41. });
  42. }
  43. }
  44. /**
  45. * Add random number to url to stop IE from caching
  46. *
  47. * @example url("data/test.html")
  48. * @result "data/test.html?10538358428943"
  49. *
  50. * @example url("data/test.php?foo=bar")
  51. * @result "data/test.php?foo=bar&10538358345554"
  52. */
  53. function url(value) {
  54. return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
  55. }
  56. module.exports = {};
  57. module.exports['element'] = test(function(test, window, jQuery, document, q, t) {
  58. var all = jQuery("*"), good = true;
  59. for ( var i = 0; i < all.length; i++ )
  60. if ( all[i].nodeType == 8 )
  61. good = false;
  62. test.ok( good, "Select all elements, no comment nodes" );
  63. t( "Element Selector", "#qunit-fixture p", ["firstp","ap","sndp","en","sap","first"] );
  64. t( "Element Selector", "body", ["body"] );
  65. t( "Element Selector", "html", ["html"] );
  66. t( "Parent Element", "div p", ["firstp","ap","sndp","en","sap","first"] );
  67. test.strictEqual( jQuery("param", "#object1").length, 2, "Object/param as context" );
  68. test.deepEqual( jQuery("p", document.getElementsByTagName("div")).get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
  69. test.deepEqual( jQuery("p", "div").get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
  70. test.deepEqual( jQuery("p", jQuery("div")).get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
  71. test.deepEqual( jQuery("div").find("p").get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context." );
  72. test.deepEqual( jQuery("#form").find("select").get(), q("select1","select2","select3","select4","select5"), "Finding selects with a context." );
  73. test.ok( jQuery("#length").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
  74. test.ok( jQuery("#lengthtest input").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
  75. // Check for unique-ness and sort order
  76. test.deepEqual( jQuery("p, div p").get(), jQuery("p").get(), "Check for duplicates: p, div p" );
  77. t( "Checking sort order", "h2, h1", ["qunit-header", "qunit-banner", "qunit-userAgent"] );
  78. t( "Checking sort order", "h2:first, h1:first", ["qunit-header", "qunit-banner"] );
  79. t( "Checking sort order", "#qunit-fixture p, #qunit-fixture p a", ["firstp", "simon1", "ap", "google", "groups", "anchor1", "mark", "sndp", "en", "yahoo", "sap", "anchor2", "simon", "first"] );
  80. // Test Conflict ID
  81. test.deepEqual( jQuery("#lengthtest").find("#idTest").get(), q("idTest"), "Finding element with id of ID." );
  82. test.deepEqual( jQuery("#lengthtest").find("[name='id']").get(), q("idTest"), "Finding element with id of ID." );
  83. test.deepEqual( jQuery("#lengthtest").find("input[id='idTest']").get(), q("idTest"), "Finding elements with a context." );
  84. test.done();
  85. });
  86. /*
  87. if ( location.protocol != "file:" ) {
  88. module.exports['XML Document Selectors'] = test(function(test, window, jQuery, document, q, t) {
  89. stop();
  90. test.ok(false, "needs xmldocument");
  91. jQuery.get("data/with_fries.xml", function(xml) {
  92. test.strictEqual( jQuery("foo_bar", xml).length, 1, "Element Selector with underscore" );
  93. test.strictEqual( jQuery(".component", xml).length, 1, "Class selector" );
  94. test.strictEqual( jQuery("[class*=component]", xml).length, 1, "Attribute selector for class" );
  95. test.strictEqual( jQuery("property[name=prop2]", xml).length, 1, "Attribute selector with name" );
  96. test.strictEqual( jQuery("[name=prop2]", xml).length, 1, "Attribute selector with name" );
  97. test.strictEqual( jQuery("#seite1", xml).length, 1, "Attribute selector with ID" );
  98. test.strictEqual( jQuery("component#seite1", xml).length, 1, "Attribute selector with ID" );
  99. test.strictEqual( jQuery("component", xml).filter("#seite1").length, 1, "Attribute selector filter with ID" );
  100. test.ok( jQuery( xml.lastChild ).is( "soap\\:Envelope" ), "Check for namespaced element" );
  101. start();
  102. test.done();
  103. });
  104. test.done()
  105. });
  106. }*/
  107. module.exports['broken'] = test(function(test, window, jQuery, document, q, t) {
  108. function broken(name, selector) {
  109. try {
  110. jQuery(selector);
  111. test.ok( false, name + ": " + selector );
  112. } catch(e){
  113. test.ok( typeof e === "string" && e.indexOf("Syntax error") >= 0,
  114. name + ": " + selector );
  115. }
  116. }
  117. broken( "Broken Selector", "[", [] );
  118. broken( "Broken Selector", "(", [] );
  119. broken( "Broken Selector", "{", [] );
  120. broken( "Broken Selector", "<", [] );
  121. broken( "Broken Selector", "()", [] );
  122. broken( "Broken Selector", "<>", [] );
  123. broken( "Broken Selector", "{}", [] );
  124. broken( "Doesn't exist", ":visble", [] );
  125. broken( "Nth-child", ":nth-child", [] );
  126. // Sigh again. IE 9 thinks this is also a real selector
  127. // not super critical that we fix this case
  128. //broken( "Nth-child", ":nth-child(-)", [] );
  129. // Sigh. WebKit thinks this is a real selector in qSA
  130. // They've already fixed this and it'll be coming into
  131. // current browsers soon.
  132. //broken( "Nth-child", ":nth-child(asdf)", [] );
  133. broken( "Nth-child", ":nth-child(2n+-0)", [] );
  134. broken( "Nth-child", ":nth-child(2+0)", [] );
  135. broken( "Nth-child", ":nth-child(- 1n)", [] );
  136. broken( "Nth-child", ":nth-child(-1 n)", [] );
  137. broken( "First-child", ":first-child(n)", [] );
  138. broken( "Last-child", ":last-child(n)", [] );
  139. broken( "Only-child", ":only-child(n)", [] );
  140. // Make sure attribute value quoting works correctly. See: #6093
  141. var attrbad = jQuery('<input type="hidden" value="2" name="foo.baz" id="attrbad1"/><input type="hidden" value="2" name="foo[baz]" id="attrbad2"/>').appendTo("body");
  142. broken( "Attribute not escaped", "input[name=foo.baz]", [] );
  143. broken( "Attribute not escaped", "input[name=foo[baz]]", [] );
  144. attrbad.remove();
  145. test.done()
  146. });
  147. module.exports['id'] = test(function(test, window, jQuery, document, q, t) {
  148. t( "ID Selector", "#body", ["body"] );
  149. t( "ID Selector w/ Element", "body#body", ["body"] );
  150. t( "ID Selector w/ Element", "ul#first", [] );
  151. t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] );
  152. t( "ID selector with non-existant descendant", "#firstp #foobar", [] );
  153. t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] );
  154. t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] );
  155. t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
  156. t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
  157. t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
  158. t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  159. t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
  160. t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  161. t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
  162. t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  163. t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
  164. t( "ID Selector, not an ancestor ID", "#form #first", [] );
  165. t( "ID Selector, not a child ID", "#form > #option1a", [] );
  166. t( "All Children of ID", "#foo > *", ["sndp", "en", "sap"] );
  167. t( "All Children of ID with no children", "#firstUL > *", [] );
  168. var a = jQuery('<div><a name="tName1">tName1 A</a><a name="tName2">tName2 A</a><div id="tName1">tName1 Div</div></div>').appendTo('#qunit-fixture');
  169. test.strictEqual( jQuery("#tName1")[0].id, 'tName1', "ID selector with same value for a name attribute" );
  170. test.strictEqual( jQuery("#tName2").length, 0, "ID selector non-existing but name attribute on an A tag" );
  171. a.remove();
  172. t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] );
  173. t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986
  174. test.deepEqual( jQuery("body").find("div#form").get(), [], "ID selector within the context of another element" );
  175. //#7533
  176. test.strictEqual( jQuery("<div id=\"A'B~C.D[E]\"><p>foo</p></div>").find("p").length, 1, "Find where context root is a node and has an ID with CSS3 meta characters" );
  177. t( "Underscore ID", "#types_all", ["types_all"] );
  178. t( "Dash ID", "#fx-queue", ["fx-queue"] );
  179. t( "ID with weird characters in it", "#name\\+value", ["name+value"] );
  180. test.done()
  181. });
  182. module.exports['class'] = test(function(test, window, jQuery, document, q, t) {
  183. t( "Class Selector", ".blog", ["mark","simon"] );
  184. t( "Class Selector", ".GROUPS", ["groups"] );
  185. t( "Class Selector", ".blog.link", ["simon"] );
  186. t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
  187. t( "Parent Class Selector", "p .blog", ["mark","simon"] );
  188. test.deepEqual( jQuery(".blog", document.getElementsByTagName("p")).get(), q("mark", "simon"), "Finding elements with a context." );
  189. test.deepEqual( jQuery(".blog", "p").get(), q("mark", "simon"), "Finding elements with a context." );
  190. test.deepEqual( jQuery(".blog", jQuery("p")).get(), q("mark", "simon"), "Finding elements with a context." );
  191. test.deepEqual( jQuery("p").find(".blog").get(), q("mark", "simon"), "Finding elements with a context." );
  192. t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
  193. //t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
  194. t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
  195. t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
  196. t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
  197. t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );
  198. t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
  199. t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  200. t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] );
  201. t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  202. t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
  203. t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
  204. var div = document.createElement("div");
  205. div.innerHTML = "<div class='test e'></div><div class='test'></div>";
  206. test.deepEqual( jQuery(".e", div).get(), [ div.firstChild ], "Finding a second class." );
  207. div.lastChild.className = "e";
  208. test.deepEqual( jQuery(".e", div).get(), [ div.firstChild, div.lastChild ], "Finding a modified class." );
  209. test.done()
  210. });
  211. module.exports['name'] = test(function(test, window, jQuery, document, q, t) {
  212. t( "Name selector", "input[name=action]", ["text1"] );
  213. t( "Name selector with single quotes", "input[name='action']", ["text1"] );
  214. t( "Name selector with double quotes", 'input[name="action"]', ["text1"] );
  215. t( "Name selector non-input", "[name=test]", ["length", "fx-queue"] );
  216. t( "Name selector non-input", "[name=div]", ["fadein"] );
  217. t( "Name selector non-input", "*[name=iframe]", ["iframe"] );
  218. t( "Name selector for grouped input", "input[name='types[]']", ["types_all", "types_anime", "types_movie"] )
  219. test.deepEqual( jQuery("#form").find("input[name=action]").get(), q("text1"), "Name selector within the context of another element" );
  220. test.deepEqual( jQuery("#form").find("input[name='foo[bar]']").get(), q("hidden2"), "Name selector for grouped form element within the context of another element" );
  221. var form = jQuery("<form><input name='id'/></form>").appendTo("body");
  222. test.strictEqual( form.find("input").length, 1, "Make sure that rooted queries on forms (with possible expandos) work." );
  223. form.remove();
  224. var a = jQuery('<div><a id="tName1ID" name="tName1">tName1 A</a><a id="tName2ID" name="tName2">tName2 A</a><div id="tName1">tName1 Div</div></div>').appendTo('#qunit-fixture').children();
  225. test.strictEqual( a.length, 3, "Make sure the right number of elements were inserted." );
  226. test.strictEqual( a[1].id, "tName2ID", "Make sure the right number of elements were inserted." );
  227. test.strictEqual( jQuery("[name=tName1]")[0], a[0], "Find elements that have similar IDs" );
  228. test.strictEqual( jQuery("[name=tName2]")[0], a[1], "Find elements that have similar IDs" );
  229. t( "Find elements that have similar IDs", "#tName2ID", ["tName2ID"] );
  230. a.remove();
  231. test.done()
  232. });
  233. module.exports['multiple'] = test(function(test, window, jQuery, document, q, t) {
  234. t( "Comma Support", "h2, #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"]);
  235. t( "Comma Support", "h2 , #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"]);
  236. t( "Comma Support", "h2 , #qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"]);
  237. t( "Comma Support", "h2,#qunit-fixture p", ["qunit-banner","qunit-userAgent","firstp","ap","sndp","en","sap","first"]);
  238. test.done()
  239. });
  240. module.exports['child and adjacent'] = test(function(test, window, jQuery, document, q, t) {
  241. t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
  242. t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
  243. t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
  244. t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
  245. t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
  246. t( "All Children", "code > *", ["anchor1","anchor2"] );
  247. t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
  248. t( "Adjacent", "#qunit-fixture a + a", ["groups"] );
  249. t( "Adjacent", "#qunit-fixture a +a", ["groups"] );
  250. t( "Adjacent", "#qunit-fixture a+ a", ["groups"] );
  251. t( "Adjacent", "#qunit-fixture a+a", ["groups"] );
  252. t( "Adjacent", "p + p", ["ap","en","sap"] );
  253. t( "Adjacent", "p#firstp + p", ["ap"] );
  254. t( "Adjacent", "p[lang=en] + p", ["sap"] );
  255. t( "Adjacent", "a.GROUPS + code + a", ["mark"] );
  256. t( "Comma, Child, and Adjacent", "#qunit-fixture a + a, code > a", ["groups","anchor1","anchor2"] );
  257. t( "Element Preceded By", "#qunit-fixture p ~ div", ["foo", "moretests","tabindex-tests", "liveHandlerOrder", "siblingTest"] );
  258. t( "Element Preceded By", "#first ~ div", ["moretests","tabindex-tests", "liveHandlerOrder", "siblingTest"] );
  259. t( "Element Preceded By", "#groups ~ a", ["mark"] );
  260. t( "Element Preceded By", "#length ~ input", ["idTest"] );
  261. t( "Element Preceded By", "#siblingfirst ~ em", ["siblingnext"] );
  262. test.deepEqual( jQuery("#siblingfirst").find("~ em").get(), q("siblingnext"), "Element Preceded By with a context." );
  263. test.deepEqual( jQuery("#siblingfirst").find("+ em").get(), q("siblingnext"), "Element Directly Preceded By with a context." );
  264. test.strictEqual( jQuery("#listWithTabIndex").length, 1, "Parent div for next test is found via ID (#8310)" );
  265. test.strictEqual( jQuery("#listWithTabIndex li:eq(2) ~ li").length, 1, "Find by general sibling combinator (#8310)" );
  266. test.strictEqual( jQuery("#__sizzle__").length, 0, "Make sure the temporary id assigned by sizzle is cleared out (#8310)" );
  267. test.strictEqual( jQuery("#listWithTabIndex").length, 1, "Parent div for previous test is still found via ID (#8310)" );
  268. t( "Verify deep class selector", "div.blah > p > a", [] );
  269. t( "No element deep selector", "div.foo > span > a", [] );
  270. test.deepEqual( jQuery("> :first", document.getElementById("nothiddendiv")).get(), q("nothiddendivchild"), "Verify child context positional selctor" );
  271. test.deepEqual( jQuery("> :eq(0)", document.getElementById("nothiddendiv")).get(), q("nothiddendivchild"), "Verify child context positional selctor" );
  272. test.deepEqual( jQuery("> *:first", document.getElementById("nothiddendiv")).get(), q("nothiddendivchild"), "Verify child context positional selctor" );
  273. t( "Non-existant ancestors", ".fototab > .thumbnails > a", [] );
  274. test.done()
  275. });
  276. module.exports['attributes'] = test(function(test, window, jQuery, document, q, t) {
  277. t( "Attribute Exists", "a[title]", ["google"] );
  278. t( "Attribute Exists", "*[title]", ["google"] );
  279. t( "Attribute Exists", "[title]", ["google"] );
  280. t( "Attribute Exists", "a[ title ]", ["google"] );
  281. t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] );
  282. t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] );
  283. t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] );
  284. t( "Attribute Equals", "a[href='http://www.google.com/']", ["google"] );
  285. t( "Attribute Equals", "a[ rel = 'bookmark' ]", ["simon1"] );
  286. document.getElementById("anchor2").href = "#2";
  287. t( "href Attribute", "p a[href^=#]", ["anchor2"] );
  288. t( "href Attribute", "p a[href*=#]", ["simon1", "anchor2"] );
  289. t( "for Attribute", "form label[for]", ["label-for"] );
  290. t( "for Attribute in form", "#form [for=action]", ["label-for"] );
  291. t( "Attribute containing []", "input[name^='foo[']", ["hidden2"] );
  292. t( "Attribute containing []", "input[name^='foo[bar]']", ["hidden2"] );
  293. t( "Attribute containing []", "input[name*='[bar]']", ["hidden2"] );
  294. t( "Attribute containing []", "input[name$='bar]']", ["hidden2"] );
  295. t( "Attribute containing []", "input[name$='[bar]']", ["hidden2"] );
  296. t( "Attribute containing []", "input[name$='foo[bar]']", ["hidden2"] );
  297. t( "Attribute containing []", "input[name*='foo[bar]']", ["hidden2"] );
  298. t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type='hidden']", ["radio1", "radio2", "hidden1"] );
  299. t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=\"hidden\"]", ["radio1", "radio2", "hidden1"] );
  300. t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=hidden]", ["radio1", "radio2", "hidden1"] );
  301. t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] );
  302. t( "Attribute Begins With", "a[href ^= 'http://www']", ["google","yahoo"] );
  303. t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] );
  304. t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] );
  305. t( "Attribute Is Not Equal", "#ap a[hreflang!='en']", ["google","groups","anchor1"] );
  306. var opt = document.getElementById("option1a"),
  307. match = (window.Sizzle || window.jQuery.find).matchesSelector;
  308. opt.setAttribute("test", "");
  309. test.ok( match( opt, "[id*=option1][type!=checkbox]" ), "Attribute Is Not Equal Matches" );
  310. test.ok( match( opt, "[id*=option1]" ), "Attribute With No Quotes Contains Matches" );
  311. test.ok( match( opt, "[test=]" ), "Attribute With No Quotes No Content Matches" );
  312. test.ok( match( opt, "[id=option1a]" ), "Attribute With No Quotes Equals Matches" );
  313. test.ok( match( document.getElementById("simon1"), "a[href*=#]" ), "Attribute With No Quotes Href Contains Matches" );
  314. return test.done();
  315. t("Empty values", "#select1 option[value='']", ["option1a"]);
  316. t("Empty values", "#select1 option[value!='']", ["option1b","option1c","option1d"]);
  317. t("Select options via :selected", "#select1 option:selected", ["option1a"] );
  318. t("Select options via :selected", "#select2 option:selected", ["option2d"] );
  319. t("Select options via :selected", "#select3 option:selected", ["option3b", "option3c"] );
  320. t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] );
  321. // Make sure attribute value quoting works correctly. See: #6093
  322. var attrbad = jQuery('<input type="hidden" value="2" name="foo.baz" id="attrbad1"/><input type="hidden" value="2" name="foo[baz]" id="attrbad2"/>').appendTo("body");
  323. t("Find escaped attribute value", "input[name=foo\\.baz]", ["attrbad1"]);
  324. t("Find escaped attribute value", "input[name=foo\\[baz\\]]", ["attrbad2"]);
  325. t("input[type=text]", "#form input[type=text]", ["text1", "text2", "hidden2", "name"]);
  326. t("input[type=search]", "#form input[type=search]", ["search"]);
  327. attrbad.remove();
  328. //#6428
  329. t("Find escaped attribute value", "#form input[name=foo\\[bar\\]]", ["hidden2"]);
  330. //#3279
  331. var div = document.createElement("div");
  332. div.innerHTML = "<div id='foo' xml:test='something'></div>";
  333. test.deepEqual( jQuery( "[xml\\:test]", div ).get(), [ div.firstChild ], "Finding by attribute with escaped characters." );
  334. test.done()
  335. });
  336. module.exports['pseudo - child'] = test(function(test, window, jQuery, document, q, t) {
  337. t( "First Child", "#qunit-fixture p:first-child", ["firstp","sndp"] );
  338. t( "Last Child", "p:last-child", ["sap"] );
  339. t( "Only Child", "#qunit-fixture a:only-child", ["simon1","anchor1","yahoo","anchor2","liveLink1","liveLink2"] );
  340. t( "Empty", "ul:empty", ["firstUL"] );
  341. t( "Is A Parent", "#qunit-fixture p:parent", ["firstp","ap","sndp","en","sap","first"] );
  342. t( "First Child", "p:first-child", ["firstp","sndp"] );
  343. t( "Nth Child", "p:nth-child(1)", ["firstp","sndp"] );
  344. t( "Nth Child With Whitespace", "p:nth-child( 1 )", ["firstp","sndp"] );
  345. t( "Not Nth Child", "#qunit-fixture p:not(:nth-child(1))", ["ap","en","sap","first"] );
  346. // Verify that the child position isn't being cached improperly
  347. jQuery("p:first-child").after("<div></div>");
  348. jQuery("p:first-child").before("<div></div>").next().remove();
  349. t( "First Child", "p:first-child", [] );
  350. t( "Last Child", "p:last-child", ["sap"] );
  351. t( "Last Child", "#qunit-fixture a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon","liveLink1","liveLink2"] );
  352. t( "Nth-child", "#qunit-fixture form#form > *:nth-child(2)", ["text1"] );
  353. t( "Nth-child", "#qunit-fixture form#form > :nth-child(2)", ["text1"] );
  354. t( "Nth-child", "#form select:first option:nth-child(-1)", [] );
  355. t( "Nth-child", "#form select:first option:nth-child(3)", ["option1c"] );
  356. t( "Nth-child", "#form select:first option:nth-child(0n+3)", ["option1c"] );
  357. t( "Nth-child", "#form select:first option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
  358. t( "Nth-child", "#form select:first option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
  359. t( "Nth-child", "#form select:first option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
  360. t( "Nth-child", "#form select:first option:nth-child(+n)", ["option1a", "option1b", "option1c", "option1d"] );
  361. t( "Nth-child", "#form select:first option:nth-child(even)", ["option1b", "option1d"] );
  362. t( "Nth-child", "#form select:first option:nth-child(odd)", ["option1a", "option1c"] );
  363. t( "Nth-child", "#form select:first option:nth-child(2n)", ["option1b", "option1d"] );
  364. t( "Nth-child", "#form select:first option:nth-child(2n+1)", ["option1a", "option1c"] );
  365. t( "Nth-child", "#form select:first option:nth-child(2n + 1)", ["option1a", "option1c"] );
  366. t( "Nth-child", "#form select:first option:nth-child(+2n + 1)", ["option1a", "option1c"] );
  367. t( "Nth-child", "#form select:first option:nth-child(3n)", ["option1c"] );
  368. t( "Nth-child", "#form select:first option:nth-child(3n+1)", ["option1a", "option1d"] );
  369. t( "Nth-child", "#form select:first option:nth-child(3n+2)", ["option1b"] );
  370. t( "Nth-child", "#form select:first option:nth-child(3n+3)", ["option1c"] );
  371. t( "Nth-child", "#form select:first option:nth-child(3n-1)", ["option1b"] );
  372. t( "Nth-child", "#form select:first option:nth-child(3n-2)", ["option1a", "option1d"] );
  373. t( "Nth-child", "#form select:first option:nth-child(3n-3)", ["option1c"] );
  374. t( "Nth-child", "#form select:first option:nth-child(3n+0)", ["option1c"] );
  375. t( "Nth-child", "#form select:first option:nth-child(-1n+3)", ["option1a", "option1b", "option1c"] );
  376. t( "Nth-child", "#form select:first option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] );
  377. t( "Nth-child", "#form select:first option:nth-child(-1n + 3)", ["option1a", "option1b", "option1c"] );
  378. test.done()
  379. });
  380. module.exports['pseudo - misc'] = test(function(test, window, jQuery, document, q, t) {
  381. t( "Headers", ":header", ["qunit-header", "qunit-banner", "qunit-userAgent"] );
  382. t( "Has Children - :has()", "p:has(a)", ["firstp","ap","en","sap"] );
  383. var select = document.getElementById("select1");
  384. test.ok( (window.Sizzle || window.jQuery.find).matchesSelector( select, ":has(option)" ), "Has Option Matches" );
  385. t( "Text Contains", "a:contains(Google)", ["google","groups"] );
  386. t( "Text Contains", "a:contains(Google Groups)", ["groups"] );
  387. t( "Text Contains", "a:contains(Google Groups (Link))", ["groups"] );
  388. t( "Text Contains", "a:contains((Link))", ["groups"] );
  389. var tmp = document.createElement("div");
  390. tmp.id = "tmp_input";
  391. document.body.appendChild( tmp );
  392. jQuery.each( [ "button", "submit", "reset" ], function( i, type ) {
  393. jQuery( tmp ).append(
  394. "<input id='input_T' type='T'/><button id='button_T' type='T'>test</button>".replace(/T/g, type) );
  395. t( "Input Buttons :" + type, "#tmp_input :" + type, [ "input_" + type, "button_" + type ] );
  396. test.ok( (window.Sizzle || window.jQuery.find).matchesSelector( jQuery("#input_" + type)[0], ":" + type ), "Input Matches :" + type );
  397. test.ok( (window.Sizzle || window.jQuery.find).matchesSelector( jQuery("#button_" + type)[0], ":" + type ), "Button Matches :" + type );
  398. });
  399. document.body.removeChild( tmp );
  400. var input = document.createElement("input");
  401. input.type = "text";
  402. input.id = "focus-input";
  403. document.body.appendChild( input );
  404. input.focus();
  405. t( "Element focused", "input:focus", [ "focus-input" ] );
  406. test.ok( (window.Sizzle || window.jQuery.find).matchesSelector( input, ":focus" ), ":focus Matches" );
  407. input.blur();
  408. test.ok( !(window.Sizzle || window.jQuery.find).matchesSelector( input, ":focus" ), ":focus Doesn't Match" );
  409. document.body.removeChild( input );
  410. test.done()
  411. });
  412. module.exports['pseudo - :not'] = test(function(test, window, jQuery, document, q, t) {
  413. t( "Not", "a.blog:not(.link)", ["mark"] );
  414. t( "Not - multiple", "#form option:not(:contains(Nothing),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e", "option4e", "option5b", "option5c"] );
  415. t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
  416. t( ":not() failing interior", "#qunit-fixture p:not(.foo)", ["firstp","ap","sndp","en","sap","first"] );
  417. t( ":not() failing interior", "#qunit-fixture p:not(div.foo)", ["firstp","ap","sndp","en","sap","first"] );
  418. t( ":not() failing interior", "#qunit-fixture p:not(p.foo)", ["firstp","ap","sndp","en","sap","first"] );
  419. t( ":not() failing interior", "#qunit-fixture p:not(#blargh)", ["firstp","ap","sndp","en","sap","first"] );
  420. t( ":not() failing interior", "#qunit-fixture p:not(div#blargh)", ["firstp","ap","sndp","en","sap","first"] );
  421. t( ":not() failing interior", "#qunit-fixture p:not(p#blargh)", ["firstp","ap","sndp","en","sap","first"] );
  422. t( ":not Multiple", "#qunit-fixture p:not(a)", ["firstp","ap","sndp","en","sap","first"] );
  423. t( ":not Multiple", "#qunit-fixture p:not(a, b)", ["firstp","ap","sndp","en","sap","first"] );
  424. t( ":not Multiple", "#qunit-fixture p:not(a, b, div)", ["firstp","ap","sndp","en","sap","first"] );
  425. t( ":not Multiple", "p:not(p)", [] );
  426. t( ":not Multiple", "p:not(a,p)", [] );
  427. t( ":not Multiple", "p:not(p,a)", [] );
  428. t( ":not Multiple", "p:not(a,p,b)", [] );
  429. t( ":not Multiple", ":input:not(:image,:input,:submit)", [] );
  430. t( "No element not selector", ".container div:not(.excluded) div", [] );
  431. t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2", "select5"]);
  432. t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3", "select4","select5"]);
  433. t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3", "select4", "select5"]);
  434. t( ":not() Multiple Class", "#foo a:not(.blog)", ["yahoo","anchor2"] );
  435. t( ":not() Multiple Class", "#foo a:not(.link)", ["yahoo","anchor2"] );
  436. t( ":not() Multiple Class", "#foo a:not(.blog.link)", ["yahoo","anchor2"] );
  437. test.done()
  438. });
  439. module.exports['pseudo - position'] = test(function(test, window, jQuery, document, q, t) {
  440. t( "nth Element", "#qunit-fixture p:nth(1)", ["ap"] );
  441. t( "First Element", "#qunit-fixture p:first", ["firstp"] );
  442. t( "Last Element", "p:last", ["first"] );
  443. t( "Even Elements", "#qunit-fixture p:even", ["firstp","sndp","sap"] );
  444. t( "Odd Elements", "#qunit-fixture p:odd", ["ap","en","first"] );
  445. t( "Position Equals", "#qunit-fixture p:eq(1)", ["ap"] );
  446. t( "Position Greater Than", "#qunit-fixture p:gt(0)", ["ap","sndp","en","sap","first"] );
  447. t( "Position Less Than", "#qunit-fixture p:lt(3)", ["firstp","ap","sndp"] );
  448. t( "Check position filtering", "div#nothiddendiv:eq(0)", ["nothiddendiv"] );
  449. t( "Check position filtering", "div#nothiddendiv:last", ["nothiddendiv"] );
  450. t( "Check position filtering", "div#nothiddendiv:not(:gt(0))", ["nothiddendiv"] );
  451. t( "Check position filtering", "#foo > :not(:first)", ["en", "sap"] );
  452. t( "Check position filtering", "select > :not(:gt(2))", ["option1a", "option1b", "option1c"] );
  453. t( "Check position filtering", "select:lt(2) :not(:first)", ["option1b", "option1c", "option1d", "option2a", "option2b", "option2c", "option2d"] );
  454. t( "Check position filtering", "div.nothiddendiv:eq(0)", ["nothiddendiv"] );
  455. t( "Check position filtering", "div.nothiddendiv:last", ["nothiddendiv"] );
  456. t( "Check position filtering", "div.nothiddendiv:not(:lt(0))", ["nothiddendiv"] );
  457. t( "Check element position", "div div:eq(0)", ["nothiddendivchild"] );
  458. t( "Check element position", "div div:eq(5)", ["t2037"] );
  459. t( "Check element position", "div div:eq(28)", ["hide"] );
  460. t( "Check element position", "div div:first", ["nothiddendivchild"] );
  461. t( "Check element position", "div > div:first", ["nothiddendivchild"] );
  462. t( "Check element position", "#dl div:first div:first", ["foo"] );
  463. t( "Check element position", "#dl div:first > div:first", ["foo"] );
  464. t( "Check element position", "div#nothiddendiv:first > div:first", ["nothiddendivchild"] );
  465. test.done()
  466. });
  467. module.exports['pseudo - visibility'] = test(function(test, window, jQuery, document, q, t) {
  468. if ( (window.Sizzle || jQuery.find).selectors.filters.visibility ) {
  469. t( "Is Visible", "#form input:visible", [] );
  470. t( "Is Visible", "div:visible:not(#qunit-testrunner-toolbar):lt(2)", ["nothiddendiv", "nothiddendivchild"] );
  471. t( "Is Hidden", "#form input:hidden", ["text1","text2","radio1","radio2","check1","check2","hidden1","hidden2","name","search"] );
  472. t( "Is Hidden", "#qunit-fixture:hidden", ["main"] );
  473. t( "Is Hidden", "#dl:hidden", ["dl"] );
  474. var $div = jQuery('<div/>').appendTo("body");
  475. $div.css({ fontSize: 0, lineHeight: 0});
  476. // IE also needs to set font-size and line-height to 0
  477. $div.width(1).height(0);
  478. t( "Is Visible", '#nothiddendivchild:visible', ['nothiddendivchild'] );
  479. t( "Is Not Visible", '#nothiddendivchild:hidden', [] );
  480. $div.width(0).height(1);
  481. t( "Is Visible", '#nothiddendivchild:visible', ['nothiddendivchild'] );
  482. t( "Is Not Visible", '#nothiddendivchild:hidden', [] );
  483. $div.width(1).height(1);
  484. t( "Is Visible", '#nothiddendivchild:visible', ['nothiddendivchild'] );
  485. t( "Is Not Visible", '#nothiddendivchild:hidden', [] );
  486. $div.remove();
  487. }
  488. test.done()
  489. });
  490. module.exports['pseudo - form'] = test(function(test, window, jQuery, document, q, t) {
  491. var implied = jQuery('<input id="impliedText"/>').appendTo("#form");
  492. t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "search", "button", "area1", "select1", "select2", "select3", "select4", "select5", "impliedText"] );
  493. t( "Form element :radio", "#form :radio", ["radio1", "radio2"] );
  494. t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] );
  495. t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name", "impliedText"] );
  496. t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
  497. t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
  498. t( "Form element :radio:checked, :checkbox:checked", "#form :radio:checked, #form :checkbox:checked", ["radio2", "check1"] );
  499. t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c","option4b","option4c","option4d","option5a"] );
  500. implied.remove();
  501. test.done()
  502. });
  503. module.exports['disconnected nodes'] = test(function(test, window, jQuery, document, q, t) {
  504. var $opt = jQuery('<option></option>').attr("value", "whipit").appendTo("#qunit-fixture").detach();
  505. test.strictEqual( $opt.val(), "whipit", "option value" );
  506. test.strictEqual( $opt.is(":selected"), false, "unselected option" );
  507. $opt.attr("selected", true);
  508. test.strictEqual( $opt.is(":selected"), true, "selected option" );
  509. var $div = jQuery( '<div/>' );
  510. test.strictEqual( $div.is("div"), true, "Make sure .is('nodeName') works on disconnect nodes." );
  511. test.done()
  512. });