1 import jQuery from "../core.js";
2 import flat from "../var/flat.js";
3 import rscriptType from "./var/rscriptType.js";
4 import getAll from "./getAll.js";
5 import buildFragment from "./buildFragment.js";
6 import dataPriv from "../data/var/dataPriv.js";
7 import DOMEval from "../core/DOMEval.js";
9 // Replace/restore the type attribute of script elements for safe DOM manipulation
10 function disableScript( elem ) {
11 elem.type = ( elem.getAttribute( "type" ) !== null ) + "/" + elem.type;
14 function restoreScript( elem ) {
15 if ( ( elem.type || "" ).slice( 0, 5 ) === "true/" ) {
16 elem.type = elem.type.slice( 5 );
18 elem.removeAttribute( "type" );
24 function domManip( collection, args, callback, ignored ) {
26 // Flatten any nested arrays
29 var fragment, first, scripts, hasScripts, node, doc,
31 l = collection.length,
34 valueIsFunction = typeof value === "function";
36 if ( valueIsFunction ) {
37 return collection.each( function( index ) {
38 var self = collection.eq( index );
39 args[ 0 ] = value.call( this, index, self.html() );
40 domManip( self, args, callback, ignored );
45 fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
46 first = fragment.firstChild;
48 if ( fragment.childNodes.length === 1 ) {
52 // Require either new content or an interest in ignored elements to invoke the callback
53 if ( first || ignored ) {
54 scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
55 hasScripts = scripts.length;
57 // Use the original fragment for the last item
58 // instead of the first because it can end up
59 // being emptied incorrectly in certain situations (trac-8070).
60 for ( ; i < l; i++ ) {
63 if ( i !== iNoClone ) {
64 node = jQuery.clone( node, true, true );
66 // Keep references to cloned scripts for later restoration
68 jQuery.merge( scripts, getAll( node, "script" ) );
72 callback.call( collection[ i ], node, i );
76 doc = scripts[ scripts.length - 1 ].ownerDocument;
79 jQuery.map( scripts, restoreScript );
81 // Evaluate executable scripts on first document insertion
82 for ( i = 0; i < hasScripts; i++ ) {
84 if ( rscriptType.test( node.type || "" ) &&
85 !dataPriv.access( node, "globalEval" ) &&
86 jQuery.contains( doc, node ) ) {
88 if ( node.src && ( node.type || "" ).toLowerCase() !== "module" ) {
90 // Optional AJAX dependency, but won't run scripts if not present
91 if ( jQuery._evalUrl && !node.noModule ) {
92 jQuery._evalUrl( node.src, {
94 crossOrigin: node.crossOrigin
98 DOMEval( node.textContent, node, doc );
109 export default domManip;