diff --git a/src/core/DOMEval.js b/src/core/DOMEval.js index 806f29ebe..21f5a463c 100644 --- a/src/core/DOMEval.js +++ b/src/core/DOMEval.js @@ -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 ); + } } diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index e197712fb..b40ba6056 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -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( + [ + "", + "