From: basil.belokon Date: Sun, 3 Dec 2017 09:58:09 +0000 (+0600) Subject: Manipulation: Add support for scripts with module type X-Git-Tag: 3.3.0~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5d3a968e031ab8dff5c07e1d6bb4f196fb82bfa0;p=jquery.git Manipulation: Add support for scripts with module type Fixes gh-3871 Close gh-3869 --- diff --git a/src/core/DOMEval.js b/src/core/DOMEval.js index c49c12e5e..199ec9518 100644 --- a/src/core/DOMEval.js +++ b/src/core/DOMEval.js @@ -3,12 +3,26 @@ define( [ ], function( document ) { "use strict"; - function DOMEval( code, doc ) { + var preservedScriptAttributes = { + type: true, + src: true, + noModule: true + }; + + function DOMEval( code, doc, node ) { doc = doc || document; - var script = doc.createElement( "script" ); + var i, + script = doc.createElement( "script" ); script.text = code; + if ( node ) { + for ( i in preservedScriptAttributes ) { + if ( node[ i ] ) { + script[ i ] = node[ i ]; + } + } + } doc.head.appendChild( script ).parentNode.removeChild( script ); } diff --git a/src/manipulation.js b/src/manipulation.js index ae713fe29..142e296ad 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -194,14 +194,14 @@ function domManip( collection, args, callback, ignored ) { !dataPriv.access( node, "globalEval" ) && jQuery.contains( doc, node ) ) { - if ( node.src ) { + if ( node.src && ( node.type || "" ).toLowerCase() !== "module" ) { // Optional AJAX dependency, but won't run scripts if not present if ( jQuery._evalUrl ) { jQuery._evalUrl( node.src ); } } else { - DOMEval( node.textContent.replace( rcleanScript, "" ), doc ); + DOMEval( node.textContent.replace( rcleanScript, "" ), doc, node ); } } } diff --git a/src/manipulation/var/rscriptType.js b/src/manipulation/var/rscriptType.js index 7237c8a31..cd1430a7e 100644 --- a/src/manipulation/var/rscriptType.js +++ b/src/manipulation/var/rscriptType.js @@ -1,5 +1,5 @@ define( function() { "use strict"; - return ( /^$|\/(?:java|ecma)script/i ); + return ( /^$|^module$|\/(?:java|ecma)script/i ); } ); diff --git a/test/data/inner_module.js b/test/data/inner_module.js new file mode 100644 index 000000000..a89a39d27 --- /dev/null +++ b/test/data/inner_module.js @@ -0,0 +1 @@ +window.ok( true, "evaluated: innert module with src" ); diff --git a/test/data/module.js b/test/data/module.js new file mode 100644 index 000000000..b0a8a21fd --- /dev/null +++ b/test/data/module.js @@ -0,0 +1 @@ +window.ok( true, "evaluated: module with src" ); diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 07ab43419..bfe41ed4e 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1797,6 +1797,22 @@ QUnit.test( "html(Function)", function( assert ) { testHtml( manipulationFunctionReturningObj, assert ); } ); +QUnit.test( "html(script type module)", function( assert ) { + assert.expect( 1 ); + var fixture = jQuery( "#qunit-fixture" ), + tmp = fixture.html( + [ + "", + "", + "
", + "", + "", + "
" + ].join( "" ) + ).find( "script" ); + assert.equal( tmp.length, 4, "All script tags remain." ); +} ); + QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) { assert.expect( 4 );