]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: Add support for scripts with module type
authorbasil.belokon <basil.belokon@gmail.com>
Sun, 3 Dec 2017 09:58:09 +0000 (15:58 +0600)
committerTimmy Willison <4timmywil@gmail.com>
Tue, 16 Jan 2018 16:39:16 +0000 (11:39 -0500)
Fixes gh-3871
Close gh-3869

src/core/DOMEval.js
src/manipulation.js
src/manipulation/var/rscriptType.js
test/data/inner_module.js [new file with mode: 0644]
test/data/module.js [new file with mode: 0644]
test/unit/manipulation.js

index c49c12e5e51335a6dd725782c6078499a9eae659..199ec9518ff36de1ffdebe372836ee6470b3a70e 100644 (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 );
        }
 
index ae713fe291afa1e2812400e384a69e0b14e4fc12..142e296ad0e196d580f5f0d1b34b2d2d4b6ae43f 100644 (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 );
                                                }
                                        }
                                }
index 7237c8a31377c8fdc76e0c0243ae8fabe90f9819..cd1430a7e17e6f4429ae47290bf34a5cb5a60900 100644 (file)
@@ -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 (file)
index 0000000..a89a39d
--- /dev/null
@@ -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 (file)
index 0000000..b0a8a21
--- /dev/null
@@ -0,0 +1 @@
+window.ok( true, "evaluated: module with src" );
index 07ab4341999df55345a287b0224fc271ccd0d387..bfe41ed4ec33d9690abe6b0e7d905117d8fab137 100644 (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 );