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