Ticket #2056: jquery.patch
| File jquery.patch, 13.2 KB (added by greg, 4 years ago) |
|---|
-
src/core.js
157 157 // Determine the position of an element within 158 158 // the matched set of elements 159 159 index: function( elem ) { 160 var ret = -1; 161 162 // Locate the position of the desired element 163 this.each(function(i){ 164 if ( this == elem ) 165 ret = i; 166 }); 167 168 return ret; 160 return jQuery.inArray( elem, this ) 169 161 }, 170 162 171 163 attr: function( name, value, type ) { … … 201 193 return this.attr( key, value, "curCSS" ); 202 194 }, 203 195 196 getText: function( elem ) { 197 return jQuery.map( elem.childNodes, function(child){ 198 var nt = child.nodeType; 199 return nt == 8 ? null 200 : nt == 1 ? jQuery.fn.getText( child ) 201 : child.nodeValue; 202 }).join(""); 203 }, 204 204 205 text: function( text ) { 205 if ( typeof text != "object" && text != null ) 206 return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) ); 207 208 var ret = ""; 209 210 jQuery.each( text || this, function(){ 211 jQuery.each( this.childNodes, function(){ 212 if ( this.nodeType != 8 ) 213 ret += this.nodeType != 1 ? 214 this.nodeValue : 215 jQuery.fn.text( [ this ] ); 216 }); 217 }); 218 219 return ret; 206 if ( typeof text != "object" && text !== null && text !== undefined ) 207 return this.empty().append( document.createTextNode( text ) ); 208 else 209 return jQuery.map( text || this, jQuery.fn.getText ).join(""); 220 210 }, 221 211 222 212 wrapAll: function( html ) { … … 476 466 }, 477 467 478 468 domManip: function( args, table, reverse, callback ) { 479 var clone = this.length > 1, elems; 469 var len = this.length, clone, elems; 470 if( len === 0 ) return this; 471 else { 472 clone = len > 1 473 elems = jQuery.clean( args, this[0].ownerDocument ); 474 if ( reverse ) elems.reverse(); 475 } 480 476 481 477 return this.each(function(){ 482 if ( !elems ) {483 elems = jQuery.clean( args, this.ownerDocument );484 485 if ( reverse )486 elems.reverse();487 }488 489 478 var obj = this; 490 479 491 480 if ( table && jQuery.nodeName( this, "table" ) && jQuery.nodeName( elems[0], "tr" ) ) … … 1077 1066 }, 1078 1067 1079 1068 makeArray: function( array ) { 1080 var ret = [];1081 1082 1069 // Need to use typeof to fight Safari childNodes crashes 1083 if ( typeof array != "array" ) 1084 for ( var i = 0, length = array.length; i < length; i++ ) 1085 ret.push( array[ i ] ); 1086 else 1087 ret = array.slice( 0 ); 1070 if ( typeof array == "array" ) return array.slice( 0 ); 1088 1071 1072 var ret = []; 1073 for ( var i = 0, length = array.length; i < length; i++ ) 1074 ret.push( array[ i ] ); 1089 1075 return ret; 1090 1076 }, 1091 1077 … … 1100 1086 merge: function( first, second ) { 1101 1087 // We have to loop this way because IE & Opera overwrite the length 1102 1088 // expando of getElementsByTagName 1103 1104 1089 // Also, we need to make sure that the correct elements are being returned 1105 // (IE returns comment nodes in a '*' query) 1106 if ( jQuery.browser.msie ) { 1107 for ( var i = 0; second[ i ]; i++ ) 1108 if ( second[ i ].nodeType != 8 ) 1109 first.push( second[ i ] ); 1090 for ( var i = 0, next=second[1]; ; ){ 1091 if( !next ){ 1092 if( second[i] ) first.push( second[i] ); 1093 break; 1094 } 1095 first.push( second[i], next ); 1096 i+=2 1097 next = second[ i+1 ]; 1098 } 1099 return first; 1100 }, 1110 1101 1111 } else 1112 for ( var i = 0; second[ i ]; i++ ) 1102 msie_merge: function(first, second){ 1103 for ( var i = 0; second[ i ]; i++ ) 1104 // (IE returns comment nodes in a '*' query) 1105 if ( second[ i ].nodeType != 8 ) 1113 1106 first.push( second[ i ] ); 1114 1115 1107 return first; 1116 1108 }, 1117 1109 … … 1130 1122 } 1131 1123 1132 1124 } catch( e ) { 1133 ret =array;1125 return array; 1134 1126 } 1135 1127 1136 1128 return ret; 1137 1129 }, 1138 1130 1131 reject: function(elems, callback) { 1132 var ret = []; 1133 for( var i = 0, el = elems.length; i < el; i++ ) { 1134 if ( !callback( elems[i], i ) ) ret.push( elems[i] ); 1135 } 1136 return ret; 1137 }, 1138 1139 1139 grep: function( elems, callback, inv ) { 1140 1140 // If a string is passed in for the function, make a function 1141 1141 // for it (a handy shortcut) 1142 1142 if ( typeof callback == "string" ) 1143 1143 callback = eval("false||function(a,i){return " + callback + "}"); 1144 1144 1145 if( inv ) return jQuery.reject(elems, callback); 1146 1145 1147 var ret = []; 1146 1147 // Go through the array, only saving the items 1148 // that pass the validator function 1149 for ( var i = 0, length = elems.length; i < length; i++ ) 1150 if ( !inv && callback( elems[ i ], i ) || inv && !callback( elems[ i ], i ) ) 1151 ret.push( elems[ i ] ); 1152 1148 for ( var i = 0, length = elems.length; i < length; i++ ) { 1149 if ( callback( elems[i], i ) ) ret.push( elems[i] ); 1150 } 1153 1151 return ret; 1154 1152 }, 1155 1153 1154 // automatically flattens returned arrays 1155 // map(xs, function(x) {return [x]}) == xs 1156 1156 map: function( elems, callback ) { 1157 var ret = [];1157 var arrays = []; // array of arrays to be concated together 1158 1158 1159 // Go through the array, translating each of the items to their 1160 // new value (or values). 1161 for ( var i = 0, length = elems.length; i < length; i++ ) { 1159 for (var i = 0, length = elems.length; i < length; i++ ) { 1162 1160 var value = callback( elems[ i ], i ); 1163 1161 1164 if ( value !== null && value != undefined ) { 1165 if ( value.constructor != Array ) 1166 value = [ value ]; 1162 if ( value !== null && value !== undefined ) { 1163 if ( value.constructor != Array ) arrays.push( [value] ); 1164 else arrays.push( value ); 1165 } 1166 } 1167 // if arrays has no members, Array.concat will return undefined 1168 if( arrays.length === 0 ) return arrays; 1169 return Array.concat.apply(Array, arrays); 1170 }, 1167 1171 1168 ret = ret.concat( value ); 1169 } 1172 1173 // map without flattening, error on undefined, filter null 1174 collect: function( elems, callback ) { 1175 ret = [] 1176 for (var i = 0, length = elems.length; i < length; i++ ) { 1177 var value = callback( elems[ i ], i ); 1178 if( value === undefined ) 1179 throw("collect: callback return undefined value "); 1180 if( value !== null ) ret.push( value ); 1170 1181 } 1171 1172 1182 return ret; 1173 1183 } 1174 1184 }); … … 1330 1340 this.css( type, size.constructor == String ? size : size + "px" ); 1331 1341 }; 1332 1342 }); 1343 1344 if ( jQuery.browser.msie ) jQuery.extend({merge: jQuery.msie_merge}) -
src/selector.js
8 8 9 9 jQuery.extend({ 10 10 expr: { 11 "" : "m[2]=='*'||jQuery.nodeName(a,m[2])",12 "#": "a.getAttribute('id')==m[2]",11 "" : function(a){return m[2]=='*'||jQuery.nodeName(a,m[2])}, 12 "#": function(a){return a.getAttribute('id')==m[2]}, 13 13 ":": { 14 14 // Position Checks 15 15 lt: "i<m[3]-0", … … 67 67 }, 68 68 69 69 // The regular expressions that power the parsing engine 70 parse: [ 70 parse: { 71 attributes: 71 72 // Match: [@value='test'], [@foo] 72 73 /^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/, 73 74 75 argument: 74 76 // Match: :contains('foo') 75 /^(:)([\w-]+)\( "?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,77 /^(:)([\w-]+)\(["']?(.*?(\(.*?\))?[^(]*?)["']?\)/, 76 78 77 // Match: :even, :last-chlid, #id, .class 78 new RegExp("^([:.#]*)(" + chars + "+)") 79 ], 79 other: 80 // Match: :even, :last-child, #id, .class 81 new RegExp("^([:.#]?)(" + chars + "+)") 82 }, 80 83 81 84 multiFilter: function( expr, elems, not ) { 82 85 var old, cur = []; … … 293 296 return tmp; 294 297 }, 295 298 296 filter: function(t,r,not) { 297 var last; 299 //variables used only in the filter function 300 //placed here so they can be bound to the expr functions 301 m: null, r: null, 302 filter: function(t,r_passed,not) { 303 var last, p = jQuery.parse; 304 m = null, r = r_passed; 305 function match(key) { 306 m = p[key].exec(t) 307 if(m){ 308 // Remove what we just matched 309 t = t.substring( m[0].length ); 298 310 311 m[2] = m[2].replace(/\\/g, ""); 312 } 313 return m; 314 } 315 299 316 // Look for common filter expressions 300 317 while ( t && t != last ) { 301 318 last = t; 302 319 303 var p = jQuery.parse, m; 304 305 for ( var i = 0; p[i]; i++ ) { 306 m = p[i].exec( t ); 307 308 if ( m ) { 309 // Remove what we just matched 310 t = t.substring( m[0].length ); 311 312 m[2] = m[2].replace(/\\/g, ""); 313 break; 314 } 315 } 316 317 if ( !m ) 318 break; 319 320 // :not() is a special case that can be optimized by 321 // keeping it out of the expression list 322 if ( m[1] == ":" && m[2] == "not" ) 323 // optimize if only one selector found (most common case) 324 r = isSimple.test( m[3] ) ? 325 jQuery.filter(m[3], r, true).r : 326 jQuery( r ).not( m[3] ); 327 328 // We can get a big speed boost by filtering by class here 329 else if ( m[1] == "." ) 330 r = jQuery.classFilter(r, m[2], not); 331 332 else if ( m[1] == "[" ) { 333 var tmp = [], type = m[3]; 320 if ( match("attributes") ){ 321 var tmp = [], type = m[3], m2 = m[2], m5 = m[5]; 334 322 335 323 for ( var i = 0, rl = r.length; i < rl; i++ ) { 336 var a = r[i], z = a[ jQuery.props[m [2]] || m[2]];324 var a = r[i], z = a[ jQuery.props[m2] || m2 ]; 337 325 338 if ( z == null || /href|src|selected/.test(m [2]) )339 z = jQuery.attr(a,m [2]) || '';326 if ( z == null || /href|src|selected/.test(m2) ) 327 z = jQuery.attr(a,m2) || ''; 340 328 341 329 if ( (type == "" && !!z || 342 type == "=" && z == m [5]||343 type == "!=" && z != m [5]||344 type == "^=" && z && !z.indexOf(m [5]) ||345 type == "$=" && z.substr(z.length - m [5].length) == m[5]||346 (type == "*=" || type == "~=") && z.indexOf(m [5]) >= 0) ^ not )330 type == "=" && z == m5 || 331 type == "!=" && z != m5 || 332 type == "^=" && z && !z.indexOf(m5) || 333 type == "$=" && z.substr(z.length - m5.length) == m5 || 334 (type == "*=" || type == "~=") && z.indexOf(m5) >= 0) ^ not ) 347 335 tmp.push( a ); 348 336 } 349 337 350 338 r = tmp; 339 continue; 340 } 351 341 352 // We can get a speed boost by handling nth-child here 353 } else if ( m[1] == ":" && m[2] == "nth-child" ) { 354 var merge = {}, tmp = [], 355 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' 356 test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec( 357 m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" || 358 !/\D/.test(m[3]) && "0n+" + m[3] || m[3]), 359 // calculate the numbers (first)n+(last) including if they are negative 360 first = (test[1] + (test[2] || 1)) - 0, last = test[3] - 0; 361 362 // loop through all the elements left in the jQuery object 363 for ( var i = 0, rl = r.length; i < rl; i++ ) { 364 var node = r[i], parentNode = node.parentNode, id = jQuery.data(parentNode); 342 if ( match("argument") ){ 343 var m2 = m[2], m3 = m[3] 344 // :not() is a special case that can be optimized by 345 // keeping it out of the expression list 346 if ( m2 == "not" ){ 347 // optimize if only one selector found (most common case) 348 r = isSimple.test( m3 ) ? 349 jQuery.filter(m3, r, true).r : 350 jQuery( r ).not( m3 ); 351 continue; 352 } 353 // We can get a speed boost by handling nth-child here 354 else if ( m2 == "nth-child" ) { 355 var merge = {}, tmp = [], 356 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' 357 test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec( 358 m3 == "even" && "2n" || m3 == "odd" && "2n+1" || 359 !/\D/.test(m3) && "0n+" + m3 || m3), 360 // calculate the numbers (first)n+(last) including if they are negative 361 first = (test[1] + (test[2] || 1)) - 0, last = test[3] - 0; 362 363 // loop through all the elements left in the jQuery object 364 for ( var i = 0, rl = r.length; i < rl; i++ ) { 365 var node = r[i], parentNode = node.parentNode, id = jQuery.data(parentNode); 365 366 366 if ( !merge[id] ) {367 var c = 1;367 if ( !merge[id] ) { 368 var c = 1; 368 369 369 for ( var n = parentNode.firstChild; n; n = n.nextSibling )370 if ( n.nodeType == 1 )371 n.nodeIndex = c++;370 for ( var n = parentNode.firstChild; n; n = n.nextSibling ) 371 if ( n.nodeType == 1 ) 372 n.nodeIndex = c++; 372 373 373 merge[id] = true;374 }374 merge[id] = true; 375 } 375 376 376 var add = false;377 var add = false; 377 378 378 if ( first == 0 ) { 379 if ( node.nodeIndex == last ) 379 if ( first == 0 ) { 380 if ( node.nodeIndex == last ) 381 add = true; 382 } else if ( (node.nodeIndex - last) % first == 0 && (node.nodeIndex - last) / first >= 0 ) 380 383 add = true; 381 } else if ( (node.nodeIndex - last) % first == 0 && (node.nodeIndex - last) / first >= 0 )382 add = true;383 384 384 if ( add ^ not ) 385 tmp.push( node ); 385 if ( add ^ not ) 386 tmp.push( node ); 387 } 388 389 r = tmp; 390 continue; 386 391 } 392 } else { 393 if ( !match("other") ) break; 387 394 388 r = tmp; 395 // We can get a big speed boost by filtering by class here 396 if ( m[0].charAt(0) == "." ){ 397 r = jQuery.classFilter(r, m[2], not); 398 continue; 399 } 389 400 401 } 402 390 403 // Otherwise, find the expression to execute 391 } else { 392 var f = jQuery.expr[m[1]]; 393 if ( typeof f != "string" ) 394 f = jQuery.expr[m[1]][m[2]]; 404 var k = m[1] 395 405 396 // Build a custom macro to enclose it 397 f = eval("false||function(a,i){return " + f + "}"); 406 if( k == ':' ){ 407 var exprs = jQuery.expr[':']; 408 k = m[2]; f = exprs[k]; 398 409 399 // Execute it against the current filter 400 r = jQuery.grep( r, f, not ); 401 } 410 if( typeof f == "string" ){ 411 eval("f=function(a,i){return " + f + "}"); 412 exprs[k] = f; 413 } 414 // Build a custom function to enclose it 415 } else f = jQuery.expr[k]; 416 417 // Execute it against the current filter 418 r = jQuery.grep( r, f, not ); 402 419 } 403 420 404 421 // Return an array of filtered elements (r)
