From: Richard Gibson Date: Fri, 10 Apr 2015 19:57:18 +0000 (-0400) Subject: Manipulation: Make an HTML interception point X-Git-Tag: 3.0.0-alpha1~75 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=225bde37c997f5ddd9fe00fdfb8e9a43545cfbbc;p=jquery.git Manipulation: Make an HTML interception point Fixes gh-1747 Closes gh-2203 --- diff --git a/src/manipulation.js b/src/manipulation.js index 160b9ec79..d085755a3 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -159,6 +159,10 @@ function fixInput( src, dest ) { } jQuery.extend({ + htmlPrefilter: function( html ) { + return html.replace( rxhtmlTag, "<$1>" ); + }, + clone: function( elem, dataAndEvents, deepDataAndEvents ) { var i, l, srcElements, destElements, clone = elem.cloneNode( true ), @@ -230,7 +234,7 @@ jQuery.extend({ // Deserialize a standard representation tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase(); wrap = wrapMap[ tag ] || wrapMap._default; - tmp.innerHTML = wrap[ 1 ] + elem.replace( rxhtmlTag, "<$1>" ) + wrap[ 2 ]; + tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ]; // Descend through wrappers to the right content j = wrap[ 0 ]; @@ -422,7 +426,7 @@ jQuery.fn.extend({ if ( typeof value === "string" && !rnoInnerhtml.test( value ) && !wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) { - value = value.replace( rxhtmlTag, "<$1>" ); + value = jQuery.htmlPrefilter( value ); try { for ( ; i < l; i++ ) { diff --git a/test/unit/core.js b/test/unit/core.js index 5d4afb152..b9fef3cf2 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -19,8 +19,7 @@ test("jQuery()", function() { img = jQuery(""), div = jQuery("

"), exec = false, - lng = "", - expected = 22, + expected = 23, attrObj = { "text": "test", "class": "test2", @@ -141,12 +140,9 @@ test("jQuery()", function() { } equal( elem[0].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" ); - // manually clean up detached elements - elem.remove(); - - for ( i = 0; i < 128; i++ ) { - lng += "12345678"; - } + elem = jQuery( "", {} ); + strictEqual( elem[ 0 ].ownerDocument, document, + "Empty attributes object is not interpreted as a document (trac-8950)" ); }); test("jQuery(selector, context)", function() { diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 1fd19a2c1..5a8452011 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -2080,7 +2080,7 @@ test( "jQuery.cleanData eliminates all private data (gh-2127)", function() { div.remove(); }); -test( "jQuery.buildFragment - no plain-text caching (Bug #6779)", function() { +test( "domManip plain-text caching (trac-6779)", function() { expect( 1 ); @@ -2099,42 +2099,43 @@ test( "jQuery.buildFragment - no plain-text caching (Bug #6779)", function() { $f.remove(); }); -test( "jQuery.html - execute scripts escaped with html comment or CDATA (#9221)", function() { +test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function() { expect( 3 ); - jQuery([ - "" - ].join("\n")).appendTo("#qunit-fixture"); - jQuery([ - "" - ].join("\n")).appendTo("#qunit-fixture"); - jQuery([ - "" - ].join("\n")).appendTo("#qunit-fixture"); -}); - -test( "jQuery.buildFragment - plain objects are not a document #8950", function() { - - expect( 1 ); - - try { - jQuery( "", {} ); - ok( true, "Does not allow attribute object to be treated like a doc object" ); - } catch ( e ) {} -}); + jQuery( [ + "" + ].join( "\n" ) ).appendTo( "#qunit-fixture" ); + + jQuery( [ + "" + ].join( "\n" ) ).appendTo( "#qunit-fixture" ); + + jQuery( [ + "" + ].join( "\n" ) ).appendTo( "#qunit-fixture" ); +}); + +testIframeWithCallback( + "domManip tolerates window-valued document[0] in IE9/10 (trac-12266)", + "manipulation/iframe-denied.html", + function( test ) { + expect( 1 ); + ok( test.status, test.description ); + } +); test( "jQuery.clone - no exceptions for object elements #9587", function() { @@ -2296,12 +2297,6 @@ test( "manipulate mixed jQuery and text (#12384, #12346)", function() { equal( div.find("*").length, 3, "added 2 paragraphs after inner div" ); }); -testIframeWithCallback( "buildFragment works even if document[0] is iframe's window object in IE9/10 (#12266)", "manipulation/iframe-denied.html", function( test ) { - expect( 1 ); - - ok( test.status, test.description ); -}); - test( "script evaluation (#11795)", function() { expect( 13 ); @@ -2385,6 +2380,46 @@ test( "jQuery._evalUrl (#12838)", function() { jQuery._evalUrl = evalUrl; }); +test( "jQuery.htmlPrefilter (gh-1747)", function( assert ) { + + assert.expect( 5 ); + + var expectedArgument, + invocations = 0, + htmlPrefilter = jQuery.htmlPrefilter, + fixture = jQuery( "
" ).appendTo( "#qunit-fixture" ), + poison = "", + done = assert.async(); + + jQuery.htmlPrefilter = function( html ) { + invocations++; + assert.equal( html, expectedArgument, "Expected input" ); + + // Remove