Moved a bunch of methods out of the jQuery-specific Sizzle code into more-appropriate files, in jQuery itself.

This commit is contained in:
John Resig 2009-10-26 18:07:57 -04:00
parent b7ccc58afe
commit bbffc99f7c
6 changed files with 75 additions and 66 deletions

View file

@ -222,7 +222,7 @@ jQuery.fn = jQuery.prototype = {
},
is: function( selector ) {
return !!selector && jQuery.multiFilter( selector, this ).length > 0;
return !!selector && jQuery.filter( selector, this ).length > 0;
},
// For internal use only.

View file

@ -197,3 +197,19 @@ jQuery.extend({
}
}
});
if ( jQuery.expr && jQuery.expr.filters ) {
jQuery.expr.filters.hidden = function(elem){
var width = elem.offsetWidth, height = elem.offsetHeight,
force = /^tr$/i.test( elem.nodeName ); // ticket #4512
return ( width === 0 && height === 0 && !force ) ?
true :
( width !== 0 && height !== 0 && !force ) ?
false :
!!( jQuery.curCSS(elem, "display") === "none" );
};
jQuery.expr.filters.visible = function(elem){
return !jQuery.expr.filters.hidden(elem);
};
}

View file

@ -420,3 +420,11 @@ jQuery.extend( jQuery.fx, {
}
}
});
if ( jQuery.expr && jQuery.expr.filters ) {
jQuery.expr.filters.animated = function(elem){
return jQuery.grep(jQuery.timers, function(fn){
return elem === fn.elem;
}).length;
};
}

View file

@ -333,7 +333,7 @@ jQuery.each({
jQuery.each({
// keepData is for internal use only--do not document
remove: function( selector, keepData ) {
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
if ( !keepData && this.nodeType === 1 ) {
cleanData( this.getElementsByTagName("*") );
cleanData( [ this ] );

View file

@ -998,67 +998,6 @@ var posProcess = function(selector, context){
jQuery.find = Sizzle;
jQuery.expr = Sizzle.selectors;
jQuery.expr[":"] = jQuery.expr.filters;
Sizzle.selectors.filters.hidden = function(elem){
var width = elem.offsetWidth, height = elem.offsetHeight,
force = /^tr$/i.test( elem.nodeName ); // ticket #4512
return ( width === 0 && height === 0 && !force ) ?
true :
( width !== 0 && height !== 0 && !force ) ?
false :
!!( jQuery.curCSS(elem, "display") === "none" );
};
Sizzle.selectors.filters.visible = function(elem){
return !Sizzle.selectors.filters.hidden(elem);
};
Sizzle.selectors.filters.animated = function(elem){
return jQuery.grep(jQuery.timers, function(fn){
return elem === fn.elem;
}).length;
};
jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
if ( not ) {
expr = ":not(" + expr + ")";
}
return Sizzle.matches(expr, elems);
};
jQuery.dir = function( elem, dir ){
var matched = [], cur = elem[dir];
while ( cur && cur != document ) {
if ( cur.nodeType == 1 )
matched.push( cur );
cur = cur[dir];
}
return matched;
};
jQuery.nth = function(cur, result, dir, elem){
result = result || 1;
var num = 0;
for ( ; cur; cur = cur[dir] )
if ( cur.nodeType == 1 && ++num == result )
break;
return cur;
};
jQuery.sibling = function(n, elem){
var r = [];
for ( ; n; n = n.nextSibling ) {
if ( n.nodeType == 1 && n != elem )
r.push( n );
}
return r;
};
jQuery.unique = Sizzle.uniqueSort;
return;

View file

@ -10,8 +10,8 @@ var winnow = function( elements, qualifier, keep ) {
} else if( typeof qualifier === "string" ) {
var filtered = jQuery.grep(elements, function(elem) { return elem.nodeType === 1 });
if(isSimple.test( qualifier )) return jQuery.multiFilter(qualifier, filtered, !keep);
else qualifier = jQuery.multiFilter( qualifier, elements );
if(isSimple.test( qualifier )) return jQuery.filter(qualifier, filtered, !keep);
else qualifier = jQuery.filter( qualifier, elements );
}
return jQuery.grep(elements, function(elem, i) {
@ -128,7 +128,7 @@ jQuery.each({
var ret = jQuery.map( this, fn );
if ( selector && typeof selector === "string" ) {
ret = jQuery.multiFilter( selector, ret );
ret = jQuery.filter( selector, ret );
}
ret = this.length > 1 ? jQuery.unique( ret ) : ret;
@ -140,3 +140,49 @@ jQuery.each({
return this.pushStack( ret, name, selector );
};
});
jQuery.extend({
filter: function( expr, elems, not ) {
if ( not ) {
expr = ":not(" + expr + ")";
}
return jQuery.find.matches(expr, elems);
},
dir: function( elem, dir ) {
var matched = [], cur = elem[dir];
while ( cur && cur.nodeType !== 9 ) {
if ( cur.nodeType === 1 ) {
matched.push( cur );
}
cur = cur[dir];
}
return matched;
},
nth: function( cur, result, dir, elem ) {
result = result || 1;
var num = 0;
for ( ; cur; cur = cur[dir] ) {
if ( cur.nodeType === 1 && ++num === result ) {
break;
}
}
return cur;
},
sibling: function( n, elem ) {
var r = [];
for ( ; n; n = n.nextSibling ) {
if ( n.nodeType === 1 && n !== elem ) {
r.push( n );
}
}
return r;
}
});