Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
jquery core refactoring
// Handle HTML strings
if ( typeof selector === "string" ) {
var mode, id, tag;
if (/^#[\w\-]*$/.test(selector)) { // single #id
mode = "id";
id = selector.substring(1);
} else if (/^\s*<[\w\W]+>.*/.test(selector)) { // at least one html tag
mode = "html";
} else { // selector expr
mode = "expr";
}
// simple #id selector without coutext
if (mode === "id" && !context) {
elem = document.getElementById( id );
// 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 !== id ) {
return rootjQuery.find( 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: $(html) -> $(array)
if ( mode === "html" ) {
context = context instanceof jQuery ? context[0] : context;
doc = ( context ? context.ownerDocument || context : document );
// If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector );
if ( ret ) {
if ( jQuery.isPlainObject( context ) ) {
selector = [ document.createElement( ret[1] ) ];
jQuery.fn.attr.call( selector, context, true );
} else {
selector = [ doc.createElement( ret[1] ) ];
}
} else {
tag = selector.match(/(<[\w\W]+>)[^>]*$/)[1];
ret = jQuery.buildFragment( [ tag ], [ doc ] );
selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes;
}
return jQuery.merge( this, selector );
}
// HANDLE: $(expr, $(...))
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: $(function)
// Shortcut for document ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment