From 0ed497b04595eab5beca1441a134761b11fc9b2f Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 8 Jan 2013 03:25:26 +0000 Subject: [PATCH] Resurrect buildFragment and sacrifice jQuery.clean. See gh-1117. --- src/core.js | 3 +-- src/manipulation.js | 48 +++++++++++++++++++-------------------- test/unit/core.js | 9 +++++++- test/unit/manipulation.js | 18 --------------- 4 files changed, 32 insertions(+), 46 deletions(-) diff --git a/src/core.js b/src/core.js index 656fc22f14..8651677578 100644 --- a/src/core.js +++ b/src/core.js @@ -496,8 +496,7 @@ jQuery.extend({ return [ context.createElement( parsed[1] ) ]; } - parsed = context.createDocumentFragment(); - jQuery.clean( [ data ], context, parsed, scripts ); + parsed = jQuery.buildFragment( [ data ], context, scripts ); if ( scripts ) { jQuery( scripts ).remove(); } diff --git a/src/manipulation.js b/src/manipulation.js index 3393dd0a56..416e4c3d64 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -310,8 +310,7 @@ jQuery.fn.extend({ if ( this[0] ) { doc = this[0].ownerDocument; - fragment = doc.createDocumentFragment(); - jQuery.clean( args, doc, fragment, undefined, this ); + fragment = jQuery.buildFragment( args, doc, false, this ); first = fragment.firstChild; if ( fragment.childNodes.length === 1 ) { @@ -556,7 +555,7 @@ function getAll( context, tag ) { found; } -// Used in clean, fixes the defaultChecked property +// Used in buildFragment, fixes the defaultChecked property function fixDefaultChecked( elem ) { if ( manipulation_rcheckableType.test( elem.type ) ) { elem.defaultChecked = elem.checked; @@ -619,9 +618,10 @@ jQuery.extend({ return clone; }, - clean: function( elems, context, fragment, scripts, selection ) { + buildFragment: function( elems, context, scripts, selection ) { var elem, i, j, tmp, tag, wrap, tbody, ret = [], + fragment = context.createDocumentFragment(), safe = context === document && safeFragment; // Ensure that context is a document @@ -708,29 +708,27 @@ jQuery.extend({ jQuery.grep( getAll( ret, "input" ), fixDefaultChecked ); } - if ( fragment ) { - for ( i = 0; (elem = ret[i]) != null; i++ ) { - safe = jQuery.contains( elem.ownerDocument, elem ); + for ( i = 0; (elem = ret[i]) != null; i++ ) { + safe = jQuery.contains( elem.ownerDocument, elem ); - // Append to fragment - // #4087 - If origin and destination elements are the same, and this is - // that element, do not append to fragment - if ( !selection || jQuery.inArray( elem, selection ) === -1 ) { - fragment.appendChild( elem ); - } - tmp = getAll( elem, "script" ); + // Append to fragment + // #4087 - If origin and destination elements are the same, and this is + // 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 - if ( safe ) { - setGlobalEval( tmp ); - } + // Preserve script evaluation history + if ( safe ) { + setGlobalEval( tmp ); + } - // Capture executables - if ( scripts ) { - for ( j = 0; (elem = tmp[j]) != null; j++ ) { - if ( rscriptType.test( elem.type || "" ) ) { - scripts.push( elem ); - } + // Capture executables + if ( scripts ) { + for ( j = 0; (elem = tmp[j]) != null; j++ ) { + if ( rscriptType.test( elem.type || "" ) ) { + scripts.push( elem ); } } } @@ -738,7 +736,7 @@ jQuery.extend({ elem = tmp = safe = null; - return ret; + return fragment; }, cleanData: function( elems, /* internal */ acceptData ) { diff --git a/test/unit/core.js b/test/unit/core.js index db21c41ccb..f7612896fa 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1210,7 +1210,7 @@ test("jQuery.proxy", function(){ }); test("jQuery.parseHTML", function() { - expect( 13 ); + expect( 17 ); var html, nodes; @@ -1237,6 +1237,13 @@ test("jQuery.parseHTML", function() { equal( jQuery.parseHTML( "\t
" )[0].nodeValue, "\t", "Preserve leading whitespace" ); equal( jQuery.parseHTML("
")[0].nodeType, 3, "Leading spaces are treated as text nodes (#11290)" ); + + html = jQuery.parseHTML( "
test div
" ); + equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" ); + equal( html[ 0 ].innerHTML, "test div", "Content should be preserved" ); + + equal( jQuery.parseHTML("").length, 1, "Incorrect html-strings should not break anything" ); + equal( jQuery.parseHTML("")[ 1 ].parentNode.nodeType, 11, "parentNode should be documentFragment" ); }); test("jQuery.parseJSON", function(){ diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index d208b3b7a9..0a110725e4 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -672,24 +672,6 @@ test( "append HTML5 sectioning elements (Bug #6485)", function() { equal( aside.length, 1, "HTML5 elements do not collapse their children" ); }); -test( "jQuery.clean, #12392", function() { - - expect( 6 ); - - var elems = jQuery.clean( [ "
test div
", "

test p

" ] ); - - ok( elems[ 0 ].parentNode == null || elems[ 0 ].parentNode.nodeType === 11, "parentNode should be documentFragment or null" ); - ok( elems[ 1 ].parentNode == null || elems[ 1 ].parentNode.nodeType === 11, "parentNode should be documentFragment or null" ); - - equal( elems[ 0 ].innerHTML, "test div", "Content should be preserved" ); - equal( elems[ 1 ].innerHTML, "test p", "Content should be preserved" ); - - equal( jQuery.clean([ "" ]).length, 1, "Incorrect html-strings should not break anything" ); - - elems = jQuery.clean([ "" ]); - ok( elems[ 1 ].parentNode == null || elems[ 1 ].parentNode.nodeType === 11, "parentNode should be documentFragment or null" ); -}); - if ( jQuery.css ) { test( "HTML5 Elements inherit styles from style rules (Bug #10501)", function() {