]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: Support $el.html(selfRemovingScript) (#5378)
authorRichard Gibson <richard.gibson@gmail.com>
Mon, 8 Jan 2024 17:30:39 +0000 (12:30 -0500)
committerGitHub <noreply@github.com>
Mon, 8 Jan 2024 17:30:39 +0000 (18:30 +0100)
Don't try to remove a script element that has already removed itself.

Also, compress `DOMEval.js`.

Fixes gh-5377
Closes gh-5378

src/core/DOMEval.js
test/unit/manipulation.js

index 806f29ebe9a564b149fbd450de01e68106c0287d..21f5a463ce4e7e281d999ab9a25305d3a38f2c16 100644 (file)
@@ -14,12 +14,13 @@ export function DOMEval( code, node, doc ) {
                script = doc.createElement( "script" );
 
        script.text = code;
-       if ( node ) {
-               for ( i in preservedScriptAttributes ) {
-                       if ( node[ i ] ) {
-                               script[ i ] = node[ i ];
-                       }
+       for ( i in preservedScriptAttributes ) {
+               if ( node && node[ i ] ) {
+                       script[ i ] = node[ i ];
                }
        }
-       doc.head.appendChild( script ).parentNode.removeChild( script );
+
+       if ( doc.head.appendChild( script ).parentNode ) {
+               script.parentNode.removeChild( script );
+       }
 }
index e197712fba068d26124edbabfcb3d6a699cdd233..b40ba6056a7dd2fec508881c0012fa4efec7b975 100644 (file)
@@ -1819,6 +1819,21 @@ QUnit.test( "html(script nomodule)", function( assert ) {
        }, 1000 );
 } );
 
+QUnit.test( "html(self-removing script) (gh-5377)", function( assert ) {
+       assert.expect( 2 );
+
+       var $fixture = jQuery( "#qunit-fixture" );
+
+       $fixture.html(
+               [
+                       "<script>document.currentScript.parentNode.removeChild( document.currentScript ); QUnit.assert.ok( true, 'removed document.currentScript' );</script>",
+                       "<div>",
+                               "<script>document.currentScript.parentNode.removeChild( document.currentScript ); QUnit.assert.ok( true, 'removed inner document.currentScript' );</script>",
+                       "</div>"
+               ].join( "" )
+       );
+} );
+
 QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) {
 
        assert.expect( 4 );