-
Notifications
You must be signed in to change notification settings - Fork 20.6k
/
manipulation.js
2436 lines (1772 loc) · 81.1 KB
/
manipulation.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
module( "manipulation", {
teardown: moduleTeardown
});
// Ensure that an extended Array prototype doesn't break jQuery
Array.prototype.arrayProtoFn = function() {
throw("arrayProtoFn should not be called");
};
function manipulationBareObj( value ) {
return value;
}
function manipulationFunctionReturningObj( value ) {
return function() {
return value;
};
}
/*
======== local reference =======
manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
See testVal below for an example
bareObj( value );
This function returns whatever value is passed in
functionReturningObj( value );
Returns a function that returns the value
*/
test( "text()", function() {
expect( 5 );
var expected, frag, $newLineTest;
expected = "This link has class=\"blog\": Simon Willison's Weblog";
equal( jQuery("#sap").text(), expected, "Check for merged text of more then one element." );
// Check serialization of text values
equal( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retrieved from .text()." );
notEqual( jQuery(document).text(), "", "Retrieving text for the document retrieves all text (#10724)." );
// Retrieve from document fragments #10864
frag = document.createDocumentFragment();
frag.appendChild( document.createTextNode("foo") );
equal( jQuery(frag).text(), "foo", "Document Fragment Text node was retrieved from .text()." );
$newLineTest = jQuery("<div>test<br/>testy</div>").appendTo("#moretests");
$newLineTest.find("br").replaceWith("\n");
equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
$newLineTest.remove();
});
test( "text(undefined)", function() {
expect( 1 );
equal( jQuery("#foo").text("<div").text(undefined)[ 0 ].innerHTML, "<div", ".text(undefined) is chainable (#5571)" );
});
function testText( valueObj ) {
expect( 7 );
var val, j, expected, $multipleElements, $parentDiv, $childDiv;
val = valueObj("<div><b>Hello</b> cruel world!</div>");
equal( jQuery("#foo").text(val)[ 0 ].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
// using contents will get comments regular, text, and comment nodes
j = jQuery("#nonnodes").contents();
j.text( valueObj("hi!") );
equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
equal( j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
// Update multiple elements #11809
expected = "New";
$multipleElements = jQuery( "<div>Hello</div>" ).add( "<div>World</div>" );
$multipleElements.text( expected );
equal( $multipleElements.eq(0).text(), expected, "text() updates multiple elements (#11809)" );
equal( $multipleElements.eq(1).text(), expected, "text() updates multiple elements (#11809)" );
// Prevent memory leaks #11809
$childDiv = jQuery( "<div/>" );
$childDiv.data("leak", true);
$parentDiv = jQuery( "<div/>" );
$parentDiv.append( $childDiv );
$parentDiv.text("Dry off");
equal( $childDiv.data("leak"), undefined, "Check for leaks (#11809)" );
}
test( "text(String)", function() {
testText( manipulationBareObj );
});
test( "text(Function)", function() {
testText( manipulationFunctionReturningObj );
});
test( "text(Function) with incoming value", function() {
expect( 2 );
var old = "This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").text(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return "foobar";
});
equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
});
function testAppendForObject( valueObj, isFragment ) {
var $base,
type = isFragment ? " (DocumentFragment)" : " (Element)",
text = "This link has class=\"blog\": Simon Willison's Weblog",
el = document.getElementById("sap").cloneNode( true ),
first = document.getElementById("first"),
yahoo = document.getElementById("yahoo");
if ( isFragment ) {
$base = document.createDocumentFragment();
jQuery( el ).contents().each(function() {
$base.appendChild( this );
});
$base = jQuery( $base );
} else {
$base = jQuery( el );
}
equal( $base.clone().append( valueObj(first.cloneNode(true)) ).text(),
text + "Try them out:",
"Check for appending of element" + type
);
equal( $base.clone().append( valueObj([ first.cloneNode(true), yahoo.cloneNode(true) ]) ).text(),
text + "Try them out:Yahoo",
"Check for appending of array of elements" + type
);
equal( $base.clone().append( valueObj(jQuery("#yahoo, #first").clone()) ).text(),
text + "YahooTry them out:",
"Check for appending of jQuery object" + type
);
equal( $base.clone().append( valueObj( 5 ) ).text(),
text + "5",
"Check for appending a number" + type
);
equal( $base.clone().append( valueObj([ jQuery("#first").clone(), jQuery("#yahoo, #google").clone() ]) ).text(),
text + "Try them out:GoogleYahoo",
"Check for appending of array of jQuery objects"
);
equal( $base.clone().append( valueObj(" text with spaces ") ).text(),
text + " text with spaces ",
"Check for appending text with spaces" + type
);
equal( $base.clone().append( valueObj([]) ).text(),
text,
"Check for appending an empty array" + type
);
equal( $base.clone().append( valueObj("") ).text(),
text,
"Check for appending an empty string" + type
);
equal( $base.clone().append( valueObj(document.getElementsByTagName("foo")) ).text(),
text,
"Check for appending an empty nodelist" + type
);
equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
$base.children().length + 3,
"Make sure that multiple arguments works." + type
);
equal( $base.clone().append( valueObj(document.getElementById("form").cloneNode(true)) ).children("form").length,
1,
"Check for appending a form (#910)" + type
);
}
function testAppend( valueObj ) {
expect( 78 );
testAppendForObject( valueObj, false );
testAppendForObject( valueObj, true );
var defaultText, result, message, iframe, iframeDoc, j, d,
$input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
defaultText = "Try them out:";
result = jQuery("#first").append( valueObj("<b>buga</b>") );
equal( result.text(), defaultText + "buga", "Check if text appending works" );
equal( jQuery("#select3").append( valueObj("<option value='appendTest'>Append Test</option>") ).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
jQuery("form").append( valueObj("<input name='radiotest' type='radio' checked='checked' />") );
jQuery("form input[name=radiotest]").each(function() {
ok( jQuery(this).is(":checked"), "Append checked radio" );
}).remove();
jQuery("form").append( valueObj("<input name='radiotest2' type='radio' checked = 'checked' />") );
jQuery("form input[name=radiotest2]").each(function() {
ok( jQuery(this).is(":checked"), "Append alternately formated checked radio" );
}).remove();
jQuery("form").append( valueObj("<input name='radiotest3' type='radio' checked />") );
jQuery("form input[name=radiotest3]").each(function() {
ok( jQuery(this).is(":checked"), "Append HTML5-formated checked radio" );
}).remove();
jQuery("form").append( valueObj("<input type='radio' checked='checked' name='radiotest4' />") );
jQuery("form input[name=radiotest4]").each(function() {
ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute" );
}).remove();
message = "Test for appending a DOM node to the contents of an iframe";
iframe = jQuery("#iframe")[ 0 ];
iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
try {
if ( iframeDoc && iframeDoc.body ) {
equal( jQuery(iframeDoc.body).append( valueObj("<div id='success'>test</div>") )[ 0 ].lastChild.id, "success", message );
} else {
ok( true, message + " - can't test" );
}
} catch( e ) {
strictEqual( e.message || e, undefined, message );
}
jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") );
t( "Append legend", "#legend", [ "legend" ] );
$map = jQuery("<map/>").append( valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>") );
equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") );
equal( jQuery("#select1 option:last-child").text(), "Test", "Appending OPTION (all caps)" );
jQuery("#select1").append( valueObj("<optgroup label='optgroup'><option>optgroup</option></optgroup>") );
equal( jQuery("#select1 optgroup").attr("label"), "optgroup", "Label attribute in newly inserted optgroup is correct" );
equal( jQuery("#select1 option").last().text(), "optgroup", "Appending optgroup" );
$table = jQuery("#table");
jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
$table.append( valueObj( "<" + name + "/>" ) );
equal( $table.find( name ).length, 1, "Append " + name );
ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
});
jQuery("#table colgroup").append( valueObj("<col/>") );
equal( jQuery("#table colgroup col").length, 1, "Append col" );
jQuery("#form")
.append( valueObj("<select id='appendSelect1'></select>") )
.append( valueObj("<select id='appendSelect2'><option>Test</option></select>") );
t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
equal( "Two nodes", jQuery("<div />").append( "Two", " nodes" ).text(), "Appending two text nodes (#4011)" );
equal( jQuery("<div />").append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
// using contents will get comments regular, text, and comment nodes
j = jQuery("#nonnodes").contents();
d = jQuery("<div/>").appendTo("#nonnodes").append( j );
equal( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
equal( d.contents().length, 3, "Check node,textnode,comment append works" );
d.contents().appendTo("#nonnodes");
d.remove();
equal( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment append cleanup worked" );
$input = jQuery("<input type='checkbox'/>").prop( "checked", true ).appendTo("#testForm");
equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
$radioChecked = jQuery("input[type='radio'][name='R1']").eq( 1 );
$radioParent = $radioChecked.parent();
$radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
$radioChecked.trigger("click");
$radioUnchecked[ 0 ].checked = false;
jQuery("<div/>").insertBefore($radioParent).append($radioParent);
equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
equal( jQuery("<div/>").append( valueObj("option<area/>") )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
}
test( "append(String|Element|Array<Element>|jQuery)", function() {
testAppend( manipulationBareObj );
});
test( "append(Function)", function() {
testAppend( manipulationFunctionReturningObj );
});
test( "append(param) to object, see #11280", function() {
expect( 5 );
var object = jQuery( document.createElement("object") ).appendTo( document.body );
equal( object.children().length, 0, "object does not start with children" );
object.append( jQuery("<param type='wmode' name='foo'>") );
equal( object.children().length, 1, "appended param" );
equal( object.children().eq(0).attr("name"), "foo", "param has name=foo" );
object = jQuery("<object><param type='baz' name='bar'></object>");
equal( object.children().length, 1, "object created with child param" );
equal( object.children().eq(0).attr("name"), "bar", "param has name=bar" );
});
test( "append(Function) returns String", function() {
expect( 4 );
var defaultText, result, select, old;
defaultText = "Try them out:";
old = jQuery("#first").html();
result = jQuery("#first").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return "<b>buga</b>";
});
equal( result.text(), defaultText + "buga", "Check if text appending works" );
select = jQuery("#select3");
old = select.html();
equal( select.append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return "<option value='appendTest'>Append Test</option>";
}).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
});
test( "append(Function) returns Element", function() {
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:",
old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return document.getElementById("first");
});
equal( jQuery("#sap").text(), expected, "Check for appending of element" );
});
test( "append(Function) returns Array<Element>", function() {
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo",
old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return [ document.getElementById("first"), document.getElementById("yahoo") ];
});
equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
});
test( "append(Function) returns jQuery", function() {
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:",
old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return jQuery("#yahoo, #first");
});
equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
});
test( "append(Function) returns Number", function() {
expect( 2 );
var old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return 5;
});
ok( jQuery("#sap")[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
});
test( "XML DOM manipulation (#9960)", function() {
expect( 5 );
var scxml1Adopted,
xmlDoc1 = jQuery.parseXML("<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state x='100' y='100' initial='actions' id='provisioning'></state><state x='100' y='100' id='error'></state><state x='100' y='100' id='finished' final='true'></state></scxml>"),
xmlDoc2 = jQuery.parseXML("<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>"),
xml1 = jQuery( xmlDoc1 ),
xml2 = jQuery( xmlDoc2 ),
scxml1 = jQuery( "scxml", xml1 ),
scxml2 = jQuery( "scxml", xml2 ),
state = scxml2.find("state");
// Android 2.3 doesn't automatically adopt nodes from foreign documents.
// Although technically this is compliant behavior, no other browser
// (including newer Android Browsers) behave in this way so do the adopting
// just for Android 2.3.
// Support: Android 2.3
if ( /android 2\.3/i.test( navigator.userAgent ) ) {
state = jQuery( xmlDoc1.adoptNode( state[ 0 ] ) );
}
scxml1.append( state );
strictEqual( scxml1[0].lastChild, state[0], "append" );
scxml1.prepend( state );
strictEqual( scxml1[0].firstChild, state[0], "prepend" );
scxml1.find("#finished").after( state );
strictEqual( scxml1[0].lastChild, state[0], "after" );
scxml1.find("#provisioning").before( state );
strictEqual( scxml1[0].firstChild, state[0], "before" );
// Support: Android 2.3
if ( /android 2\.3/i.test( navigator.userAgent ) ) {
scxml1Adopted = jQuery( xmlDoc2.adoptNode( scxml1[ 0 ] ) );
scxml2.replaceWith( scxml1Adopted );
} else {
scxml2.replaceWith( scxml1 );
}
deepEqual( jQuery( "state", xml2 ).get(), scxml1.find("state").get(), "replaceWith" );
});
test( "append HTML5 sectioning elements (Bug #6485)", function() {
expect( 2 );
var article, aside;
jQuery("#qunit-fixture").append("<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>");
article = jQuery("article");
aside = jQuery("aside");
equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
equal( aside.length, 1, "HTML5 elements do not collapse their children" );
});
if ( jQuery.css ) {
test( "HTML5 Elements inherit styles from style rules (Bug #10501)", function() {
expect( 1 );
jQuery("#qunit-fixture").append("<article id='article'></article>");
jQuery("#article").append("<section>This section should have a pink background.</section>");
// In IE, the missing background color will claim its value is "transparent"
notEqual( jQuery("section").css("background-color"), "transparent", "HTML5 elements inherit styles" );
});
}
test( "html(String) with HTML5 (Bug #6485)", function() {
expect( 2 );
jQuery("#qunit-fixture").html("<article><section><aside>HTML5 elements</aside></section></article>");
equal( jQuery("#qunit-fixture").children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
equal( jQuery("#qunit-fixture").children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
});
test( "IE8 serialization bug", function() {
expect( 2 );
var wrapper = jQuery("<div></div>");
wrapper.html("<div></div><article></article>");
equal( wrapper.children("article").length, 1, "HTML5 elements are insertable with .html()" );
wrapper.html("<div></div><link></link>");
equal( wrapper.children("link").length, 1, "Link elements are insertable with .html()" );
});
test( "html() object element #10324", function() {
expect( 1 );
var object = jQuery("<object id='object2'><param name='object2test' value='test'></param></object>?").appendTo("#qunit-fixture"),
clone = object.clone();
equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
});
test( "append(xml)", function() {
expect( 1 );
var xmlDoc, xml1, xml2;
function createXMLDoc() {
// Initialize DOM based upon latest installed MSXML or Netscape
var elem, n, len,
aActiveX =
[ "MSXML6.DomDocument",
"MSXML3.DomDocument",
"MSXML2.DomDocument",
"MSXML.DomDocument",
"Microsoft.XmlDom" ];
if ( document.implementation && "createDocument" in document.implementation ) {
return document.implementation.createDocument( "", "", null );
} else {
// IE
for ( n = 0, len = aActiveX.length; n < len; n++ ) {
try {
elem = new ActiveXObject( aActiveX[ n ] );
return elem;
} catch(_) {}
}
}
}
xmlDoc = createXMLDoc();
xml1 = xmlDoc.createElement("head");
xml2 = xmlDoc.createElement("test");
ok( jQuery(xml1).append(xml2), "Append an xml element to another without raising an exception." );
});
test( "appendTo(String)", function() {
expect( 4 );
var l, defaultText;
defaultText = "Try them out:";
jQuery("<b>buga</b>").appendTo("#first");
equal( jQuery("#first").text(), defaultText + "buga", "Check if text appending works" );
equal( jQuery("<option value='appendTest'>Append Test</option>").appendTo("#select3").parent().find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
l = jQuery("#first").children().length + 2;
jQuery("<strong>test</strong>");
jQuery("<strong>test</strong>");
jQuery([ jQuery("<strong>test</strong>")[ 0 ], jQuery("<strong>test</strong>")[ 0 ] ])
.appendTo("#first");
equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
equal( jQuery("#first").children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
});
test( "appendTo(Element|Array<Element>)", function() {
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
jQuery( document.getElementById("first") ).appendTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for appending of element" );
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
jQuery([ document.getElementById("first"), document.getElementById("yahoo") ]).appendTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
});
test( "appendTo(jQuery)", function() {
expect( 10 );
var expected, num, div;
ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
jQuery("#yahoo, #first").appendTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
jQuery("#select1").appendTo("#foo");
t( "Append select", "#foo select", [ "select1" ] );
div = jQuery("<div/>").on( "click", function() {
ok( true, "Running a cloned click." );
});
div.appendTo("#qunit-fixture, #moretests");
jQuery("#qunit-fixture div").last().trigger("click");
jQuery("#moretests div").last().trigger("click");
div = jQuery("<div/>").appendTo("#qunit-fixture, #moretests");
equal( div.length, 2, "appendTo returns the inserted elements" );
div.addClass("test");
ok( jQuery("#qunit-fixture div").last().hasClass("test"), "appendTo element was modified after the insertion" );
ok( jQuery("#moretests div").last().hasClass("test"), "appendTo element was modified after the insertion" );
div = jQuery("<div/>");
jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
equal( div.children().length, 1, "Make sure the right number of children were inserted." );
div = jQuery("#moretests div");
num = jQuery("#qunit-fixture div").length;
div.remove().appendTo("#qunit-fixture");
equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." );
});
test( "prepend(String)", function() {
expect( 2 );
var result, expected;
expected = "Try them out:";
result = jQuery("#first").prepend( "<b>buga</b>" );
equal( result.text(), "buga" + expected, "Check if text prepending works" );
equal( jQuery("#select3").prepend( "<option value='prependTest'>Prepend Test</option>" ).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
});
test( "prepend(Element)", function() {
expect( 1 );
var expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( document.getElementById("first") );
equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
test( "prepend(Array<Element>)", function() {
expect( 1 );
var expected;
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( [ document.getElementById("first"), document.getElementById("yahoo") ] );
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
test( "prepend(jQuery)", function() {
expect( 1 );
var expected;
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( jQuery("#yahoo, #first") );
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
});
test( "prepend(Array<jQuery>)", function() {
expect( 1 );
var expected;
expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( [ jQuery("#first"), jQuery("#yahoo, #google") ] );
equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" );
});
test( "prepend(Function) with incoming value -- String", function() {
expect( 4 );
var defaultText, old, result;
defaultText = "Try them out:";
old = jQuery("#first").html();
result = jQuery("#first").prepend(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return "<b>buga</b>";
});
equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
old = jQuery("#select3").html();
equal( jQuery("#select3").prepend(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return "<option value='prependTest'>Prepend Test</option>";
}).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
});
test( "prepend(Function) with incoming value -- Element", function() {
expect( 2 );
var old, expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html();
jQuery("#sap").prepend(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return document.getElementById("first");
});
equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
test( "prepend(Function) with incoming value -- Array<Element>", function() {
expect( 2 );
var old, expected;
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html();
jQuery("#sap").prepend(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return [ document.getElementById("first"), document.getElementById("yahoo") ];
});
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
test( "prepend(Function) with incoming value -- jQuery", function() {
expect( 2 );
var old, expected;
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html();
jQuery("#sap").prepend(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." );
return jQuery("#yahoo, #first");
});
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
});
test( "prependTo(String)", function() {
expect( 2 );
var defaultText;
defaultText = "Try them out:";
jQuery("<b>buga</b>").prependTo("#first");
equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" );
equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
});
test( "prependTo(Element)", function() {
expect( 1 );
var expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery( document.getElementById("first") ).prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
test( "prependTo(Array<Element>)", function() {
expect( 1 );
var expected;
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery( [ document.getElementById("first"), document.getElementById("yahoo") ] ).prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
test( "prependTo(jQuery)", function() {
expect( 1 );
var expected;
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#yahoo, #first").prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
});
test( "prependTo(Array<jQuery>)", function() {
expect( 1 );
jQuery("<select id='prependSelect1'></select>").prependTo("#form");
jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("#form");
t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
});
test( "before(String)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: bugaYahoo";
jQuery("#yahoo").before( manipulationBareObj("<b>buga</b>") );
equal( jQuery("#en").text(), expected, "Insert String before" );
});
test( "before(Element)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:Yahoo";
jQuery("#yahoo").before( manipulationBareObj(document.getElementById("first")) );
equal( jQuery("#en").text(), expected, "Insert element before" );
});
test( "before(Array<Element>)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery("#yahoo").before( manipulationBareObj([ document.getElementById("first"), document.getElementById("mark") ]) );
equal( jQuery("#en").text(), expected, "Insert array of elements before" );
});
test( "before(jQuery)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
jQuery("#yahoo").before( manipulationBareObj(jQuery("#mark, #first")) );
equal( jQuery("#en").text(), expected, "Insert jQuery before" );
});
test( "before(Array<jQuery>)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
jQuery("#yahoo").before( manipulationBareObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
});
test( "before(Function) -- Returns String", function() {
expect( 1 );
var expected;
expected = "This is a normal link: bugaYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj("<b>buga</b>") );
equal( jQuery("#en").text(), expected, "Insert String before" );
});
test( "before(Function) -- Returns Element", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:Yahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj(document.getElementById("first")) );
equal( jQuery("#en").text(), expected, "Insert element before" );
});
test( "before(Function) -- Returns Array<Element>", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj([ document.getElementById("first"), document.getElementById("mark") ]) );
equal( jQuery("#en").text(), expected, "Insert array of elements before" );
});
test( "before(Function) -- Returns jQuery", function() {
expect( 1 );
var expected;
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj(jQuery("#mark, #first")) );
equal( jQuery("#en").text(), expected, "Insert jQuery before" );
});
test( "before(Function) -- Returns Array<jQuery>", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
});
test( "before(no-op)", function() {
expect( 2 );
var set;
set = jQuery("<div/>").before("<span>test</span>");
equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
});
test( "before and after w/ empty object (#10812)", function() {
expect( 1 );
var res;
res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
equal( res.length, 0, "didn't choke on empty object" );
});
test( ".before() and .after() disconnected node", function() {
expect(2);
equal( jQuery("<input type='checkbox'/>").before("<div/>").length, 1, "before() on disconnected node is no-op" );
equal( jQuery("<input type='checkbox'/>").after("<div/>").length, 1, "after() on disconnected node is no-op" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedBefore = "This is a normal link: bugaYahoo";
jQuery("#yahoo").add("<span/>").before("<b>buga</b>");
equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node last" );
});
test( "insert with .before() on disconnected node first", function() {
expect(1);
var expectedBefore = "This is a normal link: bugaYahoo";
jQuery("<span/>").add("#yahoo").before("<b>buga</b>");
equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node first" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedAfter = "This is a normal link: Yahoobuga";
jQuery("#yahoo").add("<span/>").after("<b>buga</b>");
equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node last" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedAfter = "This is a normal link: Yahoobuga";
jQuery("<span/>").add("#yahoo").after("<b>buga</b>");
equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node first" );
});
test( "insertBefore(String)", function() {
expect( 1 );
var expected = "This is a normal link: bugaYahoo";
jQuery("<b>buga</b>").insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert String before" );
});
test( "insertBefore(Element)", function() {
expect( 1 );
var expected = "This is a normal link: Try them out:Yahoo";
jQuery( document.getElementById("first") ).insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert element before" );
});
test( "insertBefore(Array<Element>)", function() {
expect( 1 );
var expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert array of elements before" );