aboutsummaryrefslogtreecommitdiffstats
path: root/src/core.js
diff options
context:
space:
mode:
authortimmywil <timmywillisn@gmail.com>2012-07-01 15:35:50 -0500
committertimmywil <timmywillisn@gmail.com>2012-07-02 11:34:26 -0400
commitf71a6ec6cfe3f748a939eaa109e92b8a9bdac6cc (patch)
tree70aecf3c8881cd861c6874fc6674a2eb499831f3 /src/core.js
parentf18b75940e61a52ef9c0cf41a4bda3bcdae5c46c (diff)
downloadjquery-f71a6ec6cfe3f748a939eaa109e92b8a9bdac6cc.tar.gz
jquery-f71a6ec6cfe3f748a939eaa109e92b8a9bdac6cc.zip
Add back ID shortcut. It's about 3 times faster than going through jQuery.fn.find and merging.
Diffstat (limited to 'src/core.js')
-rw-r--r--src/core.js54
1 files changed, 40 insertions, 14 deletions
diff --git a/src/core.js b/src/core.js
index a0fdac823..29fc29bc4 100644
--- a/src/core.js
+++ b/src/core.js
@@ -39,7 +39,7 @@ var
// A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
- rhtmlString = /^(?:[^#<]*(<[\w\W]+>)[^>]*$)/,
+ rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
@@ -79,8 +79,8 @@ jQuery.fn = jQuery.prototype = {
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
- // Handle $(""), $(null), $(undefined), $(false), or $("#") for location.hash
- if ( !selector || selector === "#" ) {
+ // Handle $(""), $(null), $(undefined), $(false)
+ if ( !selector ) {
return this;
}
@@ -98,21 +98,47 @@ jQuery.fn = jQuery.prototype = {
match = [ null, selector, null ];
} else {
- match = rhtmlString.exec( selector );
+ match = rquickExpr.exec( selector );
}
- // HANDLE: $(html) -> $(array)
- if ( match && match[1] ) {
- context = context instanceof jQuery ? context[0] : context;
- doc = ( context && context.nodeType ? context.ownerDocument || context : document );
+ // Match html or make sure no context is specified for #id
+ if ( match && (match[1] || !context) ) {
- // scripts is true for back-compat
- selector = jQuery.parseHTML( match[1], doc, true );
- if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
- this.attr.call( selector, context, true );
- }
+ // HANDLE: $(html) -> $(array)
+ if ( match[1] ) {
+ context = context instanceof jQuery ? context[0] : context;
+ doc = ( context && context.nodeType ? context.ownerDocument || context : document );
+
+ // scripts is true for back-compat
+ selector = jQuery.parseHTML( match[1], doc, true );
+ if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
+ this.attr.call( selector, context, true );
+ }
+
+ return jQuery.merge( this, selector );
+
+ // 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 ) {
+ // Handle the case where IE and Opera return items
+ // by name instead of ID
+ if ( elem.id !== match[2] ) {
+ return rootjQuery.find( selector );
+ }
- return jQuery.merge( this, selector );
+ // Otherwise, we 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 ) {