Skip to content

Commit 63c1414

Browse files
committed
Manipulation: privatize buildFragment() function
Fixes gh-2224
1 parent b9b5c23 commit 63c1414

File tree

11 files changed

+265
-202
lines changed

11 files changed

+265
-202
lines changed

src/core/parseHTML.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
define([
22
"../core",
33
"./var/rsingleTag",
4+
"../manipulation/buildFragment",
45

56
// This is the only module that needs core/support
6-
"./support",
7-
8-
// buildFragment
9-
"../manipulation"
10-
], function( jQuery, rsingleTag, support ) {
7+
"./support"
8+
], function( jQuery, rsingleTag, buildFragment, support ) {
119

1210
// data: string of html
1311
// context (optional): If specified, the fragment will be created in this context,
@@ -35,7 +33,7 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
3533
return [ context.createElement( parsed[1] ) ];
3634
}
3735

38-
parsed = jQuery.buildFragment( [ data ], context, scripts );
36+
parsed = buildFragment( [ data ], context, scripts );
3937

4038
if ( scripts && scripts.length ) {
4139
jQuery( scripts ).remove();

src/manipulation.js

Lines changed: 17 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,40 @@ define([
44
"./var/push",
55
"./var/deletedIds",
66
"./core/access",
7+
78
"./manipulation/var/rcheckableType",
9+
"./manipulation/var/rtagName",
10+
"./manipulation/var/rscriptType",
11+
"./manipulation/var/rleadingWhitespace",
12+
"./manipulation/var/nodeNames",
13+
"./manipulation/createSafeFragment",
14+
"./manipulation/wrapMap",
15+
"./manipulation/getAll",
16+
"./manipulation/setGlobalEval",
17+
"./manipulation/buildFragment",
818
"./manipulation/support",
919

1020
"./core/init",
1121
"./data/accepts",
1222
"./traversing",
1323
"./selector",
1424
"./event"
15-
], function( jQuery, concat, push, deletedIds, access, rcheckableType, support ) {
16-
17-
function createSafeFragment( document ) {
18-
var list = nodeNames.split( "|" ),
19-
safeFrag = document.createDocumentFragment();
25+
], function( jQuery, concat, push, deletedIds, access,
26+
rcheckableType, rtagName, rscriptType, rleadingWhitespace, nodeNames,
27+
createSafeFragment, wrapMap, getAll, setGlobalEval,
28+
buildFragment, support ) {
2029

21-
if ( safeFrag.createElement ) {
22-
while ( list.length ) {
23-
safeFrag.createElement(
24-
list.pop()
25-
);
26-
}
27-
}
28-
return safeFrag;
29-
}
30-
31-
var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|" +
32-
"details|dialog|figcaption|figure|footer|header|hgroup|main|" +
33-
"mark|meter|nav|output|picture|progress|section|summary|template|time|video",
34-
rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g,
35-
rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i"),
36-
rleadingWhitespace = /^\s+/,
30+
var rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g,
31+
rnoshimcache = new RegExp( "<(?:" + nodeNames + ")[\\s/>]", "i" ),
3732
rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:-]+)[^>]*)\/>/gi,
38-
rtagName = /<([\w:-]+)/,
39-
rhtml = /<|&#?\w+;/,
4033
rnoInnerhtml = /<(?:script|style|link)/i,
4134
// checked="checked" or checked
4235
rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
43-
rscriptType = /^$|\/(?:java|ecma)script/i,
4436
rscriptTypeMasked = /^true\/(.*)/,
4537
rcleanScript = /^\s*<!(?:\[CDATA\[|--)|(?:\]\]|--)>\s*$/g,
46-
47-
// We have to close these tags to support XHTML (#13200)
48-
wrapMap = {
49-
option: [ 1, "<select multiple='multiple'>", "</select>" ],
50-
51-
// Support: IE8
52-
param: [ 1, "<object>", "</object>" ],
53-
54-
thead: [ 1, "<table>", "</table>" ],
55-
56-
// Some of the following wrappers are not fully defined, because
57-
// their parent elements (except for "table" element) could be omitted
58-
// since browser parsers are smart enough to auto-insert them
59-
60-
// Support: Android 2.3
61-
// Android browser doesn't auto-insert colgroup
62-
col: [ 2, "<table><colgroup>", "</colgroup></table>" ],
63-
64-
// Auto-insert "tbody" element
65-
tr: [ 2, "<table>", "</table>" ],
66-
67-
// Auto-insert "tbody" and "tr" elements
68-
td: [ 3, "<table>", "</table>" ],
69-
70-
// IE8 can't serialize link, script, style, or any html5 (NoScope) tags,
71-
// unless wrapped in a div with non-breaking characters in front of it.
72-
_default: support.htmlSerialize ? [ 0, "", "" ] : [ 1, "X<div>", "</div>" ]
73-
},
7438
safeFragment = createSafeFragment( document ),
7539
fragmentDiv = safeFragment.appendChild( document.createElement("div") );
7640

77-
wrapMap.optgroup = wrapMap.option;
78-
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
79-
wrapMap.th = wrapMap.td;
80-
81-
function getAll( context, tag ) {
82-
var elems, elem,
83-
i = 0,
84-
found = typeof context.getElementsByTagName !== "undefined" ?
85-
context.getElementsByTagName( tag || "*" ) :
86-
typeof context.querySelectorAll !== "undefined" ?
87-
context.querySelectorAll( tag || "*" ) :
88-
undefined;
89-
90-
if ( !found ) {
91-
for ( found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++ ) {
92-
if ( !tag || jQuery.nodeName( elem, tag ) ) {
93-
found.push( elem );
94-
} else {
95-
jQuery.merge( found, getAll( elem, tag ) );
96-
}
97-
}
98-
}
99-
100-
return tag === undefined || tag && jQuery.nodeName( context, tag ) ?
101-
jQuery.merge( [ context ], found ) :
102-
found;
103-
}
104-
10541
// Manipulating tables requires a tbody
10642
function manipulationTarget( elem, content ) {
10743
if ( jQuery.nodeName( elem, "table" ) &&
@@ -128,21 +64,7 @@ function restoreScript( elem ) {
12864
return elem;
12965
}
13066

131-
// Mark scripts as having already been evaluated
132-
function setGlobalEval( elems, refElements ) {
133-
var elem,
134-
i = 0;
135-
for ( ; (elem = elems[i]) != null; i++ ) {
136-
jQuery._data(
137-
elem,
138-
"globalEval",
139-
!refElements || jQuery._data( refElements[i], "globalEval" )
140-
);
141-
}
142-
}
143-
14467
function cloneCopyEvent( src, dest ) {
145-
14668
if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) {
14769
return;
14870
}
@@ -278,107 +200,6 @@ jQuery.extend({
278200
return clone;
279201
},
280202

281-
buildFragment: function( elems, context, scripts, selection, ignored ) {
282-
var j, elem, contains,
283-
tmp, tag, wrap,
284-
l = elems.length,
285-
286-
// Ensure a safe fragment
287-
safe = createSafeFragment( context ),
288-
nodes = [],
289-
i = 0;
290-
291-
for ( ; i < l; i++ ) {
292-
elem = elems[ i ];
293-
294-
if ( elem || elem === 0 ) {
295-
296-
// Add nodes directly
297-
if ( jQuery.type( elem ) === "object" ) {
298-
jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
299-
300-
// Convert non-html into a text node
301-
} else if ( !rhtml.test( elem ) ) {
302-
nodes.push( context.createTextNode( elem ) );
303-
304-
// Convert html into DOM nodes
305-
} else {
306-
tmp = tmp || safe.appendChild( context.createElement("div") );
307-
308-
// Deserialize a standard representation
309-
tag = (rtagName.exec( elem ) || [ "", "" ])[ 1 ].toLowerCase();
310-
wrap = wrapMap[ tag ] || wrapMap._default;
311-
tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ];
312-
313-
// Descend through wrappers to the right content
314-
j = wrap[0];
315-
while ( j-- ) {
316-
tmp = tmp.lastChild;
317-
}
318-
319-
// Manually add leading whitespace removed by IE
320-
if ( !support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
321-
nodes.push( context.createTextNode( rleadingWhitespace.exec( elem )[0] ) );
322-
}
323-
324-
jQuery.merge( nodes, tmp.childNodes );
325-
326-
// Fix #12392 for WebKit and IE > 9
327-
tmp.textContent = "";
328-
329-
// Fix #12392 for oldIE
330-
while ( tmp.firstChild ) {
331-
tmp.removeChild( tmp.firstChild );
332-
}
333-
334-
// Remember the top-level container for proper cleanup
335-
tmp = safe.lastChild;
336-
}
337-
}
338-
}
339-
340-
// Fix #11356: Clear elements from fragment
341-
if ( tmp ) {
342-
safe.removeChild( tmp );
343-
}
344-
345-
i = 0;
346-
while ( (elem = nodes[ i++ ]) ) {
347-
348-
// Skip elements already in the context collection (trac-4087)
349-
if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
350-
if ( ignored ) {
351-
ignored.push( elem );
352-
}
353-
continue;
354-
}
355-
356-
contains = jQuery.contains( elem.ownerDocument, elem );
357-
358-
// Append to fragment
359-
tmp = getAll( safe.appendChild( elem ), "script" );
360-
361-
// Preserve script evaluation history
362-
if ( contains ) {
363-
setGlobalEval( tmp );
364-
}
365-
366-
// Capture executables
367-
if ( scripts ) {
368-
j = 0;
369-
while ( (elem = tmp[ j++ ]) ) {
370-
if ( rscriptType.test( elem.type || "" ) ) {
371-
scripts.push( elem );
372-
}
373-
}
374-
}
375-
}
376-
377-
tmp = null;
378-
379-
return safe;
380-
},
381-
382203
cleanData: function( elems, /* internal */ acceptData ) {
383204
var elem, type, id, data,
384205
i = 0,
@@ -626,7 +447,7 @@ jQuery.fn.extend({
626447
}
627448

628449
if ( l ) {
629-
fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this, ignored );
450+
fragment = buildFragment( args, this[ 0 ].ownerDocument, false, this, ignored );
630451
first = fragment.firstChild;
631452

632453
if ( fragment.childNodes.length === 1 ) {

0 commit comments

Comments
 (0)