/
selector.js
2132 lines (1795 loc) · 98.7 KB
/
selector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
QUnit.module( "selector", {
beforeEach: function() {
this.safari = /\bsafari\b/i.test( navigator.userAgent ) &&
!/\bchrome\b/i.test( navigator.userAgent );
},
afterEach: moduleTeardown
} );
QUnit.test( "empty", function( assert ) {
assert.expect( 5 );
var form;
assert.strictEqual( jQuery( "" ).length, 0,
"Empty selector returns an empty array" );
assert.deepEqual( jQuery( "div", document.createTextNode( "" ) ).get(), [],
"Text element as context fails silently" );
form = document.getElementById( "form" );
assert.ok( !jQuery( form ).is( "" ), "Empty string passed to .is() does not match" );
if ( QUnit.jQuerySelectors ) {
assert.equal( jQuery( " " ).length, 0, "Empty selector returns an empty array" );
assert.equal( jQuery( "\t" ).length, 0, "Empty selector returns an empty array" );
} else {
assert.ok( "skip", "whitespace-only selector not supported in selector-native" );
assert.ok( "skip", "whitespace-only selector not supported in selector-native" );
}
} );
QUnit.test( "star", function( assert ) {
assert.expect( 2 );
var good, i;
var all = jQuery( "*" );
assert.ok( all.length >= 30, "Select all" );
good = true;
for ( i = 0; i < all.length; i++ ) {
if ( all[ i ].nodeType === 8 ) {
good = false;
}
}
assert.ok( good, "Select all elements, no comment nodes" );
} );
QUnit.test( "element", function( assert ) {
assert.expect( 37 );
var i, lengthtest, siblingTest, html;
var fixture = document.getElementById( "qunit-fixture" );
assert.deepEqual( jQuery( "p", fixture ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a Node context." );
assert.deepEqual( jQuery( "p", "#qunit-fixture" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a selector context." );
assert.deepEqual( jQuery( "p", jQuery( "#qunit-fixture" ) ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a jQuery object context." );
assert.deepEqual( jQuery( "#qunit-fixture" ).find( "p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a context via .find()." );
assert.ok( jQuery( "#length" ).length, "<input name=\"length\"> cannot be found under IE, see trac-945" );
assert.ok( jQuery( "#lengthtest input" ).length, "<input name=\"length\"> cannot be found under IE, see trac-945" );
// trac-7533
assert.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" );
assert.equal( jQuery( "" ).length, 0, "Empty selector returns an empty array" );
assert.deepEqual( jQuery( "div", document.createTextNode( "" ) ).get(), [],
"Text element as context fails silently" );
assert.t( "Element Selector", "html", [ "html" ] );
assert.t( "Element Selector", "body", [ "body" ] );
assert.t( "Element Selector", "#qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Leading space", " #qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Leading tab", "\t#qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Leading carriage return", "\r#qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Leading line feed", "\n#qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Leading form feed", "\f#qunit-fixture p", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Trailing space", "#qunit-fixture p ", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Trailing tab", "#qunit-fixture p\t", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Trailing carriage return", "#qunit-fixture p\r",
[ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Trailing line feed", "#qunit-fixture p\n", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.t( "Trailing form feed", "#qunit-fixture p\f", [ "firstp", "ap", "sndp", "en", "sap", "first" ] );
assert.deepEqual(
jQuery( jQuery( "div ol" ) ).filter( "#qunit-fixture *" ).get(),
q( "empty", "listWithTabIndex" ),
"Parent Element"
);
assert.deepEqual(
jQuery( jQuery( "div\tol" ) ).filter( "#qunit-fixture *" ).get(),
q( "empty", "listWithTabIndex" ),
"Parent Element (non-space descendant combinator)"
);
// Check for unique-ness and sort order
assert.deepEqual( jQuery( "p, div p" ), jQuery( "p" ), "Check for duplicates: p, div p" );
jQuery( "<h1 id='h1'></h1><h2 id='h2'></h2><h2 id='h2-2'></h2>" ).prependTo( "#qunit-fixture" );
assert.t( "Checking sort order", "#qunit-fixture h2, #qunit-fixture h1", [ "h1", "h2", "h2-2" ] );
if ( QUnit.jQuerySelectorsPos ) {
assert.t( "Checking sort order", "#qunit-fixture h2:first, #qunit-fixture h1:first", [ "h1", "h2" ] );
} else {
assert.ok( "skip", "Positional selectors are not supported" );
}
assert.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" ] );
// Test Conflict ID
lengthtest = document.getElementById( "lengthtest" );
assert.deepEqual( jQuery( "#idTest", lengthtest ).get(), q( "idTest" ),
"Finding element with id of ID." );
assert.deepEqual( jQuery( "[name='id']", lengthtest ).get(), q( "idTest" ),
"Finding element with id of ID." );
assert.deepEqual( jQuery( "input[id='idTest']", lengthtest ).get(), q( "idTest" ),
"Finding elements with id of ID." );
if ( QUnit.jQuerySelectors ) {
siblingTest = document.getElementById( "siblingTest" );
assert.deepEqual( jQuery( "div em", siblingTest ).get(), [],
"Element-rooted QSA does not select based on document context" );
assert.deepEqual( jQuery( "div em, div em, div em:not(div em)", siblingTest ).get(), [],
"Element-rooted QSA does not select based on document context" );
assert.deepEqual( jQuery( "div em, em\\,", siblingTest ).get(), [],
"Escaped commas do not get treated with an id in element-rooted QSA" );
} else {
assert.ok( "skip", "Element-rooted QSA behavior different in selector-native" );
assert.ok( "skip", "Element-rooted QSA behavior different in selector-native" );
assert.ok( "skip", "Element-rooted QSA behavior different in selector-native" );
}
html = "";
for ( i = 0; i < 100; i++ ) {
html = "<div>" + html + "</div>";
}
html = jQuery( html ).appendTo( document.body );
assert.ok( !!jQuery( "body div div div" ).length,
"No stack or performance problems with large amounts of descendants" );
assert.ok( !!jQuery( "body>div div div" ).length,
"No stack or performance problems with large amounts of descendants" );
html.remove();
// Real use case would be using .watch in browsers with window.watch
// (see https://github.com/jquery/sizzle/pull/157)
q( "qunit-fixture" )[ 0 ].appendChild( document.createElement( "toString" ) ).id = "toString";
assert.t( "Element name matches Object.prototype property", "toString#toString", [ "toString" ] );
} );
QUnit.test( "XML Document Selectors", function( assert ) {
assert.expect( 11 );
var xml = createWithFriesXML();
assert.equal( jQuery( "foo_bar", xml ).length, 1, "Element Selector with underscore" );
assert.equal( jQuery( ".component", xml ).length, 1, "Class selector" );
assert.equal( jQuery( "[class*=component]", xml ).length, 1, "Attribute selector for class" );
assert.equal( jQuery( "property[name=prop2]", xml ).length, 1, "Attribute selector with name" );
assert.equal( jQuery( "[name=prop2]", xml ).length, 1, "Attribute selector with name" );
assert.equal( jQuery( "#seite1", xml ).length, 1, "Attribute selector with ID" );
assert.equal( jQuery( "component#seite1", xml ).length, 1, "Attribute selector with ID" );
assert.equal( jQuery( "component", xml ).filter( "#seite1" ).length, 1,
"Attribute selector filter with ID" );
assert.equal( jQuery( "meta property thing", xml ).length, 2,
"Descendent selector and dir caching" );
if ( QUnit.jQuerySelectors ) {
assert.ok( jQuery( xml.lastChild ).is( "soap\\:Envelope" ), "Check for namespaced element" );
xml = jQuery.parseXML( "<?xml version='1.0' encoding='UTF-8'?><root><elem id='1'/></root>" );
assert.equal( jQuery( "elem:not(:has(*))", xml ).length, 1,
"Non-qSA path correctly handles numeric ids (jQuery trac-14142)" );
} else {
assert.ok( "skip", "namespaced elements not matching correctly in selector-native" );
assert.ok( "skip", ":not(complex selector) not supported in selector-native" );
}
} );
QUnit.test( "broken selectors throw", function( assert ) {
assert.expect( 33 );
function broken( name, selector ) {
assert.throws( function() {
jQuery( selector );
}, name + ": " + selector );
}
broken( "Broken Selector", "[" );
broken( "Broken Selector", "(" );
broken( "Broken Selector", "{" );
broken( "Broken Selector", "<" );
broken( "Broken Selector", "()" );
broken( "Broken Selector", "<>" );
broken( "Broken Selector", "{}" );
broken( "Broken Selector", "," );
broken( "Broken Selector", ",a" );
broken( "Broken Selector", "a," );
broken( "Post-comma invalid selector", "*,:x" );
broken( "Identifier with bad escape", "foo\\\fbaz" );
broken( "Broken Selector", "[id=012345678901234567890123456789" );
broken( "Doesn't exist", ":visble" );
broken( "Nth-child", ":nth-child" );
broken( "Nth-child", ":nth-child(-)" );
broken( "Nth-child", ":nth-child(asdf)", [] );
broken( "Nth-child", ":nth-child(2n+-0)" );
broken( "Nth-child", ":nth-child(2+0)" );
broken( "Nth-child", ":nth-child(- 1n)" );
broken( "Nth-child", ":nth-child(-1 n)" );
broken( "First-child", ":first-child(n)" );
broken( "Last-child", ":last-child(n)" );
broken( "Only-child", ":only-child(n)" );
broken( "Nth-last-last-child", ":nth-last-last-child(1)" );
broken( "First-last-child", ":first-last-child" );
broken( "Last-last-child", ":last-last-child" );
broken( "Only-last-child", ":only-last-child" );
// Make sure attribute value quoting works correctly. See: trac-6093
jQuery( "<input type='hidden' value='2' name='foo.baz' id='attrbad1'/>" +
"<input type='hidden' value='2' name='foo[baz]' id='attrbad2'/>" )
.appendTo( "#qunit-fixture" );
broken( "Attribute equals non-value", "input[name=]" );
broken( "Attribute equals unquoted non-identifier", "input[name=foo.baz]" );
broken( "Attribute equals unquoted non-identifier", "input[name=foo[baz]]" );
broken( "Attribute equals bad string", "input[name=''double-quoted'']" );
broken( "Attribute equals bad string", "input[name='apostrophe'd']" );
} );
QUnit.test( "id", function( assert ) {
assert.expect( 34 );
var fiddle, a;
assert.t( "ID Selector", "#body", [ "body" ] );
assert.t( "ID Selector w/ Element", "body#body", [ "body" ] );
assert.t( "ID Selector w/ Element", "ul#first", [] );
assert.t( "ID selector with existing ID descendant", "#firstp #simon1", [ "simon1" ] );
assert.t( "ID selector with non-existent descendant", "#firstp #foobar", [] );
assert.t( "ID selector using UTF8", "#台北Táiběi", [ "台北Táiběi" ] );
assert.t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", [ "台北Táiběi", "台北" ] );
assert.t( "Descendant ID selector using UTF8", "div #台北", [ "台北" ] );
assert.t( "Child ID selector using UTF8", "form > #台北", [ "台北" ] );
assert.t( "Escaped ID", "#foo\\:bar", [ "foo:bar" ] );
if ( QUnit.jQuerySelectors ) {
assert.t( "Escaped ID with descendant", "#foo\\:bar span:not(:input)", [ "foo_descendant" ] );
} else {
assert.ok( "skip", ":input not supported in selector-native" );
}
assert.t( "Escaped ID", "#test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
assert.t( "Descendant escaped ID", "div #foo\\:bar", [ "foo:bar" ] );
assert.t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
assert.t( "Child escaped ID", "form > #foo\\:bar", [ "foo:bar" ] );
assert.t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
fiddle = jQuery( "<div id='fiddle\\Foo'><span id='fiddleSpan'></span></div>" )
.appendTo( "#qunit-fixture" );
if ( QUnit.jQuerySelectors ) {
assert.deepEqual( jQuery( "> span", jQuery( "#fiddle\\\\Foo" )[ 0 ] ).get(),
q( [ "fiddleSpan" ] ), "Escaped ID as context" );
} else {
assert.ok( "skip", "leading > not supported in selector-native" );
}
fiddle.remove();
assert.t( "ID Selector, child ID present", "#form > #radio1", [ "radio1" ] ); // bug trac-267
assert.t( "ID Selector, not an ancestor ID", "#form #first", [] );
assert.t( "ID Selector, not a child ID", "#form > #option1a", [] );
assert.t( "All Children of ID", "#foo > *", [ "sndp", "en", "sap" ] );
assert.t( "All Children of ID with no children", "#firstUL > *", [] );
assert.equal( jQuery( "#tName1" )[ 0 ].id, "tName1",
"ID selector with same value for a name attribute" );
assert.t( "ID selector non-existing but name attribute on an A tag", "#tName2", [] );
assert.t( "Leading ID selector non-existing but name attribute on an A tag", "#tName2 span", [] );
assert.t( "Leading ID selector existing, retrieving the child", "#tName1 span", [ "tName1-span" ] );
assert.equal( jQuery( "div > div #tName1" )[ 0 ].id, jQuery( "#tName1-span" )[ 0 ].parentNode.id,
"Ending with ID" );
a = jQuery( "<a id='backslash\\foo'></a>" ).appendTo( "#qunit-fixture" );
assert.t( "ID Selector contains backslash", "#backslash\\\\foo", [ "backslash\\foo" ] );
a.remove();
assert.t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", [ "lengthtest" ] );
assert.t( "ID selector with non-existent ancestor", "#asdfasdf #foobar", [] ); // bug trac-986
assert.deepEqual( jQuery( "div#form", document.body ).get(), [],
"ID selector within the context of another element" );
assert.t( "Underscore ID", "#types_all", [ "types_all" ] );
assert.t( "Dash ID", "#qunit-fixture", [ "qunit-fixture" ] );
assert.t( "ID with weird characters in it", "#name\\+value", [ "name+value" ] );
} );
QUnit.test( "class", function( assert ) {
assert.expect( 32 );
assert.deepEqual( jQuery( ".blog", document.getElementsByTagName( "p" ) ).get(),
q( "mark", "simon" ), "Finding elements with a context." );
assert.deepEqual( jQuery( ".blog", "p" ).get(),
q( "mark", "simon" ), "Finding elements with a context." );
assert.deepEqual( jQuery( ".blog", jQuery( "p" ) ).get(),
q( "mark", "simon" ), "Finding elements with a context." );
assert.deepEqual( jQuery( "p" ).find( ".blog" ).get(),
q( "mark", "simon" ), "Finding elements with a context." );
assert.t( "Class Selector", ".blog", [ "mark", "simon" ] );
assert.t( "Class Selector", ".GROUPS", [ "groups" ] );
assert.t( "Class Selector", ".blog.link", [ "simon" ] );
assert.t( "Class Selector w/ Element", "a.blog", [ "mark", "simon" ] );
assert.t( "Parent Class Selector", "p .blog", [ "mark", "simon" ] );
assert.t( "Class selector using UTF8", ".台北Táiběi", [ "utf8class1" ] );
assert.t( "Class selector using UTF8", ".台北", [ "utf8class1", "utf8class2" ] );
assert.t( "Class selector using UTF8", ".台北Táiběi.台北", [ "utf8class1" ] );
assert.t( "Class selector using UTF8", ".台北Táiběi, .台北", [ "utf8class1", "utf8class2" ] );
assert.t( "Descendant class selector using UTF8", "div .台北Táiběi", [ "utf8class1" ] );
assert.t( "Child class selector using UTF8", "form > .台北Táiběi", [ "utf8class1" ] );
assert.t( "Escaped Class", ".foo\\:bar", [ "foo:bar" ] );
assert.t( "Escaped Class", ".test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
assert.t( "Descendant escaped Class", "div .foo\\:bar", [ "foo:bar" ] );
assert.t( "Descendant escaped Class", "div .test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
assert.t( "Child escaped Class", "form > .foo\\:bar", [ "foo:bar" ] );
assert.t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] );
var div = document.createElement( "div" );
div.innerHTML = "<div class='test e'></div><div class='test'></div>";
assert.deepEqual( jQuery( ".e", div ).get(), [ div.firstChild ], "Finding a second class." );
div.lastChild.className = "e";
assert.ok( !jQuery( div ).is( ".null" ),
".null does not match an element with no class" );
assert.ok( !jQuery( div.firstChild ).is( ".null div" ),
".null does not match an element with no class" );
div.className = "null";
assert.ok( jQuery( div ).is( ".null" ), ".null matches element with class 'null'" );
assert.ok( jQuery( div.firstChild ).is( ".null div" ),
"caching system respects DOM changes" );
assert.ok( !jQuery( document ).is( ".foo" ),
"testing class on document doesn't error" );
assert.ok( !jQuery( window ).is( ".foo" ), "testing class on window doesn't error" );
assert.deepEqual( jQuery( ".e", div ).get(), [ div.firstChild, div.lastChild ],
"Finding a modified class." );
div.lastChild.className += " hasOwnProperty toString";
assert.deepEqual( jQuery( ".e.hasOwnProperty.toString", div ).get(), [ div.lastChild ],
"Classes match Object.prototype properties" );
div = jQuery( "<div><svg width='200' height='250' version='1.1'" +
" xmlns='http://www.w3.org/2000/svg'><rect x='10' y='10' width='30' height='30'" +
"class='foo'></rect></svg></div>" )[ 0 ];
assert.equal( jQuery( ".foo", div ).length, 1, "Class selector against SVG container" );
assert.equal( jQuery( ".foo", div.firstChild ).length, 1,
"Class selector directly against SVG" );
} );
QUnit.test( "name", function( assert ) {
assert.expect( 14 );
var form;
assert.t( "Name selector", "input[name=action]", [ "text1" ] );
assert.t( "Name selector with single quotes", "input[name='action']", [ "text1" ] );
assert.t( "Name selector with double quotes", "input[name=\"action\"]", [ "text1" ] );
assert.t( "Name selector non-input", "[name=example]", [ "name-is-example" ] );
assert.t( "Name selector non-input", "[name=div]", [ "name-is-div" ] );
assert.t( "Name selector non-input", "*[name=iframe]", [ "iframe" ] );
assert.t( "Name selector for grouped input", "input[name='types[]']", [ "types_all", "types_anime", "types_movie" ] );
form = document.getElementById( "form" );
assert.deepEqual( jQuery( "input[name=action]", form ).get(), q( "text1" ),
"Name selector within the context of another element" );
assert.deepEqual( jQuery( "input[name='foo[bar]']", form ).get(), q( "hidden2" ),
"Name selector for grouped form element within the context of another element" );
form = jQuery( "<form><input name='id'/></form>" ).appendTo( "body" );
assert.equal( jQuery( "input", form[ 0 ] ).length, 1,
"Make sure that rooted queries on forms (with possible expandos) work." );
form.remove();
assert.t( "Find elements that have similar IDs", "[name=tName1]", [ "tName1ID" ] );
assert.t( "Find elements that have similar IDs", "[name=tName2]", [ "tName2ID" ] );
assert.t( "Find elements that have similar IDs", "#tName2ID", [ "tName2ID" ] );
assert.t( "Case-sensitivity", "[name=tname1]", [] );
} );
QUnit.test( "comma-separated", function( assert ) {
assert.expect( 4 );
var fixture = jQuery( "<div><h2><span></span></h2><div><p><span></span></p><p></p></div></div>" );
assert.equal( fixture.find( "h2, div p" ).filter( "p" ).length, 2, "has to find two <p>" );
assert.equal( fixture.find( "h2, div p" ).filter( "h2" ).length, 1, "has to find one <h2>" );
assert.equal( fixture.find( "h2 , div p" ).filter( "p" ).length, 2, "has to find two <p>" );
assert.equal( fixture.find( "h2 , div p" ).filter( "h2" ).length, 1, "has to find one <h2>" );
} );
QUnit.test( "child and adjacent", function( assert ) {
assert.expect( 43 );
var siblingFirst, en, nothiddendiv;
assert.t( "Child", "p > a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] );
assert.t( "Child minus leading whitespace", "p> a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] );
assert.t( "Child minus trailing whitespace", "p >a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] );
assert.t( "Child minus whitespace", "p>a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] );
assert.t( "Child w/ Class", "p > a.blog", [ "mark", "simon" ] );
assert.t( "All Children", "code > *", [ "anchor1", "anchor2" ] );
assert.selectInFixture( "All Grandchildren", "p > * > *", [ "anchor1", "anchor2" ] );
assert.t( "Rooted tag adjacent", "#qunit-fixture a + a", [ "groups", "tName2ID" ] );
assert.t( "Rooted tag adjacent minus whitespace", "#qunit-fixture a+a", [ "groups", "tName2ID" ] );
assert.t( "Rooted tag adjacent minus leading whitespace", "#qunit-fixture a +a",
[ "groups", "tName2ID" ] );
assert.t( "Rooted tag adjacent minus trailing whitespace", "#qunit-fixture a+ a",
[ "groups", "tName2ID" ] );
assert.t( "Tag adjacent", "p + p", [ "ap", "en", "sap" ] );
assert.t( "#id adjacent", "#firstp + p", [ "ap" ] );
assert.t( "Tag#id adjacent", "p#firstp + p", [ "ap" ] );
assert.t( "Tag[attr] adjacent", "p[lang=en] + p", [ "sap" ] );
assert.t( "Tag.class adjacent", "a.GROUPS + code + a", [ "mark" ] );
assert.t( "Comma, Child, and Adjacent", "#qunit-fixture a + a, code > a",
[ "groups", "anchor1", "anchor2", "tName2ID" ] );
assert.t( "Element Preceded By", "#qunit-fixture p ~ div",
[ "foo", "nothiddendiv", "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ] );
assert.t( "Element Preceded By", "#first ~ div",
[ "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ] );
assert.t( "Element Preceded By", "#groups ~ a", [ "mark" ] );
assert.t( "Element Preceded By", "#length ~ input", [ "idTest" ] );
assert.t( "Element Preceded By", "#siblingfirst ~ em", [ "siblingnext", "siblingthird" ] );
assert.t( "Element Preceded By (multiple)", "#siblingTest em ~ em ~ em ~ span", [ "siblingspan" ] );
siblingFirst = document.getElementById( "siblingfirst" );
if ( QUnit.jQuerySelectors ) {
assert.deepEqual( jQuery( "+ em", siblingFirst ).get(), q( "siblingnext" ),
"Element Directly Preceded By with a context." );
assert.deepEqual( jQuery( "~ em", siblingFirst ).get(), q( "siblingnext", "siblingthird" ),
"Element Preceded By with a context." );
} else {
assert.ok( "skip", "leading + not supported in selector-native" );
assert.ok( "skip", "leading ~ not supported in selector-native" );
}
if ( QUnit.jQuerySelectorsPos ) {
assert.deepEqual( jQuery( "~ em:first", siblingFirst ).get(), q( "siblingnext" ),
"Element Preceded By positional with a context." );
} else {
assert.ok( "skip", "Positional selectors are not supported" );
}
if ( QUnit.jQuerySelectors ) {
en = document.getElementById( "en" );
assert.deepEqual( jQuery( "+ p, a", en ).get(), q( "yahoo", "sap" ),
"Compound selector with context, beginning with sibling test." );
assert.deepEqual( jQuery( "a, + p", en ).get(), q( "yahoo", "sap" ),
"Compound selector with context, containing sibling test." );
} else {
assert.ok( "skip", "leading + not supported in selector-native" );
assert.ok( "skip", "leading + not supported in selector-native" );
}
if ( QUnit.jQuerySelectors ) {
assert.t( "Element Preceded By, Containing", "#liveHandlerOrder ~ div em:contains('1')", [ "siblingfirst" ] );
assert.t( "Combinators are not skipped when mixing general and specific", "#siblingTest > em:contains('x') + em ~ span", [] );
} else {
assert.ok( "skip", ":contains not supported in selector-native" );
assert.ok( "skip", ":contains not supported in selector-native" );
}
if ( QUnit.jQuerySelectorsPos ) {
assert.equal( jQuery( "#listWithTabIndex li:eq(2) ~ li" ).length, 1, "Find by general sibling combinator (trac-8310)" );
nothiddendiv = document.getElementById( "nothiddendiv" );
assert.deepEqual( jQuery( "> :first", nothiddendiv ).get(), q( "nothiddendivchild" ),
"Verify child context positional selector" );
assert.deepEqual( jQuery( "> :eq(0)", nothiddendiv ).get(), q( "nothiddendivchild" ),
"Verify child context positional selector" );
assert.deepEqual( jQuery( "> *:first", nothiddendiv ).get(), q( "nothiddendivchild" ),
"Verify child context positional selector" );
} else {
assert.ok( "skip", "Positional selectors are not supported" );
assert.ok( "skip", "Positional selectors are not supported" );
assert.ok( "skip", "Positional selectors are not supported" );
assert.ok( "skip", "Positional selectors are not supported" );
}
assert.t( "Multiple combinators selects all levels", "#siblingTest em *", [ "siblingchild", "siblinggrandchild", "siblinggreatgrandchild" ] );
assert.t( "Multiple combinators selects all levels", "#siblingTest > em *", [ "siblingchild", "siblinggrandchild", "siblinggreatgrandchild" ] );
assert.t( "Multiple sibling combinators doesn't miss general siblings", "#siblingTest > em:first-child + em ~ span", [ "siblingspan" ] );
assert.equal( jQuery( "#listWithTabIndex" ).length, 1, "Parent div for next test is found via ID (trac-8310)" );
assert.equal( jQuery( "#__sizzle__" ).length, 0, "Make sure the temporary id assigned by sizzle is cleared out (trac-8310)" );
assert.equal( jQuery( "#listWithTabIndex" ).length, 1, "Parent div for previous test is still found via ID (trac-8310)" );
assert.t( "Verify deep class selector", "div.blah > p > a", [] );
assert.t( "No element deep selector", "div.foo > span > a", [] );
assert.t( "Non-existent ancestors", ".fototab > .thumbnails > a", [] );
} );
QUnit.test( "attributes - existence", function( assert ) {
assert.expect( 7 );
assert.t( "On element", "#qunit-fixture a[title]", [ "google" ] );
assert.t( "On element (whitespace ignored)", "#qunit-fixture a[ title ]", [ "google" ] );
assert.t( "On element (case-insensitive)", "#qunit-fixture a[TITLE]", [ "google" ] );
assert.t( "On any element", "#qunit-fixture *[title]", [ "google" ] );
assert.t( "On implicit element", "#qunit-fixture [title]", [ "google" ] );
assert.t( "Boolean", "#select2 option[selected]", [ "option2d" ] );
assert.t( "For attribute on label", "form label[for]", [ "label-for" ] );
} );
QUnit.test( "attributes - equals", function( assert ) {
assert.expect( 20 );
var withScript;
assert.t( "Identifier", "#qunit-fixture a[rel=bookmark]", [ "simon1" ] );
assert.t( "Identifier with underscore", "input[id=types_all]", [ "types_all" ] );
assert.t( "String", "#qunit-fixture a[rel='bookmark']", [ "simon1" ] );
assert.t( "String (whitespace ignored)", "#qunit-fixture a[ rel = 'bookmark' ]", [ "simon1" ] );
assert.t( "Non-identifier string", "#qunit-fixture a[href='https://www.google.com/']", [ "google" ] );
assert.t( "Empty string", "#select1 option[value='']", [ "option1a" ] );
if ( QUnit.jQuerySelectors ) {
assert.t( "Number",
"#qunit-fixture option[value=1]",
[ "option1b", "option2b", "option3b", "option4b", "option5c" ] );
assert.t( "negative number",
"#qunit-fixture li[tabIndex=-1]", [ "foodWithNegativeTabIndex" ] );
} else {
assert.ok( "skip", "Number value not supported in selector-native" );
assert.ok( "skip", "Negative number value not supported in selector-native" );
}
assert.t( "Non-ASCII identifier", "span[lang=中文]", [ "台北" ] );
assert.t( "input[type=text]", "#form input[type=text]", [ "text1", "text2", "hidden2", "name" ] );
assert.t( "input[type=search]", "#form input[type=search]", [ "search" ] );
withScript = supportjQuery( "<div><span><script src=''></script></span></div>" );
assert.ok( withScript.find( "#moretests script[src]" ).has( "script" ), "script[src] (jQuery trac-13777)" );
assert.t( "Boolean attribute equals name", "#select2 option[selected='selected']", [ "option2d" ] );
assert.t( "for Attribute in form", "#form [for=action]", [ "label-for" ] );
assert.t( "Grouped Form Elements - name", "input[name='foo[bar]']", [ "hidden2" ] );
assert.t( "Value", "input[value=Test]", [ "text1", "text2" ] );
assert.deepEqual(
jQuery( "input[data-comma='0,1']" ).get(),
q( "el12087" ),
"Without context, single-quoted attribute containing ','" );
assert.deepEqual(
jQuery( "input[data-comma=\"0,1\"]" ).get(),
q( "el12087" ),
"Without context, double-quoted attribute containing ','" );
assert.deepEqual(
jQuery( "input[data-comma='0,1']", document.getElementById( "t12087" ) ).get(),
q( "el12087" ),
"With context, single-quoted attribute containing ','" );
assert.deepEqual(
jQuery( "input[data-comma=\"0,1\"]", document.getElementById( "t12087" ) ).get(),
q( "el12087" ),
"With context, double-quoted attribute containing ','" );
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "attributes - does not equal", function( assert ) {
assert.expect( 2 );
assert.t( "string", "#ap a[hreflang!='en']", [ "google", "groups", "anchor1" ] );
assert.t( "Empty values", "#select1 option[value!='']", [ "option1b", "option1c", "option1d" ] );
} );
QUnit.test( "attributes - starts with", function( assert ) {
assert.expect( 4 );
assert.t( "string (whitespace ignored)", "a[href ^= 'https://www']", [ "google", "yahoo" ] );
assert.t( "href starts with hash", "p a[href^='#']", [ "anchor2" ] );
assert.t( "string containing '['", "input[name^='foo[']", [ "hidden2" ] );
assert.t( "string containing '[' ... ']'", "input[name^='foo[bar]']", [ "hidden2" ] );
} );
QUnit.test( "attributes - contains", function( assert ) {
assert.expect( 4 );
assert.t( "string (whitespace ignored)", "a[href *= 'google']", [ "google", "groups" ] );
assert.t( "string like '[' ... ']']", "input[name*='[bar]']", [ "hidden2" ] );
assert.t( "string containing '['...']", "input[name*='foo[bar]']", [ "hidden2" ] );
assert.t( "href contains hash", "p a[href*='#']", [ "simon1", "anchor2" ] );
} );
QUnit.test( "attributes - ends with", function( assert ) {
assert.expect( 4 );
assert.t( "string (whitespace ignored)", "a[href $= 'org/']", [ "mark" ] );
assert.t( "string ending with ']'", "input[name$='bar]']", [ "hidden2" ] );
assert.t( "string like '[' ... ']'", "input[name$='[bar]']", [ "hidden2" ] );
assert.t( "Attribute containing []", "input[name$='foo[bar]']", [ "hidden2" ] );
} );
QUnit.test( "attributes - whitespace list includes", function( assert ) {
assert.expect( 3 );
assert.t( "string found at the beginning",
"input[data-15233~='foo']",
[ "t15233-single", "t15233-double", "t15233-double-tab", "t15233-double-nl", "t15233-triple" ] );
assert.t( "string found in the middle",
"input[data-15233~='bar']",
[ "t15233-double", "t15233-double-tab", "t15233-double-nl", "t15233-triple" ] );
assert.t( "string found at the end", "input[data-15233~='baz']", [ "t15233-triple" ] );
} );
QUnit.test( "attributes - hyphen-prefix matches", function( assert ) {
assert.expect( 3 );
assert.t( "string", "#names-group span[id|='name']", [ "name-is-example", "name-is-div" ] );
assert.t( "string containing hyphen",
"#names-group span[id|='name-is']",
[ "name-is-example", "name-is-div" ] );
assert.t( "string ending with hyphen", "#names-group span[id|='name-is-']", [] );
} );
QUnit.test( "attributes - special characters", function( assert ) {
assert.expect( 16 );
var attrbad;
var div = document.createElement( "div" );
// trac-3729
div.innerHTML = "<div id='foo' xml:test='something'></div>";
assert.deepEqual( jQuery( "[xml\\:test]", div ).get(),
[ div.firstChild ],
"attribute name containing colon" );
// Make sure attribute value quoting works correctly.
// See jQuery trac-6093; trac-6428; trac-13894.
// Use seeded results to bypass querySelectorAll optimizations.
attrbad = jQuery(
"<input type='hidden' id='attrbad_space' name='foo bar'/>" +
"<input type='hidden' id='attrbad_dot' value='2' name='foo.baz'/>" +
"<input type='hidden' id='attrbad_brackets' value='2' name='foo[baz]'/>" +
"<input type='hidden' id='attrbad_leading_digits' name='agent' value='007'/>" +
"<input type='hidden' id='attrbad_injection' data-attr='foo_baz']'/>" +
"<input type='hidden' id='attrbad_quote' data-attr='''/>" +
"<input type='hidden' id='attrbad_backslash' data-attr='\'/>" +
"<input type='hidden' id='attrbad_backslash_quote' data-attr='\''/>" +
"<input type='hidden' id='attrbad_backslash_backslash' data-attr='\\'/>" +
"<input type='hidden' id='attrbad_unicode' data-attr='一'/>"
).appendTo( "#qunit-fixture" ).get();
assert.deepEqual( jQuery( attrbad ).filter( "input[name=foo\\ bar]" ).get(),
q( "attrbad_space" ),
"identifier containing space" );
assert.deepEqual( jQuery( attrbad ).filter( "input[name=foo\\.baz]" ).get(),
q( "attrbad_dot" ),
"identifier containing dot" );
assert.deepEqual( jQuery( attrbad ).filter( "input[name=foo\\[baz\\]]" ).get(),
q( "attrbad_brackets" ),
"identifier containing brackets" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='foo_baz\\']']" ).get(),
q( "attrbad_injection" ),
"string containing quote and right bracket" );
assert.deepEqual( jQuery( attrbad ).filter( "input[value=\\30 \\30\\37 ]" ).get(),
q( "attrbad_leading_digits" ),
"identifier containing escaped leading digits with whitespace termination" );
assert.deepEqual( jQuery( attrbad ).filter( "input[value=\\00003007]" ).get(),
q( "attrbad_leading_digits" ),
"identifier containing escaped leading digits without whitespace termination" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\'']" ).get(),
q( "attrbad_quote" ),
"string containing quote" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\\\']" ).get(),
q( "attrbad_backslash" ),
"string containing backslash" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\\\\\'']" ).get(),
q( "attrbad_backslash_quote" ),
"string containing backslash and quote" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\\\\\\\']" ).get(),
q( "attrbad_backslash_backslash" ),
"string containing adjacent backslashes" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\5C\\\\']" ).get(),
q( "attrbad_backslash_backslash" ),
"string containing numeric-escape backslash and backslash" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\5C \\\\']" ).get(),
q( "attrbad_backslash_backslash" ),
"string containing numeric-escape-with-trailing-space backslash and backslash" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\5C\t\\\\']" ).get(),
q( "attrbad_backslash_backslash" ),
"string containing numeric-escape-with-trailing-tab backslash and backslash" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\04e00']" ).get(),
q( "attrbad_unicode" ),
"Long numeric escape (BMP)" );
document.getElementById( "attrbad_unicode" ).setAttribute( "data-attr", "\uD834\uDF06A" );
assert.deepEqual( jQuery( attrbad ).filter( "input[data-attr='\\01D306A']" ).get(),
q( "attrbad_unicode" ),
"Long numeric escape (non-BMP)" );
} );
QUnit.test( "attributes - others", function( assert ) {
assert.expect( 14 );
var div = document.getElementById( "foo" );
assert.t( "Find elements with a tabindex attribute", "[tabindex]", [ "listWithTabIndex", "foodWithNegativeTabIndex", "linkWithTabIndex", "linkWithNegativeTabIndex", "linkWithNoHrefWithTabIndex", "linkWithNoHrefWithNegativeTabIndex" ] );
assert.t( "Selector list with multiple quoted attribute-equals",
"#form input[type='radio'], #form input[type='hidden']",
[ "radio1", "radio2", "hidden1" ] );
assert.t( "Selector list with differently-quoted attribute-equals",
"#form input[type='radio'], #form input[type=\"hidden\"]",
[ "radio1", "radio2", "hidden1" ] );
assert.t( "Selector list with quoted and unquoted attribute-equals",
"#form input[type='radio'], #form input[type=hidden]",
[ "radio1", "radio2", "hidden1" ] );
assert.t( "Object.prototype property \"constructor\" (negative)", "[constructor]", [] );
assert.t( "Gecko Object.prototype property \"watch\" (negative)", "[watch]", [] );
div.setAttribute( "constructor", "foo" );
div.setAttribute( "watch", "bar" );
assert.t( "Object.prototype property \"constructor\"", "[constructor='foo']", [ "foo" ] );
assert.t( "Gecko Object.prototype property \"watch\"", "[watch='bar']", [ "foo" ] );
// trac-11115
assert.ok( jQuery( "<input type='checkbox' checked='checked'/>" ).prop( "checked", false ).is( "[checked]" ),
"[checked] selects by attribute (positive)"
);
assert.ok( !jQuery( "<input type='checkbox'/>" ).prop( "checked", true ).is( "[checked]" ),
"[checked] selects by attribute (negative)"
);
assert.t( "empty name", "[name='']", [ "name-empty" ] );
assert.t( "prefixed empty name", "#empty-name-parent [name='']", [ "name-empty" ] );
var emptyNameContainer = jQuery( ".empty-name-container" );
assert.deepEqual( emptyNameContainer.find( "[name='']" ).get(),
q( "name-empty" ),
"empty name with context" );
assert.deepEqual( emptyNameContainer.find( "#empty-name-parent [name='']" ).get(),
q( "name-empty" ),
"prefixed empty name with context" );
} );
QUnit.test( "pseudo - (parent|empty)", function( assert ) {
assert.expect( 3 );
assert.t( "Empty", "#qunit-fixture ul:empty", [ "firstUL" ] );
assert.t( "Empty with comment node", "#qunit-fixture ol:empty", [ "empty" ] );
if ( QUnit.jQuerySelectors ) {
assert.t( "Is A Parent", "#qunit-fixture p:parent",
[ "firstp", "ap", "sndp", "en", "sap", "first" ] );
} else {
assert.ok( "skip", ":parent not supported in selector-native" );
}
} );
QUnit.test( "pseudo - (first|last|only)-(child|of-type)", function( assert ) {
assert.expect( 12 );
assert.t( "First Child", "#qunit-fixture p:first-child", [ "firstp", "sndp" ] );
assert.t( "First Child (leading id)", "#qunit-fixture p:first-child", [ "firstp", "sndp" ] );
assert.t( "First Child (leading class)", ".nothiddendiv div:first-child", [ "nothiddendivchild" ] );
assert.t( "First Child (case-insensitive)", "#qunit-fixture p:FIRST-CHILD", [ "firstp", "sndp" ] );
assert.t( "Last Child", "#qunit-fixture p:last-child", [ "sap" ] );
assert.t( "Last Child (leading id)", "#qunit-fixture a:last-child", [ "simon1", "anchor1", "mark", "yahoo", "anchor2", "simon", "liveLink1", "liveLink2" ] );
assert.t( "Only Child", "#qunit-fixture a:only-child", [ "simon1", "anchor1", "yahoo", "anchor2", "liveLink1", "liveLink2" ] );
assert.t( "First-of-type", "#qunit-fixture > p:first-of-type", [ "firstp" ] );
assert.t( "Last-of-type", "#qunit-fixture > p:last-of-type", [ "first" ] );
assert.t( "Only-of-type", "#qunit-fixture > :only-of-type", [ "name+value", "firstUL", "empty", "floatTest", "iframe", "table", "last" ] );
// Verify that the child position isn't being cached improperly
var secondChildren = jQuery( "p:nth-child(2)" ).before( "<div></div>" );
assert.t( "No longer second child", "p:nth-child(2)", [] );
secondChildren.prev().remove();
assert.t( "Restored second child", "p:nth-child(2)", [ "ap", "en" ] );
} );
QUnit.test( "pseudo - nth-child", function( assert ) {
assert.expect( 30 );
assert.t( "Nth-child", "p:nth-child(1)", [ "firstp", "sndp" ] );
assert.t( "Nth-child (with whitespace)", "p:nth-child( 1 )", [ "firstp", "sndp" ] );
assert.t( "Nth-child (case-insensitive)", "#form #select1 option:NTH-child(3)", [ "option1c" ] );
assert.t( "Not nth-child", "#qunit-fixture p:not(:nth-child(1))", [ "ap", "en", "sap", "first" ] );
assert.t( "Nth-child(2)", "#qunit-fixture form#form > *:nth-child(2)", [ "text1" ] );
assert.t( "Nth-child(2)", "#qunit-fixture form#form > :nth-child(2)", [ "text1" ] );
assert.t( "Nth-child(-1)", "#form #select1 option:nth-child(-1)", [] );
assert.t( "Nth-child(3)", "#form #select1 option:nth-child(3)", [ "option1c" ] );
assert.t( "Nth-child(0n+3)", "#form #select1 option:nth-child(0n+3)", [ "option1c" ] );
assert.t( "Nth-child(1n+0)", "#form #select1 option:nth-child(1n+0)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-child(1n)", "#form #select1 option:nth-child(1n)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-child(n)", "#form #select1 option:nth-child(n)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-child(even)", "#form #select1 option:nth-child(even)", [ "option1b", "option1d" ] );
assert.t( "Nth-child(odd)", "#form #select1 option:nth-child(odd)", [ "option1a", "option1c" ] );
assert.t( "Nth-child(2n)", "#form #select1 option:nth-child(2n)", [ "option1b", "option1d" ] );
assert.t( "Nth-child(2n+1)", "#form #select1 option:nth-child(2n+1)", [ "option1a", "option1c" ] );
assert.t( "Nth-child(2n + 1)", "#form #select1 option:nth-child(2n + 1)", [ "option1a", "option1c" ] );
assert.t( "Nth-child(+2n + 1)", "#form #select1 option:nth-child(+2n + 1)", [ "option1a", "option1c" ] );
assert.t( "Nth-child(3n)", "#form #select1 option:nth-child(3n)", [ "option1c" ] );
assert.t( "Nth-child(3n+1)", "#form #select1 option:nth-child(3n+1)", [ "option1a", "option1d" ] );
assert.t( "Nth-child(3n+2)", "#form #select1 option:nth-child(3n+2)", [ "option1b" ] );
assert.t( "Nth-child(3n+3)", "#form #select1 option:nth-child(3n+3)", [ "option1c" ] );
assert.t( "Nth-child(3n-1)", "#form #select1 option:nth-child(3n-1)", [ "option1b" ] );
assert.t( "Nth-child(3n-2)", "#form #select1 option:nth-child(3n-2)", [ "option1a", "option1d" ] );
assert.t( "Nth-child(3n-3)", "#form #select1 option:nth-child(3n-3)", [ "option1c" ] );
assert.t( "Nth-child(3n+0)", "#form #select1 option:nth-child(3n+0)", [ "option1c" ] );
assert.t( "Nth-child(-1n+3)", "#form #select1 option:nth-child(-1n+3)", [ "option1a", "option1b", "option1c" ] );
assert.t( "Nth-child(-n+3)", "#form #select1 option:nth-child(-n+3)", [ "option1a", "option1b", "option1c" ] );
assert.t( "Nth-child(-1n + 3)", "#form #select1 option:nth-child(-1n + 3)", [ "option1a", "option1b", "option1c" ] );
if ( QUnit.jQuerySelectors || this.safari ) {
assert.deepEqual(
jQuery( [ document.createElement( "a" ) ].concat( q( "ap" ) ) )
.filter( ":nth-child(n)" )
.get(),
q( "ap" ),
"Seeded nth-child"
);
} else {
// Support: Chrome 75+, Firefox 67+
// Some browsers mark disconnected elements as matching `:nth-child(n)`
// so let's skip the test.
assert.ok( "skip", "disconnected elements match ':nth-child(n)' in Chrome/Firefox" );
}
} );
QUnit.test( "pseudo - nth-last-child", function( assert ) {
assert.expect( 30 );
jQuery( "#qunit-fixture" ).append( "<form id='nth-last-child-form'></form><i></i><i></i><i></i><i></i>" );
assert.t( "Nth-last-child", "form:nth-last-child(5)", [ "nth-last-child-form" ] );
assert.t( "Nth-last-child (with whitespace)", "form:nth-last-child( 5 )", [ "nth-last-child-form" ] );
assert.t( "Nth-last-child (case-insensitive)", "#form #select1 option:NTH-last-child(3)", [ "option1b" ] );
assert.t( "Not nth-last-child", "#qunit-fixture p:not(:nth-last-child(1))", [ "firstp", "ap", "sndp", "en", "first" ] );
assert.t( "Nth-last-child(-1)", "#form #select1 option:nth-last-child(-1)", [] );
assert.t( "Nth-last-child(3)", "#form #select1 :nth-last-child(3)", [ "option1b" ] );
assert.t( "Nth-last-child(3)", "#form #select1 *:nth-last-child(3)", [ "option1b" ] );
assert.t( "Nth-last-child(3)", "#form #select1 option:nth-last-child(3)", [ "option1b" ] );
assert.t( "Nth-last-child(0n+3)", "#form #select1 option:nth-last-child(0n+3)", [ "option1b" ] );
assert.t( "Nth-last-child(1n+0)", "#form #select1 option:nth-last-child(1n+0)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-last-child(1n)", "#form #select1 option:nth-last-child(1n)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-last-child(n)", "#form #select1 option:nth-last-child(n)", [ "option1a", "option1b", "option1c", "option1d" ] );
assert.t( "Nth-last-child(even)", "#form #select1 option:nth-last-child(even)", [ "option1a", "option1c" ] );
assert.t( "Nth-last-child(odd)", "#form #select1 option:nth-last-child(odd)", [ "option1b", "option1d" ] );
assert.t( "Nth-last-child(2n)", "#form #select1 option:nth-last-child(2n)", [ "option1a", "option1c" ] );
assert.t( "Nth-last-child(2n+1)", "#form #select1 option:nth-last-child(2n+1)", [ "option1b", "option1d" ] );
assert.t( "Nth-last-child(2n + 1)", "#form #select1 option:nth-last-child(2n + 1)", [ "option1b", "option1d" ] );
assert.t( "Nth-last-child(+2n + 1)", "#form #select1 option:nth-last-child(+2n + 1)", [ "option1b", "option1d" ] );
assert.t( "Nth-last-child(3n)", "#form #select1 option:nth-last-child(3n)", [ "option1b" ] );
assert.t( "Nth-last-child(3n+1)", "#form #select1 option:nth-last-child(3n+1)", [ "option1a", "option1d" ] );
assert.t( "Nth-last-child(3n+2)", "#form #select1 option:nth-last-child(3n+2)", [ "option1c" ] );
assert.t( "Nth-last-child(3n+3)", "#form #select1 option:nth-last-child(3n+3)", [ "option1b" ] );
assert.t( "Nth-last-child(3n-1)", "#form #select1 option:nth-last-child(3n-1)", [ "option1c" ] );
assert.t( "Nth-last-child(3n-2)", "#form #select1 option:nth-last-child(3n-2)", [ "option1a", "option1d" ] );
assert.t( "Nth-last-child(3n-3)", "#form #select1 option:nth-last-child(3n-3)", [ "option1b" ] );
assert.t( "Nth-last-child(3n+0)", "#form #select1 option:nth-last-child(3n+0)", [ "option1b" ] );
assert.t( "Nth-last-child(-1n+3)", "#form #select1 option:nth-last-child(-1n+3)", [ "option1b", "option1c", "option1d" ] );
assert.t( "Nth-last-child(-n+3)", "#form #select1 option:nth-last-child(-n+3)", [ "option1b", "option1c", "option1d" ] );
assert.t( "Nth-last-child(-1n + 3)", "#form #select1 option:nth-last-child(-1n + 3)", [ "option1b", "option1c", "option1d" ] );
if ( QUnit.jQuerySelectors || this.safari ) {
assert.deepEqual(
jQuery( [ document.createElement( "a" ) ].concat( q( "ap" ) ) )
.filter( ":nth-last-child(n)" )
.get(),
q( "ap" ),
"Seeded nth-last-child"
);
} else {
// Support: Chrome 75+, Firefox 67+
// Some browsers mark disconnected elements as matching `:nth-last-child(n)`
// so let's skip the test.
assert.ok( "skip", "disconnected elements match ':nth-last-child(n)' in Chrome/Firefox" );
}
} );
QUnit.test( "pseudo - nth-of-type", function( assert ) {
assert.expect( 9 );
assert.t( "Nth-of-type(-1)", ":nth-of-type(-1)", [] );
assert.t( "Nth-of-type(3)", "#ap :nth-of-type(3)", [ "mark" ] );
assert.t( "Nth-of-type(n)", "#ap :nth-of-type(n)", [ "google", "groups", "code1", "anchor1", "mark" ] );
assert.t( "Nth-of-type(0n+3)", "#ap :nth-of-type(0n+3)", [ "mark" ] );
assert.t( "Nth-of-type(2n)", "#ap :nth-of-type(2n)", [ "groups" ] );
assert.t( "Nth-of-type(even)", "#ap :nth-of-type(even)", [ "groups" ] );
assert.t( "Nth-of-type(2n+1)", "#ap :nth-of-type(2n+1)", [ "google", "code1", "anchor1", "mark" ] );
assert.t( "Nth-of-type(odd)", "#ap :nth-of-type(odd)", [ "google", "code1", "anchor1", "mark" ] );
assert.t( "Nth-of-type(-n+2)", "#qunit-fixture > :nth-of-type(-n+2)", [ "firstp", "ap", "foo", "nothiddendiv", "name+value", "firstUL", "empty", "form", "floatTest", "iframe", "lengthtest", "table", "last" ] );
} );
QUnit.test( "pseudo - nth-last-of-type", function( assert ) {
assert.expect( 9 );
assert.t( "Nth-last-of-type(-1)", ":nth-last-of-type(-1)", [] );
assert.t( "Nth-last-of-type(3)", "#ap :nth-last-of-type(3)", [ "google" ] );
assert.t( "Nth-last-of-type(n)", "#ap :nth-last-of-type(n)", [ "google", "groups", "code1", "anchor1", "mark" ] );
assert.t( "Nth-last-of-type(0n+3)", "#ap :nth-last-of-type(0n+3)", [ "google" ] );
assert.t( "Nth-last-of-type(2n)", "#ap :nth-last-of-type(2n)", [ "groups" ] );
assert.t( "Nth-last-of-type(even)", "#ap :nth-last-of-type(even)", [ "groups" ] );
assert.t( "Nth-last-of-type(2n+1)", "#ap :nth-last-of-type(2n+1)", [ "google", "code1", "anchor1", "mark" ] );
assert.t( "Nth-last-of-type(odd)", "#ap :nth-last-of-type(odd)", [ "google", "code1", "anchor1", "mark" ] );
assert.t( "Nth-last-of-type(-n+2)", "#qunit-fixture > :nth-last-of-type(-n+2)", [ "ap", "name+value", "first", "firstUL", "empty", "floatTest", "iframe", "table", "testForm", "disabled-tests", "siblingTest", "fx-test-group", "last" ] );
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "pseudo - has", function( assert ) {
assert.expect( 3 );
assert.t( "Basic test", "p:has(a)", [ "firstp", "ap", "en", "sap" ] );
assert.t( "Basic test (irrelevant whitespace)", "p:has( a )", [ "firstp", "ap", "en", "sap" ] );
assert.t( "Nested with overlapping candidates",
"#qunit-fixture div:has(div:has(div:not([id])))",
[ "moretests", "t2037", "fx-test-group", "fx-queue" ] );
} );
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "pseudo - contains", function( assert ) {
assert.expect( 9 );
var gh335 = document.getElementById( "qunit-fixture" ).appendChild(
document.createElement( "mark" ) );
gh335.id = "gh-335";
gh335.appendChild( document.createTextNode( "raw line 1\nline 2" ) );
assert.ok( jQuery( "a:contains('')" ).length, "empty string" );
assert.t( "unquoted argument", "a:contains(Google)", [ "google", "groups" ] );
assert.t( "unquoted argument with whitespace", "a:contains(Google Groups)", [ "groups" ] );
assert.t( "quoted argument with whitespace and parentheses",
"a:contains('Google Groups (Link)')", [ "groups" ] );
assert.t( "quoted argument with double quotes and parentheses",
"a:contains(\"(Link)\")", [ "groups" ] );
assert.t( "unquoted argument with whitespace and paired parentheses",
"a:contains(Google Groups (Link))", [ "groups" ] );
assert.t( "unquoted argument with paired parentheses", "a:contains((Link))", [ "groups" ] );
assert.t( "quoted argument with CSS escapes",
"span:contains(\"\\\"'\\53F0 \\5317 Ta\\301 ibe\\30C i\")",
[ "utf8class1" ] );
assert.t( "collapsed whitespace", "mark:contains('line 1\\A line')", [ "gh-335" ] );
} );
QUnit.test( "pseudo - misc", function( assert ) {
assert.expect( 32 );
var select, tmp, input;
jQuery( "<h1 id='h1'></h1><h2 id='h2'></h2><h2 id='h2-2'></h2>" ).prependTo( "#qunit-fixture" );
if ( QUnit.jQuerySelectors ) {
assert.t( "Headers", "#qunit-fixture :header", [ "h1", "h2", "h2-2" ] );
assert.t( "Headers(case-insensitive)", "#qunit-fixture :Header", [ "h1", "h2", "h2-2" ] );
} else {
assert.ok( "skip", ":header not supported in selector-native" );
assert.ok( "skip", ":header not supported in selector-native" );
}
if ( QUnit.jQuerySelectors ) {
assert.t( "Multiple matches with the same context (cache check)",
"#form select:has(option:first-child:contains('o'))",
[ "select1", "select2", "select3", "select4" ]
);
assert.ok( jQuery( "#qunit-fixture :not(:has(:has(*)))" ).length, "All not grandparents" );
select = document.getElementById( "select1" );
assert.ok( jQuery( select ).is( ":has(option)" ), "Has Option Matches" );
} else {
assert.ok( "skip", ":has not supported in selector-native" );
assert.ok( "skip", ":has not supported in selector-native" );
assert.ok( "skip", ":has not supported in selector-native" );
}
tmp = document.createElement( "div" );
tmp.id = "tmp_input";
document.body.appendChild( tmp );