]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: Support $el.html(selfRemovingScript)
authorRichard Gibson <richard.gibson@gmail.com>
Mon, 8 Jan 2024 17:30:39 +0000 (12:30 -0500)
committerMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Mon, 8 Jan 2024 17:54:48 +0000 (18:54 +0100)
Don't try to remove a script element that has already removed itself.

Fixes gh-5377
Closes gh-5378

(cherry-picked from commit 937923d9ee8dfd19008447b5059cbd13ee5a23ac)

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

index 59f6e024775f697f3863235666bb73dac6339a42..e3ecd60acd5993cf678a1af0caae88151137f3b4 100644 (file)
@@ -36,7 +36,9 @@ define( [
                                }
                        }
                }
-               doc.head.appendChild( script ).parentNode.removeChild( script );
+               if ( doc.head.appendChild( script ).parentNode ) {
+                       script.parentNode.removeChild( script );
+               }
        }
 
        return DOMEval;
index 975813e344537c6475784c386769fdce36535e78..f894cc6262a306c4704e9d309c93471f1b175b6c 100644 (file)
@@ -1871,6 +1871,21 @@ QUnit[
        }, 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 );