Commit dc93f8cb authored by Rick Waldron's avatar Rick Waldron

Merge branch '2.0-manipulation' of https://github.com/orkel/jquery

* '2.0-manipulation' of https://github.com/orkel/jquery: (51 commits)
  Rename fixCloneNodeIssues helper to fixInput Move support comment
  Add tests for th element
  Add empty line
  Add more tests for wrapMap
  Add test for td append
  wrapMap.col = wrapMap.tr
  wrapMap.thead = wrapMap.tr Add support comments
  Rewrite tests for colgroup, caption Add tests for thead, tbody, tfoot
  Remove fix for #11280
  Remove fieldset from wrapMap
  Add comments for optgroup and option elements in wrapMap Add tests for optgroup element
  Remove fix for #4484, add more tests
  Use tabs instead of spaces
  Simplify getAll helper
  Simplify check for non-Elements
  Remove fix for object element
  Save 43 bytes
  Use common loop iteration idiom
  Remove fix for #9587
  Use correct comments
  ...
parents a205aac7 39e6792b
function createSafeFragment( document ) { var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,
var list = nodeNames.split( "|" ),
safeFrag = document.createDocumentFragment();
if ( safeFrag.createElement ) {
while ( list.length ) {
safeFrag.createElement(
list.pop()
);
}
}
return safeFrag;
}
var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|" +
"header|hgroup|mark|meter|nav|output|progress|section|summary|time|video",
rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g,
rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i"),
rleadingWhitespace = /^\s+/,
rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,
rtagName = /<([\w:]+)/, rtagName = /<([\w:]+)/,
rtbody = /<tbody/i,
rhtml = /<|&#?\w+;/, rhtml = /<|&#?\w+;/,
rnoInnerhtml = /<(?:script|style|link)/i, rnoInnerhtml = /<(?:script|style|link)/i,
manipulation_rcheckableType = /^(?:checkbox|radio)$/i, manipulation_rcheckableType = /^(?:checkbox|radio)$/i,
...@@ -29,24 +9,19 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca ...@@ -29,24 +9,19 @@ var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figca
rscriptTypeMasked = /^true\/(.*)/, rscriptTypeMasked = /^true\/(.*)/,
rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g, rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g,
wrapMap = { wrapMap = {
// Support: IE 9
option: [ 1, "<select multiple='multiple'>" ], option: [ 1, "<select multiple='multiple'>" ],
legend: [ 1, "<fieldset>" ],
area: [ 1, "<map>" ],
param: [ 1, "<object>" ],
thead: [ 1, "<table>" ],
tr: [ 2, "<table><tbody>" ],
col: [ 2, "<table><tbody></tbody><colgroup>", "</table>" ],
td: [ 3, "<table><tbody><tr>" ],
// IE6-8 can't serialize link, script, style, or any html5 (NoScope) tags, tr: [ 1, "<table>" ],
// unless wrapped in a div with non-breaking characters in front of it. td: [ 3, "<table><tbody><tr>" ],
_default: jQuery.support.htmlSerialize ? [ 0, "" ] : [ 1, "X<div>" ] _default: [ 0, "" ]
}, };
safeFragment = createSafeFragment( document ),
fragmentDiv = safeFragment.appendChild( document.createElement("div") );
// Support: IE 9
wrapMap.optgroup = wrapMap.option; wrapMap.optgroup = wrapMap.option;
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead = wrapMap.col = wrapMap.tr;
wrapMap.th = wrapMap.td; wrapMap.th = wrapMap.td;
jQuery.fn.extend({ jQuery.fn.extend({
...@@ -89,8 +64,8 @@ jQuery.fn.extend({ ...@@ -89,8 +64,8 @@ jQuery.fn.extend({
wrapInner: function( html ) { wrapInner: function( html ) {
if ( jQuery.isFunction( html ) ) { if ( jQuery.isFunction( html ) ) {
return this.each(function(i) { return this.each(function( i ) {
jQuery(this).wrapInner( html.call(this, i) ); jQuery( this ).wrapInner( html.call(this, i) );
}); });
} }
...@@ -110,7 +85,7 @@ jQuery.fn.extend({ ...@@ -110,7 +85,7 @@ jQuery.fn.extend({
wrap: function( html ) { wrap: function( html ) {
var isFunction = jQuery.isFunction( html ); var isFunction = jQuery.isFunction( html );
return this.each(function(i) { return this.each(function( i ) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html ); jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
}); });
}, },
...@@ -140,7 +115,7 @@ jQuery.fn.extend({ ...@@ -140,7 +115,7 @@ jQuery.fn.extend({
}, },
before: function() { before: function() {
return this.domManip( arguments, false, function( elem ) { return this.domManip(arguments, false, function( elem ) {
if ( this.parentNode ) { if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this ); this.parentNode.insertBefore( elem, this );
} }
...@@ -148,7 +123,7 @@ jQuery.fn.extend({ ...@@ -148,7 +123,7 @@ jQuery.fn.extend({
}, },
after: function() { after: function() {
return this.domManip( arguments, false, function( elem ) { return this.domManip(arguments, false, function( elem ) {
if ( this.parentNode ) { if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this.nextSibling ); this.parentNode.insertBefore( elem, this.nextSibling );
} }
...@@ -158,9 +133,12 @@ jQuery.fn.extend({ ...@@ -158,9 +133,12 @@ jQuery.fn.extend({
// keepData is for internal use only--do not document // keepData is for internal use only--do not document
remove: function( selector, keepData ) { remove: function( selector, keepData ) {
var elem, var elem,
i = 0; i = 0,
l = this.length;
for ( ; i < l; i++ ) {
elem = this[ i ];
for ( ; (elem = this[i]) != null; i++ ) {
if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) { if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) {
if ( !keepData && elem.nodeType === 1 ) { if ( !keepData && elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem ) ); jQuery.cleanData( getAll( elem ) );
...@@ -180,17 +158,19 @@ jQuery.fn.extend({ ...@@ -180,17 +158,19 @@ jQuery.fn.extend({
empty: function() { empty: function() {
var elem, var elem,
i = 0; i = 0,
l = this.length;
for ( ; i < l; i++ ) {
elem = this[ i ];
for ( ; (elem = this[i]) != null; i++ ) {
// Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
// Prevent memory leaks
jQuery.cleanData( getAll( elem, false ) ); jQuery.cleanData( getAll( elem, false ) );
}
// Remove any remaining nodes // Remove any remaining nodes
while ( elem.firstChild ) { elem.textContent = "";
elem.removeChild( elem.firstChild );
} }
} }
...@@ -212,24 +192,21 @@ jQuery.fn.extend({ ...@@ -212,24 +192,21 @@ jQuery.fn.extend({
i = 0, i = 0,
l = this.length; l = this.length;
if ( value === undefined ) { if ( value === undefined && elem.nodeType === 1 ) {
return elem.nodeType === 1 ? return elem.innerHTML;
elem.innerHTML.replace( rinlinejQuery, "" ) :
undefined;
} }
// See if we can take a shortcut and just use innerHTML // See if we can take a shortcut and just use innerHTML
if ( typeof value === "string" && !rnoInnerhtml.test( value ) && if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
( jQuery.support.htmlSerialize || !rnoshimcache.test( value ) ) && !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
( jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value ) ) &&
!wrapMap[ ( rtagName.exec( value ) || ["", ""] )[1].toLowerCase() ] ) {
value = value.replace( rxhtmlTag, "<$1></$2>" ); value = value.replace( rxhtmlTag, "<$1></$2>" );
try { try {
for (; i < l; i++ ) { for ( ; i < l; i++ ) {
elem = this[ i ] || {};
// Remove element nodes and prevent memory leaks // Remove element nodes and prevent memory leaks
elem = this[i] || {};
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) ); jQuery.cleanData( getAll( elem, false ) );
elem.innerHTML = value; elem.innerHTML = value;
...@@ -239,7 +216,7 @@ jQuery.fn.extend({ ...@@ -239,7 +216,7 @@ jQuery.fn.extend({
elem = 0; elem = 0;
// If using innerHTML throws an exception, use the fallback method // If using innerHTML throws an exception, use the fallback method
} catch(e) {} } catch( e ) {}
} }
if ( elem ) { if ( elem ) {
...@@ -249,11 +226,11 @@ jQuery.fn.extend({ ...@@ -249,11 +226,11 @@ jQuery.fn.extend({
}, },
replaceWith: function( value ) { replaceWith: function( value ) {
var isFunc = jQuery.isFunction( value ); var isFunction = jQuery.isFunction( value );
// Make sure that the elements are removed from the DOM before they are inserted // Make sure that the elements are removed from the DOM before they are inserted
// this can help fix replacing a parent with child elements // this can help fix replacing a parent with child elements
if ( !isFunc && typeof value !== "string" ) { if ( !isFunction && typeof value !== "string" ) {
value = jQuery( value ).not( this ).detach(); value = jQuery( value ).not( this ).detach();
} }
...@@ -296,14 +273,14 @@ jQuery.fn.extend({ ...@@ -296,14 +273,14 @@ jQuery.fn.extend({
return this.each(function( index ) { return this.each(function( index ) {
var self = set.eq( index ); var self = set.eq( index );
if ( isFunction ) { if ( isFunction ) {
args[0] = value.call( this, index, table ? self.html() : undefined ); args[ 0 ] = value.call( this, index, table ? self.html() : undefined );
} }
self.domManip( args, table, callback ); self.domManip( args, table, callback );
}); });
} }
if ( this[0] ) { if ( l ) {
doc = this[0].ownerDocument; doc = this[ 0 ].ownerDocument;
fragment = doc.createDocumentFragment(); fragment = doc.createDocumentFragment();
jQuery.clean( args, doc, fragment, undefined, this ); jQuery.clean( args, doc, fragment, undefined, this );
first = fragment.firstChild; first = fragment.firstChild;
...@@ -327,14 +304,14 @@ jQuery.fn.extend({ ...@@ -327,14 +304,14 @@ jQuery.fn.extend({
// Keep references to cloned scripts for later restoration // Keep references to cloned scripts for later restoration
if ( hasScripts ) { if ( hasScripts ) {
jQuery.merge( scripts, getAll( node, "script" ) ); core_push.apply( 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 ],
node, node,
i i
); );
...@@ -363,14 +340,11 @@ jQuery.fn.extend({ ...@@ -363,14 +340,11 @@ jQuery.fn.extend({
"throws": true "throws": true
}); });
} else { } else {
jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) ); jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
} }
} }
} }
} }
// Fix #11809: Avoid leaking memory
fragment = first = null;
} }
} }
...@@ -378,130 +352,6 @@ jQuery.fn.extend({ ...@@ -378,130 +352,6 @@ jQuery.fn.extend({
} }
}); });
function findOrAppend( elem, 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 ) {
if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
return;
}
var type, i, l,
oldData = jQuery._data( src ),
curData = jQuery._data( dest, oldData ),
events = oldData.events;
if ( events ) {
delete curData.handle;
curData.events = {};
for ( type in events ) {
for ( i = 0, l = events[ type ].length; i < l; i++ ) {
jQuery.event.add( dest, type, events[ type ][ i ] );
}
}
}
// make the cloned public data object a copy from the original
if ( curData.data ) {
curData.data = jQuery.extend( {}, curData.data );
}
}
function fixCloneNodeIssues( src, dest ) {
var nodeName, data, e;
// We do not need to do anything for non-Elements
if ( dest.nodeType !== 1 ) {
return;
}
nodeName = dest.nodeName.toLowerCase();
// IE6-8 copies events bound via attachEvent when using cloneNode.
if ( !jQuery.support.noCloneEvent && dest[ jQuery.expando ] ) {
data = jQuery._data( dest );
for ( e in data.events ) {
jQuery.removeEvent( dest, e, data.handle );
}
// Event data gets referenced instead of copied if the expando gets copied too
dest.removeAttribute( jQuery.expando );
}
// 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.
// IE10 throws NoModificationAllowedError if parent is null, #12132.
} else if ( nodeName === "object" ) {
if ( dest.parentNode ) {
dest.outerHTML = src.outerHTML;
}
// This path appears unavoidable for IE9. When cloning an object
// element in IE9, the outerHTML strategy above is not sufficient.
// If the src has innerHTML and the destination does not,
// copy the src.innerHTML into the dest.innerHTML. #10324
if ( jQuery.support.html5Clone && ( src.innerHTML && !jQuery.trim(dest.innerHTML) ) ) {
dest.innerHTML = src.innerHTML;
}
} else if ( nodeName === "input" && manipulation_rcheckableType.test( src.type ) ) {
// IE6-8 fails to persist the checked state of a cloned checkbox
// or radio button. Worse, IE6-7 fail to give the cloned element
// a checked appearance if the defaultChecked value isn't also set
dest.defaultChecked = dest.checked = src.checked;
// IE6-7 get confused and end up setting the value of a cloned
// checkbox/radio button to an empty string instead of "on"
if ( dest.value !== src.value ) {
dest.value = src.value;
}
// IE6-8 fails to return the selected option to the default selected
// state when cloning options
} else if ( nodeName === "option" ) {
dest.defaultSelected = dest.selected = src.defaultSelected;
// IE6-8 fails to set the defaultValue to the correct value when
// cloning other types of input fields
} else if ( nodeName === "input" || nodeName === "textarea" ) {
dest.defaultValue = src.defaultValue;
}
}
jQuery.each({ jQuery.each({
appendTo: "append", appendTo: "append",
prependTo: "prepend", prependTo: "prepend",
...@@ -511,79 +361,38 @@ jQuery.each({ ...@@ -511,79 +361,38 @@ jQuery.each({
}, function( name, original ) { }, function( name, original ) {
jQuery.fn[ name ] = function( selector ) { jQuery.fn[ name ] = function( selector ) {
var elems, var elems,
i = 0,
ret = [], ret = [],
insert = jQuery( selector ), insert = jQuery( selector ),
last = insert.length - 1; last = insert.length - 1,
i = 0;
for ( ; i <= last; i++ ) { for ( ; i <= last; i++ ) {
elems = i === last ? this : this.clone(true); elems = i === last ? this : this.clone( true );
jQuery( insert[i] )[ original ]( elems ); jQuery( insert[ i ] )[ original ]( elems );
// Modern browsers can apply jQuery collections as arrays, but oldIE needs a .get() core_push.apply( ret, elems );
core_push.apply( ret, elems.get() );
} }
return this.pushStack( ret ); return this.pushStack( ret );
}; };
}); });
function getAll( context, tag ) {
var elems, elem,
i = 0,
found = typeof context.getElementsByTagName !== "undefined" ? context.getElementsByTagName( tag || "*" ) :
typeof context.querySelectorAll !== "undefined" ? context.querySelectorAll( tag || "*" ) :
undefined;
if ( !found ) {
for ( found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++ ) {
if ( !tag || jQuery.nodeName( elem, tag ) ) {
found.push( elem );
} else {
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
function fixDefaultChecked( elem ) {
if ( manipulation_rcheckableType.test( elem.type ) ) {
elem.defaultChecked = elem.checked;
}
}
jQuery.extend({ jQuery.extend({
clone: function( elem, dataAndEvents, deepDataAndEvents ) { clone: function( elem, dataAndEvents, deepDataAndEvents ) {
var destElements, srcElements, node, i, clone, var i, l, srcElements, destElements,
clone = elem.cloneNode( true ),
inPage = jQuery.contains( elem.ownerDocument, elem ); inPage = jQuery.contains( elem.ownerDocument, elem );
if ( jQuery.support.html5Clone || jQuery.isXMLDoc(elem) || !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) { // Support: IE >=9
clone = elem.cloneNode( true ); // Fix Cloning issues
if ( !jQuery.support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) && !jQuery.isXMLDoc( elem ) ) {
// IE<=8 does not properly clone detached, unknown element nodes
} else {
fragmentDiv.innerHTML = elem.outerHTML;
fragmentDiv.removeChild( clone = fragmentDiv.firstChild );
}
if ( (!jQuery.support.noCloneEvent || !jQuery.support.noCloneChecked) &&
(elem.nodeType === 1 || elem.nodeType === 11) && !jQuery.isXMLDoc(elem) ) {
// We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2 // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2
destElements = getAll( clone ); destElements = getAll( clone );
srcElements = getAll( elem ); srcElements = getAll( elem );
// Fix all IE cloning issues for ( i = 0, l = srcElements.length; i < l; i++ ) {
for ( i = 0; (node = srcElements[i]) != null; ++i ) { fixInput( srcElements[ i ], destElements[ i ] );
// Ensure that the destination node is not null; Fixes #9587
if ( destElements[i] ) {
fixCloneNodeIssues( node, destElements[i] );
}
} }
} }
...@@ -593,8 +402,8 @@ jQuery.extend({ ...@@ -593,8 +402,8 @@ jQuery.extend({
srcElements = srcElements || getAll( elem ); srcElements = srcElements || getAll( elem );
destElements = destElements || getAll( clone ); destElements = destElements || getAll( clone );
for ( i = 0; (node = srcElements[i]) != null; i++ ) { for ( i = 0, l = srcElements.length; i < l; i++ ) {
cloneCopyEvent( node, destElements[i] ); cloneCopyEvent( srcElements[ i ], destElements[ i ] );
} }
} else { } else {
cloneCopyEvent( elem, clone ); cloneCopyEvent( elem, clone );
...@@ -607,27 +416,30 @@ jQuery.extend({ ...@@ -607,27 +416,30 @@ jQuery.extend({
setGlobalEval( destElements, !inPage && getAll( elem, "script" ) ); 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, selection ) { clean: function( elems, context, fragment, scripts, selection ) {
var elem, i, j, tmp, tag, wrap, tbody, var elem, tmp, tag, wrap, j, ll,
i = 0,
l = elems.length,
ret = [], ret = [],
safe = context === document && safeFragment; container = context === document && fragment;
// 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;
} }
for ( i = 0; (elem = elems[i]) != null; i++ ) { for ( ; i < l; 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( ret, elem.nodeType ? [ elem ] : elem ); core_push.apply( ret, elem.nodeType ? [ elem ] : elem );
// Convert non-html into a text node // Convert non-html into a text node
} else if ( !rhtml.test( elem ) ) { } else if ( !rhtml.test( elem ) ) {
...@@ -635,14 +447,15 @@ jQuery.extend({ ...@@ -635,14 +447,15 @@ jQuery.extend({
// Convert html into DOM nodes // Convert html into DOM nodes
} else { } else {
// Ensure a safe container // Ensure a safe container
safe = safe || createSafeFragment( context ); container = container || context.createDocumentFragment();
tmp = tmp || safe.appendChild( context.createElement("div") ); tmp = tmp || container.appendChild( context.createElement("div") );
// Deserialize a standard representation // 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;
tmp.innerHTML = wrap[1] + elem.replace( rxhtmlTag, "<$1></$2>" ) + ( wrap[2] || "" ); tmp.innerHTML = wrap[ 1 ] + elem.replace( rxhtmlTag, "<$1></$2>" );
// Descend through wrappers to the right content // Descend through wrappers to the right content
j = wrap[0]; j = wrap[0];
...@@ -650,61 +463,26 @@ jQuery.extend({ ...@@ -650,61 +463,26 @@ jQuery.extend({
tmp = tmp.lastChild; tmp = tmp.lastChild;
} }
// Manually add leading whitespace removed by IE core_push.apply( ret, tmp.childNodes );
if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
ret.push( context.createTextNode( rleadingWhitespace.exec( elem )[0] ) );
}
// Remove IE's autoinserted <tbody> from table fragments
if ( !jQuery.support.tbody ) {
// String was a <table>, *may* have spurious <tbody>
elem = tag === "table" && !rtbody.test( elem ) ?
tmp.firstChild :
// String was a bare <thead> or <tfoot> // Fix #12392 - remove childNodes parent
wrap[1] === "<table>" && !rtbody.test( elem ) ?
tmp :
0;
j = elem && elem.childNodes.length;
while ( j-- ) {
if ( jQuery.nodeName( (tbody = elem.childNodes[j]), "tbody" ) && !tbody.childNodes.length ) {
elem.removeChild( tbody );
}
}
}
jQuery.merge( ret, tmp.childNodes );
// Fix #12392 for WebKit and IE > 9
tmp.textContent = ""; tmp.textContent = "";
// Fix #12392 for oldIE
while ( tmp.firstChild ) {
tmp.removeChild( tmp.firstChild );
}
// Remember the top-level container for proper cleanup // Remember the top-level container for proper cleanup
tmp = safe.lastChild; tmp = container.lastChild;
} }
} }
} }
// Fix #11356: Clear elements from safeFragment // Fix #11356: Clear elements from fragment
if ( tmp ) { if ( tmp ) {
safe.removeChild( tmp ); container.removeChild( tmp );
}
// Reset defaultChecked for any radios and checkboxes
// about to be appended to the DOM in IE 6/7 (#8060)
if ( !jQuery.support.appendChecked ) {
jQuery.grep( getAll( ret, "input" ), fixDefaultChecked );
} }
if ( fragment ) { if ( fragment ) {
for ( i = 0; (elem = ret[i]) != null; i++ ) { for ( i = 0, l = ret.length; i < l; i++ ) {
safe = jQuery.contains( elem.ownerDocument, elem ); elem = ret[ i ];
container = jQuery.contains( elem.ownerDocument, elem );
// Append to fragment // Append to fragment
// #4087 - If origin and destination elements are the same, and this is // #4087 - If origin and destination elements are the same, and this is
...@@ -715,13 +493,15 @@ jQuery.extend({ ...@@ -715,13 +493,15 @@ jQuery.extend({
tmp = getAll( elem, "script" ); tmp = getAll( elem, "script" );
// Preserve script evaluation history // Preserve script evaluation history
if ( safe ) { if ( container ) {
setGlobalEval( tmp ); setGlobalEval( tmp );
} }
// Capture executables // Capture executables
if ( scripts ) { if ( scripts ) {
for ( j = 0; (elem = tmp[j]) != null; j++ ) { for ( j = 0, ll = tmp.length; j < ll; j++ ) {
elem = tmp[ j ];
if ( rscriptType.test( elem.type || "" ) ) { if ( rscriptType.test( elem.type || "" ) ) {
scripts.push( elem ); scripts.push( elem );
} }
...@@ -730,20 +510,19 @@ jQuery.extend({ ...@@ -730,20 +510,19 @@ jQuery.extend({
} }
} }
elem = tmp = safe = null;
return ret; return ret;
}, },
cleanData: function( elems, /* internal */ acceptData ) { cleanData: function( elems, /* internal */ acceptData ) {
var data, id, elem, type, var id, data, elem, type,
l = elems.length,
i = 0, i = 0,
internalKey = jQuery.expando, internalKey = jQuery.expando,
cache = jQuery.cache, cache = jQuery.cache,
deleteExpando = jQuery.support.deleteExpando,
special = jQuery.event.special; special = jQuery.event.special;
for ( ; (elem = elems[i]) != null; i++ ) { for ( ; i < l; i++ ) {
elem = elems[ i ];
if ( acceptData || jQuery.acceptData( elem ) ) { if ( acceptData || jQuery.acceptData( elem ) ) {
...@@ -751,40 +530,108 @@ jQuery.extend({ ...@@ -751,40 +530,108 @@ jQuery.extend({
data = id && cache[ id ]; data = id && cache[ id ];
if ( data ) { if ( data ) {
if ( data.events ) { for ( type in data.events ) {
for ( type in data.events ) { if ( special[ type ] ) {
if ( special[ type ] ) { jQuery.event.remove( elem, type );
jQuery.event.remove( elem, type );
// This is a shortcut to avoid jQuery.event.remove's overhead // This is a shortcut to avoid jQuery.event.remove's overhead
} else { } else {
jQuery.removeEvent( elem, type, data.handle ); jQuery.removeEvent( elem, type, data.handle );
}
} }
} }
// Remove cache only if it was not already removed by jQuery.event.remove // Remove cache only if it was not already removed by jQuery.event.remove
if ( cache[ id ] ) { if ( cache[ id ] ) {
delete cache[ id ]; delete cache[ id ];
delete elem[ internalKey ];
}
}
}
}
}
});
// IE does not allow us to delete expando properties from nodes, function findOrAppend( elem, tag ) {
// nor does it have a removeAttribute function on Document nodes; return elem.getElementsByTagName( tag )[ 0 ] || elem.appendChild( elem.ownerDocument.createElement(tag) );
// we must handle all of these cases }
if ( deleteExpando ) {
delete elem[ internalKey ];
} else if ( elem.removeAttribute ) { // Replace/restore the type attribute of script elements for safe DOM manipulation
elem.removeAttribute( internalKey ); 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 );
} else { if ( match ) {
elem[ internalKey ] = null; elem.type = match[ 1 ];
}
core_deletedIds.push( id ); } else {
} elem.removeAttribute("type");
} }
return elem;
}
// Mark scripts as having already been evaluated
function setGlobalEval( elems, refElements ) {
var l = elems.length,
i = 0;
for ( ; i < l; i++ ) {
jQuery._data( elems[ i ], "globalEval", !refElements || jQuery._data( refElements[ i ], "globalEval" ) );
}
}
function cloneCopyEvent( src, dest ) {
if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
return;
}
var i, l, type,
oldData = jQuery._data( src ),
curData = jQuery._data( dest, oldData ),
events = oldData.events;
if ( events ) {
delete curData.handle;
curData.events = {};
for ( type in events ) {
for ( i = 0, l = events[ type ].length; i < l; i++ ) {
jQuery.event.add( dest, type, events[ type ][ i ] );
} }
} }
} }
});
// make the cloned public data object a copy from the original
if ( curData.data ) {
curData.data = jQuery.extend( {}, curData.data );
}
}
function getAll( context, tag ) {
var ret = context.getElementsByTagName ? context.getElementsByTagName( tag || "*" ) :
context.querySelectorAll ? context.querySelectorAll( tag || "*" ) :
[];
return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
jQuery.merge( [ context ], ret ) :
ret;
}
// Support: IE >= 9
function fixInput( src, dest ) {
var nodeName = dest.nodeName.toLowerCase();
// Fails to persist the checked state of a cloned checkbox or radio button.
if ( nodeName === "input" && manipulation_rcheckableType.test( src.type ) ) {
dest.checked = src.checked;
// Fails to return the selected option to the default selected state when cloning options
} else if ( nodeName === "input" || nodeName === "textarea" ) {
dest.defaultValue = src.defaultValue;
}
}
...@@ -395,13 +395,13 @@ var testAppendForObject = function( valueObj, isFragment ) { ...@@ -395,13 +395,13 @@ var testAppendForObject = function( valueObj, isFragment ) {
var testAppend = function( valueObj ) { var testAppend = function( valueObj ) {
expect( 59 ); expect( 77 );
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 ) { ...@@ -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");
jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
$table.append( valueObj( "<" + name + "/>" ) );
equal( $table.find( name ).length, 1, "Append " + name );
ok( jQuery.clean( ["<" + 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>") );
...@@ -2240,9 +2251,19 @@ test( "insertAfter, insertBefore, etc do not work when destination is original e ...@@ -2240,9 +2251,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" );
});
});
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();
jQuery("<div/><div/>").before(function( index ) { equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
equal( index, i++, "Index should be correct" );
});
}); });
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