comparison default/node_modules/jquery/src/manipulation/buildFragment.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 define( [
2 "../core",
3 "../core/toType",
4 "./var/rtagName",
5 "./var/rscriptType",
6 "./wrapMap",
7 "./getAll",
8 "./setGlobalEval"
9 ], function( jQuery, toType, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) {
10
11 "use strict";
12
13 var rhtml = /<|&#?\w+;/;
14
15 function buildFragment( elems, context, scripts, selection, ignored ) {
16 var elem, tmp, tag, wrap, contains, j,
17 fragment = context.createDocumentFragment(),
18 nodes = [],
19 i = 0,
20 l = elems.length;
21
22 for ( ; i < l; i++ ) {
23 elem = elems[ i ];
24
25 if ( elem || elem === 0 ) {
26
27 // Add nodes directly
28 if ( toType( elem ) === "object" ) {
29
30 // Support: Android <=4.0 only, PhantomJS 1 only
31 // push.apply(_, arraylike) throws on ancient WebKit
32 jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
33
34 // Convert non-html into a text node
35 } else if ( !rhtml.test( elem ) ) {
36 nodes.push( context.createTextNode( elem ) );
37
38 // Convert html into DOM nodes
39 } else {
40 tmp = tmp || fragment.appendChild( context.createElement( "div" ) );
41
42 // Deserialize a standard representation
43 tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
44 wrap = wrapMap[ tag ] || wrapMap._default;
45 tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ];
46
47 // Descend through wrappers to the right content
48 j = wrap[ 0 ];
49 while ( j-- ) {
50 tmp = tmp.lastChild;
51 }
52
53 // Support: Android <=4.0 only, PhantomJS 1 only
54 // push.apply(_, arraylike) throws on ancient WebKit
55 jQuery.merge( nodes, tmp.childNodes );
56
57 // Remember the top-level container
58 tmp = fragment.firstChild;
59
60 // Ensure the created nodes are orphaned (#12392)
61 tmp.textContent = "";
62 }
63 }
64 }
65
66 // Remove wrapper from fragment
67 fragment.textContent = "";
68
69 i = 0;
70 while ( ( elem = nodes[ i++ ] ) ) {
71
72 // Skip elements already in the context collection (trac-4087)
73 if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
74 if ( ignored ) {
75 ignored.push( elem );
76 }
77 continue;
78 }
79
80 contains = jQuery.contains( elem.ownerDocument, elem );
81
82 // Append to fragment
83 tmp = getAll( fragment.appendChild( elem ), "script" );
84
85 // Preserve script evaluation history
86 if ( contains ) {
87 setGlobalEval( tmp );
88 }
89
90 // Capture executables
91 if ( scripts ) {
92 j = 0;
93 while ( ( elem = tmp[ j++ ] ) ) {
94 if ( rscriptType.test( elem.type || "" ) ) {
95 scripts.push( elem );
96 }
97 }
98 }
99 }
100
101 return fragment;
102 }
103
104 return buildFragment;
105 } );