@@ -288,7 +288,68 @@ test("append(String|Element|Array<Element>|jQuery)", function() {
288
288
289
289
test ( "append(Function)" , function ( ) {
290
290
testAppend ( functionReturningObj ) ;
291
- } )
291
+ } ) ;
292
+
293
+ test ( "append(Function) with incoming value" , function ( ) {
294
+ expect ( 12 ) ;
295
+
296
+ var defaultText = 'Try them out:' , old = jQuery ( "#first" ) . html ( ) ;
297
+
298
+ var result = jQuery ( '#first' ) . append ( function ( i , val ) {
299
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
300
+ return '<b>buga</b>' ;
301
+ } ) ;
302
+ equals ( result . text ( ) , defaultText + 'buga' , 'Check if text appending works' ) ;
303
+
304
+ var select = jQuery ( '#select3' ) ;
305
+ old = select . html ( ) ;
306
+
307
+ equals ( select . append ( function ( i , val ) {
308
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
309
+ return '<option value="appendTest">Append Test</option>' ;
310
+ } ) . find ( 'option:last-child' ) . attr ( 'value' ) , 'appendTest' , 'Appending html options to select element' ) ;
311
+
312
+ reset ( ) ;
313
+ var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:" ;
314
+ old = jQuery ( "#sap" ) . html ( ) ;
315
+
316
+ jQuery ( '#sap' ) . append ( function ( i , val ) {
317
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
318
+ return document . getElementById ( 'first' ) ;
319
+ } ) ;
320
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for appending of element" ) ;
321
+
322
+ reset ( ) ;
323
+ expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo" ;
324
+ old = jQuery ( "#sap" ) . html ( ) ;
325
+
326
+ jQuery ( '#sap' ) . append ( function ( i , val ) {
327
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
328
+ return [ document . getElementById ( 'first' ) , document . getElementById ( 'yahoo' ) ] ;
329
+ } ) ;
330
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for appending of array of elements" ) ;
331
+
332
+ reset ( ) ;
333
+ expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:" ;
334
+ old = jQuery ( "#sap" ) . html ( ) ;
335
+
336
+ jQuery ( '#sap' ) . append ( function ( i , val ) {
337
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
338
+ return jQuery ( "#first, #yahoo" ) ;
339
+ } ) ;
340
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for appending of jQuery object" ) ;
341
+
342
+ reset ( ) ;
343
+ old = jQuery ( "#sap" ) . html ( ) ;
344
+
345
+ jQuery ( "#sap" ) . append ( function ( i , val ) {
346
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
347
+ return 5 ;
348
+ } ) ;
349
+ ok ( jQuery ( "#sap" ) [ 0 ] . innerHTML . match ( / 5 $ / ) , "Check for appending a number" ) ;
350
+
351
+ reset ( ) ;
352
+ } ) ;
292
353
293
354
test ( "appendTo(String|Element|Array<Element>|jQuery)" , function ( ) {
294
355
expect ( 12 ) ;
@@ -362,15 +423,66 @@ var testPrepend = function(val) {
362
423
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog" ;
363
424
jQuery ( '#sap' ) . prepend ( val ( jQuery ( "#first, #yahoo" ) ) ) ;
364
425
equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for prepending of jQuery object" ) ;
365
- }
426
+ } ;
366
427
367
428
test ( "prepend(String|Element|Array<Element>|jQuery)" , function ( ) {
368
429
testPrepend ( bareObj ) ;
369
430
} ) ;
370
431
371
432
test ( "prepend(Function)" , function ( ) {
372
433
testPrepend ( functionReturningObj ) ;
373
- } )
434
+ } ) ;
435
+
436
+ test ( "prepend(Function) with incoming value" , function ( ) {
437
+ expect ( 10 ) ;
438
+
439
+ var defaultText = 'Try them out:' , old = jQuery ( '#first' ) . html ( ) ;
440
+ var result = jQuery ( '#first' ) . prepend ( function ( i , val ) {
441
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
442
+ return '<b>buga</b>' ;
443
+ } ) ;
444
+ equals ( result . text ( ) , 'buga' + defaultText , 'Check if text prepending works' ) ;
445
+
446
+ old = jQuery ( "#select3" ) . html ( ) ;
447
+
448
+ equals ( jQuery ( '#select3' ) . prepend ( function ( i , val ) {
449
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
450
+ return '<option value="prependTest">Prepend Test</option>' ;
451
+ } ) . find ( 'option:first-child' ) . attr ( 'value' ) , 'prependTest' , 'Prepending html options to select element' ) ;
452
+
453
+ reset ( ) ;
454
+ var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog" ;
455
+ old = jQuery ( '#sap' ) . html ( ) ;
456
+
457
+ jQuery ( '#sap' ) . prepend ( function ( i , val ) {
458
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
459
+ return document . getElementById ( 'first' ) ;
460
+ } ) ;
461
+
462
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for prepending of element" ) ;
463
+
464
+ reset ( ) ;
465
+ expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog" ;
466
+ old = jQuery ( '#sap' ) . html ( ) ;
467
+
468
+ jQuery ( '#sap' ) . prepend ( function ( i , val ) {
469
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
470
+ return [ document . getElementById ( 'first' ) , document . getElementById ( 'yahoo' ) ] ;
471
+ } ) ;
472
+
473
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for prepending of array of elements" ) ;
474
+
475
+ reset ( ) ;
476
+ expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog" ;
477
+ old = jQuery ( '#sap' ) . html ( ) ;
478
+
479
+ jQuery ( '#sap' ) . prepend ( function ( i , val ) {
480
+ equals ( val , old , "Make sure the incoming value is correct." ) ;
481
+ return jQuery ( "#first, #yahoo" ) ;
482
+ } ) ;
483
+
484
+ equals ( expected , jQuery ( '#sap' ) . text ( ) , "Check for prepending of jQuery object" ) ;
485
+ } ) ;
374
486
375
487
test ( "prependTo(String|Element|Array<Element>|jQuery)" , function ( ) {
376
488
expect ( 6 ) ;
@@ -714,6 +826,66 @@ test("html(Function)", function() {
714
826
testHtml ( functionReturningObj ) ;
715
827
} ) ;
716
828
829
+ test ( "html(Function) with incoming value" , function ( ) {
830
+ expect ( 20 ) ;
831
+
832
+ var div = jQuery ( "#main > div" ) , old = div . map ( function ( ) { return jQuery ( this ) . html ( ) } ) ;
833
+
834
+ div . html ( function ( i , val ) {
835
+ equals ( val , old [ i ] , "Make sure the incoming value is correct." ) ;
836
+ return "<b>test</b>" ;
837
+ } ) ;
838
+
839
+ var pass = true ;
840
+ div . each ( function ( ) {
841
+ if ( this . childNodes . length !== 1 ) {
842
+ pass = false ;
843
+ }
844
+ } )
845
+ ok ( pass , "Set HTML" ) ;
846
+
847
+ reset ( ) ;
848
+ // using contents will get comments regular, text, and comment nodes
849
+ var j = jQuery ( "#nonnodes" ) . contents ( ) ;
850
+ old = j . map ( function ( ) { return jQuery ( this ) . html ( ) ; } ) ;
851
+
852
+ j . html ( function ( i , val ) {
853
+ equals ( val , old [ i ] , "Make sure the incoming value is correct." ) ;
854
+ return "<b>bold</b>" ;
855
+ } ) ;
856
+
857
+ j . find ( 'b' ) . removeData ( ) ;
858
+ equals ( j . html ( ) . replace ( / x m l n s = " [ ^ " ] + " / g, "" ) . toLowerCase ( ) , "<b>bold</b>" , "Check node,textnode,comment with html()" ) ;
859
+
860
+ var $div = jQuery ( '<div />' ) ;
861
+
862
+ equals ( $div . html ( function ( i , val ) {
863
+ equals ( val , "" , "Make sure the incoming value is correct." ) ;
864
+ return 5 ;
865
+ } ) . html ( ) , '5' , 'Setting a number as html' ) ;
866
+
867
+ equals ( $div . html ( function ( i , val ) {
868
+ equals ( val , "5" , "Make sure the incoming value is correct." ) ;
869
+ return 0 ;
870
+ } ) . html ( ) , '0' , 'Setting a zero as html' ) ;
871
+
872
+ var $div2 = jQuery ( '<div/>' ) , insert = "<div>hello1</div>" ;
873
+ equals ( $div2 . html ( function ( i , val ) {
874
+ equals ( val , "" , "Make sure the incoming value is correct." ) ;
875
+ return insert ;
876
+ } ) . html ( ) , insert , "Verify escaped insertion." ) ;
877
+
878
+ equals ( $div2 . html ( function ( i , val ) {
879
+ equals ( val , insert , "Make sure the incoming value is correct." ) ;
880
+ return "x" + insert ;
881
+ } ) . html ( ) , "x" + insert , "Verify escaped insertion." ) ;
882
+
883
+ equals ( $div2 . html ( function ( i , val ) {
884
+ equals ( val , "x" + insert , "Make sure the incoming value is correct." ) ;
885
+ return " " + insert ;
886
+ } ) . html ( ) , " " + insert , "Verify escaped insertion." ) ;
887
+ } ) ;
888
+
717
889
var testRemove = function ( method ) {
718
890
expect ( 9 ) ;
719
891
0 commit comments