Skip to content

Commit

Permalink
Fix #12959: Optimize library-wide patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Nov 26, 2012
1 parent 53a0666 commit 0766240
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 111 deletions.
1 change: 1 addition & 0 deletions src/.jshintrc
Expand Up @@ -8,6 +8,7 @@
"maxerr": 100, "maxerr": 100,
"newcap": false, "newcap": false,
"quotmark": "double", "quotmark": "double",
"regexdash": true,
"strict": true, "strict": true,
"sub": true, "sub": true,
"trailing": true, "trailing": true,
Expand Down
6 changes: 3 additions & 3 deletions src/ajax.js
Expand Up @@ -13,7 +13,7 @@ var
rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
rnoContent = /^(?:GET|HEAD)$/, rnoContent = /^(?:GET|HEAD)$/,
rprotocol = /^\/\//, rprotocol = /^\/\//,
rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/, rurl = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,


// Keep a copy of the old load method // Keep a copy of the old load method
_load = jQuery.fn.load, _load = jQuery.fn.load,
Expand Down Expand Up @@ -67,7 +67,7 @@ function addToPrefiltersOrTransports( structure ) {


var dataType, var dataType,
i = 0, i = 0,
dataTypes = dataTypeExpression.toLowerCase().split( core_rspace ); dataTypes = dataTypeExpression.toLowerCase().match( core_rnotwhite ) || [];


if ( jQuery.isFunction( func ) ) { if ( jQuery.isFunction( func ) ) {
// For each dataType in the dataTypeExpression // For each dataType in the dataTypeExpression
Expand Down Expand Up @@ -432,7 +432,7 @@ jQuery.extend({
s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" ).replace( rprotocol, ajaxLocParts[ 1 ] + "//" ); s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" ).replace( rprotocol, ajaxLocParts[ 1 ] + "//" );


// Extract dataTypes list // Extract dataTypes list
s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( core_rspace ); s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( core_rnotwhite ) || [""];


// A cross-domain request is in order when we have a protocol:host:port mismatch // A cross-domain request is in order when we have a protocol:host:port mismatch
if ( s.crossDomain == null ) { if ( s.crossDomain == null ) {
Expand Down
115 changes: 57 additions & 58 deletions src/attributes.js
@@ -1,9 +1,9 @@
var nodeHook, boolHook, fixSpecified, var nodeHook, boolHook, fixSpecified,
rclass = /[\t\r\n]/g, rclass = /[\t\r\n]/g,
rreturn = /\r/g, rreturn = /\r/g,
rtype = /^(?:button|input)$/i, rtype = /^(?:input|button)$/i,
rfocusable = /^(?:button|input|object|select|textarea)$/i, rfocusable = /^(?:input|select|textarea|button|object)$/i,
rclickable = /^a(?:rea|)$/i, rclickable = /^(?:a|area)$/i,
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i, rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i,
getSetAttribute = jQuery.support.getSetAttribute; getSetAttribute = jQuery.support.getSetAttribute;


Expand Down Expand Up @@ -34,35 +34,36 @@ jQuery.fn.extend({
}, },


addClass: function( value ) { addClass: function( value ) {
var classNames, i, l, elem, var classes, elem, cur, clazz, j,
setClass, c, cl; i = 0,
len = this.length,
proceed = typeof value === "string" && value;


if ( jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
return this.each(function( j ) { return this.each(function( j ) {
jQuery( this ).addClass( value.call(this, j, this.className) ); jQuery( this ).addClass( value.call( this, j, this.className ) );
}); });
} }


if ( value && typeof value === "string" ) { if ( proceed ) {
classNames = value.split( core_rspace ); // The disjunction here is for better compressibility (see removeClass)
classes = ( value || "" ).match( core_rnotwhite ) || [];


for ( i = 0, l = this.length; i < l; i++ ) { for ( ; i < len; i++ ) {
elem = this[ i ]; elem = this[ i ];


if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
if ( !elem.className && classNames.length === 1 ) { cur = elem.className ?
elem.className = value; ( " " + elem.className + " " ).replace( rclass, " " ) :

" ";
} else { j = 0;
setClass = " " + elem.className + " "; while ( (clazz = classes[j++]) ) {

if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
for ( c = 0, cl = classNames.length; c < cl; c++ ) { cur += clazz + " ";
if ( setClass.indexOf( " " + classNames[ c ] + " " ) < 0 ) {
setClass += classNames[ c ] + " ";
}
} }
elem.className = jQuery.trim( setClass );
} }
elem.className = jQuery.trim( cur );

} }
} }
} }
Expand All @@ -71,30 +72,34 @@ jQuery.fn.extend({
}, },


removeClass: function( value ) { removeClass: function( value ) {
var removes, className, elem, c, cl, i, l; var classes, elem, cur, clazz, j,
i = 0,
len = this.length,
proceed = arguments.length === 0 || typeof value === "string" && value;


if ( jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
return this.each(function( j ) { return this.each(function( j ) {
jQuery( this ).removeClass( value.call(this, j, this.className) ); jQuery( this ).removeClass( value.call( this, j, this.className ) );
}); });
} }
if ( (value && typeof value === "string") || !arguments.length ) { if ( proceed ) {
removes = ( value || "" ).split( core_rspace ); classes = ( value || "" ).match( core_rnotwhite ) || [];


for ( i = 0, l = this.length; i < l; i++ ) { for ( ; i < len; i++ ) {
elem = this[ i ]; elem = this[ i ];
if ( elem.nodeType === 1 && elem.className ) { if ( elem.nodeType === 1 ) {

// This expression is here for better compressibility (see addClass)
className = (" " + elem.className + " ").replace( rclass, " " ); cur = elem.className ?

( " " + elem.className + " " ).replace( rclass, " " ) :
// loop over each item in the removal list " ";
for ( c = 0, cl = removes.length; c < cl; c++ ) { j = 0;
// Remove until there is nothing to remove, while ( (clazz = classes[j++]) ) {
while ( className.indexOf(" " + removes[ c ] + " ") >= 0 ) { // Remove *all* instances
className = className.replace( " " + removes[ c ] + " " , " " ); while ( cur.indexOf( " " + clazz + " " ) >= 0 ) {
cur = cur.replace( " " + clazz + " ", " " );
} }
} }
elem.className = value ? jQuery.trim( className ) : ""; elem.className = value ? jQuery.trim( cur ) : "";
} }
} }
} }
Expand All @@ -119,7 +124,7 @@ jQuery.fn.extend({
i = 0, i = 0,
self = jQuery( this ), self = jQuery( this ),
state = stateVal, state = stateVal,
classNames = value.split( core_rspace ); classNames = value.match( core_rnotwhite ) || [];


while ( (className = classNames[ i++ ]) ) { while ( (className = classNames[ i++ ]) ) {
// check each className given, space separated list // check each className given, space separated list
Expand Down Expand Up @@ -331,31 +336,25 @@ jQuery.extend({
}, },


removeAttr: function( elem, value ) { removeAttr: function( elem, value ) {
var propName, attrNames, name, isBool, var name, propName, isBool,
i = 0; i = 0,

attrNames = value && value.match( core_rnotwhite );
if ( value && elem.nodeType === 1 ) {

attrNames = value.split( core_rspace );

for ( ; i < attrNames.length; i++ ) {
name = attrNames[ i ];


if ( name ) { if ( attrNames && elem.nodeType === 1 ) {
propName = jQuery.propFix[ name ] || name; while ( (name = attrNames[i++]) ) {
isBool = rboolean.test( name ); propName = jQuery.propFix[ name ] || name;
isBool = rboolean.test( name );


// See #9699 for explanation of this approach (setting first, then removal) // See #9699 for explanation of this approach (setting first, then removal)
// Do not do this for boolean attributes (see #10870) // Do not do this for boolean attributes (see #10870)
if ( !isBool ) { if ( !isBool ) {
jQuery.attr( elem, name, "" ); jQuery.attr( elem, name, "" );
} }
elem.removeAttribute( getSetAttribute ? name : propName ); elem.removeAttribute( getSetAttribute ? name : propName );


// Set corresponding property to false for boolean attributes // Set corresponding property to false for boolean attributes
if ( isBool && propName in elem ) { if ( isBool && propName in elem ) {
elem[ propName ] = false; elem[ propName ] = false;
}
} }
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks.js
Expand Up @@ -4,7 +4,7 @@ var optionsCache = {};
// Convert String-formatted options into Object-formatted ones and store in cache // Convert String-formatted options into Object-formatted ones and store in cache
function createOptions( options ) { function createOptions( options ) {
var object = optionsCache[ options ] = {}; var object = optionsCache[ options ] = {};
jQuery.each( options.split( core_rspace ), function( _, flag ) { jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) {
object[ flag ] = true; object[ flag ] = true;
}); });
return object; return object;
Expand Down
51 changes: 26 additions & 25 deletions src/core.js
Expand Up @@ -15,14 +15,22 @@ var
// Map over the $ in case of overwrite // Map over the $ in case of overwrite
_$ = window.$, _$ = window.$,


// [[Class]] -> type pairs
class2type = {},

// List of deleted data cache ids, so we can reuse them
core_deletedIds = [],

core_version = "@VERSION",

// Save a reference to some core methods // Save a reference to some core methods
core_concat = Array.prototype.concat, core_concat = core_deletedIds.concat,
core_push = Array.prototype.push, core_push = core_deletedIds.push,
core_slice = Array.prototype.slice, core_slice = core_deletedIds.slice,
core_indexOf = Array.prototype.indexOf, core_indexOf = core_deletedIds.indexOf,
core_toString = Object.prototype.toString, core_toString = class2type.toString,
core_hasOwn = Object.prototype.hasOwnProperty, core_hasOwn = class2type.hasOwnProperty,
core_trim = String.prototype.trim, core_trim = core_version.trim,


// Define a local copy of jQuery // Define a local copy of jQuery
jQuery = function( selector, context ) { jQuery = function( selector, context ) {
Expand All @@ -31,21 +39,17 @@ var
}, },


// Used for matching numbers // Used for matching numbers
core_pnum = /[\-+]?(?:\d*\.|)\d+(?:[eE][\-+]?\d+|)/.source, core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,


// Used for detecting and trimming whitespace // Used for splitting on whitespace
core_rnotwhite = /\S/, core_rnotwhite = /\S+/g,
core_rspace = /\s+/,

// List of deleted data cache ids, so we can reuse them
core_deletedIds = [],


// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE) // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,


// A simple way to check for HTML strings // A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/, rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*|#([\w-]*))$/,


// Match a standalone tag // Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
Expand All @@ -54,15 +58,15 @@ var
rvalidchars = /^[\],:{}\s]*$/, rvalidchars = /^[\],:{}\s]*$/,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g, rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d\d*\.|)\d+(?:[eE][\-+]?\d+|)/g, rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g,


// Matches dashed string for camelizing // Matches dashed string for camelizing
rmsPrefix = /^-ms-/, rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi, rdashAlpha = /-([\da-z])/gi,


// Used by jQuery.camelCase as callback to replace() // Used by jQuery.camelCase as callback to replace()
fcamelCase = function( all, letter ) { fcamelCase = function( all, letter ) {
return ( letter + "" ).toUpperCase(); return letter.toUpperCase();
}, },


// The ready event handler and self cleanup method // The ready event handler and self cleanup method
Expand All @@ -76,12 +80,12 @@ var
document.detachEvent( "onreadystatechange", DOMContentLoaded ); document.detachEvent( "onreadystatechange", DOMContentLoaded );
jQuery.ready(); jQuery.ready();
} }
}, };

// [[Class]] -> type pairs
class2type = {};


jQuery.fn = jQuery.prototype = { jQuery.fn = jQuery.prototype = {
// The current version of jQuery being used
jquery: core_version,

constructor: jQuery, constructor: jQuery,
init: function( selector, context, rootjQuery ) { init: function( selector, context, rootjQuery ) {
var match, elem; var match, elem;
Expand Down Expand Up @@ -188,9 +192,6 @@ jQuery.fn = jQuery.prototype = {
// Start with an empty selector // Start with an empty selector
selector: "", selector: "",


// The current version of jQuery being used
jquery: "@VERSION",

// The default length of a jQuery object is 0 // The default length of a jQuery object is 0
length: 0, length: 0,


Expand Down Expand Up @@ -560,7 +561,7 @@ jQuery.extend({
// Workarounds based on findings by Jim Driscoll // Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
globalEval: function( data ) { globalEval: function( data ) {
if ( data && core_rnotwhite.test( data ) ) { if ( data && jQuery.trim( data ) ) {
// We use execScript on Internet Explorer // We use execScript on Internet Explorer
// We use an anonymous function so that context is window // We use an anonymous function so that context is window
// rather than jQuery in Firefox // rather than jQuery in Firefox
Expand Down
2 changes: 1 addition & 1 deletion src/css.js
Expand Up @@ -8,7 +8,7 @@ var curCSS, iframe, iframeDoc,
rmargin = /^margin/, rmargin = /^margin/,
rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "i" ), rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "i" ),
rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ), rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ),
rrelNum = new RegExp( "^([-+])=(" + core_pnum + ")", "i" ), rrelNum = new RegExp( "^([+-])=(" + core_pnum + ")", "i" ),
elemdisplay = { BODY: "block" }, elemdisplay = { BODY: "block" },


cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssShow = { position: "absolute", visibility: "hidden", display: "block" },
Expand Down
2 changes: 1 addition & 1 deletion src/data.js
Expand Up @@ -188,7 +188,7 @@ jQuery.extend({


// Unique for each copy of jQuery on the page // Unique for each copy of jQuery on the page
// Non-digits removed to match rinlinejQuery // Non-digits removed to match rinlinejQuery
expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ), expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ),


// The following elements throw uncatchable exceptions if you // The following elements throw uncatchable exceptions if you
// attempt to add expando properties to them. // attempt to add expando properties to them.
Expand Down
2 changes: 1 addition & 1 deletion src/effects.js
@@ -1,6 +1,6 @@
var fxNow, timerId, var fxNow, timerId,
rfxtypes = /^(?:toggle|show|hide)$/, rfxtypes = /^(?:toggle|show|hide)$/,
rfxnum = new RegExp( "^(?:([-+])=|)(" + core_pnum + ")([a-z%]*)$", "i" ), rfxnum = new RegExp( "^(?:([+-])=|)(" + core_pnum + ")([a-z%]*)$", "i" ),
rrun = /queueHooks$/, rrun = /queueHooks$/,
animationPrefilters = [ defaultPrefilter ], animationPrefilters = [ defaultPrefilter ],
tweeners = { tweeners = {
Expand Down

0 comments on commit 0766240

Please sign in to comment.