1 QUnit.module( "manipulation", {
2 afterEach: moduleTeardown
5 // Ensure that an extended Array prototype doesn't break jQuery
6 Array.prototype.arrayProtoFn = function() {
9 function manipulationBareObj( value ) {
13 function manipulationFunctionReturningObj( value ) {
20 ======== local reference =======
21 manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
22 See testVal below for an example
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 ) {
35 var expected, frag, $newLineTest, doc;
37 expected = "This link has class=\"blog\": Timmy Willison's Weblog";
38 assert.equal( jQuery( "#sap" ).text(), expected, "Check for merged text of more than 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 (trac-10724)." );
44 // Retrieve from document fragments trac-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 (trac-11153)" );
54 $newLineTest.remove();
56 doc = new DOMParser().parseFromString( "<span>example</span>", "text/html" );
57 assert.equal( jQuery( doc ).text(), "example", "text() on HTMLDocument (gh-5264)" );
60 QUnit.test( "text(undefined)", function( assert ) {
64 assert.equal( jQuery( "#foo" ).text( "<div" ).text( undefined )[ 0 ].innerHTML, "<div", ".text(undefined) is chainable (trac-5571)" );
67 function testText( valueObj, assert ) {
71 var val, j, expected, $multipleElements, $parentDiv, $childDiv;
73 val = valueObj( "<div><b>Hello</b> cruel world!</div>" );
74 assert.equal( jQuery( "#foo" ).text( val )[ 0 ].innerHTML.replace( />/g, ">" ), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
76 // using contents will get comments regular, text, and comment nodes
77 j = jQuery( "#nonnodes" ).contents();
78 j.text( valueObj( "hi!" ) );
79 assert.equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
80 assert.equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
82 assert.equal( j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
84 // Update multiple elements trac-11809
87 $multipleElements = jQuery( "<div>Hello</div>" ).add( "<div>World</div>" );
88 $multipleElements.text( expected );
90 assert.equal( $multipleElements.eq( 0 ).text(), expected, "text() updates multiple elements (trac-11809)" );
91 assert.equal( $multipleElements.eq( 1 ).text(), expected, "text() updates multiple elements (trac-11809)" );
93 // Prevent memory leaks trac-11809
94 $childDiv = jQuery( "<div></div>" );
95 $childDiv.data( "leak", true );
96 $parentDiv = jQuery( "<div></div>" );
97 $parentDiv.append( $childDiv );
98 $parentDiv.text( "Dry off" );
101 QUnit.test( "text(String)", function( assert ) {
102 testText( manipulationBareObj, assert );
105 QUnit.test( "text(Function)", function( assert ) {
106 testText( manipulationFunctionReturningObj, assert );
109 QUnit.test( "text(Function) with incoming value", function( assert ) {
113 var old = "This link has class=\"blog\": Timmy Willison's Weblog";
115 jQuery( "#sap" ).text( function( i, val ) {
116 assert.equal( val, old, "Make sure the incoming value is correct." );
120 assert.equal( jQuery( "#sap" ).text(), "foobar", "Check for merged text of more then one element." );
123 function testAppendForObject( valueObj, isFragment, assert ) {
125 type = isFragment ? " (DocumentFragment)" : " (Element)",
126 text = "This link has class=\"blog\": Timmy Willison's Weblog",
127 el = document.getElementById( "sap" ).cloneNode( true ),
128 first = document.getElementById( "first" ),
129 yahoo = document.getElementById( "yahoo" );
132 $base = document.createDocumentFragment();
133 jQuery( el ).contents().each( function() {
134 $base.appendChild( this );
136 $base = jQuery( $base );
138 $base = jQuery( el );
141 assert.equal( $base.clone().append( valueObj( first.cloneNode( true ) ) ).text(),
142 text + "Try them out:",
143 "Check for appending of element" + type
146 assert.equal( $base.clone().append( valueObj( [ first.cloneNode( true ), yahoo.cloneNode( true ) ] ) ).text(),
147 text + "Try them out:Yahoo",
148 "Check for appending of array of elements" + type
151 assert.equal( $base.clone().append( valueObj( jQuery( "#yahoo, #first" ).clone() ) ).text(),
152 text + "YahooTry them out:",
153 "Check for appending of jQuery object" + type
156 assert.equal( $base.clone().append( valueObj( 5 ) ).text(),
158 "Check for appending a number" + type
161 assert.equal( $base.clone().append( valueObj( [ jQuery( "#first" ).clone(), jQuery( "#yahoo, #google" ).clone() ] ) ).text(),
162 text + "Try them out:GoogleYahoo",
163 "Check for appending of array of jQuery objects"
166 assert.equal( $base.clone().append( valueObj( " text with spaces " ) ).text(),
167 text + " text with spaces ",
168 "Check for appending text with spaces" + type
171 assert.equal( $base.clone().append( valueObj( [] ) ).text(),
173 "Check for appending an empty array" + type
176 assert.equal( $base.clone().append( valueObj( "" ) ).text(),
178 "Check for appending an empty string" + type
181 assert.equal( $base.clone().append( valueObj( document.getElementsByTagName( "foo" ) ) ).text(),
183 "Check for appending an empty nodelist" + type
186 assert.equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
187 $base.children().length + 3,
188 "Make sure that multiple arguments works." + type
191 assert.equal( $base.clone().append( valueObj( document.getElementById( "form" ).cloneNode( true ) ) ).children( "form" ).length,
193 "Check for appending a form (trac-910)" + type
197 function testAppend( valueObj, assert ) {
201 testAppendForObject( valueObj, false, assert );
202 testAppendForObject( valueObj, true, assert );
204 var defaultText, result, message, iframe, iframeDoc, j, d,
205 $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
207 defaultText = "Try them out:";
208 result = jQuery( "#first" ).append( valueObj( "<b>buga</b>" ) );
210 assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
211 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" );
213 jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest' type='radio' checked='checked' />" ) );
214 jQuery( "#qunit-fixture form input[name=radiotest]" ).each( function() {
215 assert.ok( jQuery( this ).is( ":checked" ), "Append checked radio" );
218 jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest2' type='radio' checked = 'checked' />" ) );
219 jQuery( "#qunit-fixture form input[name=radiotest2]" ).each( function() {
220 assert.ok( jQuery( this ).is( ":checked" ), "Append alternately formatted checked radio" );
223 jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest3' type='radio' checked />" ) );
224 jQuery( "#qunit-fixture form input[name=radiotest3]" ).each( function() {
225 assert.ok( jQuery( this ).is( ":checked" ), "Append HTML5-formatted checked radio" );
228 jQuery( "#qunit-fixture form" ).append( valueObj( "<input type='radio' checked='checked' name='radiotest4' />" ) );
229 jQuery( "#qunit-fixture form input[name=radiotest4]" ).each( function() {
230 assert.ok( jQuery( this ).is( ":checked" ), "Append with name attribute after checked attribute" );
233 message = "Test for appending a DOM node to the contents of an iframe";
234 iframe = jQuery( "#iframe" )[ 0 ];
235 iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
238 if ( iframeDoc && iframeDoc.body ) {
239 assert.equal( jQuery( iframeDoc.body ).append( valueObj( "<div id='success'>test</div>" ) )[ 0 ].lastChild.id, "success", message );
241 assert.ok( true, message + " - can't test" );
244 assert.strictEqual( e.message || e, undefined, message );
247 jQuery( "<fieldset></fieldset>" ).appendTo( "#form" ).append( valueObj( "<legend id='legend'>test</legend>" ) );
248 assert.t( "Append legend", "#legend", [ "legend" ] );
250 $map = jQuery( "<map></map>" ).append( valueObj( "<area id='map01' shape='rect' coords='50,50,150,150' href='https://www.jquery.com/' alt='jQuery'>" ) );
252 assert.equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
253 assert.equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
255 jQuery( "#select1" ).append( valueObj( "<OPTION>Test</OPTION>" ) );
256 assert.equal( jQuery( "#select1 option:last-child" ).text(), "Test", "Appending OPTION (all caps)" );
258 jQuery( "#select1" ).append( valueObj( "<optgroup label='optgroup'><option>optgroup</option></optgroup>" ) );
259 assert.equal( jQuery( "#select1 optgroup" ).attr( "label" ), "optgroup", "Label attribute in newly inserted optgroup is correct" );
260 assert.equal( jQuery( "#select1 option" ).last().text(), "optgroup", "Appending optgroup" );
262 $table = jQuery( "#table" );
264 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
265 $table.append( valueObj( "<" + name + "/>" ) );
266 assert.equal( $table.find( name ).length, 1, "Append " + name );
267 assert.ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
270 jQuery( "#table colgroup" ).append( valueObj( "<col></col>" ) );
271 assert.equal( jQuery( "#table colgroup col" ).length, 1, "Append col" );
274 .append( valueObj( "<select id='appendSelect1'></select>" ) )
275 .append( valueObj( "<select id='appendSelect2'><option>Test</option></select>" ) );
276 assert.t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
278 assert.equal( "Two nodes", jQuery( "<div></div>" ).append( "Two", " nodes" ).text(), "Appending two text nodes (trac-4011)" );
279 assert.equal( jQuery( "<div></div>" ).append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
281 // using contents will get comments regular, text, and comment nodes
282 j = jQuery( "#nonnodes" ).contents();
283 d = jQuery( "<div></div>" ).appendTo( "#nonnodes" ).append( j );
285 assert.equal( jQuery( "#nonnodes" ).length, 1, "Check node,textnode,comment append moved leaving just the div" );
286 assert.equal( d.contents().length, 3, "Check node,textnode,comment append works" );
287 d.contents().appendTo( "#nonnodes" );
289 assert.equal( jQuery( "#nonnodes" ).contents().length, 3, "Check node,textnode,comment append cleanup worked" );
291 $input = jQuery( "<input type='checkbox'/>" ).prop( "checked", true ).appendTo( "#testForm" );
292 assert.equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
294 $radioChecked = jQuery( "input[type='radio'][name='R1']" ).eq( 1 );
295 $radioParent = $radioChecked.parent();
296 $radioUnchecked = jQuery( "<input type='radio' name='R1' checked='checked'/>" ).appendTo( $radioParent );
297 $radioChecked.trigger( "click" );
298 $radioUnchecked[ 0 ].checked = false;
300 jQuery( "<div></div>" ).insertBefore( $radioParent ).append( $radioParent );
302 assert.equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
303 assert.equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
305 assert.equal( jQuery( "<div></div>" ).append( valueObj( "option<area></area>" ) )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
308 QUnit.test( "append(String|Element|Array<Element>|jQuery)", function( assert ) {
309 testAppend( manipulationBareObj, assert );
312 QUnit.test( "append(Function)", function( assert ) {
313 testAppend( manipulationFunctionReturningObj, assert );
316 QUnit.test( "append(param) to object, see trac-11280", function( assert ) {
320 var object = jQuery( document.createElement( "object" ) ).appendTo( document.body );
322 assert.equal( object.children().length, 0, "object does not start with children" );
324 object.append( jQuery( "<param type='wmode' name='foo'>" ) );
325 assert.equal( object.children().length, 1, "appended param" );
326 assert.equal( object.children().eq( 0 ).attr( "name" ), "foo", "param has name=foo" );
328 object = jQuery( "<object><param type='baz' name='bar'></object>" );
329 assert.equal( object.children().length, 1, "object created with child param" );
330 assert.equal( object.children().eq( 0 ).attr( "name" ), "bar", "param has name=bar" );
333 QUnit.test( "append(Function) returns String", function( assert ) {
337 var defaultText, result, select, old;
339 defaultText = "Try them out:";
340 old = jQuery( "#first" ).html();
342 result = jQuery( "#first" ).append( function( i, val ) {
343 assert.equal( val, old, "Make sure the incoming value is correct." );
344 return "<b>buga</b>";
346 assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
348 select = jQuery( "#select3" );
351 assert.equal( select.append( function( i, val ) {
352 assert.equal( val, old, "Make sure the incoming value is correct." );
353 return "<option value='appendTest'>Append Test</option>";
354 } ).find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
357 QUnit.test( "append(Function) returns Element", function( assert ) {
360 var expected = "This link has class=\"blog\": Timmy Willison's WeblogTry them out:",
361 old = jQuery( "#sap" ).html();
363 jQuery( "#sap" ).append( function( i, val ) {
364 assert.equal( val, old, "Make sure the incoming value is correct." );
365 return document.getElementById( "first" );
367 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
370 QUnit.test( "append(Function) returns Array<Element>", function( assert ) {
373 var expected = "This link has class=\"blog\": Timmy Willison's WeblogTry them out:Yahoo",
374 old = jQuery( "#sap" ).html();
376 jQuery( "#sap" ).append( function( i, val ) {
377 assert.equal( val, old, "Make sure the incoming value is correct." );
378 return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
380 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
383 QUnit.test( "append(Function) returns jQuery", function( assert ) {
386 var expected = "This link has class=\"blog\": Timmy Willison's WeblogYahooTry them out:",
387 old = jQuery( "#sap" ).html();
389 jQuery( "#sap" ).append( function( i, val ) {
390 assert.equal( val, old, "Make sure the incoming value is correct." );
391 return jQuery( "#yahoo, #first" );
393 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
396 QUnit.test( "append(Function) returns Number", function( assert ) {
399 var old = jQuery( "#sap" ).html();
401 jQuery( "#sap" ).append( function( i, val ) {
402 assert.equal( val, old, "Make sure the incoming value is correct." );
405 assert.ok( jQuery( "#sap" )[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
408 QUnit.test( "XML DOM manipulation (trac-9960)", function( assert ) {
412 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>" ),
413 xmlDoc2 = jQuery.parseXML( "<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>" ),
414 xml1 = jQuery( xmlDoc1 ),
415 xml2 = jQuery( xmlDoc2 ),
416 scxml1 = jQuery( "scxml", xml1 ),
417 scxml2 = jQuery( "scxml", xml2 ),
418 state = scxml2.find( "state" );
420 scxml1.append( state );
421 assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "append" );
423 scxml1.prepend( state );
424 assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "prepend" );
426 scxml1.find( "#finished" ).after( state );
427 assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "after" );
429 scxml1.find( "#provisioning" ).before( state );
430 assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "before" );
432 scxml2.replaceWith( scxml1 );
433 assert.deepEqual( jQuery( "state", xml2 ).get(), scxml1.find( "state" ).get(), "replaceWith" );
436 QUnit.test( "append HTML5 sectioning elements (Bug trac-6485)", function( assert ) {
442 jQuery( "#qunit-fixture" ).append( "<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>" );
444 article = jQuery( "article" );
445 aside = jQuery( "aside" );
447 assert.equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
448 assert.equal( aside.length, 1, "HTML5 elements do not collapse their children" );
451 QUnit[ includesModule( "css" ) ? "test" : "skip" ]( "HTML5 Elements inherit styles from style rules (Bug trac-10501)", function( assert ) {
455 jQuery( "#qunit-fixture" ).append( "<article id='article'></article>" );
456 jQuery( "#article" ).append( "<section>This section should have a pink background.</section>" );
458 // In IE, the missing background color will claim its value is "transparent"
459 assert.notEqual( jQuery( "section" ).css( "background-color" ), "transparent", "HTML5 elements inherit styles" );
462 QUnit.test( "html(String) with HTML5 (Bug trac-6485)", function( assert ) {
466 jQuery( "#qunit-fixture" ).html( "<article><section><aside>HTML5 elements</aside></section></article>" );
467 assert.equal( jQuery( "#qunit-fixture" ).children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
468 assert.equal( jQuery( "#qunit-fixture" ).children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
471 QUnit.test( "html(String) tag-hyphenated elements (Bug gh-1987)", function( assert ) {
475 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
476 var j = jQuery( "<" + name + "-d></" + name + "-d><" + name + "-d></" + name + "-d>" );
477 assert.ok( j[ 0 ], "Create a tag-hyphenated element" );
478 assert.ok( j[ 0 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
479 assert.ok( j[ 1 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
482 var j = jQuery( "<tr-multiple-hyphens><td-with-hyphen>text</td-with-hyphen></tr-multiple-hyphens>" );
483 assert.ok( j[ 0 ].nodeName === "TR-MULTIPLE-HYPHENS", "Tags with multiple hyphens" );
484 assert.ok( j.children()[ 0 ].nodeName === "TD-WITH-HYPHEN", "Tags with multiple hyphens" );
485 assert.equal( j.children().text(), "text", "Tags with multiple hyphens behave normally" );
488 QUnit.test( "Tag name processing respects the HTML Standard (gh-2005)", function( assert ) {
490 assert.expect( 240 );
492 var wrapper = jQuery( "<div></div>" ),
493 nameTerminatingChars = "\x20\t\r\n\f".split( "" ),
494 specialChars = "[ ] { } _ - = + \\ ( ) * & ^ % $ # @ ! ~ ` ' ; ? ¥ « µ λ ⊕ ≈ ξ ℜ ♣ €"
497 specialChars.push( specialChars.join( "" ) );
499 jQuery.each( specialChars, function( i, characters ) {
500 assertSpecialCharsSupport( "html", characters );
501 assertSpecialCharsSupport( "append", characters );
504 jQuery.each( nameTerminatingChars, function( i, character ) {
505 assertNameTerminatingCharsHandling( "html", character );
506 assertNameTerminatingCharsHandling( "append", character );
509 function buildChild( method, html ) {
510 wrapper[ method ]( html );
511 return wrapper.children()[ 0 ];
514 function assertSpecialCharsSupport( method, characters ) {
516 codepoint = characters.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
517 description = characters.length === 1 ?
518 "U+" + ( "000" + codepoint ).slice( -4 ) + " " + characters :
519 "all special characters",
520 nodeName = "valid" + characters + "tagname";
522 child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
523 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
524 method + "(): Paired tag name includes " + description );
526 child = buildChild( method, "<" + nodeName + ">" );
527 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
528 method + "(): Unpaired tag name includes " + description );
530 child = buildChild( method, "<" + nodeName + "/>" );
531 assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
532 method + "(): Self-closing tag name includes " + description );
535 function assertNameTerminatingCharsHandling( method, character ) {
537 codepoint = character.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
538 description = "U+" + ( "000" + codepoint ).slice( -4 ) + " " + character,
539 nodeName = "div" + character + "this-will-be-discarded";
541 child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
542 assert.equal( child.nodeName.toUpperCase(), "DIV",
543 method + "(): Paired tag name terminated by " + description );
545 child = buildChild( method, "<" + nodeName + ">" );
546 assert.equal( child.nodeName.toUpperCase(), "DIV",
547 method + "(): Unpaired open tag name terminated by " + description );
549 child = buildChild( method, "<" + nodeName + "/>" );
550 assert.equal( child.nodeName.toUpperCase(), "DIV",
551 method + "(): Self-closing tag name terminated by " + description );
555 QUnit.test( "IE8 serialization bug", function( assert ) {
558 var wrapper = jQuery( "<div></div>" );
560 wrapper.html( "<div></div><article></article>" );
561 assert.equal( wrapper.children( "article" ).length, 1, "HTML5 elements are insertable with .html()" );
563 wrapper.html( "<div></div><link></link>" );
564 assert.equal( wrapper.children( "link" ).length, 1, "Link elements are insertable with .html()" );
567 QUnit.test( "html() object element trac-10324", function( assert ) {
571 var object = jQuery( "<object id='object2'><param name='object2test' value='test'></param></object>?" ).appendTo( "#qunit-fixture" ),
572 clone = object.clone();
574 assert.equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
577 QUnit.test( "append(xml)", function( assert ) {
581 var xmlDoc, xml1, xml2;
583 function createXMLDoc() {
584 return document.implementation.createDocument( "", "", null );
587 xmlDoc = createXMLDoc();
588 xml1 = xmlDoc.createElement( "head" );
589 xml2 = xmlDoc.createElement( "test" );
591 assert.ok( jQuery( xml1 ).append( xml2 ), "Append an xml element to another without raising an exception." );
595 QUnit.test( "appendTo(String)", function( assert ) {
601 defaultText = "Try them out:";
602 jQuery( "<b>buga</b>" ).appendTo( "#first" );
603 assert.equal( jQuery( "#first" ).text(), defaultText + "buga", "Check if text appending works" );
604 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" );
606 l = jQuery( "#first" ).children().length + 2;
607 jQuery( "<strong>test</strong>" );
608 jQuery( "<strong>test</strong>" );
609 jQuery( [ jQuery( "<strong>test</strong>" )[ 0 ], jQuery( "<strong>test</strong>" )[ 0 ] ] )
610 .appendTo( "#first" );
611 assert.equal( jQuery( "#first" ).children().length, l, "Make sure the elements were inserted." );
612 assert.equal( jQuery( "#first" ).children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
615 QUnit.test( "appendTo(Element|Array<Element>)", function( assert ) {
619 var expected = "This link has class=\"blog\": Timmy Willison's WeblogTry them out:";
620 jQuery( document.getElementById( "first" ) ).appendTo( "#sap" );
621 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
623 expected = "This link has class=\"blog\": Timmy Willison's WeblogTry them out:Yahoo";
624 jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).appendTo( "#sap" );
625 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
629 QUnit.test( "appendTo(jQuery)", function( assert ) {
633 var expected, num, div;
634 assert.ok( jQuery( document.createElement( "script" ) ).appendTo( "body" ).length, "Make sure a disconnected script can be appended." );
636 expected = "This link has class=\"blog\": Timmy Willison's WeblogYahooTry them out:";
637 jQuery( "#yahoo, #first" ).appendTo( "#sap" );
638 assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
640 jQuery( "#select1" ).appendTo( "#foo" );
641 assert.t( "Append select", "#foo select", [ "select1" ] );
643 div = jQuery( "<div></div>" ).on( "click", function() {
644 assert.ok( true, "Running a cloned click." );
646 div.appendTo( "#qunit-fixture, #moretests" );
648 jQuery( "#qunit-fixture div" ).last().trigger( "click" );
649 jQuery( "#moretests div" ).last().trigger( "click" );
651 div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture, #moretests" );
653 assert.equal( div.length, 2, "appendTo returns the inserted elements" );
655 div.addClass( "test" );
657 assert.ok( jQuery( "#qunit-fixture div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
658 assert.ok( jQuery( "#moretests div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
660 div = jQuery( "<div></div>" );
661 jQuery( "<span>a</span><b>b</b>" ).filter( "span" ).appendTo( div );
663 assert.equal( div.children().length, 1, "Make sure the right number of children were inserted." );
665 div = jQuery( "#moretests div" );
667 num = jQuery( "#qunit-fixture div" ).length;
668 div.remove().appendTo( "#qunit-fixture" );
670 assert.equal( jQuery( "#qunit-fixture div" ).length, num, "Make sure all the removed divs were inserted." );
673 QUnit.test( "prepend(String)", function( assert ) {
677 var result, expected;
678 expected = "Try them out:";
679 result = jQuery( "#first" ).prepend( "<b>buga</b>" );
680 assert.equal( result.text(), "buga" + expected, "Check if text prepending works" );
681 assert.equal( jQuery( "#select3" ).prepend( "<option value='prependTest'>Prepend Test</option>" ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
684 QUnit.test( "prepend(Element)", function( assert ) {
689 expected = "Try them out:This link has class=\"blog\": Timmy Willison's Weblog";
690 jQuery( "#sap" ).prepend( document.getElementById( "first" ) );
691 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
694 QUnit.test( "prepend(Array<Element>)", function( assert ) {
699 expected = "Try them out:YahooThis link has class=\"blog\": Timmy Willison's Weblog";
700 jQuery( "#sap" ).prepend( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] );
701 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
704 QUnit.test( "prepend(jQuery)", function( assert ) {
709 expected = "YahooTry them out:This link has class=\"blog\": Timmy Willison's Weblog";
710 jQuery( "#sap" ).prepend( jQuery( "#yahoo, #first" ) );
711 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
714 QUnit.test( "prepend(Array<jQuery>)", function( assert ) {
719 expected = "Try them out:GoogleYahooThis link has class=\"blog\": Timmy Willison's Weblog";
720 jQuery( "#sap" ).prepend( [ jQuery( "#first" ), jQuery( "#yahoo, #google" ) ] );
721 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of jQuery objects" );
724 QUnit.test( "prepend(Function) with incoming value -- String", function( assert ) {
728 var defaultText, old, result;
730 defaultText = "Try them out:";
731 old = jQuery( "#first" ).html();
732 result = jQuery( "#first" ).prepend( function( i, val ) {
733 assert.equal( val, old, "Make sure the incoming value is correct." );
734 return "<b>buga</b>";
737 assert.equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
739 old = jQuery( "#select3" ).html();
741 assert.equal( jQuery( "#select3" ).prepend( function( i, val ) {
742 assert.equal( val, old, "Make sure the incoming value is correct." );
743 return "<option value='prependTest'>Prepend Test</option>";
744 } ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
747 QUnit.test( "prepend(Function) with incoming value -- Element", function( assert ) {
752 expected = "Try them out:This link has class=\"blog\": Timmy Willison's Weblog";
753 old = jQuery( "#sap" ).html();
755 jQuery( "#sap" ).prepend( function( i, val ) {
756 assert.equal( val, old, "Make sure the incoming value is correct." );
757 return document.getElementById( "first" );
760 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
763 QUnit.test( "prepend(Function) with incoming value -- Array<Element>", function( assert ) {
768 expected = "Try them out:YahooThis link has class=\"blog\": Timmy Willison's Weblog";
769 old = jQuery( "#sap" ).html();
771 jQuery( "#sap" ).prepend( function( i, val ) {
772 assert.equal( val, old, "Make sure the incoming value is correct." );
773 return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
776 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
779 QUnit.test( "prepend(Function) with incoming value -- jQuery", function( assert ) {
784 expected = "YahooTry them out:This link has class=\"blog\": Timmy Willison's Weblog";
785 old = jQuery( "#sap" ).html();
787 jQuery( "#sap" ).prepend( function( i, val ) {
788 assert.equal( val, old, "Make sure the incoming value is correct." );
789 return jQuery( "#yahoo, #first" );
792 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
795 QUnit.test( "prependTo(String)", function( assert ) {
801 defaultText = "Try them out:";
802 jQuery( "<b>buga</b>" ).prependTo( "#first" );
803 assert.equal( jQuery( "#first" ).text(), "buga" + defaultText, "Check if text prepending works" );
804 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" );
808 QUnit.test( "prependTo(Element)", function( assert ) {
814 expected = "Try them out:This link has class=\"blog\": Timmy Willison's Weblog";
815 jQuery( document.getElementById( "first" ) ).prependTo( "#sap" );
816 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
819 QUnit.test( "prependTo(Array<Element>)", function( assert ) {
825 expected = "Try them out:YahooThis link has class=\"blog\": Timmy Willison's Weblog";
826 jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).prependTo( "#sap" );
827 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
830 QUnit.test( "prependTo(jQuery)", function( assert ) {
836 expected = "YahooTry them out:This link has class=\"blog\": Timmy Willison's Weblog";
837 jQuery( "#yahoo, #first" ).prependTo( "#sap" );
838 assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
841 QUnit.test( "prependTo(Array<jQuery>)", function( assert ) {
845 jQuery( "<select id='prependSelect1'></select>" ).prependTo( "#form" );
846 jQuery( "<select id='prependSelect2'><option>Test</option></select>" ).prependTo( "#form" );
848 assert.t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
851 QUnit.test( "before(String)", function( assert ) {
857 expected = "This is a normal link: bugaYahoo";
858 jQuery( "#yahoo" ).before( manipulationBareObj( "<b>buga</b>" ) );
859 assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
862 QUnit.test( "before(Element)", function( assert ) {
868 expected = "This is a normal link: Try them out:Yahoo";
869 jQuery( "#yahoo" ).before( manipulationBareObj( document.getElementById( "first" ) ) );
870 assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
873 QUnit.test( "before(Array<Element>)", function( assert ) {
878 expected = "This is a normal link: Try them out:mozillaYahoo";
879 jQuery( "#yahoo" ).before( manipulationBareObj( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ) );
880 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
883 QUnit.test( "before(jQuery)", function( assert ) {
888 expected = "This is a normal link: mozillaTry them out:Yahoo";
889 jQuery( "#yahoo" ).before( manipulationBareObj( jQuery( "#mozilla, #first" ) ) );
890 assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
893 QUnit.test( "before(Array<jQuery>)", function( assert ) {
898 expected = "This is a normal link: Try them out:GooglemozillaYahoo";
899 jQuery( "#yahoo" ).before( manipulationBareObj( [ jQuery( "#first" ), jQuery( "#mozilla, #google" ) ] ) );
900 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
903 QUnit.test( "before(Function) -- Returns String", function( assert ) {
909 expected = "This is a normal link: bugaYahoo";
910 jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( "<b>buga</b>" ) );
911 assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
914 QUnit.test( "before(Function) -- Returns Element", function( assert ) {
920 expected = "This is a normal link: Try them out:Yahoo";
921 jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( document.getElementById( "first" ) ) );
922 assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
925 QUnit.test( "before(Function) -- Returns Array<Element>", function( assert ) {
930 expected = "This is a normal link: Try them out:mozillaYahoo";
931 jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ) );
932 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
935 QUnit.test( "before(Function) -- Returns jQuery", function( assert ) {
940 expected = "This is a normal link: mozillaTry them out:Yahoo";
941 jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( jQuery( "#mozilla, #first" ) ) );
942 assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
945 QUnit.test( "before(Function) -- Returns Array<jQuery>", function( assert ) {
950 expected = "This is a normal link: Try them out:GooglemozillaYahoo";
951 jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ jQuery( "#first" ), jQuery( "#mozilla, #google" ) ] ) );
952 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
955 QUnit.test( "before(no-op)", function( assert ) {
960 set = jQuery( "<div></div>" ).before( "<span>test</span>" );
961 assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
962 assert.equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
965 QUnit.test( "before and after w/ empty object (trac-10812)", function( assert ) {
971 res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
972 assert.equal( res.length, 0, "didn't choke on empty object" );
975 QUnit.test( ".before() and .after() disconnected node", function( assert ) {
979 assert.equal( jQuery( "<input type='checkbox'/>" ).before( "<div></div>" ).length, 1, "before() on disconnected node is no-op" );
980 assert.equal( jQuery( "<input type='checkbox'/>" ).after( "<div></div>" ).length, 1, "after() on disconnected node is no-op" );
983 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
987 var expectedBefore = "This is a normal link: bugaYahoo";
989 jQuery( "#yahoo" ).add( "<span></span>" ).before( "<b>buga</b>" );
990 assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node last" );
993 QUnit.test( "insert with .before() on disconnected node first", function( assert ) {
997 var expectedBefore = "This is a normal link: bugaYahoo";
999 jQuery( "<span></span>" ).add( "#yahoo" ).before( "<b>buga</b>" );
1000 assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node first" );
1003 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
1007 var expectedAfter = "This is a normal link: Yahoobuga";
1009 jQuery( "#yahoo" ).add( "<span></span>" ).after( "<b>buga</b>" );
1010 assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node last" );
1013 QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
1017 var expectedAfter = "This is a normal link: Yahoobuga";
1019 jQuery( "<span></span>" ).add( "#yahoo" ).after( "<b>buga</b>" );
1020 assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node first" );
1023 QUnit.test( "insertBefore(String)", function( assert ) {
1027 var expected = "This is a normal link: bugaYahoo";
1028 jQuery( "<b>buga</b>" ).insertBefore( "#yahoo" );
1029 assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
1032 QUnit.test( "insertBefore(Element)", function( assert ) {
1036 var expected = "This is a normal link: Try them out:Yahoo";
1037 jQuery( document.getElementById( "first" ) ).insertBefore( "#yahoo" );
1038 assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
1041 QUnit.test( "insertBefore(Array<Element>)", function( assert ) {
1045 var expected = "This is a normal link: Try them out:mozillaYahoo";
1046 jQuery( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ).insertBefore( "#yahoo" );
1047 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
1050 QUnit.test( "insertBefore(jQuery)", function( assert ) {
1054 var expected = "This is a normal link: mozillaTry them out:Yahoo";
1055 jQuery( "#mozilla, #first" ).insertBefore( "#yahoo" );
1056 assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
1059 QUnit.test( ".after(String)", function( assert ) {
1063 var expected = "This is a normal link: Yahoobuga";
1064 jQuery( "#yahoo" ).after( "<b>buga</b>" );
1065 assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1068 QUnit.test( ".after(Element)", function( assert ) {
1072 var expected = "This is a normal link: YahooTry them out:";
1073 jQuery( "#yahoo" ).after( document.getElementById( "first" ) );
1074 assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1077 QUnit.test( ".after(Array<Element>)", function( assert ) {
1081 var expected = "This is a normal link: YahooTry them out:mozilla";
1082 jQuery( "#yahoo" ).after( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] );
1083 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1086 QUnit.test( ".after(jQuery)", function( assert ) {
1090 var expected = "This is a normal link: YahooTry them out:Googlemozilla";
1091 jQuery( "#yahoo" ).after( [ jQuery( "#first" ), jQuery( "#mozilla, #google" ) ] );
1092 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
1095 QUnit.test( ".after(Function) returns String", function( assert ) {
1099 var expected = "This is a normal link: Yahoobuga",
1100 val = manipulationFunctionReturningObj;
1101 jQuery( "#yahoo" ).after( val( "<b>buga</b>" ) );
1102 assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1105 QUnit.test( ".after(Function) returns Element", function( assert ) {
1109 var expected = "This is a normal link: YahooTry them out:",
1110 val = manipulationFunctionReturningObj;
1111 jQuery( "#yahoo" ).after( val( document.getElementById( "first" ) ) );
1112 assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1115 QUnit.test( ".after(Function) returns Array<Element>", function( assert ) {
1119 var expected = "This is a normal link: YahooTry them out:mozilla",
1120 val = manipulationFunctionReturningObj;
1121 jQuery( "#yahoo" ).after( val( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ) );
1122 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1125 QUnit.test( ".after(Function) returns jQuery", function( assert ) {
1129 var expected = "This is a normal link: YahooTry them out:Googlemozilla",
1130 val = manipulationFunctionReturningObj;
1131 jQuery( "#yahoo" ).after( val( [ jQuery( "#first" ), jQuery( "#mozilla, #google" ) ] ) );
1132 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
1135 QUnit.test( ".after(disconnected node)", function( assert ) {
1139 var set = jQuery( "<div></div>" ).before( "<span>test</span>" );
1140 assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
1141 assert.equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
1144 QUnit.test( "insertAfter(String)", function( assert ) {
1148 var expected = "This is a normal link: Yahoobuga";
1149 jQuery( "<b>buga</b>" ).insertAfter( "#yahoo" );
1150 assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
1153 QUnit.test( "insertAfter(Element)", function( assert ) {
1157 var expected = "This is a normal link: YahooTry them out:";
1158 jQuery( document.getElementById( "first" ) ).insertAfter( "#yahoo" );
1159 assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
1162 QUnit.test( "insertAfter(Array<Element>)", function( assert ) {
1166 var expected = "This is a normal link: YahooTry them out:mozilla";
1167 jQuery( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ).insertAfter( "#yahoo" );
1168 assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
1171 QUnit.test( "insertAfter(jQuery)", function( assert ) {
1175 var expected = "This is a normal link: YahoomozillaTry them out:";
1176 jQuery( "#mozilla, #first" ).insertAfter( "#yahoo" );
1177 assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery after" );
1180 function testReplaceWith( val, assert ) {
1182 var tmp, y, child, child2, set, nonExistent, $div,
1185 assert.expect( expected );
1187 jQuery( "#yahoo" ).replaceWith( val( "<b id='replace'>buga</b>" ) );
1188 assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with element from string" );
1189 assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
1191 jQuery( "#anchor2" ).replaceWith( val( document.getElementById( "first" ) ) );
1192 assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
1193 assert.ok( !jQuery( "#anchor2" )[ 0 ], "Verify that original element is gone, after element" );
1195 jQuery( "#qunit-fixture" ).append( "<div id='bar'><div id='baz'></div></div>" );
1196 jQuery( "#baz" ).replaceWith( val( "Baz" ) );
1197 assert.equal( jQuery( "#bar" ).text(), "Baz", "Replace element with text" );
1198 assert.ok( !jQuery( "#baz" )[ 0 ], "Verify that original element is gone, after element" );
1200 jQuery( "#bar" ).replaceWith( "<div id='yahoo'></div>", "...", "<div id='baz'></div>" );
1201 assert.deepEqual( jQuery( "#yahoo, #baz" ).get(), q( "yahoo", "baz" ), "Replace element with multiple arguments (trac-13722)" );
1202 assert.strictEqual( jQuery( "#yahoo" )[ 0 ].nextSibling, jQuery( "#baz" )[ 0 ].previousSibling, "Argument order preserved" );
1203 assert.deepEqual( jQuery( "#bar" ).get(), [], "Verify that original element is gone, after multiple arguments" );
1205 jQuery( "#google" ).replaceWith( val( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ) );
1206 assert.deepEqual( jQuery( "#mozilla, #first" ).get(), q( "first", "mozilla" ), "Replace element with array of elements" );
1207 assert.ok( !jQuery( "#google" )[ 0 ], "Verify that original element is gone, after array of elements" );
1209 jQuery( "#groups" ).replaceWith( val( jQuery( "#mozilla, #first" ) ) );
1210 assert.deepEqual( jQuery( "#mozilla, #first" ).get(), q( "first", "mozilla" ), "Replace element with jQuery collection" );
1211 assert.ok( !jQuery( "#groups" )[ 0 ], "Verify that original element is gone, after jQuery collection" );
1213 jQuery( "#mozilla, #first" ).replaceWith( val( "<span class='replacement'></span><span class='replacement'></span>" ) );
1214 assert.equal( jQuery( "#qunit-fixture .replacement" ).length, 4, "Replace multiple elements (trac-12449)" );
1215 assert.deepEqual( jQuery( "#mozilla, #first" ).get(), [], "Verify that original elements are gone, after replace multiple" );
1217 tmp = jQuery( "<b>content</b>" )[ 0 ];
1218 jQuery( "#anchor1" ).contents().replaceWith( val( tmp ) );
1219 assert.deepEqual( jQuery( "#anchor1" ).contents().get(), [ tmp ], "Replace text node with element" );
1221 tmp = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1222 assert.ok( true, "Newly bound click run." );
1224 y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1225 assert.ok( false, "Previously bound click run." );
1227 child = y.append( "<b>test</b>" ).find( "b" ).on( "click", function() {
1228 assert.ok( true, "Child bound click run." );
1232 y.replaceWith( val( tmp ) );
1234 tmp.trigger( "click" );
1235 y.trigger( "click" ); // Shouldn't be run
1236 child.trigger( "click" ); // Shouldn't be run
1238 y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
1239 assert.ok( false, "Previously bound click run." );
1241 child2 = y.append( "<u>test</u>" ).find( "u" ).on( "click", function() {
1242 assert.ok( true, "Child 2 bound click run." );
1246 y.replaceWith( val( child2 ) );
1248 child2.trigger( "click" );
1250 set = jQuery( "<div></div>" ).replaceWith( val( "<span>test</span>" ) );
1251 assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "No effect on a disconnected node." );
1252 assert.equal( set.length, 1, "No effect on a disconnected node." );
1253 assert.equal( set[ 0 ].childNodes.length, 0, "No effect on a disconnected node." );
1255 child = jQuery( "#qunit-fixture" ).children().first();
1256 $div = jQuery( "<div class='pathological'></div>" ).insertBefore( child );
1257 $div.replaceWith( $div );
1258 assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
1259 "Self-replacement" );
1260 $div.replaceWith( child );
1261 assert.deepEqual( jQuery( "#qunit-fixture" ).children().first().get(), child.get(),
1262 "Replacement with following sibling (trac-13810)" );
1263 assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
1264 "Replacement with following sibling (context removed)" );
1266 nonExistent = jQuery( "#does-not-exist" ).replaceWith( val( "<b>should not throw an error</b>" ) );
1267 assert.equal( nonExistent.length, 0, "Length of non existent element." );
1269 $div = jQuery( "<div class='replacewith'></div>" ).appendTo( "#qunit-fixture" );
1270 $div.replaceWith( val( "<div class='replacewith'></div><script>" +
1271 "QUnit.assert.equal( jQuery('.replacewith').length, 1, 'Check number of elements in page.' );" +
1274 jQuery( "#qunit-fixture" ).append( "<div id='replaceWith'></div>" );
1275 assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists." );
1276 jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
1277 assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after replacement." );
1278 jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
1279 assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after subsequent replacement." );
1284 QUnit.test( "replaceWith(String|Element|Array<Element>|jQuery)", function( assert ) {
1285 testReplaceWith( manipulationBareObj, assert );
1288 QUnit.test( "replaceWith(Function)", function( assert ) {
1289 assert.expect( testReplaceWith( manipulationFunctionReturningObj, assert ) + 1 );
1291 var y = jQuery( "#foo" )[ 0 ];
1293 jQuery( y ).replaceWith( function() {
1294 assert.equal( this, y, "Make sure the context is coming in correctly." );
1298 QUnit.test( "replaceWith(string) for more than one element", function( assert ) {
1302 assert.equal( jQuery( "#foo p" ).length, 3, "ensuring that test data has not changed" );
1304 jQuery( "#foo p" ).replaceWith( "<span>bar</span>" );
1305 assert.equal( jQuery( "#foo span" ).length, 3, "verify that all the three original element have been replaced" );
1306 assert.equal( jQuery( "#foo p" ).length, 0, "verify that all the three original element have been replaced" );
1309 QUnit.test( "Empty replaceWith (trac-13401; trac-13596; gh-2204)", function( assert ) {
1311 assert.expect( 25 );
1313 var $el = jQuery( "<div></div><div></div>" ).html( "<p>0</p>" ),
1314 expectedHTML = $el.html(),
1318 "array of empty string": [ "" ],
1319 "empty collection": jQuery( "#nonexistent" ),
1321 // in case of jQuery(...).replaceWith();
1322 "undefined": undefined
1325 jQuery.each( tests, function( label, input ) {
1326 $el.html( "<a></a>" ).children().replaceWith( input );
1327 assert.strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
1328 $el.html( "<b></b>" ).children().replaceWith( function() {
1331 assert.strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
1332 $el.html( "<i></i>" ).children().replaceWith( function() {
1335 assert.strictEqual( $el.html(), "", "replaceWith(other function returning " + label + ")" );
1336 $el.html( "<p></p>" ).children().replaceWith( function( i ) {
1339 jQuery( this ).html( i + "" );
1341 assert.strictEqual( $el.eq( 0 ).html(), expectedHTML,
1342 "replaceWith(function conditionally returning context)" );
1343 assert.strictEqual( $el.eq( 1 ).html(), "",
1344 "replaceWith(function conditionally returning " + label + ")" );
1348 QUnit.test( "replaceAll(String)", function( assert ) {
1352 jQuery( "<b id='replace'>buga</b>" ).replaceAll( "#yahoo" );
1353 assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with string" );
1354 assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
1357 QUnit.test( "replaceAll(Element)", function( assert ) {
1361 jQuery( document.getElementById( "first" ) ).replaceAll( "#yahoo" );
1362 assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
1363 assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after element" );
1366 QUnit.test( "replaceAll(Array<Element>)", function( assert ) {
1370 jQuery( [ document.getElementById( "first" ), document.getElementById( "mozilla" ) ] ).replaceAll( "#yahoo" );
1371 assert.ok( jQuery( "#first" )[ 0 ], "Replace element with array of elements" );
1372 assert.ok( jQuery( "#mozilla" )[ 0 ], "Replace element with array of elements" );
1373 assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after array of elements" );
1376 QUnit.test( "replaceAll(jQuery)", function( assert ) {
1380 jQuery( "#mozilla, #first" ).replaceAll( "#yahoo" );
1381 assert.ok( jQuery( "#first" )[ 0 ], "Replace element with set of elements" );
1382 assert.ok( jQuery( "#mozilla" )[ 0 ], "Replace element with set of elements" );
1383 assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after set of elements" );
1386 QUnit.test( "jQuery.clone() (trac-8017)", function( assert ) {
1390 assert.ok( jQuery.clone && typeof jQuery.clone === "function", "jQuery.clone() utility exists and is a function." );
1392 var main = jQuery( "#qunit-fixture" )[ 0 ],
1393 clone = jQuery.clone( main );
1395 assert.equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
1398 QUnit.test( "append to multiple elements (trac-8070)", function( assert ) {
1402 var selects = jQuery( "<select class='test8070'></select><select class='test8070'></select>" ).appendTo( "#qunit-fixture" );
1403 selects.append( "<OPTION>1</OPTION><OPTION>2</OPTION>" );
1405 assert.equal( selects[ 0 ].childNodes.length, 2, "First select got two nodes" );
1406 assert.equal( selects[ 1 ].childNodes.length, 2, "Second select got two nodes" );
1409 QUnit.test( "table manipulation", function( assert ) {
1412 var table = jQuery( "<table style='font-size:16px'></table>" ).appendTo( "#qunit-fixture" ).empty(),
1413 height = table[ 0 ].offsetHeight;
1415 table.append( "<tr><td>DATA</td></tr>" );
1416 assert.ok( table[ 0 ].offsetHeight - height >= 15, "appended rows are visible" );
1419 height = table[ 0 ].offsetHeight;
1420 table.prepend( "<tr><td>DATA</td></tr>" );
1421 assert.ok( table[ 0 ].offsetHeight - height >= 15, "prepended rows are visible" );
1424 QUnit.test( "clone()", function( assert ) {
1426 assert.expect( 45 );
1428 var div, clone, form, body;
1430 assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Assert text for #en" );
1431 assert.equal( jQuery( "#first" ).append( jQuery( "#yahoo" ).clone() ).text(), "Try them out:Yahoo", "Check for clone" );
1432 assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Reassert text for #en" );
1434 jQuery.each( "table thead tbody tfoot tr td div button ul ol li select option textarea iframe".split( " " ), function( i, nodeName ) {
1435 assert.equal( jQuery( "<" + nodeName + "/>" ).clone()[ 0 ].nodeName.toLowerCase(), nodeName, "Clone a " + nodeName );
1437 assert.equal( jQuery( "<input type='checkbox' />" ).clone()[ 0 ].nodeName.toLowerCase(), "input", "Clone a <input type='checkbox' />" );
1439 // Check cloning non-elements
1440 assert.equal( jQuery( "#nonnodes" ).contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1442 // Verify that clones of clones can keep event listeners
1443 div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
1444 assert.ok( true, "Bound event still exists." );
1446 clone = div.clone( true ); div.remove();
1447 div = clone.clone( true ); clone.remove();
1449 assert.equal( div.length, 1, "One element cloned" );
1450 assert.equal( div[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1451 div.trigger( "click" );
1453 // Manually clean up detached elements
1456 // Verify that cloned children can keep event listeners
1457 div = jQuery( "<div></div>" ).append( [ document.createElement( "table" ), document.createElement( "table" ) ] );
1458 div.find( "table" ).on( "click", function() {
1459 assert.ok( true, "Bound event still exists." );
1462 clone = div.clone( true );
1463 assert.equal( clone.length, 1, "One element cloned" );
1464 assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1465 clone.find( "table" ).trigger( "click" );
1467 // Manually clean up detached elements
1471 // Make sure that doing .clone() doesn't clone event listeners
1472 div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
1473 assert.ok( false, "Bound event still exists after .clone()." );
1475 clone = div.clone();
1477 clone.trigger( "click" );
1479 // Manually clean up detached elements
1483 // Test both html() and clone() for <embed> and <object> types
1484 div = jQuery( "<div></div>" ).html( "<embed height='355' width='425' src='https://www.youtube.com/v/3KANI2dpXLw&hl=en'></embed>" );
1486 clone = div.clone( true );
1487 assert.equal( clone.length, 1, "One element cloned" );
1488 assert.equal( clone.html(), div.html(), "Element contents cloned" );
1489 assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1491 // this is technically an invalid object, but because of the special
1492 // classid instantiation it is the only kind that IE has trouble with,
1493 // so let's test with it too.
1494 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&hl=en'> <param name='wmode' value='transparent'> </object>" );
1496 clone = div.clone( true );
1497 assert.equal( clone.length, 1, "One element cloned" );
1498 assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1499 div = div.find( "object" );
1500 clone = clone.find( "object" );
1502 // oldIE adds extra attributes and <param> elements, so just test for existence of the defined set
1503 jQuery.each( [ "height", "width", "classid" ], function( i, attr ) {
1504 assert.equal( clone.attr( attr ), div.attr( attr ), "<object> attribute cloned: " + attr );
1509 clone.find( "param" ).each( function( index, param ) {
1510 params[ param.attributes.name.nodeValue.toLowerCase() ] =
1511 param.attributes.value.nodeValue.toLowerCase();
1514 div.find( "param" ).each( function( index, param ) {
1515 var key = param.attributes.name.nodeValue.toLowerCase();
1516 assert.equal( params[ key ], param.attributes.value.nodeValue.toLowerCase(), "<param> cloned: " + key );
1520 // and here's a valid one.
1521 div = jQuery( "<div></div>" ).html( "<object height='355' width='425' type='application/x-shockwave-flash' data='https://www.youtube.com/v/3KANI2dpXLw&hl=en'> <param name='movie' value='https://www.youtube.com/v/3KANI2dpXLw&hl=en'> <param name='wmode' value='transparent'> </object>" );
1523 clone = div.clone( true );
1524 assert.equal( clone.length, 1, "One element cloned" );
1525 assert.equal( clone.html(), div.html(), "Element contents cloned" );
1526 assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1528 div = jQuery( "<div></div>" ).data( { "a": true } );
1529 clone = div.clone( true );
1530 assert.equal( clone.data( "a" ), true, "Data cloned." );
1531 clone.data( "a", false );
1532 assert.equal( clone.data( "a" ), false, "Ensure cloned element data object was correctly modified" );
1533 assert.equal( div.data( "a" ), true, "Ensure cloned element data object is copied, not referenced" );
1535 // manually clean up detached elements
1539 form = document.createElement( "form" );
1540 form.action = "/test/";
1542 div = document.createElement( "div" );
1543 div.appendChild( document.createTextNode( "test" ) );
1544 form.appendChild( div );
1546 assert.equal( jQuery( form ).clone().children().length, 1, "Make sure we just get the form back." );
1548 body = jQuery( "body" ).clone();
1549 assert.equal( body.children()[ 0 ].id, "qunit", "Make sure cloning body works" );
1553 QUnit.test( "clone(script type=non-javascript) (trac-11359)", function( assert ) {
1557 var src = jQuery( "<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>" ),
1560 assert.equal( dest[ 0 ].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
1561 assert.equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
1562 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" );
1566 QUnit.test( "clone(form element) (Bug trac-3879, trac-6655)", function( assert ) {
1572 element = jQuery( "<select><option>Foo</option><option value='selected' selected>Bar</option></select>" );
1574 assert.equal( element.clone().find( "option" ).filter( function() {
1575 return this.selected;
1576 } ).val(), "selected", "Selected option cloned correctly" );
1578 element = jQuery( "<input type='checkbox' value='foo'>" ).attr( "checked", "checked" );
1579 clone = element.clone();
1581 assert.equal( clone.is( ":checked" ), element.is( ":checked" ), "Checked input cloned correctly" );
1582 assert.equal( clone[ 0 ].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
1584 element = jQuery( "<input type='text' value='foo'>" );
1585 clone = element.clone();
1586 assert.equal( clone[ 0 ].defaultValue, "foo", "Text input defaultValue cloned correctly" );
1588 element = jQuery( "<textarea>foo</textarea>" );
1589 clone = element.clone();
1590 assert.equal( clone[ 0 ].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
1593 QUnit.test( "clone(multiple selected options) (Bug trac-8129)", function( assert ) {
1597 var element = jQuery( "<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>" );
1599 function getSelectedOptions( collection ) {
1600 return collection.find( "option" ).filter( function( option ) {
1601 return option.selected;
1606 getSelectedOptions( element.clone() ).length,
1607 getSelectedOptions( element ).length,
1608 "Multiple selected options cloned correctly"
1612 QUnit.test( "clone() on XML nodes", function( assert ) {
1616 var xml = createDashboardXML(),
1617 root = jQuery( xml.documentElement ).clone(),
1618 origTab = jQuery( "tab", xml ).eq( 0 ),
1619 cloneTab = jQuery( "tab", root ).eq( 0 );
1621 origTab.text( "origval" );
1622 cloneTab.text( "cloneval" );
1623 assert.equal( origTab.text(), "origval", "Check original XML node was correctly set" );
1624 assert.equal( cloneTab.text(), "cloneval", "Check cloned XML node was correctly set" );
1627 QUnit.test( "clone() on local XML nodes with html5 nodename", function( assert ) {
1631 var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
1632 $meter = $xmlDoc.find( "meter" ).clone();
1634 assert.equal( $meter[ 0 ].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
1635 assert.equal( $meter[ 0 ].nodeType, 1, "Check if nodeType is not changed due to cloning" );
1638 QUnit.test( "html(undefined)", function( assert ) {
1642 assert.equal( jQuery( "#foo" ).html( "<i>test</i>" ).html( undefined ).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (trac-5571)" );
1645 QUnit.test( "html() on empty set", function( assert ) {
1649 assert.strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (trac-11962)" );
1652 function childNodeNames( node ) {
1653 return jQuery.map( node.childNodes, function( child ) {
1654 return child.nodeName.toUpperCase();
1658 function testHtml( valueObj, assert ) {
1659 assert.expect( 40 );
1661 var actual, expected, tmp,
1662 div = jQuery( "<div></div>" ),
1663 fixture = jQuery( "#qunit-fixture" );
1665 div.html( valueObj( "<div id='parent_1'><div id='child_1'></div></div><div id='parent_2'></div>" ) );
1666 assert.equal( div.children().length, 2, "Found children" );
1667 assert.equal( div.children().children().length, 1, "Found grandchild" );
1669 actual = []; expected = [];
1670 tmp = jQuery( "<map></map>" ).html( valueObj( "<area alt='area'></area>" ) ).each( function() {
1671 expected.push( "AREA" );
1672 actual.push( childNodeNames( this ) );
1674 assert.equal( expected.length, 1, "Expecting one parent" );
1675 assert.deepEqual( actual, expected, "Found the inserted area element" );
1677 assert.equal( div.html( valueObj( 5 ) ).html(), "5", "Setting a number as html" );
1678 assert.equal( div.html( valueObj( 0 ) ).html(), "0", "Setting a zero as html" );
1679 assert.equal( div.html( valueObj( Infinity ) ).html(), "Infinity", "Setting Infinity as html" );
1680 assert.equal( div.html( valueObj( NaN ) ).html(), "", "Setting NaN as html" );
1681 assert.equal( div.html( valueObj( 1e2 ) ).html(), "100", "Setting exponential number notation as html" );
1683 div.html( valueObj( " &" ) );
1685 div[ 0 ].innerHTML.replace( /\xA0/, " " ),
1687 "Entities are passed through correctly"
1690 tmp = "<div>hello1</div>";
1691 assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, ">" ), tmp, "Escaped html" );
1693 assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, ">" ), tmp, "Escaped html, leading x" );
1694 tmp = " " + tmp.slice( 1 );
1695 assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, ">" ), tmp, "Escaped html, leading space" );
1697 actual = []; expected = []; tmp = {};
1698 jQuery( "#nonnodes" ).contents().html( valueObj( "<b>bold</b>" ) ).each( function() {
1699 var html = jQuery( this ).html();
1700 tmp[ this.nodeType ] = true;
1701 expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
1702 actual.push( html ? html.toLowerCase() : html );
1704 assert.deepEqual( actual, expected, "Set containing element, text node, comment" );
1705 assert.ok( tmp[ 1 ], "element" );
1706 assert.ok( tmp[ 3 ], "text node" );
1707 assert.ok( tmp[ 8 ], "comment" );
1709 actual = []; expected = [];
1710 fixture.children( "div" ).html( valueObj( "<b>test</b>" ) ).each( function() {
1711 expected.push( "B" );
1712 actual.push( childNodeNames( this ) );
1714 assert.equal( expected.length, 7, "Expecting many parents" );
1715 assert.deepEqual( actual, expected, "Correct childNodes after setting HTML" );
1717 actual = []; expected = [];
1718 fixture.html( valueObj( "<style>.foobar{color:green;}</style>" ) ).each( function() {
1719 expected.push( "STYLE" );
1720 actual.push( childNodeNames( this ) );
1722 assert.equal( expected.length, 1, "Expecting one parent" );
1723 assert.deepEqual( actual, expected, "Found the inserted style element" );
1725 fixture.html( valueObj( "<select></select>" ) );
1726 jQuery( "#qunit-fixture select" ).html( valueObj( "<option>O1</option><option selected='selected'>O2</option><option>O3</option>" ) );
1727 assert.equal( jQuery( "#qunit-fixture select" ).val(), "O2", "Selected option correct" );
1731 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
1732 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
1733 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
1734 "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
1736 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
1737 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
1738 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
1739 "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
1743 assert.equal( tmp.length, 8, "All script tags remain." );
1744 assert.equal( tmp[ 0 ].type, "something/else", "Non-evaluated type." );
1745 assert.equal( tmp[ 1 ].type, "text/javascript", "Evaluated type." );
1747 fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1748 fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1749 fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
1750 fixture.html( valueObj( "foo <form><script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script (trac-975)' );</script></form>" ) );
1752 jQuery.scriptorder = 0;
1753 fixture.html( valueObj( [
1755 "QUnit.assert.equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
1756 "QUnit.assert.equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
1758 "<span id='scriptorder'><script>QUnit.assert.equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
1759 "<script>QUnit.assert.equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
1762 fixture.html( valueObj( fixture.text() ) );
1763 assert.ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
1766 QUnit.test( "html(String|Number)", function( assert ) {
1767 testHtml( manipulationBareObj, assert );
1770 QUnit.test( "html(Function)", function( assert ) {
1771 testHtml( manipulationFunctionReturningObj, assert );
1774 // Support: IE 9 - 11+
1775 // IE doesn't support modules.
1776 QUnit.testUnlessIE( "html(script type module)", function( assert ) {
1778 var done = assert.async(),
1779 $fixture = jQuery( "#qunit-fixture" );
1783 "<script type='module'>QUnit.assert.ok( true, 'evaluated: module' );</script>",
1784 "<script type='module' src='" + url( "module.js" ) + "'></script>",
1786 "<script type='module'>QUnit.assert.ok( true, 'evaluated: inner module' );</script>",
1787 "<script type='module' src='" + url( "inner_module.js" ) + "'></script>",
1792 // Allow asynchronous script execution to generate assertions
1793 setTimeout( function() {
1798 QUnit.test( "html(script nomodule)", function( assert ) {
1800 // `nomodule` scripts should be executed by legacy browsers only.
1801 assert.expect( QUnit.isIE ? 4 : 0 );
1802 var done = assert.async(),
1803 $fixture = jQuery( "#qunit-fixture" );
1807 "<script nomodule>QUnit.assert.ok( QUnit.isIE, 'evaluated: nomodule script' );</script>",
1808 "<script nomodule src='" + url( "nomodule.js" ) + "'></script>",
1810 "<script nomodule>QUnit.assert.ok( QUnit.isIE, 'evaluated: inner nomodule script' );</script>",
1811 "<script nomodule src='" + url( "inner_nomodule.js" ) + "'></script>",
1816 // Allow asynchronous script execution to generate assertions
1817 setTimeout( function() {
1822 QUnit.test( "html(self-removing script) (gh-5377)", function( assert ) {
1825 var $fixture = jQuery( "#qunit-fixture" );
1829 "<script id='gh5377-1'>",
1830 "(function removeScript() {",
1831 "var id = 'gh5377-1';",
1832 "var script = document.currentScript || document.getElementById(id);",
1833 "script.parentNode.removeChild( script );",
1834 "QUnit.assert.ok( true, 'removed document.currentScript' );",
1838 "<script id='gh5377-2'>",
1839 "(function removeInnerScript() {",
1840 "var id = 'gh5377-2';",
1841 "var innerScript = document.currentScript || document.getElementById(id);",
1842 "innerScript.parentNode.removeChild( innerScript );",
1843 "QUnit.assert.ok( true, 'removed inner document.currentScript' );",
1851 QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) {
1855 var els, actualhtml, pass;
1857 els = jQuery( "#foo > p" );
1858 actualhtml = els.map( function() {
1859 return jQuery( this ).html();
1862 els.html( function( i, val ) {
1863 assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1864 return "<b>test</b>";
1868 els.each( function() {
1869 if ( this.childNodes.length !== 1 ) {
1873 assert.ok( pass, "Set HTML" );
1876 QUnit.test( "html(Function) with incoming value -- jQuery.contents()", function( assert ) {
1878 assert.expect( 14 );
1880 var actualhtml, j, $div, $div2, insert;
1882 j = jQuery( "#nonnodes" ).contents();
1883 actualhtml = j.map( function() {
1884 return jQuery( this ).html();
1887 j.html( function( i, val ) {
1888 assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1889 return "<b>bold</b>";
1892 // Handle the case where no comment is in the document
1893 if ( j.length === 2 ) {
1894 assert.equal( null, null, "Make sure the incoming value is correct." );
1897 assert.equal( j.html().replace( / xmlns="[^"]+"/g, "" ).toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1899 $div = jQuery( "<div></div>" );
1901 assert.equal( $div.html( function( i, val ) {
1902 assert.equal( val, "", "Make sure the incoming value is correct." );
1904 } ).html(), "5", "Setting a number as html" );
1906 assert.equal( $div.html( function( i, val ) {
1907 assert.equal( val, "5", "Make sure the incoming value is correct." );
1909 } ).html(), "0", "Setting a zero as html" );
1911 $div2 = jQuery( "<div></div>" );
1912 insert = "<div>hello1</div>";
1913 assert.equal( $div2.html( function( i, val ) {
1914 assert.equal( val, "", "Make sure the incoming value is correct." );
1916 } ).html().replace( />/g, ">" ), insert, "Verify escaped insertion." );
1918 assert.equal( $div2.html( function( i, val ) {
1919 assert.equal( val.replace( />/g, ">" ), insert, "Make sure the incoming value is correct." );
1920 return "x" + insert;
1921 } ).html().replace( />/g, ">" ), "x" + insert, "Verify escaped insertion." );
1923 assert.equal( $div2.html( function( i, val ) {
1924 assert.equal( val.replace( />/g, ">" ), "x" + insert, "Make sure the incoming value is correct." );
1925 return " " + insert;
1926 } ).html().replace( />/g, ">" ), " " + insert, "Verify escaped insertion." );
1929 QUnit.test( "clone()/html() don't expose jQuery/Sizzle expandos (trac-12858)", function( assert ) {
1933 var $content = jQuery( "<div><b><i>text</i></b></div>" ).appendTo( "#qunit-fixture" ),
1934 expected = /^<b><i>text<\/i><\/b>$/i;
1936 // Attach jQuery and Sizzle data (the latter with a non-qSA nth-child)
1938 $content.find( ":nth-child(1):lt(4)" ).data( "test", true );
1940 // But don't break on a non-Sizzle build
1942 $content.find( "*" ).data( "test", true );
1945 assert.ok( expected.test( $content.clone( false )[ 0 ].innerHTML ), "clone()" );
1946 assert.ok( expected.test( $content.html() ), "html()" );
1949 QUnit.test( "remove() no filters", function( assert ) {
1953 var first = jQuery( "#ap" ).children().first();
1955 first.data( "foo", "bar" );
1957 jQuery( "#ap" ).children().remove();
1958 assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
1959 assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
1962 QUnit.test( "remove() with filters", function( assert ) {
1967 jQuery( "#ap" ).children().remove( "a" );
1968 assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
1969 assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
1971 jQuery( "#ap" ).children().remove( "a, code" );
1972 assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
1974 // Positional and relative selectors
1975 markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
1976 div = jQuery( markup );
1977 div.children().remove( "span:nth-child(2n)" );
1978 assert.equal( div.text(), "13", "relative selector in remove" );
1980 if ( QUnit.jQuerySelectorsPos ) {
1981 div = jQuery( markup );
1982 div.children().remove( "span:first" );
1983 assert.equal( div.text(), "234", "positional selector in remove" );
1984 div = jQuery( markup );
1985 div.children().remove( "span:last" );
1986 assert.equal( div.text(), "123", "positional selector in remove" );
1988 assert.ok( "skip", "Positional selectors are not supported" );
1989 assert.ok( "skip", "Positional selectors are not supported" );
1992 // using contents will get comments regular, text, and comment nodes
1993 // Handle the case where no comment is in the document
1994 assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
1995 jQuery( "#nonnodes" ).contents().remove();
1996 assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
1999 QUnit.test( "remove() event cleaning ", function( assert ) {
2002 var count, first, cleanUp;
2005 first = jQuery( "#ap" ).children().first();
2006 cleanUp = first.on( "click", function() {
2008 } ).remove().appendTo( "#qunit-fixture" ).trigger( "click" );
2010 assert.strictEqual( 0, count, "Event handler has been removed" );
2012 // Clean up detached data
2016 QUnit.test( "remove() in document order trac-13779", function( assert ) {
2020 cleanData = jQuery.cleanData;
2022 jQuery.cleanData = function( nodes ) {
2023 last = jQuery.text( nodes[ 0 ] );
2024 cleanData.call( this, nodes );
2027 jQuery( "#qunit-fixture" ).append(
2029 "<div class='removal-fixture'>1</div>" +
2030 "<div class='removal-fixture'>2</div>" +
2031 "<div class='removal-fixture'>3</div>"
2035 jQuery( ".removal-fixture" ).remove();
2037 assert.equal( last, 3, "The removal fixtures were removed in document order" );
2039 jQuery.cleanData = cleanData;
2042 QUnit.test( "detach() no filters", function( assert ) {
2046 var first = jQuery( "#ap" ).children().first();
2048 first.data( "foo", "bar" );
2050 jQuery( "#ap" ).children().detach();
2051 assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
2052 assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
2054 assert.equal( first.data( "foo" ), "bar" );
2059 QUnit.test( "detach() with filters", function( assert ) {
2064 jQuery( "#ap" ).children().detach( "a" );
2065 assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
2066 assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
2068 jQuery( "#ap" ).children().detach( "a, code" );
2069 assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
2071 // Positional and relative selectors
2072 markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
2073 div = jQuery( markup );
2074 div.children().detach( "span:nth-child(2n)" );
2075 assert.equal( div.text(), "13", "relative selector in detach" );
2077 if ( QUnit.jQuerySelectorsPos ) {
2078 div = jQuery( markup );
2079 div.children().detach( "span:first" );
2080 assert.equal( div.text(), "234", "positional selector in detach" );
2081 div = jQuery( markup );
2082 div.children().detach( "span:last" );
2083 assert.equal( div.text(), "123", "positional selector in detach" );
2085 assert.ok( "skip", "Positional selectors are not supported" );
2086 assert.ok( "skip", "Positional selectors are not supported" );
2089 // using contents will get comments regular, text, and comment nodes
2090 // Handle the case where no comment is in the document
2091 assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
2092 jQuery( "#nonnodes" ).contents().detach();
2093 assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
2096 QUnit.test( "detach() event cleaning ", function( assert ) {
2099 var count, first, cleanUp;
2102 first = jQuery( "#ap" ).children().first();
2103 cleanUp = first.on( "click", function() {
2105 } ).detach().appendTo( "#qunit-fixture" ).trigger( "click" );
2107 assert.strictEqual( 1, count, "Event handler has not been removed" );
2109 // Clean up detached data
2113 QUnit.test( "empty()", function( assert ) {
2117 assert.equal( jQuery( "#ap" ).children().empty().text().length, 0, "Check text is removed" );
2118 assert.equal( jQuery( "#ap" ).children().length, 4, "Check elements are not removed" );
2120 // using contents will get comments regular, text, and comment nodes
2121 var j = jQuery( "#nonnodes" ).contents();
2123 assert.equal( j.html(), "", "Check node,textnode,comment empty works" );
2126 QUnit.test( "jQuery.cleanData", function( assert ) {
2128 assert.expect( 14 );
2130 var type, pos, div, child;
2134 // Should trigger 4 remove event
2135 div = getDiv().remove();
2137 // Should both do nothing
2139 div.trigger( "click" );
2142 div.children().trigger( "click" );
2146 child = div.children();
2148 // Should trigger 2 remove event
2153 div.trigger( "click" );
2155 // Should do nothing
2157 child.trigger( "click" );
2165 child = div.children();
2167 // Should trigger 2 remove event
2168 div.html( "<div></div>" );
2172 div.trigger( "click" );
2174 // Should do nothing
2176 child.trigger( "click" );
2182 var div = jQuery( "<div class='outer'><div class='inner'></div></div>" ).on( "click", function() {
2183 assert.ok( true, type + " " + pos + " Click event fired." );
2184 } ).on( "focus", function() {
2185 assert.ok( true, type + " " + pos + " Focus event fired." );
2186 } ).find( "div" ).on( "click", function() {
2187 assert.ok( false, type + " " + pos + " Click event fired." );
2188 } ).on( "focus", function() {
2189 assert.ok( false, type + " " + pos + " Focus event fired." );
2190 } ).end().appendTo( "body" );
2192 div[ 0 ].detachEvent = div[ 0 ].removeEventListener = function( t ) {
2193 assert.ok( true, type + " Outer " + t + " event unbound" );
2196 div[ 0 ].firstChild.detachEvent = div[ 0 ].firstChild.removeEventListener = function( t ) {
2197 assert.ok( true, type + " Inner " + t + " event unbound" );
2204 QUnit.test( "jQuery.cleanData eliminates all private data (gh-2127)", function( assert ) {
2207 var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
2209 jQuery._data( div[ 0 ], "gh-2127", "testing" );
2211 assert.ok( !jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ), "Ensure some private data exists" );
2215 assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
2217 assert.ok( jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ),
2218 "Private data is empty after node is removed" );
2223 QUnit.test( "jQuery.cleanData eliminates all public data", function( assert ) {
2227 div = jQuery( "<div></div>" );
2228 div.data( "some", "data" );
2229 assert.ok( !jQuery.isEmptyObject( jQuery.data( div[ 0 ] ) ), "Ensure some public data exists" );
2233 assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
2235 // Make sure the expando is gone
2236 for ( key in div[ 0 ] ) {
2237 if ( /^jQuery/.test( key ) ) {
2238 assert.strictEqual( div[ 0 ][ key ], undefined, "Expando was not removed when there was no more data" );
2243 QUnit.test( "domManip plain-text caching (trac-6779)", function( assert ) {
2247 // DOM manipulation fails if added text matches an Object method
2249 $f = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
2250 bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
2252 for ( i = 0; i < bad.length; i++ ) {
2254 $f.append( bad[ i ] );
2257 assert.equal( $f.text(), bad.join( "" ), "Cached strings that match Object properties" );
2261 QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function( assert ) {
2266 "<script type='text/javascript'>",
2268 "QUnit.assert.ok( true, '<!-- handled' );",
2271 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2273 // This test requires XHTML mode as CDATA is not recognized in HTML.
2275 // "<script type='text/javascript'>",
2277 // "QUnit.assert.ok( true, '<![CDATA[ handled' );",
2280 // ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2283 "<script type='text/javascript'>",
2284 "<!--//--><![CDATA[//><!--",
2285 "QUnit.assert.ok( true, '<!--//--><![CDATA[//><!-- (Drupal case) handled' );",
2288 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2290 // ES2015 in Annex B requires HTML-style comment delimiters (`<!--` & `-->`) to act as
2291 // single-line comment delimiters; i.e. they should be treated as `//`.
2294 "<script type='text/javascript'>",
2295 "<!-- Same-line HTML comment",
2296 "QUnit.assert.ok( true, '<!-- Same-line HTML comment' );",
2299 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2303 "domManip tolerates window-valued document[0] in IE9/10 (trac-12266)",
2304 "manipulation/iframe-denied.html",
2305 function( assert, jQuery, window, document, test ) {
2307 assert.ok( test.status, test.description );
2312 "domManip executes scripts in iframes in the iframes' context",
2313 "manipulation/scripts-context.html",
2314 function( assert, framejQuery, frameWindow, frameDocument ) {
2316 jQuery( frameDocument.body ).append( "<script>window.scriptTest = true;<\x2fscript>" );
2317 assert.ok( !window.scriptTest, "script executed in iframe context" );
2318 assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2323 "domManip executes external scripts in iframes in the iframes' context",
2324 "manipulation/scripts-context.html",
2325 function( assert, framejQuery, frameWindow, frameDocument ) {
2328 Globals.register( "finishTest" );
2330 return new Promise( function( resolve ) {
2331 window.finishTest = resolve;
2332 jQuery( frameDocument.body ).append(
2333 "<script src='" + url( "manipulation/set-global-scripttest.js" ) + "'></script>" );
2334 assert.ok( !window.scriptTest, "script executed in iframe context" );
2335 assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2339 // The AJAX module is needed for jQuery._evalUrl.
2340 QUnit[ includesModule( "ajax" ) ? "test" : "skip" ]
2344 // We need to simulate cross-domain requests with the feature that
2345 // both 127.0.0.1 and localhost point to the mock http server.
2346 // Skip the the test if we are not in localhost but make sure we run
2349 includesModule( "ajax" ) && location.hostname === "localhost" ?
2352 ]( "jQuery.append with crossorigin attribute", function( assert ) {
2355 var done = assert.async(),
2358 Globals.register( "corsCallback" );
2359 window.corsCallback = function( response ) {
2360 assert.ok( typeof response.headers.origin === "string", "Origin header sent" );
2361 window.clearTimeout( timeout );
2365 var src = baseURL + "mock.php?action=script&cors=1&callback=corsCallback";
2366 src = src.replace( "localhost", "127.0.0.1" );
2367 var html = "<script type=\"text/javascript\" src=\"" + src + "\" crossorigin=\"anonymous\"><\/script>";
2369 jQuery( document.body ).append( html );
2370 timeout = window.setTimeout( function() {
2371 assert.ok( false, "Origin header should have been sent" );
2376 QUnit.test( "jQuery.clone - no exceptions for object elements trac-9587", function( assert ) {
2381 jQuery( "#no-clone-exception" ).clone();
2382 assert.ok( true, "cloned with no exceptions" );
2384 assert.ok( false, e.message );
2388 QUnit.test( "Cloned, detached HTML5 elems (trac-10667, trac-10670)", function( assert ) {
2393 $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
2396 $clone = $section.clone();
2398 // This branch tests a known behavior in modern browsers that should never fail.
2399 // Included for expected test count symmetry (expecting 1)
2400 assert.equal( $clone[ 0 ].nodeName, "SECTION", "detached clone nodeName matches 'SECTION'" );
2403 $section.on( "click", function() {
2404 assert.ok( true, "clone fired event" );
2407 // Second clone (will have an event bound)
2408 $clone = $section.clone( true );
2410 // Trigger an event from the first clone
2411 $clone.trigger( "click" );
2412 $clone.off( "click" );
2414 // Add a child node with text to the original
2415 $section.append( "<p>Hello</p>" );
2417 // Third clone (will have child node and text)
2418 $clone = $section.clone( true );
2420 assert.equal( $clone.find( "p" ).text(), "Hello", "Assert text in child of clone" );
2422 // Trigger an event from the third clone
2423 $clone.trigger( "click" );
2424 $clone.off( "click" );
2426 // Add attributes to copy
2428 "class": "foo bar baz",
2429 "title": "This is a title"
2432 // Fourth clone (will have newly added attributes)
2433 $clone = $section.clone( true );
2435 assert.equal( $clone.attr( "class" ), $section.attr( "class" ), "clone and element have same class attribute" );
2436 assert.equal( $clone.attr( "title" ), $section.attr( "title" ), "clone and element have same title attribute" );
2438 // Remove the original
2442 $section = $clone.clone( true );
2447 // Trigger an event from the clone of the clone
2448 $section.trigger( "click" );
2450 // Unbind any remaining events
2451 $section.off( "click" );
2452 $clone.off( "click" );
2455 QUnit.test( "Guard against exceptions when clearing safeChildNodes", function( assert ) {
2462 div = jQuery( "<div></div><hr/><code></code><b></b>" );
2465 assert.ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );
2468 QUnit.test( "Ensure oldIE creates a new set on appendTo (trac-8894)", function( assert ) {
2472 assert.strictEqual( jQuery( "<div></div>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.clone" );
2473 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" );
2474 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" );
2475 assert.strictEqual( jQuery( "<bdi></bdi>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after clone html5 element" );
2476 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" );
2479 QUnit.test( "html() - script exceptions bubble (trac-11743)", function( assert ) {
2481 var done = assert.async(),
2482 onerror = window.onerror;
2484 setTimeout( function() {
2485 window.onerror = onerror;
2490 window.onerror = function() {
2491 assert.ok( true, "Exception thrown" );
2493 if ( includesModule( "ajax" ) ) {
2494 window.onerror = function() {
2495 assert.ok( true, "Exception thrown in remote script" );
2498 jQuery( "#qunit-fixture" ).html( "<script src='" + baseURL + "badcall.js'></script>" );
2499 assert.ok( true, "Exception ignored" );
2501 assert.ok( true, "No jQuery.ajax" );
2505 jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
2508 QUnit.test( "checked state is cloned with clone()", function( assert ) {
2512 var elem = jQuery.parseHTML( "<input type='checkbox' checked='checked'/>" )[ 0 ];
2513 elem.checked = false;
2514 assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, false, "Checked false state correctly cloned" );
2516 elem = jQuery.parseHTML( "<input type='checkbox'/>" )[ 0 ];
2517 elem.checked = true;
2518 assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, true, "Checked true state correctly cloned" );
2521 QUnit.test( "manipulate mixed jQuery and text (trac-12384, trac-12346)", function( assert ) {
2525 var div = jQuery( "<div>a</div>" ).append( " ", jQuery( "<span>b</span>" ), " ", jQuery( "<span>c</span>" ) ),
2526 nbsp = String.fromCharCode( 160 );
2528 assert.equal( div.text(), "a" + nbsp + "b" + nbsp + "c", "Appending mixed jQuery with text nodes" );
2530 div = jQuery( "<div><div></div></div>" )
2532 .after( "<p>a</p>", "<p>b</p>" )
2534 assert.equal( div.find( "*" ).length, 3, "added 2 paragraphs after inner div" );
2537 QUnit.test( "script evaluation (trac-11795)", function( assert ) {
2539 assert.expect( 13 );
2541 var scriptsIn, scriptsOut,
2542 fixture = jQuery( "#qunit-fixture" ).empty(),
2543 objGlobal = ( function() {
2546 isOk = objGlobal.ok,
2547 notOk = function() {
2548 var args = arguments;
2549 args[ 0 ] = !args[ 0 ];
2550 return isOk.apply( this, args );
2553 objGlobal.ok = notOk;
2554 scriptsIn = jQuery( [
2555 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
2556 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
2557 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
2558 "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
2560 "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
2561 "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
2562 "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
2563 "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
2566 scriptsIn.appendTo( jQuery( "<div class='detached'></div>" ) );
2567 objGlobal.ok = isOk;
2569 scriptsOut = fixture.append( scriptsIn ).find( "script" );
2570 assert.equal( scriptsOut[ 0 ].type, "something/else", "Non-evaluated type." );
2571 assert.equal( scriptsOut[ 1 ].type, "text/javascript", "Evaluated type." );
2572 assert.deepEqual( scriptsOut.get(), fixture.find( "script" ).get(), "All script tags remain." );
2574 objGlobal.ok = notOk;
2575 scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find( "div" ) );
2576 assert.deepEqual( fixture.find( "div script" ).get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
2577 fixture.append( scriptsOut.detach() );
2578 assert.deepEqual( fixture.children( "script" ).get(), scriptsOut.get(), "Scripts detached without reevaluation" );
2579 objGlobal.ok = isOk;
2581 if ( includesModule( "ajax" ) ) {
2582 Globals.register( "testBar" );
2583 jQuery( "#qunit-fixture" ).append( "<script src='" + url( "mock.php?action=testbar" ) + "'></script>" );
2584 assert.strictEqual( window.testBar, "bar", "Global script evaluation" );
2586 assert.ok( true, "No jQuery.ajax" );
2587 assert.ok( true, "No jQuery.ajax" );
2591 QUnit[ includesModule( "ajax" ) ? "test" : "skip" ]( "jQuery._evalUrl (trac-12838)", function( assert ) {
2595 var message, expectedArgument,
2597 evalUrl = jQuery._evalUrl;
2599 message = "jQuery.ajax implementation";
2600 expectedArgument = 1;
2601 jQuery.ajax = function( input ) {
2602 assert.equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
2605 jQuery( "#qunit-fixture" ).append( "<script src='1'></script><script src='2'></script>" );
2606 assert.equal( expectedArgument, 3, "synchronous execution" );
2608 message = "custom implementation";
2609 expectedArgument = 3;
2610 jQuery._evalUrl = jQuery.ajax;
2611 jQuery.ajax = function( options ) {
2612 assert.strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
2614 jQuery( "#qunit-fixture" ).append( "<script src='3'></script><script src='4'></script>" );
2617 jQuery._evalUrl = evalUrl;
2620 QUnit.test( "jQuery.htmlPrefilter (gh-1747)", function( assert ) {
2624 var expectedArgument,
2626 done = assert.async(),
2627 htmlPrefilter = jQuery.htmlPrefilter,
2628 fixture = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
2629 poison = "<script>jQuery.htmlPrefilter.assert.ok( false, 'script not executed' );</script>";
2631 jQuery.htmlPrefilter = function( html ) {
2633 assert.equal( html, expectedArgument, "Expected input" );
2635 // Remove <script> and <del> elements
2636 return htmlPrefilter.apply( this, arguments )
2637 .replace( /<(script|del)(?=[\s>])[\w\W]*?<\/\1\s*>/ig, "" );
2639 jQuery.htmlPrefilter.assert = assert;
2641 expectedArgument = "A-" + poison + "B-" + poison + poison + "C-";
2642 fixture.html( expectedArgument );
2644 expectedArgument = "D-" + poison + "E-" + "<del></del><div>" + poison + poison + "</div>" + "F-";
2645 fixture.append( expectedArgument );
2647 expectedArgument = poison;
2648 fixture.find( "div" ).replaceWith( expectedArgument );
2650 assert.equal( invocations, 3, "htmlPrefilter invoked for all DOM manipulations" );
2651 assert.equal( fixture.html(), "A-B-C-D-E-F-", "htmlPrefilter modified HTML" );
2653 // Allow asynchronous script execution to generate assertions
2654 setTimeout( function() {
2655 jQuery.htmlPrefilter = htmlPrefilter;
2660 QUnit.test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (trac-4087)", function( assert ) {
2662 assert.expect( 10 );
2670 ], function( index, name ) {
2672 "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
2673 "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
2674 "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
2675 ].join( "" ) ).appendTo( "#qunit-fixture" );
2677 // complex case based on https://jsfiddle.net/pbramos/gZ7vB/
2678 jQuery( "#test4087-complex div" )[ name ]( "#test4087-complex li:last-child div:last-child" );
2679 assert.equal( jQuery( "#test4087-complex li:last-child div" ).length, name === "replaceAll" ? 1 : 2, name + " a node to itself, complex case." );
2682 jQuery( ".test4087-1" )[ name ]( ".test4087-1" );
2683 assert.equal( jQuery( ".test4087-1" ).length, 1, name + " a node to itself, simple case." );
2685 // clean for next test
2686 jQuery( "#test4087-complex" ).remove();
2687 jQuery( "#test4087-simple" ).remove();
2688 jQuery( "#test4087-multiple" ).remove();
2692 QUnit.test( "Index for function argument should be received (trac-13094)", function( assert ) {
2697 jQuery( "<div></div><div></div>" ).before( function( index ) {
2698 assert.equal( index, i++, "Index should be correct" );
2703 QUnit.test( "Make sure jQuery.fn.remove can work on elements in documentFragment", function( assert ) {
2706 var fragment = document.createDocumentFragment(),
2707 div = fragment.appendChild( document.createElement( "div" ) );
2709 jQuery( div ).remove();
2711 assert.equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
2714 QUnit.test( "Make sure specific elements with content created correctly (trac-13232)", function( assert ) {
2715 assert.expect( 20 );
2720 thead: "<tr><td>thead</td></tr>",
2721 tbody: "<tr><td>tbody</td></tr>",
2722 tfoot: "<tr><td>tfoot</td></tr>",
2723 colgroup: "<col span='5'></col>",
2727 td: "<div>td</div>",
2728 optgroup: "<option>optgroup</option>",
2732 jQuery.each( elems, function( name, value ) {
2733 var html = "<" + name + ">" + value + "</" + name + ">";
2735 jQuery.parseHTML( "<" + name + ">" + value + "</" + name + ">" )[ 0 ].nodeName.toLowerCase(),
2737 name + " is created correctly"
2740 results.push( name );
2744 jQuery.fn.append.apply( jQuery( "<div></div>" ), args ).children().each( function( i ) {
2745 assert.strictEqual( this.nodeName.toLowerCase(), results[ i ] );
2749 QUnit.test( "Validate creation of multiple quantities of certain elements (trac-13818)", function( assert ) {
2750 assert.expect( 22 );
2752 var tags = [ "thead", "tbody", "tfoot", "colgroup", "col", "caption", "tr", "th", "td", "optgroup", "option" ];
2754 jQuery.each( tags, function( index, tag ) {
2755 jQuery( "<" + tag + "></" + tag + "><" + tag + "></" + tag + ">" ).each( function() {
2756 assert.ok( this.nodeName.toLowerCase() === tag, tag + " elements created correctly" );
2761 QUnit.test( "Make sure tr element will be appended to tbody element of table when present", function( assert ) {
2765 table = document.createElement( "table" );
2767 table.appendChild( document.createElement( "tbody" ) );
2768 document.getElementById( "qunit-fixture" ).appendChild( table );
2770 jQuery( table ).append( "<tr><td>test</td></tr>" );
2772 // Lowercase and replace spaces to remove possible browser inconsistencies
2773 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2775 assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2778 QUnit.test( "Make sure tr elements will be appended to tbody element of table when present", function( assert ) {
2782 table = document.createElement( "table" );
2784 table.appendChild( document.createElement( "tbody" ) );
2785 document.getElementById( "qunit-fixture" ).appendChild( table );
2787 jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
2789 // Lowercase and replace spaces to remove possible browser inconsistencies
2790 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2792 assert.strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
2795 QUnit.test( "Make sure tfoot element will not be appended to tbody element of table when present", function( assert ) {
2799 table = document.createElement( "table" );
2801 table.appendChild( document.createElement( "tbody" ) );
2802 document.getElementById( "qunit-fixture" ).appendChild( table );
2804 jQuery( table ).append( "<tfoot></tfoot>" );
2806 // Lowercase and replace spaces to remove possible browser inconsistencies
2807 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2809 assert.strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
2812 QUnit.test( "Make sure document fragment will be appended to tbody element of table when present", function( assert ) {
2816 fragment = document.createDocumentFragment(),
2817 table = document.createElement( "table" ),
2818 tr = document.createElement( "tr" ),
2819 td = document.createElement( "td" );
2821 table.appendChild( document.createElement( "tbody" ) );
2822 document.getElementById( "qunit-fixture" ).appendChild( table );
2824 fragment.appendChild( tr );
2825 tr.appendChild( td );
2826 td.innerHTML = "test";
2828 jQuery( table ).append( fragment );
2830 // Lowercase and replace spaces to remove possible browser inconsistencies
2831 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2833 assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2836 QUnit.test( "Make sure col element is appended correctly", function( assert ) {
2839 var table = jQuery( "<table cellpadding='0'><tr><td>test</td></tr></table>" );
2841 jQuery( table ).appendTo( "#qunit-fixture" );
2843 jQuery( "<col width='150'></col>" ).prependTo( table );
2845 assert.strictEqual( table.find( "td" ).width(), 150 );
2848 QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
2854 "<table><tbody><tr><td>nested</td></tr></tbody></table>" +
2855 "</td></tr></thead>",
2856 newRow = "<tr><td>added</td></tr>",
2857 htmlExpected = htmlIn.replace( "</thead>", "</thead>" + newRow ),
2858 table = supportjQuery( "<table></table>" ).html( htmlIn ).appendTo( "#qunit-fixture" )[ 0 ];
2860 jQuery( table ).append( newRow );
2862 // Lowercase and replace spaces to remove possible browser inconsistencies
2863 htmlOut = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2865 assert.strictEqual( htmlOut, htmlExpected );
2868 [ true, false ].forEach( function( adoptedCase ) {
2870 "Manip within <template /> content moved back & forth doesn't throw - " + (
2871 adoptedCase ? "explicitly adopted" : "not explicitly adopted"
2873 function( assert ) {
2876 var fragment, diva, divb,
2879 " <div><div class='a'></div></div>\n" +
2880 " <div><div class='b'></div></div>\n" +
2883 template = jQuery( "<template></template>" );
2885 jQuery( "#qunit-fixture" )
2887 .append( template );
2889 fragment = template[ 0 ].content;
2890 diva = div.find( ".a" );
2891 divb = div.find( ".b" );
2893 if ( adoptedCase ) {
2894 document.adoptNode( fragment );
2897 fragment.appendChild( div.children()[ 0 ] );
2898 fragment.appendChild( div.children()[ 0 ] );
2900 diva.insertBefore( divb );
2902 assert.strictEqual( diva.siblings( ".b" ).length, 1,
2903 "Insertion worked" );
2908 QUnit.test( "Make sure tags with single-character names are found (gh-4124)", function( assert ) {
2912 htmlIn = "<p>foo<!--<td>--></p>",
2913 $el = jQuery( "<div></div>" );
2917 // Lowercase and replace spaces to remove possible browser inconsistencies
2918 htmlOut = $el[ 0 ].innerHTML.toLowerCase().replace( /\s/g, "" );
2920 assert.strictEqual( htmlOut, htmlIn );
2923 // The AJAX module is needed for jQuery._evalUrl.
2924 QUnit[ includesModule( "ajax" ) ? "test" : "skip" ]( "Insert script with data-URI (gh-1887)", function( assert ) {
2927 Globals.register( "testFoo" );
2928 Globals.register( "testSrcFoo" );
2930 var script = document.createElement( "script" ),
2931 fixture = document.getElementById( "qunit-fixture" ),
2932 done = assert.async();
2934 script.src = "data:text/javascript,testSrcFoo = 'foo';";
2936 fixture.appendChild( script );
2938 jQuery( fixture ).append( "<script src=\"data:text/javascript,testFoo = 'foo';\"></script>" );
2940 setTimeout( function() {
2941 if ( window.testSrcFoo === "foo" ) {
2942 assert.strictEqual( window.testFoo, window.testSrcFoo, "data-URI script executed" );
2945 assert.ok( true, "data-URI script is not supported by this environment" );
2952 QUnit.test( "Ignore content from unsuccessful responses (gh-4126)", function( assert ) {
2955 var globalEval = jQuery.globalEval;
2956 jQuery.globalEval = function( _code ) {
2957 assert.ok( false, "no attempt to evaluate code from an unsuccessful response" );
2961 jQuery( "#qunit-fixture" ).append(
2962 "<script src='" + url( "mock.php?action=error" ) + "'></script>" );
2963 assert.ok( true, "no error thrown from embedding script with unsuccessful-response src" );
2967 jQuery.globalEval = globalEval;
2972 "Check if CSP nonce is preserved",
2973 "mock.php?action=cspNonce",
2974 function( assert ) {
2975 var done = assert.async();
2979 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
2980 assert.equal( data, "", "No log request should be sent" );
2981 supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
2987 "Check if CSP nonce is preserved for external scripts with src attribute",
2988 "mock.php?action=cspNonce&test=external",
2989 function( assert ) {
2990 var done = assert.async();
2994 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
2995 assert.equal( data, "", "No log request should be sent" );
2996 supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
3000 // The AJAX module is needed for jQuery._evalUrl.
3001 QUnit[ includesModule( "ajax" ) ? "test" : "skip" ]
3005 "jQuery.globalEval supports nonce",
3006 "mock.php?action=cspNonce&test=globaleval",
3007 function( assert ) {
3008 var done = assert.async();
3012 supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
3013 assert.equal( data, "", "No log request should be sent" );
3014 supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
3019 QUnit.test( "Sanitized HTML doesn't get unsanitized", function( assert ) {
3023 oldIos = /iphone os (?:8|9|10|11|12)_/i.test( navigator.userAgent ),
3024 assertCount = oldIos ? 12 : 13,
3025 done = assert.async( assertCount );
3027 assert.expect( assertCount );
3029 Globals.register( "xss" );
3030 window.xss = sinon.spy();
3032 container = jQuery( "<div></div>" );
3033 container.appendTo( "#qunit-fixture" );
3035 function test( htmlString ) {
3036 var currCounter = counter,
3037 div = jQuery( "<div></div>" );
3041 div.appendTo( container );
3042 div.html( htmlString );
3044 setTimeout( function() {
3045 assert.ok( window.xss.withArgs( currCounter ).notCalled,
3046 "Insecure code wasn't executed, input: " + htmlString );
3051 // Note: below test cases need to invoke the xss function with consecutive
3052 // decimal parameters for the assertion messages to be correct.
3053 // Thanks to Masato Kinugawa from Cure53 for providing the following test cases.
3054 test( "<img alt=\"<x\" title=\"/><img src=url404 onerror=xss(0)>\">" );
3055 test( "<img alt=\"\n<x\" title=\"/>\n<img src=url404 onerror=xss(1)>\">" );
3056 test( "<style><style/><img src=url404 onerror=xss(2)>" );
3057 test( "<xmp><xmp/><img src=url404 onerror=xss(3)>" );
3058 test( "<title><title /><img src=url404 onerror=xss(4)>" );
3059 test( "<iframe><iframe/><img src=url404 onerror=xss(5)>" );
3060 test( "<noframes><noframes/><img src=url404 onerror=xss(6)>" );
3061 test( "<noscript><noscript/><img src=url404 onerror=xss(7)>" );
3062 test( "<foo\" alt=\"\" title=\"/><img src=url404 onerror=xss(8)>\">" );
3063 test( "<img alt=\"<x\" title=\"\" src=\"/><img src=url404 onerror=xss(9)>\">" );
3064 test( "<noscript/><img src=url404 onerror=xss(10)>" );
3066 test( "<option><style></option></select><img src=url404 onerror=xss(11)></style>" );
3068 // Support: iOS 8 - 12 only.
3069 // Old iOS parses `<noembed>` tags differently, executing this code. This is no
3070 // different to native behavior on that OS, though, so just accept it.
3072 test( "<noembed><noembed/><img src=url404 onerror=xss(12)>" );
3076 QUnit.test( "Works with invalid attempts to close the table wrapper", function( assert ) {
3079 // This test case attempts to close the tags which wrap input
3080 // based on matching done in wrapMap which should be ignored.
3081 var elem = jQuery( "<td></td></tr></tbody></table><td></td>" );
3082 assert.strictEqual( elem.length, 2, "Two elements created" );
3083 assert.strictEqual( elem[ 0 ].nodeName.toLowerCase(), "td", "First element is td" );
3084 assert.strictEqual( elem[ 1 ].nodeName.toLowerCase(), "td", "Second element is td" );
3087 // Test trustedTypes support in browsers where they're supported (currently Chrome 83+).
3088 // Browsers with no TrustedHTML support still run tests on object wrappers with
3089 // a proper `toString` function.
3091 "Basic TrustedHTML support (gh-4409)",
3092 "mock.php?action=trustedHtml",
3093 function( assert, jQuery, window, document, test ) {
3097 test.forEach( function( result ) {
3098 assert.deepEqual( result.actual, result.expected, result.message );
3103 QUnit.test( "should handle node removal in event's remove hook (gh-5214)", function( assert ) {
3108 "<div id='container'>" +
3109 " <div class='guarded removeself' data-elt='one'>" +
3112 " <div class='guarded' data-elt='two'>" +
3115 " <div class='guarded' data-elt='three'>" +
3119 ).appendTo( "#qunit-fixture" );
3121 // Define the custom event handler
3122 jQuery.event.special.removeondestroy = {
3123 remove: function( ) {
3124 var $t = jQuery( this );
3125 assert.step( $t.data( "elt" ) );
3126 if ( $t.is( ".removeself" ) ) {
3132 // Attach an empty handler to trigger the `remove`
3133 // logic for the custom event when the element is removed.
3134 jQuery( ".guarded" ).on( "removeondestroy", function( ) { } );
3136 // Trigger the event's removal logic by emptying the container
3137 jQuery( "#container" ).empty();
3139 assert.verifySteps( [ "one", "two", "three" ], "All elements were processed in order" );