selector.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. module("selector", { teardown: moduleTeardown });
  2. /**
  3. * This test page is for selector tests that require jQuery in order to do the selection
  4. */
  5. test("element - jQuery only", function() {
  6. expect( 7 );
  7. var fixture = document.getElementById("qunit-fixture");
  8. deepEqual( jQuery("p", fixture).get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a Node context." );
  9. deepEqual( jQuery("p", "#qunit-fixture").get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a selector context." );
  10. deepEqual( jQuery("p", jQuery("#qunit-fixture")).get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a jQuery object context." );
  11. deepEqual( jQuery("#qunit-fixture").find("p").get(), q("firstp","ap","sndp","en","sap","first"), "Finding elements with a context via .find()." );
  12. ok( jQuery("#length").length, "<input name=\"length\"> cannot be found under IE, see #945" );
  13. ok( jQuery("#lengthtest input").length, "<input name=\"length\"> cannot be found under IE, see #945" );
  14. // #7533
  15. equal( 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" );
  16. });
  17. test("class - jQuery only", function() {
  18. expect( 4 );
  19. deepEqual( jQuery(".blog", document.getElementsByTagName("p")).get(), q("mark", "simon"), "Finding elements with a context." );
  20. deepEqual( jQuery(".blog", "p").get(), q("mark", "simon"), "Finding elements with a context." );
  21. deepEqual( jQuery(".blog", jQuery("p")).get(), q("mark", "simon"), "Finding elements with a context." );
  22. deepEqual( jQuery("p").find(".blog").get(), q("mark", "simon"), "Finding elements with a context." );
  23. });
  24. test("attributes - jQuery only", function() {
  25. expect( 6 );
  26. t( "Find elements with a tabindex attribute", "[tabindex]", ["listWithTabIndex", "foodWithNegativeTabIndex", "linkWithTabIndex", "linkWithNegativeTabIndex", "linkWithNoHrefWithTabIndex", "linkWithNoHrefWithNegativeTabIndex"] );
  27. // #12523
  28. deepEqual(
  29. jQuery.find( "[title]", null, null, jQuery("#qunit-fixture a").get().concat( document.createTextNode("") ) ),
  30. q("google"),
  31. "Text nodes fail attribute tests without exception"
  32. );
  33. // #12600
  34. ok(
  35. jQuery("<select value='12600'><option value='option' selected='selected'></option><option value=''></option></select>")
  36. .prop( "value", "option" )
  37. .is(":input[value='12600']"),
  38. ":input[value=foo] selects select by attribute"
  39. );
  40. ok( jQuery("<input type='text' value='12600'/>").prop( "value", "option" ).is(":input[value='12600']"),
  41. ":input[value=foo] selects text input by attribute"
  42. );
  43. // #11115
  44. ok( jQuery("<input type='checkbox' checked='checked'/>").prop( "checked", false ).is("[checked]"),
  45. "[checked] selects by attribute (positive)"
  46. );
  47. ok( !jQuery("<input type='checkbox'/>").prop( "checked", true ).is("[checked]"),
  48. "[checked] selects by attribute (negative)"
  49. );
  50. });
  51. test("disconnected nodes", function() {
  52. expect( 4 );
  53. var $opt = jQuery("<option></option>").attr("value", "whipit").appendTo("#qunit-fixture").detach();
  54. equal( $opt.val(), "whipit", "option value" );
  55. equal( $opt.is(":selected"), false, "unselected option" );
  56. $opt.prop("selected", true);
  57. equal( $opt.is(":selected"), true, "selected option" );
  58. var $div = jQuery("<div/>");
  59. equal( $div.is("div"), true, "Make sure .is('nodeName') works on disconnected nodes." );
  60. });
  61. test("jQuery only - broken", 1, function() {
  62. raises(function() {
  63. // Setting context to null here somehow avoids QUnit's window.error handling
  64. // making the e & e.message correct
  65. // For whatever reason, without this,
  66. // Sizzle.error will be called but no error will be seen in oldIE
  67. jQuery.call( null, " <div/> " );
  68. }, function( e ) {
  69. return e.message.indexOf("Syntax error") >= 0;
  70. }, "leading space invalid: $(' <div/> ')" );
  71. });
  72. testIframe("selector/html5_selector", "attributes - jQuery.attr", function( jQuery, window, document ) {
  73. expect( 35 );
  74. /**
  75. * Returns an array of elements with the given IDs
  76. * q & t are added here for the iFrame's context
  77. */
  78. function q() {
  79. var r = [],
  80. i = 0;
  81. for ( ; i < arguments.length; i++ ) {
  82. r.push( document.getElementById( arguments[i] ) );
  83. }
  84. return r;
  85. }
  86. /**
  87. * Asserts that a select matches the given IDs
  88. * @example t("Check for something", "//[a]", ["foo", "baar"]);
  89. * @param {String} a - Assertion name
  90. * @param {String} b - Sizzle selector
  91. * @param {Array} c - Array of ids to construct what is expected
  92. */
  93. function t( a, b, c ) {
  94. var f = jQuery(b).get(),
  95. s = "",
  96. i = 0;
  97. for ( ; i < f.length; i++ ) {
  98. s += (s && ",") + "'" + f[i].id + "'";
  99. }
  100. deepEqual(f, q.apply( q, c ), a + " (" + b + ")");
  101. }
  102. // ====== All known boolean attributes, including html5 booleans ======
  103. // autobuffer, autofocus, autoplay, async, checked,
  104. // compact, controls, declare, defer, disabled,
  105. // formnovalidate, hidden, indeterminate (property only),
  106. // ismap, itemscope, loop, multiple, muted, nohref, noresize,
  107. // noshade, nowrap, novalidate, open, pubdate, readonly, required,
  108. // reversed, scoped, seamless, selected, truespeed, visible (skipping visible attribute, which is on a barprop object)
  109. t( "Attribute Exists", "[autobuffer]", ["video1"]);
  110. t( "Attribute Exists", "[autofocus]", ["text1"]);
  111. t( "Attribute Exists", "[autoplay]", ["video1"]);
  112. t( "Attribute Exists", "[async]", ["script1"]);
  113. t( "Attribute Exists", "[checked]", ["check1"]);
  114. t( "Attribute Exists", "[compact]", ["dl"]);
  115. t( "Attribute Exists", "[controls]", ["video1"]);
  116. t( "Attribute Exists", "[declare]", ["object1"]);
  117. t( "Attribute Exists", "[defer]", ["script1"]);
  118. t( "Attribute Exists", "[disabled]", ["check1"]);
  119. t( "Attribute Exists", "[formnovalidate]", ["form1"]);
  120. t( "Attribute Exists", "[hidden]", ["div1"]);
  121. t( "Attribute Exists", "[indeterminate]", []);
  122. t( "Attribute Exists", "[ismap]", ["img1"]);
  123. t( "Attribute Exists", "[itemscope]", ["div1"]);
  124. // t( "Attribute Exists", "[loop]", ["video1"]); // IE 6/7 cannot differentiate here. loop is also used on img, input, and marquee tags as well as video/audio. getAttributeNode unfortunately also retrieves the property value.
  125. t( "Attribute Exists", "[multiple]", ["select1"]);
  126. t( "Attribute Exists", "[muted]", ["audio1"]);
  127. // t( "Attribute Exists", "[nohref]", ["area1"]); // IE 6/7 keep this set to false regardless of presence. The attribute node is not retrievable.
  128. t( "Attribute Exists", "[noresize]", ["textarea1"]);
  129. t( "Attribute Exists", "[noshade]", ["hr1"]);
  130. t( "Attribute Exists", "[nowrap]", ["td1", "div1"]);
  131. t( "Attribute Exists", "[novalidate]", ["form1"]);
  132. t( "Attribute Exists", "[open]", ["details1"]);
  133. t( "Attribute Exists", "[pubdate]", ["article1"]);
  134. t( "Attribute Exists", "[readonly]", ["text1"]);
  135. t( "Attribute Exists", "[required]", ["text1"]);
  136. t( "Attribute Exists", "[reversed]", ["ol1"]);
  137. t( "Attribute Exists", "[scoped]", ["style1"]);
  138. t( "Attribute Exists", "[seamless]", ["iframe1"]);
  139. t( "Attribute Exists", "[selected]", ["option1"]);
  140. t( "Attribute Exists", "[truespeed]", ["marquee1"]);
  141. // Enumerated attributes (these are not boolean content attributes)
  142. jQuery.expandedEach = jQuery.each;
  143. jQuery.expandedEach([ "draggable", "contenteditable", "aria-disabled" ], function( i, val ) {
  144. t( "Enumerated attribute", "[" + val + "]", ["div1"]);
  145. });
  146. t( "Enumerated attribute", "[spellcheck]", ["span1"]);
  147. // t( "tabindex selector does not retrieve all elements in IE6/7(#8473)", "form, [tabindex]", ["form1", "text1"] ); // sigh, FF12 QSA mistakenly includes video elements even though they have no tabindex attribute (see https://bugzilla.mozilla.org/show_bug.cgi?id=618737)
  148. t( "Improperly named form elements do not interfere with form selections (#9570)", "form[name='formName']", ["form1"] );
  149. });
  150. testIframe("selector/sizzle_cache", "Sizzle cache collides with multiple Sizzles on a page", function( jQuery, window, document ) {
  151. var $cached = window["$cached"];
  152. expect(3);
  153. deepEqual( $cached(".test a").get(), [ document.getElementById("collision") ], "Select collision anchor with first sizzle" );
  154. equal( jQuery(".evil a").length, 0, "Select nothing with second sizzle" );
  155. equal( jQuery(".evil a").length, 0, "Select nothing again with second sizzle" );
  156. });