Ref 0ed497b. Close gh-1125.

This commit is contained in:
Oleg 2013-01-09 13:52:15 +04:00 committed by Richard Gibson
parent f40eac1d37
commit 8b82af9ef2
2 changed files with 56 additions and 37 deletions

View File

@ -33,7 +33,7 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca
legend: [ 1, "<fieldset>" ], legend: [ 1, "<fieldset>" ],
area: [ 1, "<map>" ], area: [ 1, "<map>" ],
param: [ 1, "<object>" ], param: [ 1, "<object>" ],
thead: [ 1, "<table>" ], thead: [ 1, "<table>" ],
tr: [ 2, "<table><tbody>" ], tr: [ 2, "<table><tbody>" ],
col: [ 2, "<table><tbody></tbody><colgroup>", "</table>" ], col: [ 2, "<table><tbody></tbody><colgroup>", "</table>" ],
td: [ 3, "<table><tbody><tr>" ], td: [ 3, "<table><tbody><tr>" ],
@ -308,9 +308,8 @@ jQuery.fn.extend({
}); });
} }
if ( this[0] ) { if ( l ) {
doc = this[0].ownerDocument; fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
fragment = jQuery.buildFragment( args, doc, false, this );
first = fragment.firstChild; first = fragment.firstChild;
if ( fragment.childNodes.length === 1 ) { if ( fragment.childNodes.length === 1 ) {
@ -619,21 +618,20 @@ jQuery.extend({
}, },
buildFragment: function( elems, context, scripts, selection ) { buildFragment: function( elems, context, scripts, selection ) {
var elem, j, tmp, tag, wrap, tbody, var contains, elem, tag, tmp, wrap, tbody, j,
nodes = [],
i = 0,
l = elems.length, l = elems.length,
fragment = context.createDocumentFragment(),
safe = context === document && safeFragment;
// Ensure that context is a document // Ensure a safe fragment
if ( !context || typeof context.createDocumentFragment === "undefined" ) { safe = createSafeFragment( context ),
context = document;
} nodes = [],
i = 0;
for ( ; i < l; i++ ) { for ( ; i < l; i++ ) {
elem = elems[ i ]; elem = elems[ i ];
if ( elem || elem === 0 ) { if ( elem || elem === 0 ) {
// Add nodes directly // Add nodes directly
if ( jQuery.type( elem ) === "object" ) { if ( jQuery.type( elem ) === "object" ) {
jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem ); jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
@ -644,8 +642,6 @@ jQuery.extend({
// Convert html into DOM nodes // Convert html into DOM nodes
} else { } else {
// Ensure a safe container
safe = safe || createSafeFragment( context );
tmp = tmp || safe.appendChild( context.createElement("div") ); tmp = tmp || safe.appendChild( context.createElement("div") );
// Deserialize a standard representation // Deserialize a standard representation
@ -700,7 +696,7 @@ jQuery.extend({
} }
} }
// Fix #11356: Clear elements from safeFragment // Fix #11356: Clear elements from fragment
if ( tmp ) { if ( tmp ) {
safe.removeChild( tmp ); safe.removeChild( tmp );
} }
@ -713,18 +709,20 @@ jQuery.extend({
i = 0; i = 0;
while ( (elem = nodes[ i++ ]) ) { while ( (elem = nodes[ i++ ]) ) {
safe = jQuery.contains( elem.ownerDocument, elem );
// #4087 - If origin and destination elements are the same, and this is
// that element, do not do anything
if ( selection && jQuery.inArray( elem, selection ) !== -1 ) {
continue;
}
contains = jQuery.contains( elem.ownerDocument, elem );
// Append to fragment // Append to fragment
// #4087 - If origin and destination elements are the same, and this is tmp = getAll( safe.appendChild( elem ), "script" );
// that element, do not append to fragment
if ( !selection || jQuery.inArray( elem, selection ) === -1 ) {
fragment.appendChild( elem );
}
tmp = getAll( elem, "script" );
// Preserve script evaluation history // Preserve script evaluation history
if ( safe ) { if ( contains ) {
setGlobalEval( tmp ); setGlobalEval( tmp );
} }
@ -739,9 +737,9 @@ jQuery.extend({
} }
} }
elem = tmp = safe = null; tmp = null;
return fragment; return safe;
}, },
cleanData: function( elems, /* internal */ acceptData ) { cleanData: function( elems, /* internal */ acceptData ) {

View File

@ -395,13 +395,13 @@ var testAppendForObject = function( valueObj, isFragment ) {
var testAppend = function( valueObj ) { var testAppend = function( valueObj ) {
expect( 60 ); expect( 78 );
testAppendForObject( valueObj, false ); testAppendForObject( valueObj, false );
testAppendForObject( valueObj, true ); testAppendForObject( valueObj, true );
var defaultText, result, message, iframe, iframeDoc, j, d, var defaultText, result, message, iframe, iframeDoc, j, d,
$input, $radioChecked, $radioUnchecked, $radioParent; $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
defaultText = "Try them out:"; defaultText = "Try them out:";
result = jQuery("#first").append( valueObj("<b>buga</b>") ); result = jQuery("#first").append( valueObj("<b>buga</b>") );
@ -446,18 +446,29 @@ var testAppend = function( valueObj ) {
jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") ); jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") );
t( "Append legend", "#legend", [ "legend" ] ); t( "Append legend", "#legend", [ "legend" ] );
$map = jQuery("<map/>").append( valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>") );
equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") ); jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") );
equal( jQuery("#select1 option:last").text(), "Test", "Appending OPTION (all caps)" ); equal( jQuery("#select1 option:last").text(), "Test", "Appending OPTION (all caps)" );
jQuery("#table").append( valueObj("<colgroup></colgroup>") ); jQuery("#select1").append( valueObj("<optgroup label='optgroup'><option>optgroup</option></optgroup>") );
equal( jQuery("#table colgroup").length, 1, "Append colgroup" ); equal( jQuery("#select1 optgroup").attr("label"), "optgroup", "Label attribute in newly inserted optgroup is correct" );
equal( jQuery("#select1 option:last").text(), "optgroup", "Appending optgroup" );
$table = jQuery("#table").empty();
jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
$table.append( valueObj( "<" + name + "/>" ) );
ok( $table.find( name ).length >= 1, "Append " + name );
ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
});
jQuery("#table colgroup").append( valueObj("<col/>") ); jQuery("#table colgroup").append( valueObj("<col/>") );
equal( jQuery("#table colgroup col").length, 1, "Append col" ); equal( jQuery("#table colgroup col").length, 1, "Append col" );
jQuery("#table").append( valueObj("<caption></caption>") );
equal( jQuery("#table caption").length, 1, "Append caption" );
jQuery("#form") jQuery("#form")
.append( valueObj("<select id='appendSelect1'></select>") ) .append( valueObj("<select id='appendSelect1'></select>") )
.append( valueObj("<select id='appendSelect2'><option>Test</option></select>") ); .append( valueObj("<select id='appendSelect2'><option>Test</option></select>") );
@ -2229,9 +2240,19 @@ test( "insertAfter, insertBefore, etc do not work when destination is original e
}); });
test( "Index for function argument should be received (#13094)", 2, function() { test( "Index for function argument should be received (#13094)", 2, function() {
var i = 0; var i = 0;
jQuery("<div/><div/>").before(function( index ) {
equal( index, i++, "Index should be correct" );
});
jQuery("<div/><div/>").before(function( index ) { });
equal( index, i++, "Index should be correct" );
}); test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1, function() {
var fragment = document.createDocumentFragment(),
div = fragment.appendChild( document.createElement("div") );
jQuery( div ).remove();
equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
}); });