Skip to content
Permalink
Browse files

Fix #12959: Optimize library-wide patterns

  • Loading branch information...
gibson042 authored and jaubourg committed Nov 26, 2012
1 parent 512b826 commit 0877d424e08d57e6f0d1da9a86289fb10d0ee136
Showing with 112 additions and 111 deletions.
  1. +1 −0 src/.jshintrc
  2. +3 −3 src/ajax.js
  3. +57 −58 src/attributes.js
  4. +1 −1 src/callbacks.js
  5. +26 −25 src/core.js
  6. +1 −1 src/css.js
  7. +1 −1 src/data.js
  8. +1 −1 src/effects.js
  9. +12 −11 src/event.js
  10. +6 −6 src/manipulation.js
  11. +2 −3 src/serialize.js
  12. +1 −1 src/sizzle
@@ -8,6 +8,7 @@
"maxerr": 100,
"newcap": false,
"quotmark": "double",
"regexdash": true,
"strict": true,
"sub": true,
"trailing": true,
@@ -13,7 +13,7 @@ var
rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
rnoContent = /^(?:GET|HEAD)$/,
rprotocol = /^\/\//,
rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,
rurl = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,

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

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

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

// 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
if ( s.crossDomain == null ) {
@@ -1,9 +1,9 @@
var nodeHook, boolHook, fixSpecified,
rclass = /[\t\r\n]/g,
rreturn = /\r/g,
rtype = /^(?:button|input)$/i,
rfocusable = /^(?:button|input|object|select|textarea)$/i,
rclickable = /^a(?:rea|)$/i,
rtype = /^(?:input|button)$/i,
rfocusable = /^(?:input|select|textarea|button|object)$/i,
rclickable = /^(?:a|area)$/i,
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i,
getSetAttribute = jQuery.support.getSetAttribute;

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

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

if ( jQuery.isFunction( value ) ) {
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" ) {
classNames = value.split( core_rspace );
if ( proceed ) {
// 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 ];

if ( elem.nodeType === 1 ) {
if ( !elem.className && classNames.length === 1 ) {
elem.className = value;

} else {
setClass = " " + elem.className + " ";

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

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

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 ) ) {
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 ) {
removes = ( value || "" ).split( core_rspace );
if ( proceed ) {
classes = ( value || "" ).match( core_rnotwhite ) || [];

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

className = (" " + elem.className + " ").replace( rclass, " " );

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

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

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

if ( value && elem.nodeType === 1 ) {

attrNames = value.split( core_rspace );

for ( ; i < attrNames.length; i++ ) {
name = attrNames[ i ];
var name, propName, isBool,
i = 0,
attrNames = value && value.match( core_rnotwhite );

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

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

// Set corresponding property to false for boolean attributes
if ( isBool && propName in elem ) {
elem[ propName ] = false;
}
// Set corresponding property to false for boolean attributes
if ( isBool && propName in elem ) {
elem[ propName ] = false;
}
}
}
@@ -4,7 +4,7 @@ var optionsCache = {};
// Convert String-formatted options into Object-formatted ones and store in cache
function createOptions( options ) {
var object = optionsCache[ options ] = {};
jQuery.each( options.split( core_rspace ), function( _, flag ) {
jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) {
object[ flag ] = true;
});
return object;
@@ -15,14 +15,22 @@ var
// Map over the $ in case of overwrite
_$ = 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
core_concat = Array.prototype.concat,
core_push = Array.prototype.push,
core_slice = Array.prototype.slice,
core_indexOf = Array.prototype.indexOf,
core_toString = Object.prototype.toString,
core_hasOwn = Object.prototype.hasOwnProperty,
core_trim = String.prototype.trim,
core_concat = core_deletedIds.concat,
core_push = core_deletedIds.push,
core_slice = core_deletedIds.slice,
core_indexOf = core_deletedIds.indexOf,
core_toString = class2type.toString,
core_hasOwn = class2type.hasOwnProperty,
core_trim = core_version.trim,

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

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

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

// List of deleted data cache ids, so we can reuse them
core_deletedIds = [],
// Used for splitting on whitespace
core_rnotwhite = /\S+/g,

// 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,

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

// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
@@ -54,15 +58,15 @@ var
rvalidchars = /^[\],:{}\s]*$/,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/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
rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi,

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

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

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

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

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

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

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

@@ -560,7 +561,7 @@ jQuery.extend({
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
globalEval: function( data ) {
if ( data && core_rnotwhite.test( data ) ) {
if ( data && jQuery.trim( data ) ) {
// We use execScript on Internet Explorer
// We use an anonymous function so that context is window
// rather than jQuery in Firefox
@@ -8,7 +8,7 @@ var curCSS, iframe, iframeDoc,
rmargin = /^margin/,
rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "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" },

cssShow = { position: "absolute", visibility: "hidden", display: "block" },
@@ -188,7 +188,7 @@ jQuery.extend({

// Unique for each copy of jQuery on the page
// 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
// attempt to add expando properties to them.
@@ -1,6 +1,6 @@
var fxNow, timerId,
rfxtypes = /^(?:toggle|show|hide)$/,
rfxnum = new RegExp( "^(?:([-+])=|)(" + core_pnum + ")([a-z%]*)$", "i" ),
rfxnum = new RegExp( "^(?:([+-])=|)(" + core_pnum + ")([a-z%]*)$", "i" ),
rrun = /queueHooks$/,
animationPrefilters = [ defaultPrefilter ],
tweeners = {
Oops, something went wrong.

5 comments on commit 0877d42

@ebuildy

This comment has been minimized.

Copy link

ebuildy replied Dec 12, 2012

I know its not the right place to post, but why data() methods are not proxies of attr('data-' + key ... methods ?

@rwaldron

This comment has been minimized.

Copy link
Member

rwaldron replied Dec 12, 2012

They are, mostly. data() existed before HTML5 data-* attributes

@ebuildy

This comment has been minimized.

Copy link

ebuildy replied Dec 12, 2012

why all this code instead of a simple jquery.data = function(key) { return jquery.attr('data-' + key) } ;-)

@dmethvin

This comment has been minimized.

Copy link
Member

dmethvin replied Dec 12, 2012

Because that would be wrong. Perhaps you could move this question to forum.jquery.com?

@dmethvin

This comment has been minimized.

Please sign in to comment.
You can’t perform that action at this time.