Commit e8891340 authored by Richard Gibson's avatar Richard Gibson

Fix #11795, #10470: keep scripts in DOM; execute only on first insertion. Close gh-864.

parent 90f9f496
...@@ -16,6 +16,7 @@ var ...@@ -16,6 +16,7 @@ var
_$ = window.$, _$ = window.$,
// Save a reference to some core methods // Save a reference to some core methods
core_concat = Array.prototype.concat,
core_push = Array.prototype.push, core_push = Array.prototype.push,
core_slice = Array.prototype.slice, core_slice = Array.prototype.slice,
core_indexOf = Array.prototype.indexOf, core_indexOf = Array.prototype.indexOf,
...@@ -472,26 +473,31 @@ jQuery.extend({ ...@@ -472,26 +473,31 @@ jQuery.extend({
// data: string of html // data: string of html
// context (optional): If specified, the fragment will be created in this context, defaults to document // context (optional): If specified, the fragment will be created in this context, defaults to document
// scripts (optional): If true, will include scripts passed in the html string // keepScripts (optional): If true, will include scripts passed in the html string
parseHTML: function( data, context, scripts ) { parseHTML: function( data, context, keepScripts ) {
var parsed;
if ( !data || typeof data !== "string" ) { if ( !data || typeof data !== "string" ) {
return null; return null;
} }
if ( typeof context === "boolean" ) { if ( typeof context === "boolean" ) {
scripts = context; keepScripts = context;
context = 0; context = false;
} }
context = context || document; context = context || document;
var parsed = rsingleTag.exec( data ),
scripts = !keepScripts && [];
// Single tag // Single tag
if ( (parsed = rsingleTag.exec( data )) ) { if ( parsed ) {
return [ context.createElement( parsed[1] ) ]; return [ context.createElement( parsed[1] ) ];
} }
parsed = jQuery.buildFragment( [ data ], context, scripts ? null : [] ); parsed = jQuery.buildFragment( [ data ], context, scripts );
if ( scripts ) {
jQuery( scripts ).remove();
}
return jQuery.merge( [], return jQuery.merge( [],
(parsed.cacheable ? jQuery.clone( parsed.fragment ) : parsed.fragment).childNodes ); ( parsed.cacheable ? jQuery.clone( parsed.fragment ) : parsed.fragment ).childNodes );
}, },
parseJSON: function( data ) { parseJSON: function( data ) {
...@@ -744,7 +750,7 @@ jQuery.extend({ ...@@ -744,7 +750,7 @@ jQuery.extend({
} }
// Flatten any nested arrays // Flatten any nested arrays
return ret.concat.apply( [], ret ); return core_concat.apply( [], ret );
}, },
// A global GUID counter for objects // A global GUID counter for objects
......
...@@ -26,7 +26,8 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca ...@@ -26,7 +26,8 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca
rcheckableType = /^(?:checkbox|radio)$/, rcheckableType = /^(?:checkbox|radio)$/,
// checked="checked" or checked // checked="checked" or checked
rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
rscriptType = /\/(java|ecma)script/i, rscriptType = /^$|\/(?:java|ecma)script/i,
rscriptTypeMasked = /^true\/(.*)/,
rcleanScript = /^\s*<!(?:\[CDATA\[|\-\-)|[\]\-]{2}>\s*$/g, rcleanScript = /^\s*<!(?:\[CDATA\[|\-\-)|[\]\-]{2}>\s*$/g,
wrapMap = { wrapMap = {
option: [ 1, "<select multiple='multiple'>", "</select>" ], option: [ 1, "<select multiple='multiple'>", "</select>" ],
...@@ -181,13 +182,15 @@ jQuery.fn.extend({ ...@@ -181,13 +182,15 @@ jQuery.fn.extend({
i = 0; i = 0;
for ( ; (elem = this[i]) != null; i++ ) { for ( ; (elem = this[i]) != null; i++ ) {
if ( !selector || jQuery.filter( selector, [ elem ] ).length ) { if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) {
if ( !keepData && elem.nodeType === 1 ) { if ( !keepData && elem.nodeType === 1 ) {
jQuery.cleanData( elem.getElementsByTagName("*") ); jQuery.cleanData( getAll( elem ) );
jQuery.cleanData( [ elem ] );
} }
if ( elem.parentNode ) { if ( elem.parentNode ) {
if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
setGlobalEval( getAll( elem, "script" ) );
}
elem.parentNode.removeChild( elem ); elem.parentNode.removeChild( elem );
} }
} }
...@@ -203,7 +206,7 @@ jQuery.fn.extend({ ...@@ -203,7 +206,7 @@ jQuery.fn.extend({
for ( ; (elem = this[i]) != null; i++ ) { for ( ; (elem = this[i]) != null; i++ ) {
// Remove element nodes and prevent memory leaks // Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
jQuery.cleanData( elem.getElementsByTagName("*") ); jQuery.cleanData( getAll( elem, false ) );
} }
// Remove any remaining nodes // Remove any remaining nodes
...@@ -249,7 +252,7 @@ jQuery.fn.extend({ ...@@ -249,7 +252,7 @@ jQuery.fn.extend({
// Remove element nodes and prevent memory leaks // Remove element nodes and prevent memory leaks
elem = this[i] || {}; elem = this[i] || {};
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
jQuery.cleanData( elem.getElementsByTagName( "*" ) ); jQuery.cleanData( getAll( elem, false ) );
elem.innerHTML = value; elem.innerHTML = value;
} }
} }
...@@ -311,31 +314,27 @@ jQuery.fn.extend({ ...@@ -311,31 +314,27 @@ jQuery.fn.extend({
domManip: function( args, table, callback ) { domManip: function( args, table, callback ) {
// Flatten any nested arrays // Flatten any nested arrays
args = [].concat.apply( [], args ); args = core_concat.apply( [], args );
var results, first, fragment, iNoClone, var fragment, first, results, scripts, hasScripts, iNoClone, node, doc,
i = 0, i = 0,
l = this.length,
value = args[0], value = args[0],
scripts = [], isFunction = jQuery.isFunction( value );
l = this.length;
// We can't cloneNode fragments that contain checked, in WebKit // We can't cloneNode fragments that contain checked, in WebKit
if ( !jQuery.support.checkClone && l > 1 && typeof value === "string" && rchecked.test( value ) ) { if ( isFunction || !( l <= 1 || typeof value !== "string" || jQuery.support.checkClone || !rchecked.test( value ) ) ) {
return this.each(function() { return this.each(function() {
jQuery(this).domManip( args, table, callback ); var self = jQuery( this );
}); if ( isFunction ) {
}
if ( jQuery.isFunction(value) ) {
return this.each(function(i) {
var self = jQuery(this);
args[0] = value.call( this, i, table ? self.html() : undefined ); args[0] = value.call( this, i, table ? self.html() : undefined );
}
self.domManip( args, table, callback ); self.domManip( args, table, callback );
}); });
} }
if ( this[0] ) { if ( this[0] ) {
results = jQuery.buildFragment( args, this, scripts ); results = jQuery.buildFragment( args, this );
fragment = results.fragment; fragment = results.fragment;
first = fragment.firstChild; first = fragment.firstChild;
...@@ -345,31 +344,46 @@ jQuery.fn.extend({ ...@@ -345,31 +344,46 @@ jQuery.fn.extend({
if ( first ) { if ( first ) {
table = table && jQuery.nodeName( first, "tr" ); table = table && jQuery.nodeName( first, "tr" );
scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
hasScripts = scripts.length;
// Use the original fragment for the last item instead of the first because it can end up // Use the original fragment for the last item instead of the first because it can end up
// being emptied incorrectly in certain situations (#8070). // being emptied incorrectly in certain situations (#8070).
// Fragments from the fragment cache must always be cloned and never used in place. // Fragments from the fragment cache must always be cloned and never used in place.
for ( iNoClone = results.cacheable || l - 1; i < l; i++ ) { for ( iNoClone = results.cacheable || l - 1; i < l; i++ ) {
node = fragment;
if ( i !== iNoClone ) {
node = jQuery.clone( node, true, true );
// Keep references to cloned scripts for later restoration
if ( hasScripts ) {
jQuery.merge( scripts, getAll( node, "script" ) );
}
}
callback.call( callback.call(
table && jQuery.nodeName( this[i], "table" ) ? table && jQuery.nodeName( this[i], "table" ) ?
findOrAppend( this[i], "tbody" ) : findOrAppend( this[i], "tbody" ) :
this[i], this[i],
i === iNoClone ? node
fragment :
jQuery.clone( fragment, true, true )
); );
} }
}
// Fix #11809: Avoid leaking memory if ( hasScripts ) {
fragment = first = null; doc = scripts[ scripts.length - 1 ].ownerDocument;
// Reenable scripts
jQuery.map( scripts, restoreScript );
// Evaluate executable scripts on first document insertion
for ( i = 0; i < hasScripts; i++ ) {
node = scripts[ i ];
if ( rscriptType.test( node.type || "" ) &&
!jQuery._data( node, "globalEval" ) && jQuery.contains( doc, node ) ) {
if ( scripts.length ) { if ( node.src ) {
jQuery.each( scripts, function( i, elem ) { // Hope ajax is available...
if ( elem.src ) {
if ( jQuery.ajax ) {
jQuery.ajax({ jQuery.ajax({
url: elem.src, url: node.src,
type: "GET", type: "GET",
dataType: "script", dataType: "script",
async: false, async: false,
...@@ -377,16 +391,14 @@ jQuery.fn.extend({ ...@@ -377,16 +391,14 @@ jQuery.fn.extend({
"throws": true "throws": true
}); });
} else { } else {
jQuery.error("no ajax"); jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) );
} }
} else {
jQuery.globalEval( ( elem.text || elem.textContent || elem.innerHTML || "" ).replace( rcleanScript, "" ) );
} }
if ( elem.parentNode ) {
elem.parentNode.removeChild( elem );
} }
}); }
// Fix #11809: Avoid leaking memory
fragment = first = null;
} }
} }
...@@ -398,6 +410,31 @@ function findOrAppend( elem, tag ) { ...@@ -398,6 +410,31 @@ function findOrAppend( elem, tag ) {
return elem.getElementsByTagName( tag )[0] || elem.appendChild( elem.ownerDocument.createElement( tag ) ); return elem.getElementsByTagName( tag )[0] || elem.appendChild( elem.ownerDocument.createElement( tag ) );
} }
// Replace/restore the type attribute of script elements for safe DOM manipulation
function disableScript( elem ) {
var attr = elem.getAttributeNode("type");
elem.type = ( attr && attr.specified ) + "/" + elem.type;
return elem;
}
function restoreScript( elem ) {
var match = rscriptTypeMasked.exec( elem.type );
if ( match ) {
elem.type = match[1];
} else {
elem.removeAttribute("type");
}
return elem;
}
// Mark scripts as having already been evaluated
function setGlobalEval( elems, refElements ) {
var elem,
i = 0;
for ( ; (elem = elems[i]) != null; i++ ) {
jQuery._data( elem, "globalEval", !refElements || jQuery._data( refElements[i], "globalEval" ) );
}
}
function cloneCopyEvent( src, dest ) { function cloneCopyEvent( src, dest ) {
if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) { if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
...@@ -448,9 +485,14 @@ function cloneFixAttributes( src, dest ) { ...@@ -448,9 +485,14 @@ function cloneFixAttributes( src, dest ) {
nodeName = dest.nodeName.toLowerCase(); nodeName = dest.nodeName.toLowerCase();
if ( nodeName === "object" ) { // IE blanks contents when cloning scripts, and tries to evaluate newly-set text
if ( nodeName === "script" && dest.text !== src.text ) {
disableScript( dest ).text = src.text;
restoreScript( dest );
// IE6-10 improperly clones children of object elements using classid. // IE6-10 improperly clones children of object elements using classid.
// IE10 throws NoModificationAllowedError if parent is null, #12132. // IE10 throws NoModificationAllowedError if parent is null, #12132.
} else if ( nodeName === "object" ) {
if ( dest.parentNode ) { if ( dest.parentNode ) {
dest.outerHTML = src.outerHTML; dest.outerHTML = src.outerHTML;
} }
...@@ -459,7 +501,7 @@ function cloneFixAttributes( src, dest ) { ...@@ -459,7 +501,7 @@ function cloneFixAttributes( src, dest ) {
// element in IE9, the outerHTML strategy above is not sufficient. // element in IE9, the outerHTML strategy above is not sufficient.
// If the src has innerHTML and the destination does not, // If the src has innerHTML and the destination does not,
// copy the src.innerHTML into the dest.innerHTML. #10324 // copy the src.innerHTML into the dest.innerHTML. #10324
if ( jQuery.support.html5Clone && (src.innerHTML && !jQuery.trim(dest.innerHTML)) ) { if ( jQuery.support.html5Clone && ( src.innerHTML && !jQuery.trim(dest.innerHTML) ) ) {
dest.innerHTML = src.innerHTML; dest.innerHTML = src.innerHTML;
} }
...@@ -485,10 +527,6 @@ function cloneFixAttributes( src, dest ) { ...@@ -485,10 +527,6 @@ function cloneFixAttributes( src, dest ) {
// cloning other types of input fields // cloning other types of input fields
} else if ( nodeName === "input" || nodeName === "textarea" ) { } else if ( nodeName === "input" || nodeName === "textarea" ) {
dest.defaultValue = src.defaultValue; dest.defaultValue = src.defaultValue;
// IE blanks contents when cloning scripts
} else if ( nodeName === "script" && dest.text !== src.text ) {
dest.text = src.text;
} }
// Event data gets referenced instead of copied if the expando // Event data gets referenced instead of copied if the expando
...@@ -569,16 +607,26 @@ jQuery.each({ ...@@ -569,16 +607,26 @@ jQuery.each({
}; };
}); });
function getAll( elem ) { function getAll( context, tag ) {
if ( typeof elem.getElementsByTagName !== "undefined" ) { var elems, elem,
return elem.getElementsByTagName( "*" ); i = 0,
found = typeof context.getElementsByTagName !== "undefined" ? context.getElementsByTagName( tag || "*" ) :
} else if ( typeof elem.querySelectorAll !== "undefined" ) { typeof context.querySelectorAll !== "undefined" ? context.querySelectorAll( tag || "*" ) :
return elem.querySelectorAll( "*" ); undefined;
if ( !found ) {
for ( found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++ ) {
if ( !tag || jQuery.nodeName( elem, tag ) ) {
found.push( elem );
} else { } else {
return []; jQuery.merge( found, getAll( elem, tag ) );
}
} }
}
return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
jQuery.merge( [ context ], found ) :
found;
} }
// Used in clean, fixes the defaultChecked property // Used in clean, fixes the defaultChecked property
...@@ -590,10 +638,8 @@ function fixDefaultChecked( elem ) { ...@@ -590,10 +638,8 @@ function fixDefaultChecked( elem ) {
jQuery.extend({ jQuery.extend({
clone: function( elem, dataAndEvents, deepDataAndEvents ) { clone: function( elem, dataAndEvents, deepDataAndEvents ) {
var srcElements, var destElements, srcElements, node, i, clone,
destElements, inPage = jQuery.contains( elem.ownerDocument, elem );
i,
clone;
if ( jQuery.support.html5Clone || jQuery.isXMLDoc(elem) || !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) { if ( jQuery.support.html5Clone || jQuery.isXMLDoc(elem) || !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) {
// Break the original-clone style connection in IE9/10 (#8909) // Break the original-clone style connection in IE9/10 (#8909)
...@@ -616,190 +662,162 @@ jQuery.extend({ ...@@ -616,190 +662,162 @@ jQuery.extend({
// proprietary methods to clear the events. Thanks to MooTools // proprietary methods to clear the events. Thanks to MooTools
// guys for this hotness. // guys for this hotness.
cloneFixAttributes( elem, clone ); // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
// Using Sizzle here is crazy slow, so we use getElementsByTagName instead
srcElements = getAll( elem );
destElements = getAll( clone ); destElements = getAll( clone );
srcElements = getAll( elem );
// Weird iteration because IE will replace the length property // Weird iteration because IE will replace the length property
// with an element if you are cloning the body and one of the // with an element if you are cloning the body and one of the
// elements on the page has a name or id of "length" // elements on the page has a name or id of "length"
for ( i = 0; srcElements[i]; ++i ) { for ( i = 0; (node = srcElements[i]) != null; ++i ) {
// Ensure that the destination node is not null; Fixes #9587 // Ensure that the destination node is not null; Fixes #9587
if ( destElements[i] ) { if ( destElements[i] ) {
cloneFixAttributes( srcElements[i], destElements[i] ); cloneFixAttributes( node, destElements[i] );
} }
} }
} }
// Copy the events from the original to the clone // Copy the events from the original to the clone
if ( dataAndEvents ) { if ( dataAndEvents ) {
cloneCopyEvent( elem, clone );
if ( deepDataAndEvents ) { if ( deepDataAndEvents ) {
srcElements = getAll( elem );
destElements = getAll( clone ); destElements = getAll( clone );
srcElements = getAll( elem );
for ( i = 0; srcElements[i]; ++i ) { for ( i = 0; (node = srcElements[i]) != null; i++ ) {
cloneCopyEvent( srcElements[i], destElements[i] ); cloneCopyEvent( node, destElements[i] );
} }
} else {
cloneCopyEvent( elem, clone );
} }
} }
srcElements = destElements = null; // Preserve script evaluation history
destElements = getAll( clone, "script" );
if ( destElements.length > 0 ) {
setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
}
destElements = srcElements = node = null;
// Return the cloned set // Return the cloned set
return clone; return clone;
}, },
clean: function( elems, context, fragment, scripts ) { clean: function( elems, context, fragment, scripts ) {
var i, j, elem, tag, wrap, depth, parent, div, hasBody, tbody, handleScript, jsTags, var elem, j, tmp, tag, wrap, tbody,
safe = context === document && safeFragment, ret = [],
ret = []; i = 0,
safe = context === document && safeFragment;
// Ensure that context is a document // Ensure that context is a document
if ( !context || typeof context.createDocumentFragment === "undefined" ) { if ( !context || typeof context.createDocumentFragment === "undefined" ) {
context = document; context = document;
} }
// Use the already-created safe fragment if context permits
for ( i = 0; (elem = elems[i]) != null; i++ ) { for ( i = 0; (elem = elems[i]) != null; i++ ) {
if ( typeof elem === "number" ) { if ( elem || elem === 0 ) {
elem += ""; // Add nodes directly
} if ( typeof elem === "object" ) {
jQuery.merge( ret, elem.nodeType ? [ elem ] : elem );
if ( !elem ) { // Convert non-html into a text node
continue; } else if ( !rhtml.test( elem ) ) {
} ret.push( context.createTextNode( elem ) );
// Convert html string into DOM nodes // Convert html into DOM nodes
if ( typeof elem === "string" ) {
if ( !rhtml.test( elem ) ) {
elem = context.createTextNode( elem );
} else { } else {
// Ensure a safe container in which to render the html // Ensure a safe container
safe = safe || createSafeFragment( context ); safe = safe || createSafeFragment( context );
div = div || safe.appendChild( context.createElement("div") ); tmp = tmp || safe.appendChild( context.createElement("div") );
// Fix "XHTML"-style tags in all browsers
elem = elem.replace(rxhtmlTag, "<$1></$2>");
// Go to html and back, then peel off extra wrappers // Deserialize a standard representation
tag = ( rtagName.exec( elem ) || ["", ""] )[1].toLowerCase(); tag = ( rtagName.exec( elem ) || ["", ""] )[1].toLowerCase();
wrap = wrapMap[ tag ] || wrapMap._default; wrap = wrapMap[ tag ] || wrapMap._default;
depth = wrap[0]; tmp.innerHTML = wrap[1] + addMandatoryAttributes( elem.replace( rxhtmlTag, "<$1></$2>" ) ) + wrap[2];
div.innerHTML = wrap[1] + addMandatoryAttributes( elem ) + wrap[2];
// Move to the right depth // Descend through wrappers to the right content
while ( depth-- ) { j = wrap[0];
div = div.lastChild; while ( j-- ) {
tmp = tmp.lastChild;
}
// Manually add leading whitespace removed by IE
if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
ret.push( context.createTextNode( rleadingWhitespace.exec( elem )[0] ) );
} }
// Remove IE's autoinserted <tbody> from table fragments // Remove IE's autoinserted <tbody> from table fragments
if ( !jQuery.support.tbody ) { if ( !jQuery.support.tbody ) {
// String was a <table>, *may* have spurious <tbody> // String was a <table>, *may* have spurious <tbody>
hasBody = rtbody.test(elem); elem = tag === "table" && !rtbody.test( elem ) ?
tbody = tag === "table" && !hasBody ? tmp.firstChild :
div.firstChild && div.firstChild.childNodes :
// String was a bare <thead> or <tfoot> // String was a bare <thead> or <tfoot>
wrap[1] === "<table>" && !hasBody ? wrap[1] === "<table>" && !rtbody.test( elem ) ?
div.childNodes : tmp :
[]; 0;
for ( j = tbody.length - 1; j >= 0 ; --j ) {
if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
tbody[ j ].parentNode.removeChild( tbody[ j ] );
}
}
}
// IE completely kills leading whitespace when innerHTML is used j = elem && elem.childNodes.length;
if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) { while ( j-- ) {
div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild ); if ( jQuery.nodeName( (tbody = elem.childNodes[j]), "tbody" ) && !tbody.childNodes.length ) {
elem.removeChild( tbody );
} }
elem = div.childNodes;
parent = div;
// Remember the top-level container for proper cleanup
div = safe.lastChild;
} }
} }
if ( elem.nodeType ) { jQuery.merge( ret, tmp.childNodes );
ret.push( elem );
} else {
jQuery.merge( ret, elem );
// Fix #12392 // Fix #12392 for WebKit and IE > 9
if ( parent ) { tmp.textContent = "";
// for WebKit and IE > 9 // Fix #12392 for oldIE
parent.textContent = ""; while ( tmp.firstChild ) {
tmp.removeChild( tmp.firstChild );
// for oldIE
while ( parent.firstChild ) {
parent.removeChild( parent.firstChild );
} }
parent = null; // Remember the top-level container for proper cleanup
tmp = safe.lastChild;
} }
} }
} }
// Fix #11356: Clear elements from safeFragment // Fix #11356: Clear elements from safeFragment
if ( div ) { if ( tmp ) {
safe.removeChild( div ); safe.removeChild( tmp );
} }
elem = div = safe = null;
// Reset defaultChecked for any radios and checkboxes // Reset defaultChecked for any radios and checkboxes
// about to be appended to the DOM in IE 6/7 (#8060) // about to be appended to the DOM in IE 6/7 (#8060)
if ( !jQuery.support.appendChecked ) { if ( !jQuery.support.appendChecked ) {
for ( i = 0; (elem = ret[i]) != null; i++ ) { jQuery.grep( getAll( ret, "input" ), fixDefaultChecked );
if ( jQuery.nodeName( elem, "input" ) ) {
fixDefaultChecked( elem );
} else if ( typeof elem.getElementsByTagName !== "undefined" ) {
jQuery.grep( elem.getElementsByTagName("input"), fixDefaultChecked );
}
}
} }
// Append elements to a provided document fragment
if ( fragment ) { if ( fragment ) {
// Special handling of each script element
handleScript = function( elem ) {
// Check if we consider it executable
if ( !elem.type || rscriptType.test( elem.type ) ) {
// Detach the script and store it in the scripts array (if provided) or the fragment
// Return truthy to indicate that it has been handled
return scripts ?
scripts.push( elem.parentNode ? elem.parentNode.removeChild( elem ) : elem ) :
fragment.appendChild( elem );
}
};
for ( i = 0; (elem = ret[i]) != null; i++ ) { for ( i = 0; (elem = ret[i]) != null; i++ ) {
// Check if we're done after handling an executable script safe = jQuery.contains( elem.ownerDocument, elem );
if ( !( jQuery.nodeName( elem, "script" ) && handleScript( elem ) ) ) {
// Append to fragment and handle embedded scripts // Append to fragment
fragment.appendChild( elem ); fragment.appendChild( elem );
if ( typeof elem.getElementsByTagName !== "undefined" ) { tmp = getAll( elem, "script" );
// handleScript alters the DOM, so use jQuery.merge to ensure snapshot iteration
jsTags = jQuery.grep( jQuery.merge( [], elem.getElementsByTagName("script") ), handleScript ); // Preserve script evaluation history
if ( safe ) {
setGlobalEval( tmp );
}
// Splice the scripts into ret after their former ancestor and advance our index beyond them // Capture executables
ret.splice.apply( ret, [i + 1, 0].concat( jsTags ) ); if ( scripts ) {
i += jsTags.length; for ( j = 0; (elem = tmp[j]) != null; j++ ) {
if ( rscriptType.test( elem.type || "" ) ) {
scripts.push( elem );
} }
} }
} }
} }
}
elem = tmp = safe = null;
return ret; return ret;
}, },
......
...@@ -1372,7 +1372,9 @@ test("clone()", function() { ...@@ -1372,7 +1372,9 @@ test("clone()", function() {
equal( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." ); equal( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
equal( jQuery("body").clone().children()[0].id, "qunit-header", "Make sure cloning body works" ); var body = jQuery("body").clone();
equal( body.children()[0].id, "qunit-header", "Make sure cloning body works" );
body.remove();
}); });
test("clone(script type=non-javascript) (#11359)", function() { test("clone(script type=non-javascript) (#11359)", function() {
...@@ -1382,6 +1384,7 @@ test("clone(script type=non-javascript) (#11359)", function() { ...@@ -1382,6 +1384,7 @@ test("clone(script type=non-javascript) (#11359)", function() {
equal( dest[0].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" ); equal( dest[0].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" ); equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
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" ); 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" );
dest.remove();
}); });
test("clone(form element) (Bug #3879, #6655)", function() { test("clone(form element) (Bug #3879, #6655)", function() {
...@@ -1449,100 +1452,124 @@ test("html() on empty set", function() { ...@@ -1449,100 +1452,124 @@ test("html() on empty set", function() {
strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" ); strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" );
}); });
var testHtml = function(valueObj) { var childNodeNames = function( node ) {
expect(35); return jQuery.map( node.childNodes, function( child ) {
return child.nodeName.toUpperCase();
jQuery["scriptorder"] = 0; }).join(" ");
};
var div = jQuery("#qunit-fixture > div"); var testHtml = function( valueObj ) {
div.html(valueObj("<b>test</b>")); expect( 37 );
var pass = true;
for ( var i = 0; i < div.size(); i++ ) {
if ( div.get(i).childNodes.length != 1 ) {
pass = false;
}
}
ok( pass, "Set HTML" );
div = jQuery("<div/>").html( valueObj("<div id='parent_1'><div id='child_1'/></div><div id='parent_2'/>") ); var actual, expected, tmp,
div = jQuery("<div></div>"),
fixture = jQuery("#qunit-fixture");
equal( div.children().length, 2, "Make sure two child nodes exist." ); div.html( valueObj("<div id='parent_1'><div id='child_1'/></div><div id='parent_2'/>") );
equal( div.children().children().length, 1, "Make sure that a grandchild exists." ); equal( div.children().length, 2, "Found children" );
equal( div.children().children().length, 1, "Found grandchild" );
var space = jQuery("<div/>").html(valueObj("&#160;"))[0].innerHTML; actual = []; expected = [];
ok( /^\xA0$|^&nbsp;$/.test( space ), "Make sure entities are passed through correctly." ); tmp = jQuery("<map/>").html( valueObj("<area alt='area'/>") ).each(function() {
equal( jQuery("<div/>").html(valueObj("&amp;"))[0].innerHTML, "&amp;", "Make sure entities are passed through correctly." ); expected.push("AREA");
actual.push( childNodeNames( this ) );
});
equal( expected.length, 1, "Expecting one parent" );
deepEqual( actual, expected, "Found the inserted area element" );
jQuery("#qunit-fixture").html(valueObj("<style>.foobar{color:green;}</style>")); equal( div.html( valueObj(5) ).html(), "5", "Setting a number as html" );
equal( div.html( valueObj(0) ).html(), "0", "Setting a zero as html" );
equal( jQuery("#qunit-fixture").children().length, 1, "Make sure there is a child element." ); div.html( valueObj("&#160;&amp;") );
equal( jQuery("#qunit-fixture").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." ); equal(
div[0].innerHTML.replace( /\xA0/, "&nbsp;" ),
"&nbsp;&amp;",
"Entities are passed through correctly"
);
QUnit.reset(); tmp = "&lt;div&gt;hello1&lt;/div&gt;";
// using contents will get comments regular, text, and comment nodes equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html" );
var j = jQuery("#nonnodes").contents(); tmp = "x" + tmp;
j.html(valueObj("<b>bold</b>")); equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading x" );
tmp = " " + tmp.slice(1);
equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading space" );
actual = []; expected = []; tmp = {};
jQuery("#nonnodes").contents().html( valueObj("<b>bold</b>") ).each(function() {
var html = jQuery( this ).html();
tmp[ this.nodeType ] = true;
expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
actual.push( html ? html.toLowerCase() : html );
});
deepEqual( actual, expected, "Set containing element, text node, comment" );
ok( tmp[1], "element" );
ok( tmp[3], "text node" );
ok( tmp[8], "comment" );
actual = []; expected = [];
fixture.find("> div").html( valueObj("<b>test</b>") ).each(function() {
expected.push("B");
actual.push( childNodeNames( this ) );
});
equal( expected.length, 6, "Expecting many parents" );
deepEqual( actual, expected, "Correct childNodes after setting HTML" );
// this is needed, or the expando added by jQuery unique will yield a different html actual = []; expected = [];
j.find("b").removeData(); fixture.html( valueObj("<style>.foobar{color:green;}</style>") ).each(function() {
equal( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" ); expected.push("STYLE");
actual.push( childNodeNames( this ) );
});
equal( expected.length, 1, "Expecting one parent" );
deepEqual( actual, expected, "Found the inserted style element" );
jQuery("#qunit-fixture").html(valueObj("<select/>")); fixture.html( valueObj("<select/>") );
jQuery("#qunit-fixture select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>")); jQuery("#qunit-fixture select").html( valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>") );
equal( jQuery("#qunit-fixture select").val(), "O2", "Selected option correct" ); equal( jQuery("#qunit-fixture select").val(), "O2", "Selected option correct" );
var $div = jQuery("<div />"); tmp = fixture.html(
equal( $div.html(valueObj( 5 )).html(), "5", "Setting a number as html" ); valueObj([
equal( $div.html(valueObj( 0 )).html(), "0", "Setting a zero as html" ); "<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
"<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
var $div2 = jQuery("<div/>"), insert = "&lt;div&gt;hello1&lt;/div&gt;"; "<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
equal( $div2.html(insert).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." ); "<script>ok( true, 'evaluated: no type' );</script>",
equal( $div2.html("x" + insert).html().replace(/>/g, "&gt;"), "x" + insert, "Verify escaped insertion." ); "<div>",
equal( $div2.html(" " + insert).html().replace(/>/g, "&gt;"), " " + insert, "Verify escaped insertion." ); "<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
"<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>")); "<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
"<script>ok( true, 'evaluated: inner no type' );</script>",
equal( map[0].childNodes.length, 1, "The area was inserted." ); "</div>"
equal( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." ); ].join(""))
).find("script");
QUnit.reset(); equal( tmp.length, 8, "All script tags remain." );
equal( tmp[0].type, "something/else", "Non-evaluated type." );
jQuery("#qunit-fixture").html(valueObj("<script type='something/else'>ok( false, 'Non-script evaluated.' );</script><script type='text/javascript'>ok( true, 'text/javascript is evaluated.' );</script><script>ok( true, 'No type is evaluated.' );</script><div><script type='text/javascript'>ok( true, 'Inner text/javascript is evaluated.' );</script><script>ok( true, 'Inner No type is evaluated.' );</script><script type='something/else'>ok( false, 'Non-script evaluated.' );</script><script type='type/ecmascript'>ok( true, 'type/ecmascript evaluated.' );</script></div>")); equal( tmp[1].type, "text/javascript", "Evaluated type." );
var child = jQuery("#qunit-fixture").find("script"); fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
equal( child.length, 2, "Make sure that two non-JavaScript script tags are left." ); fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
equal( child[0].type, "something/else", "Verify type of script tag." ); fixture.html( valueObj("foo <form><script type='text/javascript'>ok( true, 'Injection of identical script (#975)' );</script></form>") );
equal( child[1].type, "something/else", "Verify type of script tag." );
jQuery.scriptorder = 0;
jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>")); fixture.html( valueObj([
jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>")); "<script>",
jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>")); "equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
"equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
jQuery("#qunit-fixture").html(valueObj("<script type='text/javascript'>ok( true, 'jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)' );</script>")); "</script>",
"<span id='scriptorder'><script>equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
jQuery("#qunit-fixture").html(valueObj("foo <form><script type='text/javascript'>ok( true, 'jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (2)' );</script></form>")); "<script>equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
].join("")) );
jQuery("#qunit-fixture").html(valueObj("<script>equal(jQuery.scriptorder++, 0, 'Script is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equal(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equal(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>"));
QUnit.reset();
fixture.html( valueObj( fixture.text() ) );
ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
}; };
test("html(String)", function() { test("html(String)", function() {
testHtml(manipulationBareObj); testHtml( manipulationBareObj );
}); });
test("html(Function)", function() { test("html(Function)", function() {
testHtml(manipulationFunctionReturningObj); testHtml( manipulationFunctionReturningObj );
expect(37);
QUnit.reset();
jQuery("#qunit-fixture").html(function(){
return jQuery(this).text();
});
ok( !/</.test( jQuery("#qunit-fixture").html() ), "Replace html with text." );
ok( jQuery("#qunit-fixture").html().length > 0, "Make sure text exists." );
}); });
test("html(Function) with incoming value", function() { test("html(Function) with incoming value", function() {
...@@ -2083,3 +2110,59 @@ testIframeWithCallback( "buildFragment works even if document[0] is iframe's win ...@@ -2083,3 +2110,59 @@ testIframeWithCallback( "buildFragment works even if document[0] is iframe's win
ok( test.status, test.description ); ok( test.status, test.description );
}); });
test("script evaluation (#11795)", function() {
expect(11);
var scriptsIn, scriptsOut,
fixture = jQuery("#qunit-fixture").empty(),
objGlobal = (function(){ return this; })(),
isOk = objGlobal.ok,
notOk = function() {
var args = arguments;
args[0] = !args[0];
return isOk.apply( this, args );
};
objGlobal.ok = notOk;
scriptsIn = jQuery([
"<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
"<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
"<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
"<script>ok( true, 'evaluated: no type' );</script>",
"<div>",
"<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
"<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
"<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
"<script>ok( true, 'evaluated: inner no type' );</script>",
"</div>"
].join(""));
scriptsIn.appendTo( jQuery("<div class='detached'/>") );
objGlobal.ok = isOk;
scriptsOut = fixture.append( scriptsIn ).find("script");
equal( scriptsOut[0].type, "something/else", "Non-evaluated type." );
equal( scriptsOut[1].type, "text/javascript", "Evaluated type." );
deepEqual( scriptsOut.get(), fixture.find("script").get(), "All script tags remain." );
objGlobal.ok = notOk;
scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find("div") );
deepEqual( fixture.find("div script").get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
fixture.append( scriptsOut.detach() );
deepEqual( fixture.find("> script").get(), scriptsOut.get(), "Scripts detached without reevaluation" );
objGlobal.ok = isOk;
});
test("wrapping scripts (#10470)", function() {
expect(2);
var script = document.createElement("script");
script.text = script.textContent =
"ok( !document.eval10470, 'script evaluated once' ); document.eval10470 = true;";
document.eval10470 = false;
jQuery("#qunit-fixture").empty()[0].appendChild( script );
jQuery("#qunit-fixture script").wrap("<b></b>");
strictEqual( script.parentNode, jQuery("#qunit-fixture > b")[0], "correctly wrapped" );
jQuery( script ).remove();
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment