Hi,
> * jQuery.find recursively collects every node in the document
> tree and copies that list into a second array;
> * jQuery.filter builds/compiles a regexp to get the class name,
> then builds/compiles a function to check the class name;
> * jQuery.grep calls that function once for each element;
> * That function calls jQuery.class.has, which builds/compiles
> a regexp to see if the class matches.
Just a suggestion: jQuery.class.has could take a regular expression as an
optional argument. In case it is given, it can be used to see if the class
matches.
How I would do all of that with speed in mind:
$('.classname')
-> create a regular Expresion to compare the class
-> walk regursively through all Elements on the page and use the regular
expression to check if the class matches
-> if an Element matches, add it to the return-array
draft:
function traverse(element,callback) {
var rval = [];
if( callback.apply(element,[]) ) rval.push(element);
for( var i = 0; i < element.childNodes.length; i++ )
if( element.childNodes[i].nodeType == 1 )
rval.concat(traverse(element.childNodes[i],callback));
return rval;
}
$ = function(param) {
if( /^[#][a-zA-Z][a-zA-Z0-9]*$/.test(param) )
return [document.getElementById(param.substr(1))];
if( /^[a-zA-Z][a-zA-Z0-9]*$/.test(param) )
return document.getElementsByTagName(param);
if( /^[.][a-zA-Z][a-zA-Z0-9]*$/.test(param) ) {
var checkclass = param.substr(1);
checkclass = Regexp
( '^'+checkclass+'$|^'+checkclass+' | '+checkclass+' |
'+checkclass'+$ );
return traverse(document,function() {
return checkclass.test(this.className);
});
}
// ...
}
Of course this is not fully functional, but the idea should be clear.
Christof
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/