Manipulation: Support $el.html(selfRemovingScript) (#5378)
[jquery.git] / src / manipulation / buildFragment.js
blob034ba89f4d47422801cb3667de30f1736400a874
1 import { jQuery } from "../core.js";
2 import { toType } from "../core/toType.js";
3 import { isAttached } from "../core/isAttached.js";
4 import { arr } from "../var/arr.js";
5 import { rtagName } from "./var/rtagName.js";
6 import { rscriptType } from "./var/rscriptType.js";
7 import { wrapMap } from "./wrapMap.js";
8 import { getAll } from "./getAll.js";
9 import { setGlobalEval } from "./setGlobalEval.js";
10 import { isArrayLike } from "../core/isArrayLike.js";
12 var rhtml = /<|&#?\w+;/;
14 export function buildFragment( elems, context, scripts, selection, ignored ) {
15         var elem, tmp, tag, wrap, attached, j,
16                 fragment = context.createDocumentFragment(),
17                 nodes = [],
18                 i = 0,
19                 l = elems.length;
21         for ( ; i < l; i++ ) {
22                 elem = elems[ i ];
24                 if ( elem || elem === 0 ) {
26                         // Add nodes directly
27                         if ( toType( elem ) === "object" && ( elem.nodeType || isArrayLike( elem ) ) ) {
28                                 jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
30                         // Convert non-html into a text node
31                         } else if ( !rhtml.test( elem ) ) {
32                                 nodes.push( context.createTextNode( elem ) );
34                         // Convert html into DOM nodes
35                         } else {
36                                 tmp = tmp || fragment.appendChild( context.createElement( "div" ) );
38                                 // Deserialize a standard representation
39                                 tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
40                                 wrap = wrapMap[ tag ] || arr;
42                                 // Create wrappers & descend into them.
43                                 j = wrap.length;
44                                 while ( --j > -1 ) {
45                                         tmp = tmp.appendChild( context.createElement( wrap[ j ] ) );
46                                 }
48                                 tmp.innerHTML = jQuery.htmlPrefilter( elem );
50                                 jQuery.merge( nodes, tmp.childNodes );
52                                 // Remember the top-level container
53                                 tmp = fragment.firstChild;
55                                 // Ensure the created nodes are orphaned (trac-12392)
56                                 tmp.textContent = "";
57                         }
58                 }
59         }
61         // Remove wrapper from fragment
62         fragment.textContent = "";
64         i = 0;
65         while ( ( elem = nodes[ i++ ] ) ) {
67                 // Skip elements already in the context collection (trac-4087)
68                 if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
69                         if ( ignored ) {
70                                 ignored.push( elem );
71                         }
72                         continue;
73                 }
75                 attached = isAttached( elem );
77                 // Append to fragment
78                 tmp = getAll( fragment.appendChild( elem ), "script" );
80                 // Preserve script evaluation history
81                 if ( attached ) {
82                         setGlobalEval( tmp );
83                 }
85                 // Capture executables
86                 if ( scripts ) {
87                         j = 0;
88                         while ( ( elem = tmp[ j++ ] ) ) {
89                                 if ( rscriptType.test( elem.type || "" ) ) {
90                                         scripts.push( elem );
91                                 }
92                         }
93                 }
94         }
96         return fragment;