Manipulation: Support $el.html(selfRemovingScript) (#5378)

Don't try to remove a script element that has already removed itself.

Also, compress `DOMEval.js`.

Fixes gh-5377
Closes gh-5378
This commit is contained in:
Richard Gibson 2024-01-08 12:30:39 -05:00 committed by GitHub
parent e8b7db4b0f
commit 937923d9ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -14,12 +14,13 @@ export function DOMEval( code, node, doc ) {
script = doc.createElement( "script" ); script = doc.createElement( "script" );
script.text = code; script.text = code;
if ( node ) { for ( i in preservedScriptAttributes ) {
for ( i in preservedScriptAttributes ) { if ( node && node[ i ] ) {
if ( node[ i ] ) { script[ i ] = node[ i ];
script[ i ] = node[ i ];
}
} }
} }
doc.head.appendChild( script ).parentNode.removeChild( script );
if ( doc.head.appendChild( script ).parentNode ) {
script.parentNode.removeChild( script );
}
} }

View File

@ -1819,6 +1819,21 @@ QUnit.test( "html(script nomodule)", function( assert ) {
}, 1000 ); }, 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 ) { QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) {
assert.expect( 4 ); assert.expect( 4 );