Skip to content

Commit

Permalink
Manipulation: Add support for scripts with module type
Browse files Browse the repository at this point in the history
  • Loading branch information
tbepdb authored and timmywil committed Jan 16, 2018
1 parent 428ee4a commit 5d3a968
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/core/DOMEval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
4 changes: 2 additions & 2 deletions src/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation/var/rscriptType.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define( function() {
"use strict";

return ( /^$|\/(?:java|ecma)script/i );
return ( /^$|^module$|\/(?:java|ecma)script/i );
} );
1 change: 1 addition & 0 deletions test/data/inner_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.ok( true, "evaluated: innert module with src" );
1 change: 1 addition & 0 deletions test/data/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.ok( true, "evaluated: module with src" );
16 changes: 16 additions & 0 deletions test/unit/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 5d3a968

Please sign in to comment.