Core:Manipulation: Add basic TrustedHTML support
[jquery.git] / test / unit / manipulation.js
blob30bf169ac46377a62dbd33195eff541897ee9873
1 QUnit.module( "manipulation", {
2         afterEach: moduleTeardown
3 } );
5 // Ensure that an extended Array prototype doesn't break jQuery
6 Array.prototype.arrayProtoFn = function() {
7 };
9 function manipulationBareObj( value ) {
10         return value;
13 function manipulationFunctionReturningObj( value ) {
14         return function() {
15                 return value;
16         };
20         ======== local reference =======
21         manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
22         See testVal below for an example
24         bareObj( value );
25                 This function returns whatever value is passed in
27         functionReturningObj( value );
28                 Returns a function that returns the value
31 QUnit.test( "text()", function( assert ) {
33         assert.expect( 5 );
35         var expected, frag, $newLineTest;
37         expected = "This link has class=\"blog\": Simon Willison's Weblog";
38         assert.equal( jQuery( "#sap" ).text(), expected, "Check for merged text of more then one element." );
40         // Check serialization of text values
41         assert.equal( jQuery( document.createTextNode( "foo" ) ).text(), "foo", "Text node was retrieved from .text()." );
42         assert.notEqual( jQuery( document ).text(), "", "Retrieving text for the document retrieves all text (#10724)." );
44         // Retrieve from document fragments #10864
45         frag = document.createDocumentFragment();
46         frag.appendChild( document.createTextNode( "foo" ) );
48         assert.equal( jQuery( frag ).text(), "foo", "Document Fragment Text node was retrieved from .text()." );
50         $newLineTest = jQuery( "<div>test<br/>testy</div>" ).appendTo( "#moretests" );
51         $newLineTest.find( "br" ).replaceWith( "\n" );
52         assert.equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
54         $newLineTest.remove();
55 } );
57 QUnit.test( "text(undefined)", function( assert ) {
59         assert.expect( 1 );
61         assert.equal( jQuery( "#foo" ).text( "<div" ).text( undefined )[ 0 ].innerHTML, "&lt;div", ".text(undefined) is chainable (#5571)" );
62 } );
64 function testText( valueObj, assert ) {
66         assert.expect( 6 );
68         var val, j, expected, $multipleElements, $parentDiv, $childDiv;
70         val = valueObj( "<div><b>Hello</b> cruel world!</div>" );
71         assert.equal( jQuery( "#foo" ).text( val )[ 0 ].innerHTML.replace( />/g, "&gt;" ), "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
73         // using contents will get comments regular, text, and comment nodes
74         j = jQuery( "#nonnodes" ).contents();
75         j.text( valueObj( "hi!" ) );
76         assert.equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
77         assert.equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
79         assert.equal( j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
81         // Update multiple elements #11809
82         expected = "New";
84         $multipleElements = jQuery( "<div>Hello</div>" ).add( "<div>World</div>" );
85         $multipleElements.text( expected );
87         assert.equal( $multipleElements.eq( 0 ).text(), expected, "text() updates multiple elements (#11809)" );
88         assert.equal( $multipleElements.eq( 1 ).text(), expected, "text() updates multiple elements (#11809)" );
90         // Prevent memory leaks #11809
91         $childDiv = jQuery( "<div></div>" );
92         $childDiv.data( "leak", true );
93         $parentDiv = jQuery( "<div></div>" );
94         $parentDiv.append( $childDiv );
95         $parentDiv.text( "Dry off" );
98 QUnit.test( "text(String)", function( assert ) {
99         testText( manipulationBareObj, assert );
100 } );
102 QUnit.test( "text(Function)", function( assert ) {
103         testText( manipulationFunctionReturningObj, assert );
104 } );
106 QUnit.test( "text(Function) with incoming value", function( assert ) {
108         assert.expect( 2 );
110         var old = "This link has class=\"blog\": Simon Willison's Weblog";
112         jQuery( "#sap" ).text( function( i, val ) {
113                 assert.equal( val, old, "Make sure the incoming value is correct." );
114                 return "foobar";
115         } );
117         assert.equal( jQuery( "#sap" ).text(), "foobar", "Check for merged text of more then one element." );
118 } );
120 function testAppendForObject( valueObj, isFragment, assert ) {
121         var $base,
122                 type = isFragment ? " (DocumentFragment)" : " (Element)",
123                 text = "This link has class=\"blog\": Simon Willison's Weblog",
124                 el = document.getElementById( "sap" ).cloneNode( true ),
125                 first = document.getElementById( "first" ),
126                 yahoo = document.getElementById( "yahoo" );
128         if ( isFragment ) {
129                 $base = document.createDocumentFragment();
130                 jQuery( el ).contents().each( function() {
131                         $base.appendChild( this );
132                 } );
133                 $base = jQuery( $base );
134         } else {
135                 $base = jQuery( el );
136         }
138         assert.equal( $base.clone().append( valueObj( first.cloneNode( true ) ) ).text(),
139                 text + "Try them out:",
140                 "Check for appending of element" + type
141         );
143         assert.equal( $base.clone().append( valueObj( [ first.cloneNode( true ), yahoo.cloneNode( true ) ] ) ).text(),
144                 text + "Try them out:Yahoo",
145                 "Check for appending of array of elements" + type
146         );
148         assert.equal( $base.clone().append( valueObj( jQuery( "#yahoo, #first" ).clone() ) ).text(),
149                 text + "YahooTry them out:",
150                 "Check for appending of jQuery object" + type
151         );
153         assert.equal( $base.clone().append( valueObj( 5 ) ).text(),
154                 text + "5",
155                 "Check for appending a number" + type
156         );
158         assert.equal( $base.clone().append( valueObj( [ jQuery( "#first" ).clone(), jQuery( "#yahoo, #google" ).clone() ] ) ).text(),
159                 text + "Try them out:GoogleYahoo",
160                 "Check for appending of array of jQuery objects"
161         );
163         assert.equal( $base.clone().append( valueObj( " text with spaces " ) ).text(),
164                 text + " text with spaces ",
165                 "Check for appending text with spaces" + type
166         );
168         assert.equal( $base.clone().append( valueObj( [] ) ).text(),
169                 text,
170                 "Check for appending an empty array" + type
171         );
173         assert.equal( $base.clone().append( valueObj( "" ) ).text(),
174                 text,
175                 "Check for appending an empty string" + type
176         );
178         assert.equal( $base.clone().append( valueObj( document.getElementsByTagName( "foo" ) ) ).text(),
179                 text,
180                 "Check for appending an empty nodelist" + type
181         );
183         assert.equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
184                 $base.children().length + 3,
185                 "Make sure that multiple arguments works." + type
186         );
188         assert.equal( $base.clone().append( valueObj( document.getElementById( "form" ).cloneNode( true ) ) ).children( "form" ).length,
189                 1,
190                 "Check for appending a form (#910)" + type
191         );
194 function testAppend( valueObj, assert ) {
196         assert.expect( 82 );
198         testAppendForObject( valueObj, false, assert );
199         testAppendForObject( valueObj, true, assert );
201         var defaultText, result, message, iframe, iframeDoc, j, d,
202                 $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
204         defaultText = "Try them out:";
205         result = jQuery( "#first" ).append( valueObj( "<b>buga</b>" ) );
207         assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
208         assert.equal( jQuery( "#select3" ).append( valueObj( "<option value='appendTest'>Append Test</option>" ) ).find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
210         jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest' type='radio' checked='checked' />" ) );
211         jQuery( "#qunit-fixture form input[name=radiotest]" ).each( function() {
212                 assert.ok( jQuery( this ).is( ":checked" ), "Append checked radio" );
213         } ).remove();
215         jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest2' type='radio' checked    =   'checked' />" ) );
216         jQuery( "#qunit-fixture form input[name=radiotest2]" ).each( function() {
217                 assert.ok( jQuery( this ).is( ":checked" ), "Append alternately formatted checked radio" );
218         } ).remove();
220         jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest3' type='radio' checked />" ) );
221         jQuery( "#qunit-fixture form input[name=radiotest3]" ).each( function() {
222                 assert.ok( jQuery( this ).is( ":checked" ), "Append HTML5-formatted checked radio" );
223         } ).remove();
225         jQuery( "#qunit-fixture form" ).append( valueObj( "<input type='radio' checked='checked' name='radiotest4' />" ) );
226         jQuery( "#qunit-fixture form input[name=radiotest4]" ).each( function() {
227                 assert.ok( jQuery( this ).is( ":checked" ), "Append with name attribute after checked attribute" );
228         } ).remove();
230         message = "Test for appending a DOM node to the contents of an iframe";
231         iframe = jQuery( "#iframe" )[ 0 ];
232         iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
234         try {
235                 if ( iframeDoc && iframeDoc.body ) {
236                         assert.equal( jQuery( iframeDoc.body ).append( valueObj( "<div id='success'>test</div>" ) )[ 0 ].lastChild.id, "success", message );
237                 } else {
238                         assert.ok( true, message + " - can't test" );
239                 }
240         } catch ( e ) {
241                 assert.strictEqual( e.message || e, undefined, message );
242         }
244         jQuery( "<fieldset></fieldset>" ).appendTo( "#form" ).append( valueObj( "<legend id='legend'>test</legend>" ) );
245         assert.t( "Append legend", "#legend", [ "legend" ] );
247         $map = jQuery( "<map></map>" ).append( valueObj( "<area id='map01' shape='rect' coords='50,50,150,150' href='https://www.jquery.com/' alt='jQuery'>" ) );
249         assert.equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
250         assert.equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
252         jQuery( "#select1" ).append( valueObj( "<OPTION>Test</OPTION>" ) );
253         assert.equal( jQuery( "#select1 option:last-child" ).text(), "Test", "Appending OPTION (all caps)" );
255         jQuery( "#select1" ).append( valueObj( "<optgroup label='optgroup'><option>optgroup</option></optgroup>" ) );
256         assert.equal( jQuery( "#select1 optgroup" ).attr( "label" ), "optgroup", "Label attribute in newly inserted optgroup is correct" );
257         assert.equal( jQuery( "#select1 option" ).last().text(), "optgroup", "Appending optgroup" );
259         $table = jQuery( "#table" );
261         jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
262                 $table.append( valueObj( "<" + name + "/>" ) );
263                 assert.equal( $table.find( name ).length, 1, "Append " + name );
264                 assert.ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
265         } );
267         jQuery( "#table colgroup" ).append( valueObj( "<col></col>" ) );
268         assert.equal( jQuery( "#table colgroup col" ).length, 1, "Append col" );
270         jQuery( "#form" )
271                 .append( valueObj( "<select id='appendSelect1'></select>" ) )
272                 .append( valueObj( "<select id='appendSelect2'><option>Test</option></select>" ) );
273         assert.t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
275         assert.equal( "Two nodes", jQuery( "<div></div>" ).append( "Two", " nodes" ).text(), "Appending two text nodes (#4011)" );
276         assert.equal( jQuery( "<div></div>" ).append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
278         // using contents will get comments regular, text, and comment nodes
279         j = jQuery( "#nonnodes" ).contents();
280         d = jQuery( "<div></div>" ).appendTo( "#nonnodes" ).append( j );
282         assert.equal( jQuery( "#nonnodes" ).length, 1, "Check node,textnode,comment append moved leaving just the div" );
283         assert.equal( d.contents().length, 3, "Check node,textnode,comment append works" );
284         d.contents().appendTo( "#nonnodes" );
285         d.remove();
286         assert.equal( jQuery( "#nonnodes" ).contents().length, 3, "Check node,textnode,comment append cleanup worked" );
288         $input = jQuery( "<input type='checkbox'/>" ).prop( "checked", true ).appendTo( "#testForm" );
289         assert.equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
291         $radioChecked = jQuery( "input[type='radio'][name='R1']" ).eq( 1 );
292         $radioParent = $radioChecked.parent();
293         $radioUnchecked = jQuery( "<input type='radio' name='R1' checked='checked'/>" ).appendTo( $radioParent );
294         $radioChecked.trigger( "click" );
295         $radioUnchecked[ 0 ].checked = false;
297         jQuery( "<div></div>" ).insertBefore( $radioParent ).append( $radioParent );
299         assert.equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
300         assert.equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
302         assert.equal( jQuery( "<div></div>" ).append( valueObj( "option<area></area>" ) )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
305 QUnit.test( "append(String|Element|Array<Element>|jQuery)", function( assert ) {
306         testAppend( manipulationBareObj, assert );
307 } );
309 QUnit.test( "append(Function)", function( assert ) {
310         testAppend( manipulationFunctionReturningObj, assert );
311 } );
313 QUnit.test( "append(param) to object, see #11280", function( assert ) {
315         assert.expect( 5 );
317         var object = jQuery( document.createElement( "object" ) ).appendTo( document.body );
319         assert.equal( object.children().length, 0, "object does not start with children" );
321         object.append( jQuery( "<param type='wmode' name='foo'>" ) );
322         assert.equal( object.children().length, 1, "appended param" );
323         assert.equal( object.children().eq( 0 ).attr( "name" ), "foo", "param has name=foo" );
325         object = jQuery( "<object><param type='baz' name='bar'></object>" );
326         assert.equal( object.children().length, 1, "object created with child param" );
327         assert.equal( object.children().eq( 0 ).attr( "name" ), "bar", "param has name=bar" );
328 } );
330 QUnit.test( "append(Function) returns String", function( assert ) {
332         assert.expect( 4 );
334         var defaultText, result, select, old;
336         defaultText = "Try them out:";
337         old = jQuery( "#first" ).html();
339         result = jQuery( "#first" ).append( function( i, val ) {
340                 assert.equal( val, old, "Make sure the incoming value is correct." );
341                 return "<b>buga</b>";
342         } );
343         assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
345         select = jQuery( "#select3" );
346         old = select.html();
348         assert.equal( select.append( function( i, val ) {
349                 assert.equal( val, old, "Make sure the incoming value is correct." );
350                 return "<option value='appendTest'>Append Test</option>";
351         } ).find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
352 } );
354 QUnit.test( "append(Function) returns Element", function( assert ) {
356         assert.expect( 2 );
357         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:",
358                 old = jQuery( "#sap" ).html();
360         jQuery( "#sap" ).append( function( i, val ) {
361                 assert.equal( val, old, "Make sure the incoming value is correct." );
362                 return document.getElementById( "first" );
363         } );
364         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
365 } );
367 QUnit.test( "append(Function) returns Array<Element>", function( assert ) {
369         assert.expect( 2 );
370         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo",
371                 old = jQuery( "#sap" ).html();
373         jQuery( "#sap" ).append( function( i, val ) {
374                 assert.equal( val, old, "Make sure the incoming value is correct." );
375                 return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
376         } );
377         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
378 } );
380 QUnit.test( "append(Function) returns jQuery", function( assert ) {
382         assert.expect( 2 );
383         var expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:",
384                 old = jQuery( "#sap" ).html();
386         jQuery( "#sap" ).append( function( i, val ) {
387                 assert.equal( val, old, "Make sure the incoming value is correct." );
388                 return jQuery( "#yahoo, #first" );
389         } );
390         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
391 } );
393 QUnit.test( "append(Function) returns Number", function( assert ) {
395         assert.expect( 2 );
396         var old = jQuery( "#sap" ).html();
398         jQuery( "#sap" ).append( function( i, val ) {
399                 assert.equal( val, old, "Make sure the incoming value is correct." );
400                 return 5;
401         } );
402         assert.ok( jQuery( "#sap" )[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
403 } );
405 QUnit.test( "XML DOM manipulation (#9960)", function( assert ) {
407         assert.expect( 5 );
409         var 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>" ),
410                 xmlDoc2 = jQuery.parseXML( "<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>" ),
411                 xml1 = jQuery( xmlDoc1 ),
412                 xml2 = jQuery( xmlDoc2 ),
413                 scxml1 = jQuery( "scxml", xml1 ),
414                 scxml2 = jQuery( "scxml", xml2 ),
415                 state = scxml2.find( "state" );
417         scxml1.append( state );
418         assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "append" );
420         scxml1.prepend( state );
421         assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "prepend" );
423         scxml1.find( "#finished" ).after( state );
424         assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "after" );
426         scxml1.find( "#provisioning" ).before( state );
427         assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "before" );
429         scxml2.replaceWith( scxml1 );
430         assert.deepEqual( jQuery( "state", xml2 ).get(), scxml1.find( "state" ).get(), "replaceWith" );
431 } );
433 QUnit.test( "append HTML5 sectioning elements (Bug #6485)", function( assert ) {
435         assert.expect( 2 );
437         var article, aside;
439         jQuery( "#qunit-fixture" ).append( "<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>" );
441         article = jQuery( "article" );
442         aside = jQuery( "aside" );
444         assert.equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
445         assert.equal( aside.length, 1, "HTML5 elements do not collapse their children" );
446 } );
448 QUnit[ jQuery.fn.css ? "test" : "skip" ]( "HTML5 Elements inherit styles from style rules (Bug #10501)", function( assert ) {
450         assert.expect( 1 );
452         jQuery( "#qunit-fixture" ).append( "<article id='article'></article>" );
453         jQuery( "#article" ).append( "<section>This section should have a pink background.</section>" );
455         // In IE, the missing background color will claim its value is "transparent"
456         assert.notEqual( jQuery( "section" ).css( "background-color" ), "transparent", "HTML5 elements inherit styles" );
457 } );
459 QUnit.test( "html(String) with HTML5 (Bug #6485)", function( assert ) {
461         assert.expect( 2 );
463         jQuery( "#qunit-fixture" ).html( "<article><section><aside>HTML5 elements</aside></section></article>" );
464         assert.equal( jQuery( "#qunit-fixture" ).children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
465         assert.equal( jQuery( "#qunit-fixture" ).children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
466 } );
468 QUnit.test( "html(String) tag-hyphenated elements (Bug #1987)", function( assert ) {
470         assert.expect( 27 );
472         jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
473                 var j = jQuery( "<" + name + "-d></" + name + "-d><" + name + "-d></" + name + "-d>" );
474                 assert.ok( j[ 0 ], "Create a tag-hyphenated element" );
475                 assert.ok( j[ 0 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
476                 assert.ok( j[ 1 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
477         } );
479         var j = jQuery( "<tr-multiple-hyphens><td-with-hyphen>text</td-with-hyphen></tr-multiple-hyphens>" );
480         assert.ok( j[ 0 ].nodeName === "TR-MULTIPLE-HYPHENS", "Tags with multiple hyphens" );
481         assert.ok( j.children()[ 0 ].nodeName === "TD-WITH-HYPHEN", "Tags with multiple hyphens" );
482         assert.equal( j.children().text(), "text", "Tags with multiple hyphens behave normally" );
483 } );
485 QUnit.test( "Tag name processing respects the HTML Standard (gh-2005)", function( assert ) {
487         assert.expect( 240 );
489         var wrapper = jQuery( "<div></div>" ),
490                 nameTerminatingChars = "\x20\t\r\n\f".split( "" ),
491                 specialChars = "[ ] { } _ - = + \\ ( ) * & ^ % $ # @ ! ~ ` ' ; ? Â¥ Â« Âµ Î» âŠ• â‰ˆ Î¾ â„œ â™£ â‚¬"
492                         .split( " " );
494         specialChars.push( specialChars.join( "" ) );
496         jQuery.each( specialChars, function( i, characters ) {
497                 assertSpecialCharsSupport( "html", characters );
498                 assertSpecialCharsSupport( "append", characters );
499         } );
501         jQuery.each( nameTerminatingChars, function( i, character ) {
502                 assertNameTerminatingCharsHandling( "html", character );
503                 assertNameTerminatingCharsHandling( "append", character );
504         } );
506         function buildChild( method, html ) {
507                 wrapper[ method ]( html );
508                 return wrapper.children()[ 0 ];
509         }
511         function assertSpecialCharsSupport( method, characters ) {
512                 var child,
513                         codepoint = characters.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
514                         description = characters.length === 1 ?
515                                 "U+" + ( "000" + codepoint ).slice( -4 ) + " " + characters :
516                                 "all special characters",
517                         nodeName = "valid" + characters + "tagname";
519                 child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
520                 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
521                         method + "(): Paired tag name includes " + description );
523                 child = buildChild( method, "<" + nodeName + ">" );
524                 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
525                         method + "(): Unpaired tag name includes " + description );
527                 child = buildChild( method, "<" + nodeName + "/>" );
528                 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
529                         method + "(): Self-closing tag name includes " + description );
530         }
532         function assertNameTerminatingCharsHandling( method, character ) {
533                 var child,
534                         codepoint = character.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
535                         description = "U+" + ( "000" + codepoint ).slice( -4 ) + " " + character,
536                         nodeName = "div" + character + "this-will-be-discarded";
538                 child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
539                 assert.equal( child.nodeName.toUpperCase(), "DIV",
540                         method + "(): Paired tag name terminated by " + description );
542                 child = buildChild( method, "<" + nodeName + ">" );
543                 assert.equal( child.nodeName.toUpperCase(), "DIV",
544                         method + "(): Unpaired open tag name terminated by " + description );
546                 child = buildChild( method, "<" + nodeName + "/>" );
547                 assert.equal( child.nodeName.toUpperCase(), "DIV",
548                         method + "(): Self-closing tag name terminated by " + description );
549         }
550 } );
552 QUnit.test( "IE8 serialization bug", function( assert ) {
554         assert.expect( 2 );
555         var wrapper = jQuery( "<div></div>" );
557         wrapper.html( "<div></div><article></article>" );
558         assert.equal( wrapper.children( "article" ).length, 1, "HTML5 elements are insertable with .html()" );
560         wrapper.html( "<div></div><link></link>" );
561         assert.equal( wrapper.children( "link" ).length, 1, "Link elements are insertable with .html()" );
562 } );
564 QUnit.test( "html() object element #10324", function( assert ) {
566         assert.expect( 1 );
568         var object = jQuery( "<object id='object2'><param name='object2test' value='test'></param></object>?" ).appendTo( "#qunit-fixture" ),
569                 clone = object.clone();
571         assert.equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
572 } );
574 QUnit.test( "append(xml)", function( assert ) {
576         assert.expect( 1 );
578         var xmlDoc, xml1, xml2;
580         function createXMLDoc() {
581                 return document.implementation.createDocument( "", "", null );
582         }
584         xmlDoc = createXMLDoc();
585         xml1 = xmlDoc.createElement( "head" );
586         xml2 = xmlDoc.createElement( "test" );
588         assert.ok( jQuery( xml1 ).append( xml2 ), "Append an xml element to another without raising an exception." );
590 } );
592 QUnit.test( "appendTo(String)", function( assert ) {
594         assert.expect( 4 );
596         var l, defaultText;
598         defaultText = "Try them out:";
599         jQuery( "<b>buga</b>" ).appendTo( "#first" );
600         assert.equal( jQuery( "#first" ).text(), defaultText + "buga", "Check if text appending works" );
601         assert.equal( jQuery( "<option value='appendTest'>Append Test</option>" ).appendTo( "#select3" ).parent().find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
603         l = jQuery( "#first" ).children().length + 2;
604         jQuery( "<strong>test</strong>" );
605         jQuery( "<strong>test</strong>" );
606         jQuery( [ jQuery( "<strong>test</strong>" )[ 0 ], jQuery( "<strong>test</strong>" )[ 0 ] ] )
607                 .appendTo( "#first" );
608         assert.equal( jQuery( "#first" ).children().length, l, "Make sure the elements were inserted." );
609         assert.equal( jQuery( "#first" ).children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
610 } );
612 QUnit.test( "appendTo(Element|Array<Element>)", function( assert ) {
614         assert.expect( 2 );
616         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
617         jQuery( document.getElementById( "first" ) ).appendTo( "#sap" );
618         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
620         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
621         jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).appendTo( "#sap" );
622         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
624 } );
626 QUnit.test( "appendTo(jQuery)", function( assert ) {
628         assert.expect( 10 );
630         var expected, num, div;
631         assert.ok( jQuery( document.createElement( "script" ) ).appendTo( "body" ).length, "Make sure a disconnected script can be appended." );
633         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
634         jQuery( "#yahoo, #first" ).appendTo( "#sap" );
635         assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
637         jQuery( "#select1" ).appendTo( "#foo" );
638         assert.t( "Append select", "#foo select", [ "select1" ] );
640         div = jQuery( "<div></div>" ).on( "click", function() {
641                 assert.ok( true, "Running a cloned click." );
642         } );
643         div.appendTo( "#qunit-fixture, #moretests" );
645         jQuery( "#qunit-fixture div" ).last().trigger( "click" );
646         jQuery( "#moretests div" ).last().trigger( "click" );
648         div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture, #moretests" );
650         assert.equal( div.length, 2, "appendTo returns the inserted elements" );
652         div.addClass( "test" );
654         assert.ok( jQuery( "#qunit-fixture div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
655         assert.ok( jQuery( "#moretests div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
657         div = jQuery( "<div></div>" );
658         jQuery( "<span>a</span><b>b</b>" ).filter( "span" ).appendTo( div );
660         assert.equal( div.children().length, 1, "Make sure the right number of children were inserted." );
662         div = jQuery( "#moretests div" );
664         num = jQuery( "#qunit-fixture div" ).length;
665         div.remove().appendTo( "#qunit-fixture" );
667         assert.equal( jQuery( "#qunit-fixture div" ).length, num, "Make sure all the removed divs were inserted." );
668 } );
670 QUnit.test( "prepend(String)", function( assert ) {
672         assert.expect( 2 );
674         var result, expected;
675         expected = "Try them out:";
676         result = jQuery( "#first" ).prepend( "<b>buga</b>" );
677         assert.equal( result.text(), "buga" + expected, "Check if text prepending works" );
678         assert.equal( jQuery( "#select3" ).prepend( "<option value='prependTest'>Prepend Test</option>"  ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
679 } );
681 QUnit.test( "prepend(Element)", function( assert ) {
683         assert.expect( 1 );
685         var expected;
686         expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
687         jQuery( "#sap" ).prepend( document.getElementById( "first" ) );
688         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
689 } );
691 QUnit.test( "prepend(Array<Element>)", function( assert ) {
693         assert.expect( 1 );
695         var expected;
696         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
697         jQuery( "#sap" ).prepend( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] );
698         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
699 } );
701 QUnit.test( "prepend(jQuery)", function( assert ) {
703         assert.expect( 1 );
705         var expected;
706         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
707         jQuery( "#sap" ).prepend( jQuery( "#yahoo, #first" ) );
708         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
709 } );
711 QUnit.test( "prepend(Array<jQuery>)", function( assert ) {
713         assert.expect( 1 );
715         var expected;
716         expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
717         jQuery( "#sap" ).prepend( [ jQuery( "#first" ), jQuery( "#yahoo, #google" ) ] );
718         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of jQuery objects" );
719 } );
721 QUnit.test( "prepend(Function) with incoming value -- String", function( assert ) {
723         assert.expect( 4 );
725         var defaultText, old, result;
727         defaultText = "Try them out:";
728         old = jQuery( "#first" ).html();
729         result = jQuery( "#first" ).prepend( function( i, val ) {
730                 assert.equal( val, old, "Make sure the incoming value is correct." );
731                 return "<b>buga</b>";
732         } );
734         assert.equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
736         old = jQuery( "#select3" ).html();
738         assert.equal( jQuery( "#select3" ).prepend( function( i, val ) {
739                 assert.equal( val, old, "Make sure the incoming value is correct." );
740                 return "<option value='prependTest'>Prepend Test</option>";
741         } ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
742 } );
744 QUnit.test( "prepend(Function) with incoming value -- Element", function( assert ) {
746         assert.expect( 2 );
748         var old, expected;
749         expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
750         old = jQuery( "#sap" ).html();
752         jQuery( "#sap" ).prepend( function( i, val ) {
753                 assert.equal( val, old, "Make sure the incoming value is correct." );
754                 return document.getElementById( "first" );
755         } );
757         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
758 } );
760 QUnit.test( "prepend(Function) with incoming value -- Array<Element>", function( assert ) {
762         assert.expect( 2 );
764         var old, expected;
765         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
766         old = jQuery( "#sap" ).html();
768         jQuery( "#sap" ).prepend( function( i, val ) {
769                 assert.equal( val, old, "Make sure the incoming value is correct." );
770                 return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
771         } );
773         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
774 } );
776 QUnit.test( "prepend(Function) with incoming value -- jQuery", function( assert ) {
778         assert.expect( 2 );
780         var old, expected;
781         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
782         old = jQuery( "#sap" ).html();
784         jQuery( "#sap" ).prepend( function( i, val ) {
785                 assert.equal( val, old, "Make sure the incoming value is correct." );
786                 return jQuery( "#yahoo, #first" );
787         } );
789         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
790 } );
792 QUnit.test( "prependTo(String)", function( assert ) {
794         assert.expect( 2 );
796         var defaultText;
798         defaultText = "Try them out:";
799         jQuery( "<b>buga</b>" ).prependTo( "#first" );
800         assert.equal( jQuery( "#first" ).text(), "buga" + defaultText, "Check if text prepending works" );
801         assert.equal( jQuery( "<option value='prependTest'>Prepend Test</option>" ).prependTo( "#select3" ).parent().find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
803 } );
805 QUnit.test( "prependTo(Element)", function( assert ) {
807         assert.expect( 1 );
809         var expected;
811         expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
812         jQuery( document.getElementById( "first" ) ).prependTo( "#sap" );
813         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
814 } );
816 QUnit.test( "prependTo(Array<Element>)", function( assert ) {
818         assert.expect( 1 );
820         var expected;
822         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
823         jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).prependTo( "#sap" );
824         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
825 } );
827 QUnit.test( "prependTo(jQuery)", function( assert ) {
829         assert.expect( 1 );
831         var expected;
833         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
834         jQuery( "#yahoo, #first" ).prependTo( "#sap" );
835         assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
836 } );
838 QUnit.test( "prependTo(Array<jQuery>)", function( assert ) {
840         assert.expect( 1 );
842         jQuery( "<select id='prependSelect1'></select>" ).prependTo( "#form" );
843         jQuery( "<select id='prependSelect2'><option>Test</option></select>" ).prependTo( "#form" );
845         assert.t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
846 } );
848 QUnit.test( "before(String)", function( assert ) {
850         assert.expect( 1 );
852         var expected;
854         expected = "This is a normal link: bugaYahoo";
855         jQuery( "#yahoo" ).before( manipulationBareObj( "<b>buga</b>" ) );
856         assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
857 } );
859 QUnit.test( "before(Element)", function( assert ) {
861         assert.expect( 1 );
863         var expected;
865         expected = "This is a normal link: Try them out:Yahoo";
866         jQuery( "#yahoo" ).before( manipulationBareObj( document.getElementById( "first" ) ) );
867         assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
868 } );
870 QUnit.test( "before(Array<Element>)", function( assert ) {
872         assert.expect( 1 );
874         var expected;
875         expected = "This is a normal link: Try them out:diveintomarkYahoo";
876         jQuery( "#yahoo" ).before( manipulationBareObj( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
877         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
878 } );
880 QUnit.test( "before(jQuery)", function( assert ) {
882         assert.expect( 1 );
884         var expected;
885         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
886         jQuery( "#yahoo" ).before( manipulationBareObj( jQuery( "#mark, #first" ) ) );
887         assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
888 } );
890 QUnit.test( "before(Array<jQuery>)", function( assert ) {
892         assert.expect( 1 );
894         var expected;
895         expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
896         jQuery( "#yahoo" ).before( manipulationBareObj( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
897         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
898 } );
900 QUnit.test( "before(Function) -- Returns String", function( assert ) {
902         assert.expect( 1 );
904         var expected;
906         expected = "This is a normal link: bugaYahoo";
907         jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( "<b>buga</b>" ) );
908         assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
909 } );
911 QUnit.test( "before(Function) -- Returns Element", function( assert ) {
913         assert.expect( 1 );
915         var expected;
917         expected = "This is a normal link: Try them out:Yahoo";
918         jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( document.getElementById( "first" ) ) );
919         assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
920 } );
922 QUnit.test( "before(Function) -- Returns Array<Element>", function( assert ) {
924         assert.expect( 1 );
926         var expected;
927         expected = "This is a normal link: Try them out:diveintomarkYahoo";
928         jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
929         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
930 } );
932 QUnit.test( "before(Function) -- Returns jQuery", function( assert ) {
934         assert.expect( 1 );
936         var expected;
937         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
938         jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( jQuery( "#mark, #first" ) ) );
939         assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
940 } );
942 QUnit.test( "before(Function) -- Returns Array<jQuery>", function( assert ) {
944         assert.expect( 1 );
946         var expected;
947         expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
948         jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
949         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
950 } );
952 QUnit.test( "before(no-op)", function( assert ) {
954         assert.expect( 2 );
956         var set;
957         set = jQuery( "<div></div>" ).before( "<span>test</span>" );
958         assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
959         assert.equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
960 } );
962 QUnit.test( "before and after w/ empty object (#10812)", function( assert ) {
964         assert.expect( 1 );
966         var res;
968         res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
969         assert.equal( res.length, 0, "didn't choke on empty object" );
970 } );
972 QUnit.test( ".before() and .after() disconnected node", function( assert ) {
974         assert.expect( 2 );
976         assert.equal( jQuery( "<input type='checkbox'/>" ).before( "<div></div>" ).length, 1, "before() on disconnected node is no-op" );
977         assert.equal( jQuery( "<input type='checkbox'/>" ).after( "<div></div>" ).length, 1, "after() on disconnected node is no-op" );
978 } );
980 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
982         assert.expect( 1 );
984         var expectedBefore = "This is a normal link: bugaYahoo";
986         jQuery( "#yahoo" ).add( "<span></span>" ).before( "<b>buga</b>" );
987         assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node last" );
988 } );
990 QUnit.test( "insert with .before() on disconnected node first", function( assert ) {
992         assert.expect( 1 );
994         var expectedBefore = "This is a normal link: bugaYahoo";
996         jQuery( "<span></span>" ).add( "#yahoo" ).before( "<b>buga</b>" );
997         assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node first" );
998 } );
1000 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
1002         assert.expect( 1 );
1004         var expectedAfter = "This is a normal link: Yahoobuga";
1006         jQuery( "#yahoo" ).add( "<span></span>" ).after( "<b>buga</b>" );
1007         assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node last" );
1008 } );
1010 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
1012         assert.expect( 1 );
1014         var expectedAfter = "This is a normal link: Yahoobuga";
1016         jQuery( "<span></span>" ).add( "#yahoo" ).after( "<b>buga</b>" );
1017         assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node first" );
1018 } );
1020 QUnit.test( "insertBefore(String)", function( assert ) {
1022         assert.expect( 1 );
1024         var expected = "This is a normal link: bugaYahoo";
1025         jQuery( "<b>buga</b>" ).insertBefore( "#yahoo" );
1026         assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
1027 } );
1029 QUnit.test( "insertBefore(Element)", function( assert ) {
1031         assert.expect( 1 );
1033         var expected = "This is a normal link: Try them out:Yahoo";
1034         jQuery( document.getElementById( "first" ) ).insertBefore( "#yahoo" );
1035         assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
1036 } );
1038 QUnit.test( "insertBefore(Array<Element>)", function( assert ) {
1040         assert.expect( 1 );
1042         var expected = "This is a normal link: Try them out:diveintomarkYahoo";
1043         jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).insertBefore( "#yahoo" );
1044         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
1045 } );
1047 QUnit.test( "insertBefore(jQuery)", function( assert ) {
1049         assert.expect( 1 );
1051         var expected = "This is a normal link: diveintomarkTry them out:Yahoo";
1052         jQuery( "#mark, #first" ).insertBefore( "#yahoo" );
1053         assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
1054 } );
1056 QUnit.test( ".after(String)", function( assert ) {
1058         assert.expect( 1 );
1060         var expected = "This is a normal link: Yahoobuga";
1061         jQuery( "#yahoo" ).after( "<b>buga</b>" );
1062         assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1063 } );
1065 QUnit.test( ".after(Element)", function( assert ) {
1067         assert.expect( 1 );
1069         var expected = "This is a normal link: YahooTry them out:";
1070         jQuery( "#yahoo" ).after( document.getElementById( "first" ) );
1071         assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1072 } );
1074 QUnit.test( ".after(Array<Element>)", function( assert ) {
1076         assert.expect( 1 );
1078         var expected = "This is a normal link: YahooTry them out:diveintomark";
1079         jQuery( "#yahoo" ).after( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] );
1080         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1081 } );
1083 QUnit.test( ".after(jQuery)", function( assert ) {
1085         assert.expect( 1 );
1087         var expected = "This is a normal link: YahooTry them out:Googlediveintomark";
1088         jQuery( "#yahoo" ).after( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] );
1089         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
1090 } );
1092 QUnit.test( ".after(Function) returns String", function( assert ) {
1094         assert.expect( 1 );
1096         var expected = "This is a normal link: Yahoobuga",
1097                 val = manipulationFunctionReturningObj;
1098         jQuery( "#yahoo" ).after( val( "<b>buga</b>" ) );
1099         assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1100 } );
1102 QUnit.test( ".after(Function) returns Element", function( assert ) {
1104         assert.expect( 1 );
1106         var expected = "This is a normal link: YahooTry them out:",
1107                 val = manipulationFunctionReturningObj;
1108         jQuery( "#yahoo" ).after( val( document.getElementById( "first" ) ) );
1109         assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1110 } );
1112 QUnit.test( ".after(Function) returns Array<Element>", function( assert ) {
1114         assert.expect( 1 );
1116         var expected = "This is a normal link: YahooTry them out:diveintomark",
1117                 val = manipulationFunctionReturningObj;
1118         jQuery( "#yahoo" ).after( val( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
1119         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1120 } );
1122 QUnit.test( ".after(Function) returns jQuery", function( assert ) {
1124         assert.expect( 1 );
1126         var expected = "This is a normal link: YahooTry them out:Googlediveintomark",
1127                 val = manipulationFunctionReturningObj;
1128         jQuery( "#yahoo" ).after( val( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
1129         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
1130 } );
1132 QUnit.test( ".after(disconnected node)", function( assert ) {
1134         assert.expect( 2 );
1136         var set = jQuery( "<div></div>" ).before( "<span>test</span>" );
1137         assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
1138         assert.equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
1139 } );
1141 QUnit.test( "insertAfter(String)", function( assert ) {
1143         assert.expect( 1 );
1145         var expected = "This is a normal link: Yahoobuga";
1146         jQuery( "<b>buga</b>" ).insertAfter( "#yahoo" );
1147         assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1148 } );
1150 QUnit.test( "insertAfter(Element)", function( assert ) {
1152         assert.expect( 1 );
1154         var expected = "This is a normal link: YahooTry them out:";
1155         jQuery( document.getElementById( "first" ) ).insertAfter( "#yahoo" );
1156         assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1157 } );
1159 QUnit.test( "insertAfter(Array<Element>)", function( assert ) {
1161         assert.expect( 1 );
1163         var expected = "This is a normal link: YahooTry them out:diveintomark";
1164         jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).insertAfter( "#yahoo" );
1165         assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1166 } );
1168 QUnit.test( "insertAfter(jQuery)", function( assert ) {
1170         assert.expect( 1 );
1172         var expected = "This is a normal link: YahoodiveintomarkTry them out:";
1173         jQuery( "#mark, #first" ).insertAfter( "#yahoo" );
1174         assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery after" );
1175 } );
1177 function testReplaceWith( val, assert ) {
1179         var tmp, y, child, child2, set, nonExistent, $div,
1180                 expected = 29;
1182         assert.expect( expected );
1184         jQuery( "#yahoo" ).replaceWith( val( "<b id='replace'>buga</b>" ) );
1185         assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with element from string" );
1186         assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
1188         jQuery( "#anchor2" ).replaceWith( val( document.getElementById( "first" ) ) );
1189         assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
1190         assert.ok( !jQuery( "#anchor2" )[ 0 ], "Verify that original element is gone, after element" );
1192         jQuery( "#qunit-fixture" ).append( "<div id='bar'><div id='baz'></div></div>" );
1193         jQuery( "#baz" ).replaceWith( val( "Baz" ) );
1194         assert.equal( jQuery( "#bar" ).text(), "Baz", "Replace element with text" );
1195         assert.ok( !jQuery( "#baz" )[ 0 ], "Verify that original element is gone, after element" );
1197         jQuery( "#bar" ).replaceWith( "<div id='yahoo'></div>", "...", "<div id='baz'></div>" );
1198         assert.deepEqual( jQuery( "#yahoo, #baz" ).get(), q( "yahoo", "baz" ),  "Replace element with multiple arguments (#13722)" );
1199         assert.strictEqual( jQuery( "#yahoo" )[ 0 ].nextSibling, jQuery( "#baz" )[ 0 ].previousSibling, "Argument order preserved" );
1200         assert.deepEqual( jQuery( "#bar" ).get(), [], "Verify that original element is gone, after multiple arguments" );
1202         jQuery( "#google" ).replaceWith( val( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
1203         assert.deepEqual( jQuery( "#mark, #first" ).get(), q( "first", "mark" ),  "Replace element with array of elements" );
1204         assert.ok( !jQuery( "#google" )[ 0 ], "Verify that original element is gone, after array of elements" );
1206         jQuery( "#groups" ).replaceWith( val( jQuery( "#mark, #first" ) ) );
1207         assert.deepEqual( jQuery( "#mark, #first" ).get(), q( "first", "mark" ),  "Replace element with jQuery collection" );
1208         assert.ok( !jQuery( "#groups" )[ 0 ], "Verify that original element is gone, after jQuery collection" );
1210         jQuery( "#mark, #first" ).replaceWith( val( "<span class='replacement'></span><span class='replacement'></span>" ) );
1211         assert.equal( jQuery( "#qunit-fixture .replacement" ).length, 4, "Replace multiple elements (#12449)" );
1212         assert.deepEqual( jQuery( "#mark, #first" ).get(), [], "Verify that original elements are gone, after replace multiple" );
1214         tmp = jQuery( "<b>content</b>" )[ 0 ];
1215         jQuery( "#anchor1" ).contents().replaceWith( val( tmp ) );
1216         assert.deepEqual( jQuery( "#anchor1" ).contents().get(), [ tmp ], "Replace text node with element" );
1218         tmp = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1219                 assert.ok( true, "Newly bound click run." );
1220         } );
1221         y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1222                 assert.ok( false, "Previously bound click run." );
1223         } );
1224         child = y.append( "<b>test</b>" ).find( "b" ).on( "click", function() {
1225                 assert.ok( true, "Child bound click run." );
1226                 return false;
1227         } );
1229         y.replaceWith( val( tmp ) );
1231         tmp.trigger( "click" );
1232         y.trigger( "click" ); // Shouldn't be run
1233         child.trigger( "click" ); // Shouldn't be run
1235         y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1236                 assert.ok( false, "Previously bound click run." );
1237         } );
1238         child2 = y.append( "<u>test</u>" ).find( "u" ).on( "click", function() {
1239                 assert.ok( true, "Child 2 bound click run." );
1240                 return false;
1241         } );
1243         y.replaceWith( val( child2 ) );
1245         child2.trigger( "click" );
1247         set = jQuery( "<div></div>" ).replaceWith( val( "<span>test</span>" ) );
1248         assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "No effect on a disconnected node." );
1249         assert.equal( set.length, 1, "No effect on a disconnected node." );
1250         assert.equal( set[ 0 ].childNodes.length, 0, "No effect on a disconnected node." );
1252         child = jQuery( "#qunit-fixture" ).children().first();
1253         $div = jQuery( "<div class='pathological'></div>" ).insertBefore( child );
1254         $div.replaceWith( $div );
1255         assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
1256                 "Self-replacement" );
1257         $div.replaceWith( child );
1258         assert.deepEqual( jQuery( "#qunit-fixture" ).children().first().get(), child.get(),
1259                 "Replacement with following sibling (#13810)" );
1260         assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
1261                 "Replacement with following sibling (context removed)" );
1263         nonExistent = jQuery( "#does-not-exist" ).replaceWith( val( "<b>should not throw an error</b>" ) );
1264         assert.equal( nonExistent.length, 0, "Length of non existent element." );
1266         $div = jQuery( "<div class='replacewith'></div>" ).appendTo( "#qunit-fixture" );
1267         $div.replaceWith( val( "<div class='replacewith'></div><script>" +
1268                 "QUnit.assert.equal( jQuery('.replacewith').length, 1, 'Check number of elements in page.' );" +
1269                 "</script>" ) );
1271         jQuery( "#qunit-fixture" ).append( "<div id='replaceWith'></div>" );
1272         assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists." );
1273         jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
1274         assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after replacement." );
1275         jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
1276         assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after subsequent replacement." );
1278         return expected;
1281 QUnit.test( "replaceWith(String|Element|Array<Element>|jQuery)", function( assert ) {
1282         testReplaceWith( manipulationBareObj, assert );
1283 } );
1285 QUnit.test( "replaceWith(Function)", function( assert ) {
1286         assert.expect( testReplaceWith( manipulationFunctionReturningObj, assert ) + 1 );
1288         var y = jQuery( "#foo" )[ 0 ];
1290         jQuery( y ).replaceWith( function() {
1291                 assert.equal( this, y, "Make sure the context is coming in correctly." );
1292         } );
1293 } );
1295 QUnit.test( "replaceWith(string) for more than one element", function( assert ) {
1297         assert.expect( 3 );
1299         assert.equal( jQuery( "#foo p" ).length, 3, "ensuring that test data has not changed" );
1301         jQuery( "#foo p" ).replaceWith( "<span>bar</span>" );
1302         assert.equal( jQuery( "#foo span" ).length, 3, "verify that all the three original element have been replaced" );
1303         assert.equal( jQuery( "#foo p" ).length, 0, "verify that all the three original element have been replaced" );
1304 } );
1306 QUnit.test( "Empty replaceWith (trac-13401; trac-13596; gh-2204)", function( assert ) {
1308         assert.expect( 25 );
1310         var $el = jQuery( "<div></div><div></div>" ).html( "<p>0</p>" ),
1311                 expectedHTML = $el.html(),
1312                 tests = {
1313                         "empty string": "",
1314                         "empty array": [],
1315                         "array of empty string": [ "" ],
1316                         "empty collection": jQuery( "#nonexistent" ),
1318                         // in case of jQuery(...).replaceWith();
1319                         "undefined": undefined
1320                 };
1322         jQuery.each( tests, function( label, input ) {
1323                 $el.html( "<a></a>" ).children().replaceWith( input );
1324                 assert.strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
1325                 $el.html( "<b></b>" ).children().replaceWith( function() { return input; } );
1326                 assert.strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
1327                 $el.html( "<i></i>" ).children().replaceWith( function( i ) { return input; } );
1328                 assert.strictEqual( $el.html(), "", "replaceWith(other function returning " + label + ")" );
1329                 $el.html( "<p></p>" ).children().replaceWith( function( i ) {
1330                         return i ?
1331                                 input :
1332                                 jQuery( this ).html( i + "" );
1333                 } );
1334                 assert.strictEqual( $el.eq( 0 ).html(), expectedHTML,
1335                         "replaceWith(function conditionally returning context)" );
1336                 assert.strictEqual( $el.eq( 1 ).html(), "",
1337                         "replaceWith(function conditionally returning " + label + ")" );
1338         } );
1339 } );
1341 QUnit.test( "replaceAll(String)", function( assert ) {
1343         assert.expect( 2 );
1345         jQuery( "<b id='replace'>buga</b>" ).replaceAll( "#yahoo" );
1346         assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with string" );
1347         assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
1348 } );
1350 QUnit.test( "replaceAll(Element)", function( assert ) {
1352         assert.expect( 2 );
1354         jQuery( document.getElementById( "first" ) ).replaceAll( "#yahoo" );
1355         assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
1356         assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after element" );
1357 } );
1359 QUnit.test( "replaceAll(Array<Element>)", function( assert ) {
1361         assert.expect( 3 );
1363         jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).replaceAll( "#yahoo" );
1364         assert.ok( jQuery( "#first" )[ 0 ], "Replace element with array of elements" );
1365         assert.ok( jQuery( "#mark" )[ 0 ], "Replace element with array of elements" );
1366         assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after array of elements" );
1367 } );
1369 QUnit.test( "replaceAll(jQuery)", function( assert ) {
1371         assert.expect( 3 );
1373         jQuery( "#mark, #first" ).replaceAll( "#yahoo" );
1374         assert.ok( jQuery( "#first" )[ 0 ], "Replace element with set of elements" );
1375         assert.ok( jQuery( "#mark" )[ 0 ], "Replace element with set of elements" );
1376         assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after set of elements" );
1377 } );
1379 QUnit.test( "jQuery.clone() (#8017)", function( assert ) {
1381         assert.expect( 2 );
1383         assert.ok( jQuery.clone && typeof jQuery.clone === "function", "jQuery.clone() utility exists and is a function." );
1385         var main = jQuery( "#qunit-fixture" )[ 0 ],
1386                 clone = jQuery.clone( main );
1388         assert.equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
1389 } );
1391 QUnit.test( "append to multiple elements (#8070)", function( assert ) {
1393         assert.expect( 2 );
1395         var selects = jQuery( "<select class='test8070'></select><select class='test8070'></select>" ).appendTo( "#qunit-fixture" );
1396         selects.append( "<OPTION>1</OPTION><OPTION>2</OPTION>" );
1398         assert.equal( selects[ 0 ].childNodes.length, 2, "First select got two nodes" );
1399         assert.equal( selects[ 1 ].childNodes.length, 2, "Second select got two nodes" );
1400 } );
1402 QUnit.test( "table manipulation", function( assert ) {
1403         assert.expect( 2 );
1405         var table = jQuery( "<table style='font-size:16px'></table>" ).appendTo( "#qunit-fixture" ).empty(),
1406                 height = table[ 0 ].offsetHeight;
1408         table.append( "<tr><td>DATA</td></tr>" );
1409         assert.ok( table[ 0 ].offsetHeight - height >= 15, "appended rows are visible" );
1411         table.empty();
1412         height = table[ 0 ].offsetHeight;
1413         table.prepend( "<tr><td>DATA</td></tr>" );
1414         assert.ok( table[ 0 ].offsetHeight - height >= 15, "prepended rows are visible" );
1415 } );
1417 QUnit.test( "clone()", function( assert ) {
1419         assert.expect( 45 );
1421         var div, clone, form, body;
1423         assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Assert text for #en" );
1424         assert.equal( jQuery( "#first" ).append( jQuery( "#yahoo" ).clone() ).text(), "Try them out:Yahoo", "Check for clone" );
1425         assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Reassert text for #en" );
1427         jQuery.each( "table thead tbody tfoot tr td div button ul ol li select option textarea iframe".split( " " ), function( i, nodeName ) {
1428                 assert.equal( jQuery( "<" + nodeName + "/>" ).clone()[ 0 ].nodeName.toLowerCase(), nodeName, "Clone a " + nodeName );
1429         } );
1430         assert.equal( jQuery( "<input type='checkbox' />" ).clone()[ 0 ].nodeName.toLowerCase(), "input", "Clone a <input type='checkbox' />" );
1432         // Check cloning non-elements
1433         assert.equal( jQuery( "#nonnodes" ).contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1435         // Verify that clones of clones can keep event listeners
1436         div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
1437                 assert.ok( true, "Bound event still exists." );
1438         } );
1439         clone = div.clone( true ); div.remove();
1440         div = clone.clone( true ); clone.remove();
1442         assert.equal( div.length, 1, "One element cloned" );
1443         assert.equal( div[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1444         div.trigger( "click" );
1446         // Manually clean up detached elements
1447         div.remove();
1449         // Verify that cloned children can keep event listeners
1450         div = jQuery( "<div></div>" ).append( [ document.createElement( "table" ), document.createElement( "table" ) ] );
1451         div.find( "table" ).on( "click", function() {
1452                 assert.ok( true, "Bound event still exists." );
1453         } );
1455         clone = div.clone( true );
1456         assert.equal( clone.length, 1, "One element cloned" );
1457         assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1458         clone.find( "table" ).trigger( "click" );
1460         // Manually clean up detached elements
1461         div.remove();
1462         clone.remove();
1464         // Make sure that doing .clone() doesn't clone event listeners
1465         div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
1466                 assert.ok( false, "Bound event still exists after .clone()." );
1467         } );
1468         clone = div.clone();
1470         clone.trigger( "click" );
1472         // Manually clean up detached elements
1473         clone.remove();
1474         div.remove();
1476         // Test both html() and clone() for <embed> and <object> types
1477         div = jQuery( "<div></div>" ).html( "<embed height='355' width='425' src='https://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'></embed>" );
1479         clone = div.clone( true );
1480         assert.equal( clone.length, 1, "One element cloned" );
1481         assert.equal( clone.html(), div.html(), "Element contents cloned" );
1482         assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1484         // this is technically an invalid object, but because of the special
1485         // classid instantiation it is the only kind that IE has trouble with,
1486         // so let's test with it too.
1487         div = jQuery( "<div></div>" ).html( "<object height='355' width='425' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'>  <param name='movie' value='https://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'>  <param name='wmode' value='transparent'> </object>" );
1489         clone = div.clone( true );
1490         assert.equal( clone.length, 1, "One element cloned" );
1491         assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1492         div = div.find( "object" );
1493         clone = clone.find( "object" );
1495         // oldIE adds extra attributes and <param> elements, so just test for existence of the defined set
1496         jQuery.each( [ "height", "width", "classid" ], function( i, attr ) {
1497                 assert.equal( clone.attr( attr ), div.attr( attr ), "<object> attribute cloned: " + attr );
1498         } );
1499         ( function() {
1500                 var params = {};
1502                 clone.find( "param" ).each( function( index, param ) {
1503                         params[ param.attributes.name.nodeValue.toLowerCase() ] =
1504                                 param.attributes.value.nodeValue.toLowerCase();
1505                 } );
1507                 div.find( "param" ).each( function( index, param ) {
1508                         var key = param.attributes.name.nodeValue.toLowerCase();
1509                         assert.equal( params[ key ], param.attributes.value.nodeValue.toLowerCase(), "<param> cloned: " + key );
1510                 } );
1511         } )();
1513         // and here's a valid one.
1514         div = jQuery( "<div></div>" ).html( "<object height='355' width='425' type='application/x-shockwave-flash' data='https://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'>  <param name='movie' value='https://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'>  <param name='wmode' value='transparent'> </object>" );
1516         clone = div.clone( true );
1517         assert.equal( clone.length, 1, "One element cloned" );
1518         assert.equal( clone.html(), div.html(), "Element contents cloned" );
1519         assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1521         div = jQuery( "<div></div>" ).data( { "a": true } );
1522         clone = div.clone( true );
1523         assert.equal( clone.data( "a" ), true, "Data cloned." );
1524         clone.data( "a", false );
1525         assert.equal( clone.data( "a" ), false, "Ensure cloned element data object was correctly modified" );
1526         assert.equal( div.data( "a" ), true, "Ensure cloned element data object is copied, not referenced" );
1528         // manually clean up detached elements
1529         div.remove();
1530         clone.remove();
1532         form = document.createElement( "form" );
1533         form.action = "/test/";
1535         div = document.createElement( "div" );
1536         div.appendChild( document.createTextNode( "test" ) );
1537         form.appendChild( div );
1539         assert.equal( jQuery( form ).clone().children().length, 1, "Make sure we just get the form back." );
1541         body = jQuery( "body" ).clone();
1542         assert.equal( body.children()[ 0 ].id, "qunit", "Make sure cloning body works" );
1543         body.remove();
1544 } );
1546 QUnit.test( "clone(script type=non-javascript) (#11359)", function( assert ) {
1548         assert.expect( 3 );
1550         var src = jQuery( "<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>" ),
1551                 dest = src.clone();
1553         assert.equal( dest[ 0 ].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
1554         assert.equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
1555         assert.ok( /^\s*<scr.pt\s+type=['"]?text\/filler['"]?\s*>consectetur adipiscing elit<\/scr.pt>\s*$/i.test( dest.last().html() ), "Cloning preserves nested script text" );
1556         dest.remove();
1557 } );
1559 QUnit.test( "clone(form element) (Bug #3879, #6655)", function( assert ) {
1561         assert.expect( 5 );
1563         var clone, element;
1565         element = jQuery( "<select><option>Foo</option><option value='selected' selected>Bar</option></select>" );
1567         assert.equal( element.clone().find( "option" ).filter( function() { return this.selected; } ).val(), "selected", "Selected option cloned correctly" );
1569         element = jQuery( "<input type='checkbox' value='foo'>" ).attr( "checked", "checked" );
1570         clone = element.clone();
1572         assert.equal( clone.is( ":checked" ), element.is( ":checked" ), "Checked input cloned correctly" );
1573         assert.equal( clone[ 0 ].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
1575         element = jQuery( "<input type='text' value='foo'>" );
1576         clone = element.clone();
1577         assert.equal( clone[ 0 ].defaultValue, "foo", "Text input defaultValue cloned correctly" );
1579         element = jQuery( "<textarea>foo</textarea>" );
1580         clone = element.clone();
1581         assert.equal( clone[ 0 ].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
1582 } );
1584 QUnit.test( "clone(multiple selected options) (Bug #8129)", function( assert ) {
1586         assert.expect( 1 );
1588         var element = jQuery( "<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>" );
1590         function getSelectedOptions( collection ) {
1591                 return collection.find( "option" ).filter( function( option ) {
1592                         return option.selected;
1593                 } );
1594         }
1596         assert.equal(
1597                 getSelectedOptions( element.clone() ).length,
1598                 getSelectedOptions( element ).length,
1599                 "Multiple selected options cloned correctly"
1600         );
1601 } );
1603 QUnit.test( "clone() on XML nodes", function( assert ) {
1605         assert.expect( 2 );
1607         var xml = createDashboardXML(),
1608                 root = jQuery( xml.documentElement ).clone(),
1609                 origTab = jQuery( "tab", xml ).eq( 0 ),
1610                 cloneTab = jQuery( "tab", root ).eq( 0 );
1612         origTab.text( "origval" );
1613         cloneTab.text( "cloneval" );
1614         assert.equal( origTab.text(), "origval", "Check original XML node was correctly set" );
1615         assert.equal( cloneTab.text(), "cloneval", "Check cloned XML node was correctly set" );
1616 } );
1618 QUnit.test( "clone() on local XML nodes with html5 nodename", function( assert ) {
1620         assert.expect( 2 );
1622         var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
1623                 $meter = $xmlDoc.find( "meter" ).clone();
1625         assert.equal( $meter[ 0 ].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
1626         assert.equal( $meter[ 0 ].nodeType, 1, "Check if nodeType is not changed due to cloning" );
1627 } );
1629 QUnit.test( "html(undefined)", function( assert ) {
1631         assert.expect( 1 );
1633         assert.equal( jQuery( "#foo" ).html( "<i>test</i>" ).html( undefined ).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (#5571)" );
1634 } );
1636 QUnit.test( "html() on empty set", function( assert ) {
1638         assert.expect( 1 );
1640         assert.strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" );
1641 } );
1643 function childNodeNames( node ) {
1644         return jQuery.map( node.childNodes, function( child ) {
1645                 return child.nodeName.toUpperCase();
1646         } ).join( " " );
1649 function testHtml( valueObj, assert ) {
1650         assert.expect( 40 );
1652         var actual, expected, tmp,
1653                 div = jQuery( "<div></div>" ),
1654                 fixture = jQuery( "#qunit-fixture" );
1656         div.html( valueObj( "<div id='parent_1'><div id='child_1'></div></div><div id='parent_2'></div>" ) );
1657         assert.equal( div.children().length, 2, "Found children" );
1658         assert.equal( div.children().children().length, 1, "Found grandchild" );
1660         actual = []; expected = [];
1661         tmp = jQuery( "<map></map>" ).html( valueObj( "<area alt='area'></area>" ) ).each( function() {
1662                 expected.push( "AREA" );
1663                 actual.push( childNodeNames( this ) );
1664         } );
1665         assert.equal( expected.length, 1, "Expecting one parent" );
1666         assert.deepEqual( actual, expected, "Found the inserted area element" );
1668         assert.equal( div.html( valueObj( 5 ) ).html(), "5", "Setting a number as html" );
1669         assert.equal( div.html( valueObj( 0 ) ).html(), "0", "Setting a zero as html" );
1670         assert.equal( div.html( valueObj( Infinity ) ).html(), "Infinity", "Setting Infinity as html" );
1671         assert.equal( div.html( valueObj( NaN ) ).html(), "", "Setting NaN as html" );
1672         assert.equal( div.html( valueObj( 1e2 ) ).html(), "100", "Setting exponential number notation as html" );
1674         div.html( valueObj( "&#160;&amp;" ) );
1675         assert.equal(
1676                 div[ 0 ].innerHTML.replace( /\xA0/, "&nbsp;" ),
1677                 "&nbsp;&amp;",
1678                 "Entities are passed through correctly"
1679         );
1681         tmp = "&lt;div&gt;hello1&lt;/div&gt;";
1682         assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html" );
1683         tmp = "x" + tmp;
1684         assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading x" );
1685         tmp = " " + tmp.slice( 1 );
1686         assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading space" );
1688         actual = []; expected = []; tmp = {};
1689         jQuery( "#nonnodes" ).contents().html( valueObj( "<b>bold</b>" ) ).each( function() {
1690                 var html = jQuery( this ).html();
1691                 tmp[ this.nodeType ] = true;
1692                 expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
1693                 actual.push( html ? html.toLowerCase() : html );
1694         } );
1695         assert.deepEqual( actual, expected, "Set containing element, text node, comment" );
1696         assert.ok( tmp[ 1 ], "element" );
1697         assert.ok( tmp[ 3 ], "text node" );
1698         assert.ok( tmp[ 8 ], "comment" );
1700         actual = []; expected = [];
1701         fixture.children( "div" ).html( valueObj( "<b>test</b>" ) ).each( function() {
1702                 expected.push( "B" );
1703                 actual.push( childNodeNames( this ) );
1704         } );
1705         assert.equal( expected.length, 7, "Expecting many parents" );
1706         assert.deepEqual( actual, expected, "Correct childNodes after setting HTML" );
1708         actual = []; expected = [];
1709         fixture.html( valueObj( "<style>.foobar{color:green;}</style>" ) ).each( function() {
1710                 expected.push( "STYLE" );
1711                 actual.push( childNodeNames( this ) );
1712         } );
1713         assert.equal( expected.length, 1, "Expecting one parent" );
1714         assert.deepEqual( actual, expected, "Found the inserted style element" );
1716         fixture.html( valueObj( "<select></select>" ) );
1717         jQuery( "#qunit-fixture select" ).html( valueObj( "<option>O1</option><option selected='selected'>O2</option><option>O3</option>" ) );
1718         assert.equal( jQuery( "#qunit-fixture select" ).val(), "O2", "Selected option correct" );
1720         tmp = fixture.html(
1721                 valueObj( [
1722                         "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
1723                         "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
1724                         "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
1725                         "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
1726                         "<div>",
1727                                 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
1728                                 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
1729                                 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
1730                                 "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
1731                         "</div>"
1732                 ].join( "" ) )
1733         ).find( "script" );
1734         assert.equal( tmp.length, 8, "All script tags remain." );
1735         assert.equal( tmp[ 0 ].type, "something/else", "Non-evaluated type." );
1736         assert.equal( tmp[ 1 ].type, "text/javascript", "Evaluated type." );
1738         fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1739         fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1740         fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1741         fixture.html( valueObj( "foo <form><script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script (#975)' );</script></form>" ) );
1743         jQuery.scriptorder = 0;
1744         fixture.html( valueObj( [
1745                 "<script>",
1746                         "QUnit.assert.equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
1747                         "QUnit.assert.equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
1748                 "</script>",
1749                 "<span id='scriptorder'><script>QUnit.assert.equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
1750                 "<script>QUnit.assert.equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
1751         ].join( "" ) ) );
1753         fixture.html( valueObj( fixture.text() ) );
1754         assert.ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
1757 QUnit.test( "html(String|Number)", function( assert ) {
1758         testHtml( manipulationBareObj, assert  );
1759 } );
1761 QUnit.test( "html(Function)", function( assert ) {
1762         testHtml( manipulationFunctionReturningObj, assert  );
1763 } );
1765 // Support: IE 9 - 11+
1766 // IE doesn't support modules.
1767 QUnit.testUnlessIE( "html(script type module)", function( assert ) {
1768         assert.expect( 4 );
1769         var done = assert.async(),
1770                 $fixture = jQuery( "#qunit-fixture" );
1772         $fixture.html(
1773                 [
1774                         "<script type='module'>QUnit.assert.ok( true, 'evaluated: module' );</script>",
1775                         "<script type='module' src='" + url( "module.js" ) + "'></script>",
1776                         "<div>",
1777                                 "<script type='module'>QUnit.assert.ok( true, 'evaluated: inner module' );</script>",
1778                                 "<script type='module' src='" + url( "inner_module.js" ) + "'></script>",
1779                         "</div>"
1780                 ].join( "" )
1781         );
1783         // Allow asynchronous script execution to generate assertions
1784         setTimeout( function() {
1785                 done();
1786         }, 1000 );
1787 } );
1789 QUnit.test( "html(script nomodule)", function( assert ) {
1791         // `nomodule` scripts should be executed by legacy browsers only.
1792         assert.expect( QUnit.isIE ? 4 : 0 );
1793         var done = assert.async(),
1794                 $fixture = jQuery( "#qunit-fixture" );
1796         $fixture.html(
1797                 [
1798                         "<script nomodule>QUnit.assert.ok( QUnit.isIE, 'evaluated: nomodule script' );</script>",
1799                         "<script nomodule src='" + url( "nomodule.js" ) + "'></script>",
1800                         "<div>",
1801                                 "<script nomodule>QUnit.assert.ok( QUnit.isIE, 'evaluated: inner nomodule script' );</script>",
1802                                 "<script nomodule src='" + url( "inner_nomodule.js" ) + "'></script>",
1803                         "</div>"
1804                 ].join( "" )
1805         );
1807         // Allow asynchronous script execution to generate assertions
1808         setTimeout( function() {
1809                 done();
1810         }, 1000 );
1811 } );
1813 QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) {
1815         assert.expect( 4 );
1817         var els, actualhtml, pass;
1819         els = jQuery( "#foo > p" );
1820         actualhtml = els.map( function() {
1821                 return jQuery( this ).html();
1822         } );
1824         els.html( function( i, val ) {
1825                 assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1826                 return "<b>test</b>";
1827         } );
1829         pass = true;
1830         els.each( function() {
1831                 if ( this.childNodes.length !== 1 ) {
1832                         pass = false;
1833                 }
1834         } );
1835         assert.ok( pass, "Set HTML" );
1836 } );
1838 QUnit.test( "html(Function) with incoming value -- jQuery.contents()", function( assert ) {
1840         assert.expect( 14 );
1842         var actualhtml, j, $div, $div2, insert;
1844         j = jQuery( "#nonnodes" ).contents();
1845         actualhtml = j.map( function() {
1846                 return jQuery( this ).html();
1847         } );
1849         j.html( function( i, val ) {
1850                 assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1851                 return "<b>bold</b>";
1852         } );
1854         // Handle the case where no comment is in the document
1855         if ( j.length === 2 ) {
1856                 assert.equal( null, null, "Make sure the incoming value is correct." );
1857         }
1859         assert.equal( j.html().replace( / xmlns="[^"]+"/g, "" ).toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1861         $div = jQuery( "<div></div>" );
1863         assert.equal( $div.html( function( i, val ) {
1864                 assert.equal( val, "", "Make sure the incoming value is correct." );
1865                 return 5;
1866         } ).html(), "5", "Setting a number as html" );
1868         assert.equal( $div.html( function( i, val ) {
1869                 assert.equal( val, "5", "Make sure the incoming value is correct." );
1870                 return 0;
1871         } ).html(), "0", "Setting a zero as html" );
1873         $div2 = jQuery( "<div></div>" );
1874         insert = "&lt;div&gt;hello1&lt;/div&gt;";
1875         assert.equal( $div2.html( function( i, val ) {
1876                 assert.equal( val, "", "Make sure the incoming value is correct." );
1877                 return insert;
1878         } ).html().replace( />/g, "&gt;" ), insert, "Verify escaped insertion." );
1880         assert.equal( $div2.html( function( i, val ) {
1881                 assert.equal( val.replace( />/g, "&gt;" ), insert, "Make sure the incoming value is correct." );
1882                 return "x" + insert;
1883         } ).html().replace( />/g, "&gt;" ), "x" + insert, "Verify escaped insertion." );
1885         assert.equal( $div2.html( function( i, val ) {
1886                 assert.equal( val.replace( />/g, "&gt;" ), "x" + insert, "Make sure the incoming value is correct." );
1887                 return " " + insert;
1888         } ).html().replace( />/g, "&gt;" ), " " + insert, "Verify escaped insertion." );
1889 } );
1891 QUnit.test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function( assert ) {
1893         assert.expect( 2 );
1895         var $content = jQuery( "<div><b><i>text</i></b></div>" ).appendTo( "#qunit-fixture" ),
1896                 expected = /^<b><i>text<\/i><\/b>$/i;
1898         // Attach jQuery and Sizzle data (the latter with a non-qSA nth-child)
1899         try {
1900                 $content.find( ":nth-child(1):lt(4)" ).data( "test", true );
1902         // But don't break on a non-Sizzle build
1903         } catch ( e ) {
1904                 $content.find( "*" ).data( "test", true );
1905         }
1907         assert.ok( expected.test( $content.clone( false )[ 0 ].innerHTML ), "clone()" );
1908         assert.ok( expected.test( $content.html() ), "html()" );
1909 } );
1911 QUnit.test( "remove() no filters", function( assert ) {
1913         assert.expect( 2 );
1915         var first = jQuery( "#ap" ).children().first();
1917         first.data( "foo", "bar" );
1919         jQuery( "#ap" ).children().remove();
1920         assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
1921         assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
1922 } );
1924 QUnit.test( "remove() with filters", function( assert ) {
1926         assert.expect( 8 );
1928         var markup, div;
1929         jQuery( "#ap" ).children().remove( "a" );
1930         assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
1931         assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
1933         jQuery( "#ap" ).children().remove( "a, code" );
1934         assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
1936         // Positional and relative selectors
1937         markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
1938         div = jQuery( markup );
1939         div.children().remove( "span:nth-child(2n)" );
1940         assert.equal( div.text(), "13", "relative selector in remove" );
1942         if ( QUnit.jQuerySelectorsPos ) {
1943                 div = jQuery( markup );
1944                 div.children().remove( "span:first" );
1945                 assert.equal( div.text(), "234", "positional selector in remove" );
1946                 div = jQuery( markup );
1947                 div.children().remove( "span:last" );
1948                 assert.equal( div.text(), "123", "positional selector in remove" );
1949         } else {
1950                 assert.ok( "skip", "Positional selectors are not supported" );
1951                 assert.ok( "skip", "Positional selectors are not supported" );
1952         }
1954         // using contents will get comments regular, text, and comment nodes
1955         // Handle the case where no comment is in the document
1956         assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
1957         jQuery( "#nonnodes" ).contents().remove();
1958         assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
1959 } );
1961 QUnit.test( "remove() event cleaning ", function( assert ) {
1962         assert.expect( 1 );
1964         var count, first, cleanUp;
1966         count = 0;
1967         first = jQuery( "#ap" ).children().first();
1968         cleanUp = first.on( "click", function() {
1969                 count++;
1970         } ).remove().appendTo( "#qunit-fixture" ).trigger( "click" );
1972         assert.strictEqual( 0, count, "Event handler has been removed" );
1974         // Clean up detached data
1975         cleanUp.remove();
1976 } );
1978 QUnit.test( "remove() in document order #13779", function( assert ) {
1979         assert.expect( 1 );
1981         var last,
1982                 cleanData = jQuery.cleanData;
1984         jQuery.cleanData = function( nodes ) {
1985                 last = jQuery.text( nodes[ 0 ] );
1986                 cleanData.call( this, nodes );
1987         };
1989         jQuery( "#qunit-fixture" ).append(
1990                 jQuery.parseHTML(
1991                         "<div class='removal-fixture'>1</div>" +
1992                         "<div class='removal-fixture'>2</div>" +
1993                         "<div class='removal-fixture'>3</div>"
1994                 )
1995         );
1997         jQuery( ".removal-fixture" ).remove();
1999         assert.equal( last, 3, "The removal fixtures were removed in document order" );
2001         jQuery.cleanData = cleanData;
2002 } );
2004 QUnit.test( "detach() no filters", function( assert ) {
2006         assert.expect( 3 );
2008         var first = jQuery( "#ap" ).children().first();
2010         first.data( "foo", "bar" );
2012         jQuery( "#ap" ).children().detach();
2013         assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
2014         assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
2016         assert.equal( first.data( "foo" ), "bar" );
2017         first.remove();
2019 } );
2021 QUnit.test( "detach() with filters", function( assert ) {
2023         assert.expect( 8 );
2025         var markup, div;
2026         jQuery( "#ap" ).children().detach( "a" );
2027         assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
2028         assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
2030         jQuery( "#ap" ).children().detach( "a, code" );
2031         assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
2033         // Positional and relative selectors
2034         markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
2035         div = jQuery( markup );
2036         div.children().detach( "span:nth-child(2n)" );
2037         assert.equal( div.text(), "13", "relative selector in detach" );
2039         if ( QUnit.jQuerySelectorsPos ) {
2040                 div = jQuery( markup );
2041                 div.children().detach( "span:first" );
2042                 assert.equal( div.text(), "234", "positional selector in detach" );
2043                 div = jQuery( markup );
2044                 div.children().detach( "span:last" );
2045                 assert.equal( div.text(), "123", "positional selector in detach" );
2046         } else {
2047                 assert.ok( "skip", "Positional selectors are not supported" );
2048                 assert.ok( "skip", "Positional selectors are not supported" );
2049         }
2051         // using contents will get comments regular, text, and comment nodes
2052         // Handle the case where no comment is in the document
2053         assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
2054         jQuery( "#nonnodes" ).contents().detach();
2055         assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
2056 } );
2058 QUnit.test( "detach() event cleaning ", function( assert ) {
2059         assert.expect( 1 );
2061         var count, first, cleanUp;
2063         count = 0;
2064         first = jQuery( "#ap" ).children().first();
2065         cleanUp = first.on( "click", function() {
2066                 count++;
2067         } ).detach().appendTo( "#qunit-fixture" ).trigger( "click" );
2069         assert.strictEqual( 1, count, "Event handler has not been removed" );
2071         // Clean up detached data
2072         cleanUp.remove();
2073 } );
2075 QUnit.test( "empty()", function( assert ) {
2077         assert.expect( 3 );
2079         assert.equal( jQuery( "#ap" ).children().empty().text().length, 0, "Check text is removed" );
2080         assert.equal( jQuery( "#ap" ).children().length, 4, "Check elements are not removed" );
2082         // using contents will get comments regular, text, and comment nodes
2083         var j = jQuery( "#nonnodes" ).contents();
2084         j.empty();
2085         assert.equal( j.html(), "", "Check node,textnode,comment empty works" );
2086 } );
2088 QUnit.test( "jQuery.cleanData", function( assert ) {
2090         assert.expect( 14 );
2092         var type, pos, div, child;
2094         type = "remove";
2096         // Should trigger 4 remove event
2097         div = getDiv().remove();
2099         // Should both do nothing
2100         pos = "Outer";
2101         div.trigger( "click" );
2103         pos = "Inner";
2104         div.children().trigger( "click" );
2106         type = "empty";
2107         div = getDiv();
2108         child = div.children();
2110         // Should trigger 2 remove event
2111         div.empty();
2113         // Should trigger 1
2114         pos = "Outer";
2115         div.trigger( "click" );
2117         // Should do nothing
2118         pos = "Inner";
2119         child.trigger( "click" );
2121         // Should trigger 2
2122         div.remove();
2124         type = "html";
2126         div = getDiv();
2127         child = div.children();
2129         // Should trigger 2 remove event
2130         div.html( "<div></div>" );
2132         // Should trigger 1
2133         pos = "Outer";
2134         div.trigger( "click" );
2136         // Should do nothing
2137         pos = "Inner";
2138         child.trigger( "click" );
2140         // Should trigger 2
2141         div.remove();
2143         function getDiv() {
2144                 var div = jQuery( "<div class='outer'><div class='inner'></div></div>" ).on( "click", function() {
2145                         assert.ok( true, type + " " + pos + " Click event fired." );
2146                 } ).on( "focus", function() {
2147                         assert.ok( true, type + " " + pos + " Focus event fired." );
2148                 } ).find( "div" ).on( "click", function() {
2149                         assert.ok( false, type + " " + pos + " Click event fired." );
2150                 } ).on( "focus", function() {
2151                         assert.ok( false, type + " " + pos + " Focus event fired." );
2152                 } ).end().appendTo( "body" );
2154                 div[ 0 ].detachEvent = div[ 0 ].removeEventListener = function( t ) {
2155                         assert.ok( true, type + " Outer " + t + " event unbound" );
2156                 };
2158                 div[ 0 ].firstChild.detachEvent = div[ 0 ].firstChild.removeEventListener = function( t ) {
2159                         assert.ok( true, type + " Inner " + t + " event unbound" );
2160                 };
2162                 return div;
2163         }
2164 } );
2166 QUnit.test( "jQuery.cleanData eliminates all private data (gh-2127)", function( assert ) {
2167         assert.expect( 3 );
2169         var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
2171         jQuery._data( div[ 0 ], "gh-2127", "testing" );
2173         assert.ok( !jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ),  "Ensure some private data exists" );
2175         div.remove();
2177         assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
2179         assert.ok( jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ),
2180                 "Private data is empty after node is removed" );
2182         div.remove();
2183 } );
2185 QUnit.test( "jQuery.cleanData eliminates all public data", function( assert ) {
2186         assert.expect( 3 );
2188         var key,
2189                 div = jQuery( "<div></div>" );
2190         div.data( "some", "data" );
2191         assert.ok( !jQuery.isEmptyObject( jQuery.data( div[ 0 ] ) ),  "Ensure some public data exists" );
2193         div.remove();
2195         assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
2197         // Make sure the expando is gone
2198         for ( key in div[ 0 ] ) {
2199                 if ( /^jQuery/.test( key ) ) {
2200                         assert.strictEqual( div[ 0 ][ key ], undefined, "Expando was not removed when there was no more data" );
2201                 }
2202         }
2203 } );
2205 QUnit.test( "domManip plain-text caching (trac-6779)", function( assert ) {
2207         assert.expect( 1 );
2209         // DOM manipulation fails if added text matches an Object method
2210         var i,
2211                 $f = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
2212                 bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
2214         for ( i = 0; i < bad.length; i++ ) {
2215                 try {
2216                         $f.append( bad[ i ] );
2217                 }
2218                 catch ( e ) {}
2219         }
2220         assert.equal( $f.text(), bad.join( "" ), "Cached strings that match Object properties" );
2221         $f.remove();
2222 } );
2224 QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function( assert ) {
2226         assert.expect( 3 );
2228         jQuery( [
2229                 "<script type='text/javascript'>",
2230                 "<!--",
2231                 "QUnit.assert.ok( true, '<!-- handled' );",
2232                 "//-->",
2233                 "</script>"
2234         ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2236         // This test requires XHTML mode as CDATA is not recognized in HTML.
2237         // jQuery( [
2238         //      "<script type='text/javascript'>",
2239         //      "<![CDATA[",
2240         //      "QUnit.assert.ok( true, '<![CDATA[ handled' );",
2241         //      "//]]>",
2242         //      "</script>"
2243         // ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2245         jQuery( [
2246                 "<script type='text/javascript'>",
2247                 "<!--//--><![CDATA[//><!--",
2248                 "QUnit.assert.ok( true, '<!--//--><![CDATA[//><!-- (Drupal case) handled' );",
2249                 "//--><!]]>",
2250                 "</script>"
2251         ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2253         // ES2015 in Annex B requires HTML-style comment delimiters (`<!--` & `-->`) to act as
2254         // single-line comment delimiters; i.e. they should be treated as `//`.
2255         // See gh-4904
2256         jQuery( [
2257                 "<script type='text/javascript'>",
2258                 "<!-- Same-line HTML comment",
2259                 "QUnit.assert.ok( true, '<!-- Same-line HTML comment' );",
2260                 "-->",
2261                 "</script>"
2262         ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2263 } );
2265 testIframe(
2266         "domManip tolerates window-valued document[0] in IE9/10 (trac-12266)",
2267         "manipulation/iframe-denied.html",
2268         function( assert, jQuery, window, document, test ) {
2269                 assert.expect( 1 );
2270                 assert.ok( test.status, test.description );
2271         }
2274 testIframe(
2275         "domManip executes scripts in iframes in the iframes' context",
2276         "manipulation/scripts-context.html",
2277         function( assert, framejQuery, frameWindow, frameDocument ) {
2278                 assert.expect( 2 );
2279                 jQuery( frameDocument.body ).append( "<script>window.scriptTest = true;<\x2fscript>" );
2280                 assert.ok( !window.scriptTest, "script executed in iframe context" );
2281                 assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2282         }
2285 testIframe(
2286         "domManip executes external scripts in iframes in the iframes' context",
2287         "manipulation/scripts-context.html",
2288         function( assert, framejQuery, frameWindow, frameDocument ) {
2289                 assert.expect( 2 );
2291                 Globals.register( "finishTest" );
2293                 return new Promise( function( resolve ) {
2294                         window.finishTest = resolve;
2295                         jQuery( frameDocument.body ).append(
2296                                 "<script src='" + url( "manipulation/set-global-scripttest.js" ) + "'></script>" );
2297                         assert.ok( !window.scriptTest, "script executed in iframe context" );
2298                         assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2299                 } );
2300         },
2302         // The AJAX module is needed for jQuery._evalUrl.
2303         QUnit[ jQuery.ajax ? "test" : "skip" ]
2307 // We need to simulate cross-domain requests with the feature that
2308 // both 127.0.0.1 and localhost point to the mock http server.
2309 // Skip the the test if we are not in localhost but make sure we run
2310 // it in Karma.
2311 QUnit[
2312         jQuery.ajax && ( window.__karma__ || location.hostname === "localhost" ) ?
2313                 "test" :
2314                 "skip"
2315 ]( "jQuery.append with crossorigin attribute", function( assert ) {
2316         assert.expect( 1 );
2318         var done = assert.async(),
2319                 timeout;
2321         Globals.register( "corsCallback" );
2322         window.corsCallback = function( response ) {
2323                 assert.ok( typeof response.headers.origin === "string", "Origin header sent" );
2324                 window.clearTimeout( timeout );
2325                 done();
2326         };
2328         var src = baseURL + "mock.php?action=script&cors=1&callback=corsCallback";
2329         src = src.replace( "localhost", "127.0.0.1" );
2330         var html = "<script type=\"text/javascript\" src=\"" + src + "\" crossorigin=\"anonymous\"><\/script>";
2332         jQuery( document.body ).append( html );
2333         timeout = window.setTimeout( function() {
2334                 assert.ok( false, "Origin header should have been sent" );
2335                 done();
2336         }, 2000 );
2337 } );
2339 QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
2341         assert.expect( 1 );
2343         try {
2344                 jQuery( "#no-clone-exception" ).clone();
2345                 assert.ok( true, "cloned with no exceptions" );
2346         } catch ( e ) {
2347                 assert.ok( false, e.message );
2348         }
2349 } );
2351 QUnit.test( "Cloned, detached HTML5 elems (#10667,10670)", function( assert ) {
2353         assert.expect( 7 );
2355         var $clone,
2356                 $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
2358         // First clone
2359         $clone = $section.clone();
2361         // This branch tests a known behavior in modern browsers that should never fail.
2362         // Included for expected test count symmetry (expecting 1)
2363         assert.equal( $clone[ 0 ].nodeName, "SECTION", "detached clone nodeName matches 'SECTION'" );
2365         // Bind an event
2366         $section.on( "click", function() {
2367                 assert.ok( true, "clone fired event" );
2368         } );
2370         // Second clone (will have an event bound)
2371         $clone = $section.clone( true );
2373         // Trigger an event from the first clone
2374         $clone.trigger( "click" );
2375         $clone.off( "click" );
2377         // Add a child node with text to the original
2378         $section.append( "<p>Hello</p>" );
2380         // Third clone (will have child node and text)
2381         $clone = $section.clone( true );
2383         assert.equal( $clone.find( "p" ).text(), "Hello", "Assert text in child of clone" );
2385         // Trigger an event from the third clone
2386         $clone.trigger( "click" );
2387         $clone.off( "click" );
2389         // Add attributes to copy
2390         $section.attr( {
2391                 "class": "foo bar baz",
2392                 "title": "This is a title"
2393         } );
2395         // Fourth clone (will have newly added attributes)
2396         $clone = $section.clone( true );
2398         assert.equal( $clone.attr( "class" ), $section.attr( "class" ), "clone and element have same class attribute" );
2399         assert.equal( $clone.attr( "title" ), $section.attr( "title" ), "clone and element have same title attribute" );
2401         // Remove the original
2402         $section.remove();
2404         // Clone the clone
2405         $section = $clone.clone( true );
2407         // Remove the clone
2408         $clone.remove();
2410         // Trigger an event from the clone of the clone
2411         $section.trigger( "click" );
2413         // Unbind any remaining events
2414         $section.off( "click" );
2415         $clone.off( "click" );
2416 } );
2418 QUnit.test( "Guard against exceptions when clearing safeChildNodes", function( assert ) {
2420         assert.expect( 1 );
2422         var div;
2424         try {
2425                 div = jQuery( "<div></div><hr/><code></code><b></b>" );
2426         } catch ( e ) {}
2428         assert.ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );
2429 } );
2431 QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( assert ) {
2433         assert.expect( 5 );
2435         assert.strictEqual( jQuery( "<div></div>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.clone" );
2436         assert.strictEqual( jQuery( "<div></div>" ).find( "p" ).end().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.fn.find" );
2437         assert.strictEqual( jQuery( "<div></div>" ).text( "test" ).addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.fn.text" );
2438         assert.strictEqual( jQuery( "<bdi></bdi>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after clone html5 element" );
2439         assert.strictEqual( jQuery( "<p></p>" ).appendTo( "<div></div>" ).end().length, jQuery( "<p>test</p>" ).appendTo( "<div></div>" ).end().length, "Elements created with createElement and with createDocumentFragment should be treated alike" );
2440 } );
2442 QUnit.test( "html() - script exceptions bubble (#11743)", function( assert ) {
2443         assert.expect( 2 );
2444         var done = assert.async(),
2445                 onerror = window.onerror;
2447         setTimeout( function() {
2448                 window.onerror = onerror;
2450                 done();
2451         }, 1000 );
2453         window.onerror = function() {
2454                 assert.ok( true, "Exception thrown" );
2456                 if ( jQuery.ajax ) {
2457                         window.onerror = function() {
2458                                 assert.ok( true, "Exception thrown in remote script" );
2459                         };
2461                         jQuery( "#qunit-fixture" ).html( "<script src='" + baseURL + "badcall.js'></script>" );
2462                         assert.ok( true, "Exception ignored" );
2463                 } else {
2464                         assert.ok( true, "No jQuery.ajax" );
2465                 }
2466         };
2468         jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
2469 } );
2471 QUnit.test( "checked state is cloned with clone()", function( assert ) {
2473         assert.expect( 2 );
2475         var elem = jQuery.parseHTML( "<input type='checkbox' checked='checked'/>" )[ 0 ];
2476         elem.checked = false;
2477         assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, false, "Checked false state correctly cloned" );
2479         elem = jQuery.parseHTML( "<input type='checkbox'/>" )[ 0 ];
2480         elem.checked = true;
2481         assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, true, "Checked true state correctly cloned" );
2482 } );
2484 QUnit.test( "manipulate mixed jQuery and text (#12384, #12346)", function( assert ) {
2486         assert.expect( 2 );
2488         var div = jQuery( "<div>a</div>" ).append( "&nbsp;", jQuery( "<span>b</span>" ), "&nbsp;", jQuery( "<span>c</span>" ) ),
2489                 nbsp = String.fromCharCode( 160 );
2491         assert.equal( div.text(), "a" + nbsp + "b" + nbsp + "c", "Appending mixed jQuery with text nodes" );
2493         div = jQuery( "<div><div></div></div>" )
2494                 .find( "div" )
2495                 .after( "<p>a</p>", "<p>b</p>" )
2496                 .parent();
2497         assert.equal( div.find( "*" ).length, 3, "added 2 paragraphs after inner div" );
2498 } );
2500 QUnit.test( "script evaluation (#11795)", function( assert ) {
2502         assert.expect( 13 );
2504         var scriptsIn, scriptsOut,
2505                 fixture = jQuery( "#qunit-fixture" ).empty(),
2506                 objGlobal = ( function() {
2507                         return this;
2508                 } )(),
2509                 isOk = objGlobal.ok,
2510                 notOk = function() {
2511                         var args = arguments;
2512                         args[ 0 ] = !args[ 0 ];
2513                         return isOk.apply( this, args );
2514                 };
2516         objGlobal.ok = notOk;
2517         scriptsIn = jQuery( [
2518                 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
2519                 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
2520                 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
2521                 "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
2522                 "<div>",
2523                         "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
2524                         "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
2525                         "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
2526                         "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
2527                 "</div>"
2528         ].join( "" ) );
2529         scriptsIn.appendTo( jQuery( "<div class='detached'></div>" ) );
2530         objGlobal.ok = isOk;
2532         scriptsOut = fixture.append( scriptsIn ).find( "script" );
2533         assert.equal( scriptsOut[ 0 ].type, "something/else", "Non-evaluated type." );
2534         assert.equal( scriptsOut[ 1 ].type, "text/javascript", "Evaluated type." );
2535         assert.deepEqual( scriptsOut.get(), fixture.find( "script" ).get(), "All script tags remain." );
2537         objGlobal.ok = notOk;
2538         scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find( "div" ) );
2539         assert.deepEqual( fixture.find( "div script" ).get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
2540         fixture.append( scriptsOut.detach() );
2541         assert.deepEqual( fixture.children( "script" ).get(), scriptsOut.get(), "Scripts detached without reevaluation" );
2542         objGlobal.ok = isOk;
2544         if ( jQuery.ajax ) {
2545                 Globals.register( "testBar" );
2546                 jQuery( "#qunit-fixture" ).append( "<script src='" + url( "mock.php?action=testbar" ) + "'></script>" );
2547                 assert.strictEqual( window.testBar, "bar", "Global script evaluation" );
2548         } else {
2549                 assert.ok( true, "No jQuery.ajax" );
2550                 assert.ok( true, "No jQuery.ajax" );
2551         }
2552 } );
2554 QUnit[ jQuery.ajax ? "test" : "skip" ]( "jQuery._evalUrl (#12838)", function( assert ) {
2556         assert.expect( 5 );
2558         var message, expectedArgument,
2559                 ajax = jQuery.ajax,
2560                 evalUrl = jQuery._evalUrl;
2562         message = "jQuery.ajax implementation";
2563         expectedArgument = 1;
2564         jQuery.ajax = function( input ) {
2565                 assert.equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
2566                 expectedArgument++;
2567         };
2568         jQuery( "#qunit-fixture" ).append( "<script src='1'></script><script src='2'></script>" );
2569         assert.equal( expectedArgument, 3, "synchronous execution" );
2571         message = "custom implementation";
2572         expectedArgument = 3;
2573         jQuery._evalUrl = jQuery.ajax;
2574         jQuery.ajax = function( options ) {
2575                 assert.strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
2576         };
2577         jQuery( "#qunit-fixture" ).append( "<script src='3'></script><script src='4'></script>" );
2579         jQuery.ajax = ajax;
2580         jQuery._evalUrl = evalUrl;
2581 } );
2583 QUnit.test( "jQuery.htmlPrefilter (gh-1747)", function( assert ) {
2585         assert.expect( 5 );
2587         var expectedArgument,
2588                 invocations = 0,
2589                 done = assert.async(),
2590                 htmlPrefilter = jQuery.htmlPrefilter,
2591                 fixture = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
2592                 poison = "<script>jQuery.htmlPrefilter.assert.ok( false, 'script not executed' );</script>";
2594         jQuery.htmlPrefilter = function( html ) {
2595                 invocations++;
2596                 assert.equal( html, expectedArgument, "Expected input" );
2598                 // Remove <script> and <del> elements
2599                 return htmlPrefilter.apply( this, arguments )
2600                         .replace( /<(script|del)(?=[\s>])[\w\W]*?<\/\1\s*>/ig, "" );
2601         };
2602         jQuery.htmlPrefilter.assert = assert;
2604         expectedArgument = "A-" + poison + "B-" + poison + poison + "C-";
2605         fixture.html( expectedArgument );
2607         expectedArgument = "D-" + poison + "E-" + "<del></del><div>" + poison + poison + "</div>" + "F-";
2608         fixture.append( expectedArgument );
2610         expectedArgument = poison;
2611         fixture.find( "div" ).replaceWith( expectedArgument );
2613         assert.equal( invocations, 3, "htmlPrefilter invoked for all DOM manipulations" );
2614         assert.equal( fixture.html(), "A-B-C-D-E-F-", "htmlPrefilter modified HTML" );
2616         // Allow asynchronous script execution to generate assertions
2617         setTimeout( function() {
2618                 jQuery.htmlPrefilter = htmlPrefilter;
2619                 done();
2620         }, 100 );
2621 } );
2623 QUnit.test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function( assert ) {
2625         assert.expect( 10 );
2627         var elems;
2629         jQuery.each( [
2630                 "appendTo",
2631                 "prependTo",
2632                 "insertBefore",
2633                 "insertAfter",
2634                 "replaceAll"
2635         ], function( index, name ) {
2636                 elems = jQuery( [
2637                         "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
2638                         "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
2639                         "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
2640                 ].join( "" ) ).appendTo( "#qunit-fixture" );
2642                 // complex case based on https://jsfiddle.net/pbramos/gZ7vB/
2643                 jQuery( "#test4087-complex div" )[ name ]( "#test4087-complex li:last-child div:last-child" );
2644                 assert.equal( jQuery( "#test4087-complex li:last-child div" ).length, name === "replaceAll" ? 1 : 2, name + " a node to itself, complex case." );
2646                 // simple case
2647                 jQuery( ".test4087-1" )[ name ]( ".test4087-1" );
2648                 assert.equal( jQuery( ".test4087-1" ).length, 1, name + " a node to itself, simple case." );
2650                 // clean for next test
2651                 jQuery( "#test4087-complex" ).remove();
2652                 jQuery( "#test4087-simple" ).remove();
2653                 jQuery( "#test4087-multiple" ).remove();
2654         } );
2655 } );
2657 QUnit.test( "Index for function argument should be received (#13094)", function( assert ) {
2658         assert.expect( 2 );
2660         var i = 0;
2662         jQuery( "<div></div><div></div>" ).before( function( index ) {
2663                 assert.equal( index, i++, "Index should be correct" );
2664         } );
2666 } );
2668 QUnit.test( "Make sure jQuery.fn.remove can work on elements in documentFragment", function( assert ) {
2669         assert.expect( 1 );
2671         var fragment = document.createDocumentFragment(),
2672                 div = fragment.appendChild( document.createElement( "div" ) );
2674         jQuery( div ).remove();
2676         assert.equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
2677 } );
2679 QUnit.test( "Make sure specific elements with content created correctly (#13232)", function( assert ) {
2680         assert.expect( 20 );
2682         var results = [],
2683                 args = [],
2684                 elems = {
2685                         thead: "<tr><td>thead</td></tr>",
2686                         tbody: "<tr><td>tbody</td></tr>",
2687                         tfoot: "<tr><td>tfoot</td></tr>",
2688                         colgroup: "<col span='5'></col>",
2689                         caption: "caption",
2690                         tr: "<td>tr</td>",
2691                         th: "th",
2692                         td: "<div>td</div>",
2693                         optgroup: "<option>optgroup</option>",
2694                         option: "option"
2695                 };
2697         jQuery.each( elems, function( name, value ) {
2698                 var html = "<" + name + ">" + value + "</" + name + ">";
2699                 assert.strictEqual(
2700                         jQuery.parseHTML( "<" + name + ">" + value + "</" + name + ">" )[ 0 ].nodeName.toLowerCase(),
2701                         name,
2702                         name + " is created correctly"
2703                 );
2705                 results.push( name );
2706                 args.push( html );
2707         } );
2709         jQuery.fn.append.apply( jQuery( "<div></div>" ), args ).children().each( function( i ) {
2710                 assert.strictEqual( this.nodeName.toLowerCase(), results[ i ] );
2711         } );
2712 } );
2714 QUnit.test( "Validate creation of multiple quantities of certain elements (#13818)", function( assert ) {
2715         assert.expect( 22 );
2717         var tags = [ "thead", "tbody", "tfoot", "colgroup", "col", "caption", "tr", "th", "td", "optgroup", "option" ];
2719         jQuery.each( tags, function( index, tag ) {
2720                 jQuery( "<" + tag + "></" + tag + "><" + tag + "></" + tag + ">" ).each( function() {
2721                         assert.ok( this.nodeName.toLowerCase() === tag, tag + " elements created correctly" );
2722                 } );
2723         } );
2724 } );
2726 QUnit.test( "Make sure tr element will be appended to tbody element of table when present", function( assert ) {
2727         assert.expect( 1 );
2729         var html,
2730                 table = document.createElement( "table" );
2732         table.appendChild( document.createElement( "tbody" ) );
2733         document.getElementById( "qunit-fixture" ).appendChild( table );
2735         jQuery( table ).append( "<tr><td>test</td></tr>" );
2737         // Lowercase and replace spaces to remove possible browser inconsistencies
2738         html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2740         assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2741 } );
2743 QUnit.test( "Make sure tr elements will be appended to tbody element of table when present", function( assert ) {
2744         assert.expect( 1 );
2746         var html,
2747                 table = document.createElement( "table" );
2749         table.appendChild( document.createElement( "tbody" ) );
2750         document.getElementById( "qunit-fixture" ).appendChild( table );
2752         jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
2754         // Lowercase and replace spaces to remove possible browser inconsistencies
2755         html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2757         assert.strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
2758 } );
2760 QUnit.test( "Make sure tfoot element will not be appended to tbody element of table when present", function( assert ) {
2761         assert.expect( 1 );
2763         var html,
2764                 table = document.createElement( "table" );
2766         table.appendChild( document.createElement( "tbody" ) );
2767         document.getElementById( "qunit-fixture" ).appendChild( table );
2769         jQuery( table ).append( "<tfoot></tfoot>" );
2771         // Lowercase and replace spaces to remove possible browser inconsistencies
2772         html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2774         assert.strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
2775 } );
2777 QUnit.test( "Make sure document fragment will be appended to tbody element of table when present", function( assert ) {
2778         assert.expect( 1 );
2780         var html,
2781                 fragment = document.createDocumentFragment(),
2782                 table = document.createElement( "table" ),
2783                 tr = document.createElement( "tr" ),
2784                 td = document.createElement( "td" );
2786         table.appendChild( document.createElement( "tbody" ) );
2787         document.getElementById( "qunit-fixture" ).appendChild( table );
2789         fragment.appendChild( tr );
2790         tr.appendChild( td );
2791         td.innerHTML = "test";
2793         jQuery( table ).append( fragment );
2795         // Lowercase and replace spaces to remove possible browser inconsistencies
2796         html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2798         assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2799 } );
2801 QUnit.test( "Make sure col element is appended correctly", function( assert ) {
2802         assert.expect( 1 );
2804         var table = jQuery( "<table cellpadding='0'><tr><td>test</td></tr></table>" );
2806         jQuery( table ).appendTo( "#qunit-fixture" );
2808         jQuery( "<col width='150'></col>" ).prependTo( table );
2810         assert.strictEqual( table.find( "td" ).width(), 150 );
2811 } );
2813 QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
2814         assert.expect( 1 );
2816         var htmlOut,
2817                 htmlIn =
2818                         "<thead><tr><td>" +
2819                                 "<table><tbody><tr><td>nested</td></tr></tbody></table>" +
2820                         "</td></tr></thead>",
2821                 newRow = "<tr><td>added</td></tr>",
2822                 htmlExpected = htmlIn.replace( "</thead>", "</thead>" + newRow ),
2823                 table = supportjQuery( "<table></table>" ).html( htmlIn ).appendTo( "#qunit-fixture" )[ 0 ];
2825         jQuery( table ).append( newRow );
2827         // Lowercase and replace spaces to remove possible browser inconsistencies
2828         htmlOut = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2830         assert.strictEqual( htmlOut, htmlExpected );
2831 } );
2833 QUnit.test( "Make sure tags with single-character names are found (gh-4124)", function( assert ) {
2834         assert.expect( 1 );
2836         var htmlOut,
2837                 htmlIn = "<p>foo<!--<td>--></p>",
2838                 $el = jQuery( "<div></div>" );
2840         $el.html( htmlIn );
2842         // Lowercase and replace spaces to remove possible browser inconsistencies
2843         htmlOut = $el[ 0 ].innerHTML.toLowerCase().replace( /\s/g, "" );
2845         assert.strictEqual( htmlOut, htmlIn );
2846 } );
2848 // The AJAX module is needed for jQuery._evalUrl.
2849 QUnit[ jQuery.ajax ? "test" : "skip" ]( "Insert script with data-URI (gh-1887)", function( assert ) {
2850         assert.expect( 1 );
2852         Globals.register( "testFoo" );
2853         Globals.register( "testSrcFoo" );
2855         var script = document.createElement( "script" ),
2856                 fixture = document.getElementById( "qunit-fixture" ),
2857                 done = assert.async();
2859         script.src = "data:text/javascript,testSrcFoo = 'foo';";
2861         fixture.appendChild( script );
2863         jQuery( fixture ).append( "<script src=\"data:text/javascript,testFoo = 'foo';\"></script>" );
2865         setTimeout( function() {
2866                 if ( window.testSrcFoo === "foo" ) {
2867                         assert.strictEqual( window.testFoo, window.testSrcFoo, "data-URI script executed" );
2869                 } else {
2870                         assert.ok( true, "data-URI script is not supported by this environment" );
2871                 }
2873                 done();
2874         }, 100 );
2875 } );
2877 QUnit.test( "Ignore content from unsuccessful responses (gh-4126)", function( assert ) {
2878         assert.expect( 1 );
2880         var globalEval = jQuery.globalEval;
2881         jQuery.globalEval = function( code ) {
2882                 assert.ok( false, "no attempt to evaluate code from an unsuccessful response" );
2883         };
2885         try {
2886                 jQuery( "#qunit-fixture" ).append(
2887                         "<script src='" + url( "mock.php?action=error" ) + "'></script>" );
2888                 assert.ok( true, "no error thrown from embedding script with unsuccessful-response src" );
2889         } catch ( e ) {
2890                 throw e;
2891         } finally {
2892                 jQuery.globalEval = globalEval;
2893         }
2894 } );
2896 testIframe(
2897         "Check if CSP nonce is preserved",
2898         "mock.php?action=cspNonce",
2899         function( assert, jQuery, window, document ) {
2900                 var done = assert.async();
2902                 assert.expect( 1 );
2904                 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
2905                         assert.equal( data, "", "No log request should be sent" );
2906                         supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
2907                 } );
2908         }
2911 testIframe(
2912         "Check if CSP nonce is preserved for external scripts with src attribute",
2913         "mock.php?action=cspNonce&test=external",
2914         function( assert, jQuery, window, document ) {
2915                 var done = assert.async();
2917                 assert.expect( 1 );
2919                 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
2920                         assert.equal( data, "", "No log request should be sent" );
2921                         supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
2922                 } );
2923         },
2925         // The AJAX module is needed for jQuery._evalUrl.
2926         QUnit[ jQuery.ajax ? "test" : "skip" ]
2929 testIframe(
2930         "jQuery.globalEval supports nonce",
2931         "mock.php?action=cspNonce&test=globaleval",
2932         function( assert, jQuery, window, document ) {
2933                 var done = assert.async();
2935                 assert.expect( 1 );
2937                 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
2938                         assert.equal( data, "", "No log request should be sent" );
2939                         supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
2940                 } );
2941         }
2944 QUnit.test( "Sanitized HTML doesn't get unsanitized", function( assert ) {
2946         var container,
2947                 counter = 0,
2948                 oldIos = /iphone os (?:8|9|10|11|12)_/i.test( navigator.userAgent ),
2949                 assertCount = oldIos ? 12 : 13,
2950                 done = assert.async( assertCount );
2952         assert.expect( assertCount );
2954         Globals.register( "xss" );
2955         window.xss = sinon.spy();
2957         container = jQuery( "<div></div>" );
2958         container.appendTo( "#qunit-fixture" );
2960         function test( htmlString ) {
2961                 var currCounter = counter,
2962                         div = jQuery( "<div></div>" );
2964                 counter++;
2966                 div.appendTo( container );
2967                 div.html( htmlString );
2969                 setTimeout( function() {
2970                         assert.ok( window.xss.withArgs( currCounter ).notCalled,
2971                                 "Insecure code wasn't executed, input: " + htmlString );
2972                         done();
2973                 }, 1000 );
2974         }
2976         // Note: below test cases need to invoke the xss function with consecutive
2977         // decimal parameters for the assertion messages to be correct.
2978         // Thanks to Masato Kinugawa from Cure53 for providing the following test cases.
2979         test( "<img alt=\"<x\" title=\"/><img src=url404 onerror=xss(0)>\">" );
2980         test( "<img alt=\"\n<x\" title=\"/>\n<img src=url404 onerror=xss(1)>\">" );
2981         test( "<style><style/><img src=url404 onerror=xss(2)>" );
2982         test( "<xmp><xmp/><img src=url404 onerror=xss(3)>" );
2983         test( "<title><title /><img src=url404 onerror=xss(4)>" );
2984         test( "<iframe><iframe/><img src=url404 onerror=xss(5)>" );
2985         test( "<noframes><noframes/><img src=url404 onerror=xss(6)>" );
2986         test( "<noscript><noscript/><img src=url404 onerror=xss(7)>" );
2987         test( "<foo\" alt=\"\" title=\"/><img src=url404 onerror=xss(8)>\">" );
2988         test( "<img alt=\"<x\" title=\"\" src=\"/><img src=url404 onerror=xss(9)>\">" );
2989         test( "<noscript/><img src=url404 onerror=xss(10)>" );
2991         test( "<option><style></option></select><img src=url404 onerror=xss(11)></style>" );
2993         // Support: iOS 8 - 12 only.
2994         // Old iOS parses `<noembed>` tags differently, executing this code. This is no
2995         // different to native behavior on that OS, though, so just accept it.
2996         if ( !oldIos ) {
2997                 test( "<noembed><noembed/><img src=url404 onerror=xss(12)>" );
2998         }
2999 } );
3001 QUnit.test( "Works with invalid attempts to close the table wrapper", function( assert ) {
3002         assert.expect( 3 );
3004         // This test case attempts to close the tags which wrap input
3005         // based on matching done in wrapMap which should be ignored.
3006         var elem = jQuery( "<td></td></tr></tbody></table><td></td>" );
3007         assert.strictEqual( elem.length, 2, "Two elements created" );
3008         assert.strictEqual( elem[ 0 ].nodeName.toLowerCase(), "td", "First element is td" );
3009         assert.strictEqual( elem[ 1 ].nodeName.toLowerCase(), "td", "Second element is td" );
3010 } );
3012 // Test trustedTypes support in browsers where they're supported (currently Chrome 83+).
3013 // Browsers with no TrustedHTML support still run tests on object wrappers with
3014 // a proper `toString` function.
3015 testIframe(
3016         "Basic TrustedHTML support (gh-4409)",
3017         "mock.php?action=trustedHtml",
3018         function( assert, jQuery, window, document, test ) {
3020                 assert.expect( 5 );
3022                 test.forEach( function( result ) {
3023                         assert.deepEqual( result.actual, result.expected, result.message );
3024                 } );
3025         }