tuiHoneyPot

front and back end of my TUI honeypot
Log | Files | Refs | README

manipulation.js (12686B)


      1 define( [
      2 	"./core",
      3 	"./core/isAttached",
      4 	"./var/flat",
      5 	"./var/isFunction",
      6 	"./var/push",
      7 	"./var/rcheckableType",
      8 	"./core/access",
      9 	"./manipulation/var/rtagName",
     10 	"./manipulation/var/rscriptType",
     11 	"./manipulation/wrapMap",
     12 	"./manipulation/getAll",
     13 	"./manipulation/setGlobalEval",
     14 	"./manipulation/buildFragment",
     15 	"./manipulation/support",
     16 
     17 	"./data/var/dataPriv",
     18 	"./data/var/dataUser",
     19 	"./data/var/acceptData",
     20 	"./core/DOMEval",
     21 	"./core/nodeName",
     22 
     23 	"./core/init",
     24 	"./traversing",
     25 	"./selector",
     26 	"./event"
     27 ], function( jQuery, isAttached, flat, isFunction, push, rcheckableType,
     28 	access, rtagName, rscriptType,
     29 	wrapMap, getAll, setGlobalEval, buildFragment, support,
     30 	dataPriv, dataUser, acceptData, DOMEval, nodeName ) {
     31 
     32 "use strict";
     33 
     34 var
     35 
     36 	// Support: IE <=10 - 11, Edge 12 - 13 only
     37 	// In IE/Edge using regex groups here causes severe slowdowns.
     38 	// See https://connect.microsoft.com/IE/feedback/details/1736512/
     39 	rnoInnerhtml = /<script|<style|<link/i,
     40 
     41 	// checked="checked" or checked
     42 	rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
     43 
     44 	rcleanScript = /^\s*<!\[CDATA\[|\]\]>\s*$/g;
     45 
     46 // Prefer a tbody over its parent table for containing new rows
     47 function manipulationTarget( elem, content ) {
     48 	if ( nodeName( elem, "table" ) &&
     49 		nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
     50 
     51 		return jQuery( elem ).children( "tbody" )[ 0 ] || elem;
     52 	}
     53 
     54 	return elem;
     55 }
     56 
     57 // Replace/restore the type attribute of script elements for safe DOM manipulation
     58 function disableScript( elem ) {
     59 	elem.type = ( elem.getAttribute( "type" ) !== null ) + "/" + elem.type;
     60 	return elem;
     61 }
     62 function restoreScript( elem ) {
     63 	if ( ( elem.type || "" ).slice( 0, 5 ) === "true/" ) {
     64 		elem.type = elem.type.slice( 5 );
     65 	} else {
     66 		elem.removeAttribute( "type" );
     67 	}
     68 
     69 	return elem;
     70 }
     71 
     72 function cloneCopyEvent( src, dest ) {
     73 	var i, l, type, pdataOld, udataOld, udataCur, events;
     74 
     75 	if ( dest.nodeType !== 1 ) {
     76 		return;
     77 	}
     78 
     79 	// 1. Copy private data: events, handlers, etc.
     80 	if ( dataPriv.hasData( src ) ) {
     81 		pdataOld = dataPriv.get( src );
     82 		events = pdataOld.events;
     83 
     84 		if ( events ) {
     85 			dataPriv.remove( dest, "handle events" );
     86 
     87 			for ( type in events ) {
     88 				for ( i = 0, l = events[ type ].length; i < l; i++ ) {
     89 					jQuery.event.add( dest, type, events[ type ][ i ] );
     90 				}
     91 			}
     92 		}
     93 	}
     94 
     95 	// 2. Copy user data
     96 	if ( dataUser.hasData( src ) ) {
     97 		udataOld = dataUser.access( src );
     98 		udataCur = jQuery.extend( {}, udataOld );
     99 
    100 		dataUser.set( dest, udataCur );
    101 	}
    102 }
    103 
    104 // Fix IE bugs, see support tests
    105 function fixInput( src, dest ) {
    106 	var nodeName = dest.nodeName.toLowerCase();
    107 
    108 	// Fails to persist the checked state of a cloned checkbox or radio button.
    109 	if ( nodeName === "input" && rcheckableType.test( src.type ) ) {
    110 		dest.checked = src.checked;
    111 
    112 	// Fails to return the selected option to the default selected state when cloning options
    113 	} else if ( nodeName === "input" || nodeName === "textarea" ) {
    114 		dest.defaultValue = src.defaultValue;
    115 	}
    116 }
    117 
    118 function domManip( collection, args, callback, ignored ) {
    119 
    120 	// Flatten any nested arrays
    121 	args = flat( args );
    122 
    123 	var fragment, first, scripts, hasScripts, node, doc,
    124 		i = 0,
    125 		l = collection.length,
    126 		iNoClone = l - 1,
    127 		value = args[ 0 ],
    128 		valueIsFunction = isFunction( value );
    129 
    130 	// We can't cloneNode fragments that contain checked, in WebKit
    131 	if ( valueIsFunction ||
    132 			( l > 1 && typeof value === "string" &&
    133 				!support.checkClone && rchecked.test( value ) ) ) {
    134 		return collection.each( function( index ) {
    135 			var self = collection.eq( index );
    136 			if ( valueIsFunction ) {
    137 				args[ 0 ] = value.call( this, index, self.html() );
    138 			}
    139 			domManip( self, args, callback, ignored );
    140 		} );
    141 	}
    142 
    143 	if ( l ) {
    144 		fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
    145 		first = fragment.firstChild;
    146 
    147 		if ( fragment.childNodes.length === 1 ) {
    148 			fragment = first;
    149 		}
    150 
    151 		// Require either new content or an interest in ignored elements to invoke the callback
    152 		if ( first || ignored ) {
    153 			scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
    154 			hasScripts = scripts.length;
    155 
    156 			// Use the original fragment for the last item
    157 			// instead of the first because it can end up
    158 			// being emptied incorrectly in certain situations (trac-8070).
    159 			for ( ; i < l; i++ ) {
    160 				node = fragment;
    161 
    162 				if ( i !== iNoClone ) {
    163 					node = jQuery.clone( node, true, true );
    164 
    165 					// Keep references to cloned scripts for later restoration
    166 					if ( hasScripts ) {
    167 
    168 						// Support: Android <=4.0 only, PhantomJS 1 only
    169 						// push.apply(_, arraylike) throws on ancient WebKit
    170 						jQuery.merge( scripts, getAll( node, "script" ) );
    171 					}
    172 				}
    173 
    174 				callback.call( collection[ i ], node, i );
    175 			}
    176 
    177 			if ( hasScripts ) {
    178 				doc = scripts[ scripts.length - 1 ].ownerDocument;
    179 
    180 				// Re-enable scripts
    181 				jQuery.map( scripts, restoreScript );
    182 
    183 				// Evaluate executable scripts on first document insertion
    184 				for ( i = 0; i < hasScripts; i++ ) {
    185 					node = scripts[ i ];
    186 					if ( rscriptType.test( node.type || "" ) &&
    187 						!dataPriv.access( node, "globalEval" ) &&
    188 						jQuery.contains( doc, node ) ) {
    189 
    190 						if ( node.src && ( node.type || "" ).toLowerCase()  !== "module" ) {
    191 
    192 							// Optional AJAX dependency, but won't run scripts if not present
    193 							if ( jQuery._evalUrl && !node.noModule ) {
    194 								jQuery._evalUrl( node.src, {
    195 									nonce: node.nonce || node.getAttribute( "nonce" )
    196 								}, doc );
    197 							}
    198 						} else {
    199 
    200 							// Unwrap a CDATA section containing script contents. This shouldn't be
    201 							// needed as in XML documents they're already not visible when
    202 							// inspecting element contents and in HTML documents they have no
    203 							// meaning but we're preserving that logic for backwards compatibility.
    204 							// This will be removed completely in 4.0. See gh-4904.
    205 							DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
    206 						}
    207 					}
    208 				}
    209 			}
    210 		}
    211 	}
    212 
    213 	return collection;
    214 }
    215 
    216 function remove( elem, selector, keepData ) {
    217 	var node,
    218 		nodes = selector ? jQuery.filter( selector, elem ) : elem,
    219 		i = 0;
    220 
    221 	for ( ; ( node = nodes[ i ] ) != null; i++ ) {
    222 		if ( !keepData && node.nodeType === 1 ) {
    223 			jQuery.cleanData( getAll( node ) );
    224 		}
    225 
    226 		if ( node.parentNode ) {
    227 			if ( keepData && isAttached( node ) ) {
    228 				setGlobalEval( getAll( node, "script" ) );
    229 			}
    230 			node.parentNode.removeChild( node );
    231 		}
    232 	}
    233 
    234 	return elem;
    235 }
    236 
    237 jQuery.extend( {
    238 	htmlPrefilter: function( html ) {
    239 		return html;
    240 	},
    241 
    242 	clone: function( elem, dataAndEvents, deepDataAndEvents ) {
    243 		var i, l, srcElements, destElements,
    244 			clone = elem.cloneNode( true ),
    245 			inPage = isAttached( elem );
    246 
    247 		// Fix IE cloning issues
    248 		if ( !support.noCloneChecked && ( elem.nodeType === 1 || elem.nodeType === 11 ) &&
    249 				!jQuery.isXMLDoc( elem ) ) {
    250 
    251 			// We eschew jQuery#find here for performance reasons:
    252 			// https://jsperf.com/getall-vs-sizzle/2
    253 			destElements = getAll( clone );
    254 			srcElements = getAll( elem );
    255 
    256 			for ( i = 0, l = srcElements.length; i < l; i++ ) {
    257 				fixInput( srcElements[ i ], destElements[ i ] );
    258 			}
    259 		}
    260 
    261 		// Copy the events from the original to the clone
    262 		if ( dataAndEvents ) {
    263 			if ( deepDataAndEvents ) {
    264 				srcElements = srcElements || getAll( elem );
    265 				destElements = destElements || getAll( clone );
    266 
    267 				for ( i = 0, l = srcElements.length; i < l; i++ ) {
    268 					cloneCopyEvent( srcElements[ i ], destElements[ i ] );
    269 				}
    270 			} else {
    271 				cloneCopyEvent( elem, clone );
    272 			}
    273 		}
    274 
    275 		// Preserve script evaluation history
    276 		destElements = getAll( clone, "script" );
    277 		if ( destElements.length > 0 ) {
    278 			setGlobalEval( destElements, !inPage && getAll( elem, "script" ) );
    279 		}
    280 
    281 		// Return the cloned set
    282 		return clone;
    283 	},
    284 
    285 	cleanData: function( elems ) {
    286 		var data, elem, type,
    287 			special = jQuery.event.special,
    288 			i = 0;
    289 
    290 		for ( ; ( elem = elems[ i ] ) !== undefined; i++ ) {
    291 			if ( acceptData( elem ) ) {
    292 				if ( ( data = elem[ dataPriv.expando ] ) ) {
    293 					if ( data.events ) {
    294 						for ( type in data.events ) {
    295 							if ( special[ type ] ) {
    296 								jQuery.event.remove( elem, type );
    297 
    298 							// This is a shortcut to avoid jQuery.event.remove's overhead
    299 							} else {
    300 								jQuery.removeEvent( elem, type, data.handle );
    301 							}
    302 						}
    303 					}
    304 
    305 					// Support: Chrome <=35 - 45+
    306 					// Assign undefined instead of using delete, see Data#remove
    307 					elem[ dataPriv.expando ] = undefined;
    308 				}
    309 				if ( elem[ dataUser.expando ] ) {
    310 
    311 					// Support: Chrome <=35 - 45+
    312 					// Assign undefined instead of using delete, see Data#remove
    313 					elem[ dataUser.expando ] = undefined;
    314 				}
    315 			}
    316 		}
    317 	}
    318 } );
    319 
    320 jQuery.fn.extend( {
    321 	detach: function( selector ) {
    322 		return remove( this, selector, true );
    323 	},
    324 
    325 	remove: function( selector ) {
    326 		return remove( this, selector );
    327 	},
    328 
    329 	text: function( value ) {
    330 		return access( this, function( value ) {
    331 			return value === undefined ?
    332 				jQuery.text( this ) :
    333 				this.empty().each( function() {
    334 					if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
    335 						this.textContent = value;
    336 					}
    337 				} );
    338 		}, null, value, arguments.length );
    339 	},
    340 
    341 	append: function() {
    342 		return domManip( this, arguments, function( elem ) {
    343 			if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
    344 				var target = manipulationTarget( this, elem );
    345 				target.appendChild( elem );
    346 			}
    347 		} );
    348 	},
    349 
    350 	prepend: function() {
    351 		return domManip( this, arguments, function( elem ) {
    352 			if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
    353 				var target = manipulationTarget( this, elem );
    354 				target.insertBefore( elem, target.firstChild );
    355 			}
    356 		} );
    357 	},
    358 
    359 	before: function() {
    360 		return domManip( this, arguments, function( elem ) {
    361 			if ( this.parentNode ) {
    362 				this.parentNode.insertBefore( elem, this );
    363 			}
    364 		} );
    365 	},
    366 
    367 	after: function() {
    368 		return domManip( this, arguments, function( elem ) {
    369 			if ( this.parentNode ) {
    370 				this.parentNode.insertBefore( elem, this.nextSibling );
    371 			}
    372 		} );
    373 	},
    374 
    375 	empty: function() {
    376 		var elem,
    377 			i = 0;
    378 
    379 		for ( ; ( elem = this[ i ] ) != null; i++ ) {
    380 			if ( elem.nodeType === 1 ) {
    381 
    382 				// Prevent memory leaks
    383 				jQuery.cleanData( getAll( elem, false ) );
    384 
    385 				// Remove any remaining nodes
    386 				elem.textContent = "";
    387 			}
    388 		}
    389 
    390 		return this;
    391 	},
    392 
    393 	clone: function( dataAndEvents, deepDataAndEvents ) {
    394 		dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
    395 		deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
    396 
    397 		return this.map( function() {
    398 			return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
    399 		} );
    400 	},
    401 
    402 	html: function( value ) {
    403 		return access( this, function( value ) {
    404 			var elem = this[ 0 ] || {},
    405 				i = 0,
    406 				l = this.length;
    407 
    408 			if ( value === undefined && elem.nodeType === 1 ) {
    409 				return elem.innerHTML;
    410 			}
    411 
    412 			// See if we can take a shortcut and just use innerHTML
    413 			if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
    414 				!wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {
    415 
    416 				value = jQuery.htmlPrefilter( value );
    417 
    418 				try {
    419 					for ( ; i < l; i++ ) {
    420 						elem = this[ i ] || {};
    421 
    422 						// Remove element nodes and prevent memory leaks
    423 						if ( elem.nodeType === 1 ) {
    424 							jQuery.cleanData( getAll( elem, false ) );
    425 							elem.innerHTML = value;
    426 						}
    427 					}
    428 
    429 					elem = 0;
    430 
    431 				// If using innerHTML throws an exception, use the fallback method
    432 				} catch ( e ) {}
    433 			}
    434 
    435 			if ( elem ) {
    436 				this.empty().append( value );
    437 			}
    438 		}, null, value, arguments.length );
    439 	},
    440 
    441 	replaceWith: function() {
    442 		var ignored = [];
    443 
    444 		// Make the changes, replacing each non-ignored context element with the new content
    445 		return domManip( this, arguments, function( elem ) {
    446 			var parent = this.parentNode;
    447 
    448 			if ( jQuery.inArray( this, ignored ) < 0 ) {
    449 				jQuery.cleanData( getAll( this ) );
    450 				if ( parent ) {
    451 					parent.replaceChild( elem, this );
    452 				}
    453 			}
    454 
    455 		// Force callback invocation
    456 		}, ignored );
    457 	}
    458 } );
    459 
    460 jQuery.each( {
    461 	appendTo: "append",
    462 	prependTo: "prepend",
    463 	insertBefore: "before",
    464 	insertAfter: "after",
    465 	replaceAll: "replaceWith"
    466 }, function( name, original ) {
    467 	jQuery.fn[ name ] = function( selector ) {
    468 		var elems,
    469 			ret = [],
    470 			insert = jQuery( selector ),
    471 			last = insert.length - 1,
    472 			i = 0;
    473 
    474 		for ( ; i <= last; i++ ) {
    475 			elems = i === last ? this : this.clone( true );
    476 			jQuery( insert[ i ] )[ original ]( elems );
    477 
    478 			// Support: Android <=4.0 only, PhantomJS 1 only
    479 			// .get() because push.apply(_, arraylike) throws on ancient WebKit
    480 			push.apply( ret, elems.get() );
    481 		}
    482 
    483 		return this.pushStack( ret );
    484 	};
    485 } );
    486 
    487 return jQuery;
    488 } );