Skip to content

Commit 62d5579

Browse files
committedMay 5, 2015
Manipulation: privatize internal domManip() function
Fixes gh-2225
1 parent a74320f commit 62d5579

File tree

1 file changed

+93
-94
lines changed

1 file changed

+93
-94
lines changed
 

‎src/manipulation.js

+93-94
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,94 @@ function fixInput( src, dest ) {
108108
}
109109
}
110110

111+
function domManip( collection, args, callback, ignored ) {
112+
113+
// Flatten any nested arrays
114+
args = concat.apply( [], args );
115+
116+
var fragment, first, scripts, hasScripts, node, doc,
117+
i = 0,
118+
l = collection.length,
119+
iNoClone = l - 1,
120+
value = args[ 0 ],
121+
isFunction = jQuery.isFunction( value );
122+
123+
// We can't cloneNode fragments that contain checked, in WebKit
124+
if ( isFunction ||
125+
( l > 1 && typeof value === "string" &&
126+
!support.checkClone && rchecked.test( value ) ) ) {
127+
return collection.each(function( index ) {
128+
var self = collection.eq( index );
129+
if ( isFunction ) {
130+
args[ 0 ] = value.call( this, index, self.html() );
131+
}
132+
domManip( self, args, callback, ignored );
133+
});
134+
}
135+
136+
if ( l ) {
137+
fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
138+
first = fragment.firstChild;
139+
140+
if ( fragment.childNodes.length === 1 ) {
141+
fragment = first;
142+
}
143+
144+
// Require either new content or an interest in ignored elements to invoke the callback
145+
if ( first || ignored ) {
146+
scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
147+
hasScripts = scripts.length;
148+
149+
// Use the original fragment for the last item
150+
// instead of the first because it can end up
151+
// being emptied incorrectly in certain situations (#8070).
152+
for ( ; i < l; i++ ) {
153+
node = fragment;
154+
155+
if ( i !== iNoClone ) {
156+
node = jQuery.clone( node, true, true );
157+
158+
// Keep references to cloned scripts for later restoration
159+
if ( hasScripts ) {
160+
// Support: Android<4.1, PhantomJS<2
161+
// push.apply(_, arraylike) throws on ancient WebKit
162+
jQuery.merge( scripts, getAll( node, "script" ) );
163+
}
164+
}
165+
166+
callback.call( collection[ i ], node, i );
167+
}
168+
169+
if ( hasScripts ) {
170+
doc = scripts[ scripts.length - 1 ].ownerDocument;
171+
172+
// Reenable scripts
173+
jQuery.map( scripts, restoreScript );
174+
175+
// Evaluate executable scripts on first document insertion
176+
for ( i = 0; i < hasScripts; i++ ) {
177+
node = scripts[ i ];
178+
if ( rscriptType.test( node.type || "" ) &&
179+
!dataPriv.access( node, "globalEval" ) &&
180+
jQuery.contains( doc, node ) ) {
181+
182+
if ( node.src ) {
183+
// Optional AJAX dependency, but won't run scripts if not present
184+
if ( jQuery._evalUrl ) {
185+
jQuery._evalUrl( node.src );
186+
}
187+
} else {
188+
jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
189+
}
190+
}
191+
}
192+
}
193+
}
194+
}
195+
196+
return collection;
197+
}
198+
111199
jQuery.extend({
112200
htmlPrefilter: function( html ) {
113201
return html.replace( rxhtmlTag, "<$1></$2>" );
@@ -193,7 +281,7 @@ jQuery.fn.extend({
193281
},
194282

195283
append: function() {
196-
return this.domManip( arguments, function( elem ) {
284+
return domManip( this, arguments, function( elem ) {
197285
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
198286
var target = manipulationTarget( this, elem );
199287
target.appendChild( elem );
@@ -202,7 +290,7 @@ jQuery.fn.extend({
202290
},
203291

204292
prepend: function() {
205-
return this.domManip( arguments, function( elem ) {
293+
return domManip( this, arguments, function( elem ) {
206294
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
207295
var target = manipulationTarget( this, elem );
208296
target.insertBefore( elem, target.firstChild );
@@ -211,15 +299,15 @@ jQuery.fn.extend({
211299
},
212300

213301
before: function() {
214-
return this.domManip( arguments, function( elem ) {
302+
return domManip( this, arguments, function( elem ) {
215303
if ( this.parentNode ) {
216304
this.parentNode.insertBefore( elem, this );
217305
}
218306
});
219307
},
220308

221309
after: function() {
222-
return this.domManip( arguments, function( elem ) {
310+
return domManip( this, arguments, function( elem ) {
223311
if ( this.parentNode ) {
224312
this.parentNode.insertBefore( elem, this.nextSibling );
225313
}
@@ -317,7 +405,7 @@ jQuery.fn.extend({
317405
var ignored = [];
318406

319407
// Make the changes, replacing each non-ignored context element with the new content
320-
return this.domManip( arguments, function( elem ) {
408+
return domManip( this, arguments, function( elem ) {
321409
var parent = this.parentNode;
322410

323411
if ( jQuery.inArray( this, ignored ) < 0 ) {
@@ -333,95 +421,6 @@ jQuery.fn.extend({
333421

334422
detach: function( selector ) {
335423
return this.remove( selector, true );
336-
},
337-
338-
domManip: function( args, callback, ignored ) {
339-
340-
// Flatten any nested arrays
341-
args = concat.apply( [], args );
342-
343-
var fragment, first, scripts, hasScripts, node, doc,
344-
i = 0,
345-
l = this.length,
346-
set = this,
347-
iNoClone = l - 1,
348-
value = args[ 0 ],
349-
isFunction = jQuery.isFunction( value );
350-
351-
// We can't cloneNode fragments that contain checked, in WebKit
352-
if ( isFunction ||
353-
( l > 1 && typeof value === "string" &&
354-
!support.checkClone && rchecked.test( value ) ) ) {
355-
return this.each(function( index ) {
356-
var self = set.eq( index );
357-
if ( isFunction ) {
358-
args[ 0 ] = value.call( this, index, self.html() );
359-
}
360-
self.domManip( args, callback, ignored );
361-
});
362-
}
363-
364-
if ( l ) {
365-
fragment = buildFragment( args, this[ 0 ].ownerDocument, false, this, ignored );
366-
first = fragment.firstChild;
367-
368-
if ( fragment.childNodes.length === 1 ) {
369-
fragment = first;
370-
}
371-
372-
// Require either new content or an interest in ignored elements to invoke the callback
373-
if ( first || ignored ) {
374-
scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
375-
hasScripts = scripts.length;
376-
377-
// Use the original fragment for the last item
378-
// instead of the first because it can end up
379-
// being emptied incorrectly in certain situations (#8070).
380-
for ( ; i < l; i++ ) {
381-
node = fragment;
382-
383-
if ( i !== iNoClone ) {
384-
node = jQuery.clone( node, true, true );
385-
386-
// Keep references to cloned scripts for later restoration
387-
if ( hasScripts ) {
388-
// Support: Android<4.1, PhantomJS<2
389-
// push.apply(_, arraylike) throws on ancient WebKit
390-
jQuery.merge( scripts, getAll( node, "script" ) );
391-
}
392-
}
393-
394-
callback.call( this[ i ], node, i );
395-
}
396-
397-
if ( hasScripts ) {
398-
doc = scripts[ scripts.length - 1 ].ownerDocument;
399-
400-
// Reenable scripts
401-
jQuery.map( scripts, restoreScript );
402-
403-
// Evaluate executable scripts on first document insertion
404-
for ( i = 0; i < hasScripts; i++ ) {
405-
node = scripts[ i ];
406-
if ( rscriptType.test( node.type || "" ) &&
407-
!dataPriv.access( node, "globalEval" ) &&
408-
jQuery.contains( doc, node ) ) {
409-
410-
if ( node.src ) {
411-
// Optional AJAX dependency, but won't run scripts if not present
412-
if ( jQuery._evalUrl ) {
413-
jQuery._evalUrl( node.src );
414-
}
415-
} else {
416-
jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
417-
}
418-
}
419-
}
420-
}
421-
}
422-
}
423-
424-
return this;
425424
}
426425
});
427426

0 commit comments

Comments
 (0)