]> source.dussan.org Git - jquery.git/commitdiff
Separate jQuery.fn.init into its own module (for lighter core dependencies across...
authorTimmy Willison <timmywillisn@gmail.com>
Tue, 10 Sep 2013 00:13:01 +0000 (19:13 -0500)
committerTimmy Willison <timmywillisn@gmail.com>
Tue, 10 Sep 2013 00:13:01 +0000 (19:13 -0500)
19 files changed:
src/ajax.js
src/attributes/classes.js
src/attributes/val.js
src/core.js
src/core/init.js [new file with mode: 0644]
src/core/parseHTML.js
src/core/ready.js
src/core/var/rsingleTag.js [new file with mode: 0644]
src/css.js
src/effects.js
src/event.js
src/manipulation.js
src/offset.js
src/serialize.js
src/traversing.js
src/traversing/findFilter.js [new file with mode: 0644]
src/traversing/var/rneedsContext.js [new file with mode: 0644]
src/var/rsingleTag.js [deleted file]
src/wrap.js

index c1468a73481fe85d0eed54babc6d16db8434405f..364e5a4b645f9522a74d9860e0a12547bcb7e47a 100644 (file)
@@ -3,6 +3,7 @@ define([
        "./var/rnotwhite",
        "./ajax/var/nonce",
        "./ajax/var/rquery",
+       "./core/init",
        "./ajax/parseJSON",
        "./ajax/parseXML",
        "./deferred"
index aed8db88afc3f7d0ba6b96294f116db9b67a76f5..d7212f9dee2dcc18f084d7df8f417b80846ded5a 100644 (file)
@@ -2,7 +2,8 @@ define([
        "../core",
        "../var/rnotwhite",
        "../var/strundefined",
-       "../data/var/data_priv"
+       "../data/var/data_priv",
+       "../core/init"
 ], function( jQuery, rnotwhite, strundefined, data_priv ) {
 
 var rclass = /[\t\r\n\f]/g;
index 74d8c9c46f9d81bd17dc4048c4236cbff0df71ba..0e7e7ce439526dcb819d571f0f2c2d355c34fd93 100644 (file)
@@ -1,6 +1,7 @@
 define([
        "../core",
-       "./support"
+       "./support",
+       "../core/init"
 ], function( jQuery, support ) {
 
 var rreturn = /\r/g;
index 3cdcc63afa0ee4b5ee1c9a7f18f63c7898cd5665..599caba6441c7b9005f4cd3b0e62858cd7a05f2d 100644 (file)
@@ -10,12 +10,8 @@ var
        toString = require( "./var/toString" ),
        hasOwn = require( "./var/hasOwn" ),
        trim = require( "./var/trim" ),
-       rsingleTag = require( "./var/rsingleTag" ),
        support = require( "./var/support" ),
 
-       // A central reference to the root jQuery(document)
-       rootjQuery,
-
        // Use the correct document accordingly with window argument (sandbox)
        document = window.document,
 
@@ -30,14 +26,10 @@ var
        // Define a local copy of jQuery
        jQuery = function( selector, context ) {
                // The jQuery object is actually just the init constructor 'enhanced'
-               return new jQuery.fn.init( selector, context, rootjQuery );
+               // Need init if jQuery is called (just allow error to be thrown if not included)
+               return new jQuery.fn.init( selector, context );
        },
 
-       // A simple way to check for HTML strings
-       // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
-       // Strict HTML recognition (#11290: must start with <)
-       rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
-
        // Matches dashed string for camelizing
        rmsPrefix = /^-ms-/,
        rdashAlpha = /-([\da-z])/gi,
@@ -53,105 +45,6 @@ jQuery.fn = jQuery.prototype = {
 
        constructor: jQuery,
 
-       init: function( selector, context, rootjQuery ) {
-               var match, elem;
-
-               // HANDLE: $(""), $(null), $(undefined), $(false)
-               if ( !selector ) {
-                       return this;
-               }
-
-               // Handle HTML strings
-               if ( typeof selector === "string" ) {
-                       if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
-                               // Assume that strings that start and end with <> are HTML and skip the regex check
-                               match = [ null, selector, null ];
-
-                       } else {
-                               match = rquickExpr.exec( selector );
-                       }
-
-                       // Match html or make sure no context is specified for #id
-                       if ( match && (match[1] || !context) ) {
-
-                               // HANDLE: $(html) -> $(array)
-                               if ( match[1] ) {
-                                       context = context instanceof jQuery ? context[0] : context;
-
-                                       // scripts is true for back-compat
-                                       // Intentionally let the error be thrown if parseHTML is not present
-                                       jQuery.merge( this, jQuery.parseHTML(
-                                               match[1],
-                                               context && context.nodeType ? context.ownerDocument || context : document,
-                                               true
-                                       ) );
-
-                                       // HANDLE: $(html, props)
-                                       if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
-                                               for ( match in context ) {
-                                                       // Properties of context are called as methods if possible
-                                                       if ( jQuery.isFunction( this[ match ] ) ) {
-                                                               this[ match ]( context[ match ] );
-
-                                                       // ...and otherwise set as attributes
-                                                       } else {
-                                                               this.attr( match, context[ match ] );
-                                                       }
-                                               }
-                                       }
-
-                                       return this;
-
-                               // HANDLE: $(#id)
-                               } else {
-                                       elem = document.getElementById( match[2] );
-
-                                       // Check parentNode to catch when Blackberry 4.6 returns
-                                       // nodes that are no longer in the document #6963
-                                       if ( elem && elem.parentNode ) {
-                                               // Inject the element directly into the jQuery object
-                                               this.length = 1;
-                                               this[0] = elem;
-                                       }
-
-                                       this.context = document;
-                                       this.selector = selector;
-                                       return this;
-                               }
-
-                       // HANDLE: $(expr, $(...))
-                       } else if ( !context || context.jquery ) {
-                               return ( context || rootjQuery ).find( selector );
-
-                       // HANDLE: $(expr, context)
-                       // (which is just equivalent to: $(context).find(expr)
-                       } else {
-                               return this.constructor( context ).find( selector );
-                       }
-
-               // HANDLE: $(DOMElement)
-               } else if ( selector.nodeType ) {
-                       this.context = this[0] = selector;
-                       this.length = 1;
-                       return this;
-
-               // HANDLE: $(function)
-               // Shortcut for document ready
-               } else if ( jQuery.isFunction( selector ) ) {
-                       return typeof rootjQuery.ready !== "undefined" ?
-                               rootjQuery.ready( selector ) :
-                               // Execute immediately if ready is not present
-                               selector( jQuery );
-               }
-
-               if ( selector.selector !== undefined ) {
-                       this.selector = selector.selector;
-                       this.context = selector.context;
-               }
-
-               return jQuery.makeArray( selector, this );
-       },
-
        // Start with an empty selector
        selector: "",
 
@@ -231,9 +124,6 @@ jQuery.fn = jQuery.prototype = {
        splice: arr.splice
 };
 
-// Give the init function the jQuery prototype for later instantiation
-jQuery.fn.init.prototype = jQuery.fn;
-
 jQuery.extend = jQuery.fn.extend = function() {
        var options, name, src, copy, copyIsArray, clone,
                target = arguments[0] || {},
@@ -621,8 +511,5 @@ function isArraylike( obj ) {
                typeof length === "number" && length > 0 && ( length - 1 ) in obj );
 }
 
-// All jQuery objects should point back to these
-rootjQuery = jQuery( document );
-
 return jQuery;
 });
diff --git a/src/core/init.js b/src/core/init.js
new file mode 100644 (file)
index 0000000..e349bb6
--- /dev/null
@@ -0,0 +1,123 @@
+// Initialize a jQuery object
+define([
+       "../core",
+       "./var/rsingleTag",
+       "../traversing/findFilter"
+], function( jQuery, rsingleTag ) {
+
+// A central reference to the root jQuery(document)
+var rootjQuery,
+
+       // A simple way to check for HTML strings
+       // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
+       // Strict HTML recognition (#11290: must start with <)
+       rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
+
+       init = jQuery.fn.init = function( selector, context ) {
+               var match, elem;
+
+               // HANDLE: $(""), $(null), $(undefined), $(false)
+               if ( !selector ) {
+                       return this;
+               }
+
+               // Handle HTML strings
+               if ( typeof selector === "string" ) {
+                       if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
+                               // Assume that strings that start and end with <> are HTML and skip the regex check
+                               match = [ null, selector, null ];
+
+                       } else {
+                               match = rquickExpr.exec( selector );
+                       }
+
+                       // Match html or make sure no context is specified for #id
+                       if ( match && (match[1] || !context) ) {
+
+                               // HANDLE: $(html) -> $(array)
+                               if ( match[1] ) {
+                                       context = context instanceof jQuery ? context[0] : context;
+
+                                       // scripts is true for back-compat
+                                       // Intentionally let the error be thrown if parseHTML is not present
+                                       jQuery.merge( this, jQuery.parseHTML(
+                                               match[1],
+                                               context && context.nodeType ? context.ownerDocument || context : document,
+                                               true
+                                       ) );
+
+                                       // HANDLE: $(html, props)
+                                       if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
+                                               for ( match in context ) {
+                                                       // Properties of context are called as methods if possible
+                                                       if ( jQuery.isFunction( this[ match ] ) ) {
+                                                               this[ match ]( context[ match ] );
+
+                                                       // ...and otherwise set as attributes
+                                                       } else {
+                                                               this.attr( match, context[ match ] );
+                                                       }
+                                               }
+                                       }
+
+                                       return this;
+
+                               // HANDLE: $(#id)
+                               } else {
+                                       elem = document.getElementById( match[2] );
+
+                                       // Check parentNode to catch when Blackberry 4.6 returns
+                                       // nodes that are no longer in the document #6963
+                                       if ( elem && elem.parentNode ) {
+                                               // Inject the element directly into the jQuery object
+                                               this.length = 1;
+                                               this[0] = elem;
+                                       }
+
+                                       this.context = document;
+                                       this.selector = selector;
+                                       return this;
+                               }
+
+                       // HANDLE: $(expr, $(...))
+                       } else if ( !context || context.jquery ) {
+                               return ( context || rootjQuery ).find( selector );
+
+                       // HANDLE: $(expr, context)
+                       // (which is just equivalent to: $(context).find(expr)
+                       } else {
+                               return this.constructor( context ).find( selector );
+                       }
+
+               // HANDLE: $(DOMElement)
+               } else if ( selector.nodeType ) {
+                       this.context = this[0] = selector;
+                       this.length = 1;
+                       return this;
+
+               // HANDLE: $(function)
+               // Shortcut for document ready
+               } else if ( jQuery.isFunction( selector ) ) {
+                       return typeof rootjQuery.ready !== "undefined" ?
+                               rootjQuery.ready( selector ) :
+                               // Execute immediately if ready is not present
+                               selector( jQuery );
+               }
+
+               if ( selector.selector !== undefined ) {
+                       this.selector = selector.selector;
+                       this.context = selector.context;
+               }
+
+               return jQuery.makeArray( selector, this );
+       };
+
+// Give the init function the jQuery prototype for later instantiation
+init.prototype = jQuery.fn;
+
+// Initialize central reference
+rootjQuery = jQuery( document );
+
+return init;
+
+});
index 15e3e512fff4a213b28894d967fc0c5c972a82db..64cf2a18a95a65362fc372f3570b9d13265dd065 100644 (file)
@@ -1,6 +1,6 @@
 define([
        "../core",
-       "../var/rsingleTag",
+       "./var/rsingleTag",
        "../manipulation" // buildFragment
 ], function( jQuery, rsingleTag ) {
 
index 2e33781c2bf60e2232442d2e30a6cde076316e5e..e64977cd974fce7470674bbd3d7a2d51e4668a5d 100644 (file)
@@ -1,5 +1,6 @@
 define([
        "../core",
+       "../core/init",
        "../deferred"
 ], function( jQuery ) {
 
diff --git a/src/core/var/rsingleTag.js b/src/core/var/rsingleTag.js
new file mode 100644 (file)
index 0000000..7e7090b
--- /dev/null
@@ -0,0 +1,4 @@
+define(function() {
+       // Match a standalone tag
+       return (/^<(\w+)\s*\/?>(?:<\/\1>|)$/);
+});
index a80d1c3b8fc5c67ff5235451ef05dd9c1213ea1c..244fd683e390f2315f99a3011d77fd6bc2f9229b 100644 (file)
@@ -29,6 +29,7 @@ var
        cssPrefixes = [ "Webkit", "O", "Moz", "ms" ];
 
 // Dependencies not needed as vars
+require( "./core/init" );
 require( "./css/swap" );
 require( "./core/ready" );
 require( "./selector" ); // contains
index 2196d9bc75fa5902f7be93ebb47e8b294a5842eb..e61e576cb995f5758225d687cca126db84282f24 100644 (file)
@@ -63,6 +63,7 @@ var
        };
 
 // Dependencies not needed as vars
+require( "./core/init" );
 require( "./effects/Tween" );
 require( "./queue" );
 require( "./css" );
index 6887b36ddcd366771493a2e4a0eb00695ed4f254..866e4f0dd66a1cb1609aef2c2e0f3c367dccc57f 100644 (file)
@@ -14,6 +14,7 @@ var
        rtypenamespace = /^([^.]*)(?:\.(.+)|)$/;
 
 // Dependencies not needed as vars
+require( "./core/init" );
 require( "./data/accepts" );
 require( "./selector" );
 
index 35a5996d71c24d471eabbbbdcf3b4529ca79d266..d8b45528cb82ff4629667466f6e84be4113eab34 100644 (file)
@@ -42,6 +42,7 @@ wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.the
 wrapMap.th = wrapMap.td;
 
 // Dependencies not needed as variables
+require( "./core/init" );
 require( "./data/accepts" );
 require( "./traversing" );
 require( "./selector" );
index fc63be731ccb9302113f289efc1c544cd6fcfcda..b53524b7695e983e3070c96bef6bf730212da51f 100644 (file)
@@ -2,6 +2,7 @@ define([
        "./core",
        "./var/strundefined",
        "./core/access",
+       "./core/init",
        "./css",
        "./selector" // contains
 ], function( jQuery, strundefined, access ) {
index 85798ef62c2f2faf04837f1eb7e1719b8a75c70f..797d735aaf64d125edf3c66a655f381cbeff8c00 100644 (file)
@@ -1,6 +1,7 @@
 define([
        "./core",
        "./manipulation/var/rcheckableType",
+       "./core/init",
        "./traversing", // filter
        "./attributes/prop"
 ], function( jQuery, rcheckableType ) {
index b66a22f6debc22ce757c50d9eaa7a307fb55d452..4e55c8257569f08290624ade7e5f57bf581697e8 100644 (file)
@@ -1,12 +1,13 @@
 define([
        "./core",
        "./var/indexOf",
+       "./traversing/var/rneedsContext",
+       "./core/init",
+       "./traversing/findFilter",
        "./selector"
-], function( jQuery, indexOf ) {
+], function( jQuery, indexOf, rneedsContext ) {
 
-var isSimple = /^.[^:#\[\.,]*$/,
-       rparentsprev = /^(?:parents|prev(?:Until|All))/,
-       rneedsContext = jQuery.expr.match.needsContext,
+var rparentsprev = /^(?:parents|prev(?:Until|All))/,
        // methods guaranteed to produce a unique set when starting from a unique set
        guaranteedUnique = {
                children: true,
@@ -15,51 +16,7 @@ var isSimple = /^.[^:#\[\.,]*$/,
                prev: true
        };
 
-// Implement the identical functionality for filter and not
-function winnow( elements, qualifier, not ) {
-       if ( jQuery.isFunction( qualifier ) ) {
-               return jQuery.grep( elements, function( elem, i ) {
-                       /* jshint -W018 */
-                       return !!qualifier.call( elem, i, elem ) !== not;
-               });
-
-       }
-
-       if ( qualifier.nodeType ) {
-               return jQuery.grep( elements, function( elem ) {
-                       return ( elem === qualifier ) !== not;
-               });
-
-       }
-
-       if ( typeof qualifier === "string" ) {
-               if ( isSimple.test( qualifier ) ) {
-                       return jQuery.filter( qualifier, elements, not );
-               }
-
-               qualifier = jQuery.filter( qualifier, elements );
-       }
-
-       return jQuery.grep( elements, function( elem ) {
-               return ( indexOf.call( qualifier, elem ) >= 0 ) !== not;
-       });
-}
-
 jQuery.extend({
-       filter: function( expr, elems, not ) {
-               var elem = elems[ 0 ];
-
-               if ( not ) {
-                       expr = ":not(" + expr + ")";
-               }
-
-               return elems.length === 1 && elem.nodeType === 1 ?
-                       jQuery.find.matchesSelector( elem, expr ) ? [ elem ] : [] :
-                       jQuery.find.matches( expr, jQuery.grep( elems, function( elem ) {
-                               return elem.nodeType === 1;
-                       }));
-       },
-
        dir: function( elem, dir, until ) {
                var matched = [],
                        truncate = until !== undefined;
@@ -89,32 +46,6 @@ jQuery.extend({
 });
 
 jQuery.fn.extend({
-       find: function( selector ) {
-               var i,
-                       ret = [],
-                       self = this,
-                       len = self.length;
-
-               if ( typeof selector !== "string" ) {
-                       return this.pushStack( jQuery( selector ).filter(function() {
-                               for ( i = 0; i < len; i++ ) {
-                                       if ( jQuery.contains( self[ i ], this ) ) {
-                                               return true;
-                                       }
-                               }
-                       }) );
-               }
-
-               for ( i = 0; i < len; i++ ) {
-                       jQuery.find( selector, self[ i ], ret );
-               }
-
-               // Needed because $( selector, context ) becomes $( context ).find( selector )
-               ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret );
-               ret.selector = this.selector ? this.selector + " " + selector : selector;
-               return ret;
-       },
-
        has: function( target ) {
                var targets = jQuery( target, this ),
                        l = targets.length;
@@ -129,27 +60,6 @@ jQuery.fn.extend({
                });
        },
 
-       not: function( selector ) {
-               return this.pushStack( winnow(this, selector || [], true) );
-       },
-
-       filter: function( selector ) {
-               return this.pushStack( winnow(this, selector || [], false) );
-       },
-
-       is: function( selector ) {
-               return !!winnow(
-                       this,
-
-                       // If this is a positional/relative selector, check membership in the returned set
-                       // so $("p:first").is("p:last") won't return true for a doc with two "p".
-                       typeof selector === "string" && rneedsContext.test( selector ) ?
-                               jQuery( selector ) :
-                               selector || [],
-                       false
-               ).length;
-       },
-
        closest: function( selectors, context ) {
                var cur,
                        i = 0,
diff --git a/src/traversing/findFilter.js b/src/traversing/findFilter.js
new file mode 100644 (file)
index 0000000..71fe52a
--- /dev/null
@@ -0,0 +1,100 @@
+define([
+       "../core",
+       "../var/indexOf",
+       "./var/rneedsContext",
+       "../selector"
+], function( jQuery, indexOf, rneedsContext ) {
+
+var risSimple = /^.[^:#\[\.,]*$/;
+
+// Implement the identical functionality for filter and not
+function winnow( elements, qualifier, not ) {
+       if ( jQuery.isFunction( qualifier ) ) {
+               return jQuery.grep( elements, function( elem, i ) {
+                       /* jshint -W018 */
+                       return !!qualifier.call( elem, i, elem ) !== not;
+               });
+
+       }
+
+       if ( qualifier.nodeType ) {
+               return jQuery.grep( elements, function( elem ) {
+                       return ( elem === qualifier ) !== not;
+               });
+
+       }
+
+       if ( typeof qualifier === "string" ) {
+               if ( risSimple.test( qualifier ) ) {
+                       return jQuery.filter( qualifier, elements, not );
+               }
+
+               qualifier = jQuery.filter( qualifier, elements );
+       }
+
+       return jQuery.grep( elements, function( elem ) {
+               return ( indexOf.call( qualifier, elem ) >= 0 ) !== not;
+       });
+}
+
+jQuery.filter = function( expr, elems, not ) {
+       var elem = elems[ 0 ];
+
+       if ( not ) {
+               expr = ":not(" + expr + ")";
+       }
+
+       return elems.length === 1 && elem.nodeType === 1 ?
+               jQuery.find.matchesSelector( elem, expr ) ? [ elem ] : [] :
+               jQuery.find.matches( expr, jQuery.grep( elems, function( elem ) {
+                       return elem.nodeType === 1;
+               }));
+};
+
+jQuery.fn.extend({
+       find: function( selector ) {
+               var i,
+                       ret = [],
+                       self = this,
+                       len = self.length;
+
+               if ( typeof selector !== "string" ) {
+                       return this.pushStack( jQuery( selector ).filter(function() {
+                               for ( i = 0; i < len; i++ ) {
+                                       if ( jQuery.contains( self[ i ], this ) ) {
+                                               return true;
+                                       }
+                               }
+                       }) );
+               }
+
+               for ( i = 0; i < len; i++ ) {
+                       jQuery.find( selector, self[ i ], ret );
+               }
+
+               // Needed because $( selector, context ) becomes $( context ).find( selector )
+               ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret );
+               ret.selector = this.selector ? this.selector + " " + selector : selector;
+               return ret;
+       },
+       filter: function( selector ) {
+               return this.pushStack( winnow(this, selector || [], false) );
+       },
+       not: function( selector ) {
+               return this.pushStack( winnow(this, selector || [], true) );
+       },
+       is: function( selector ) {
+               return !!winnow(
+                       this,
+
+                       // If this is a positional/relative selector, check membership in the returned set
+                       // so $("p:first").is("p:last") won't return true for a doc with two "p".
+                       typeof selector === "string" && rneedsContext.test( selector ) ?
+                               jQuery( selector ) :
+                               selector || [],
+                       false
+               ).length;
+       }
+});
+
+});
diff --git a/src/traversing/var/rneedsContext.js b/src/traversing/var/rneedsContext.js
new file mode 100644 (file)
index 0000000..3d6ae40
--- /dev/null
@@ -0,0 +1,6 @@
+define([
+       "../../core",
+       "../../selector"
+], function( jQuery ) {
+       return jQuery.expr.match.needsContext;
+});
diff --git a/src/var/rsingleTag.js b/src/var/rsingleTag.js
deleted file mode 100644 (file)
index 7e7090b..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-define(function() {
-       // Match a standalone tag
-       return (/^<(\w+)\s*\/?>(?:<\/\1>|)$/);
-});
index a7a1a14c0a8dc8083a72a981c5d802513e934f1f..b6dce72e60d05900b75196f230226062d7970ac3 100644 (file)
@@ -1,5 +1,6 @@
 define([
        "./core",
+       "./core/init",
        "./traversing" // parent, contents
 ], function( jQuery ) {