Manipulation: Add support for scripts with module type

Fixes gh-3871
Close gh-3869
This commit is contained in:
basil.belokon 2017-12-03 15:58:09 +06:00 committed by Timmy Willison
parent 428ee4a624
commit 5d3a968e03
No known key found for this signature in database
GPG Key ID: 5F0C8B73EF56CE6F
6 changed files with 37 additions and 5 deletions

View File

@ -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 );
}

View File

@ -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 );
}
}
}

View File

@ -1,5 +1,5 @@
define( function() {
"use strict";
return ( /^$|\/(?:java|ecma)script/i );
return ( /^$|^module$|\/(?:java|ecma)script/i );
} );

View File

@ -0,0 +1 @@
window.ok( true, "evaluated: innert module with src" );

1
test/data/module.js Normal file
View File

@ -0,0 +1 @@
window.ok( true, "evaluated: module with src" );

View File

@ -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(
[
"<script type='module'>ok( true, 'evaluated: module' );</script>",
"<script type='module' src='./data/module.js'></script>",
"<div>",
"<script type='module'>ok( true, 'evaluated: inner module' );</script>",
"<script type='module' src='./data/inner_module.js'></script>",
"</div>"
].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 );