So I'm looking at your code - it looks pretty good - but it can enjoy
from a huge optimization. The precedes method has 3 possible states.

sourceIndex: Available in IE
compareDocumentPosition: Available in Firefox, Opera, and WebKit Nightlies
other: Safari/Chrome

Now the "problem" is the other "other" slot - but currently the only
browsers that fall in to that slot (Safari and Chrome) ALSO support
querySelectorAll -- so we never end up needing the sorting code to
begin with (since that's handled by querySelectorAll exclusively).

I did some work and landed a solution that takes care of both document
order and uniqueness:
http://github.com/jeresig/sizzle/commit/1702e1edb79c4c210b468c0eacc105571093da27
http://github.com/jeresig/sizzle/commit/99871e8c97e2cb6b619572be7f607e06b5869dfd

Works well in all browsers. I tossed a copy of the test suite online here:
http://ejohn.org/apps/sizzle/test/

--John



On Fri, Feb 13, 2009 at 6:26 PM, Robert Katić <[email protected]> wrote:
>
> The querySelectorAll returns elements in documents order, but Sizzle
> (for now?) treat each selector separately.
>
> Maybe this can be easily resolved joining each result array in one
> with nodes in documents order?
> If so, maybe something like this would help (not tested). Or you are
> thinking to sort results all together?
>
> var precedes = ( document.documentElement.sourceIndex == 0 ) &&
>        function( a, b ) {
>            return a.sourceIndex < b.sourceIndex;
>        }
>    || ( document.documentElement.compareDocumentPosition ) &&
>        function( a, b ) {
>            return !!( a.compareDocumentPosition(b) & 4 );
>        }
>    ||  function( a, b ) {
>            if ( a === b || b.contains(a) )
>                return false
>
>            if ( a.contains(b) )
>                return true;
>
>            var c = a.parentNode;
>            while ( !c.contains(b) ) {
>                a = c;
>                c = c.parentNode;
>            }
>
>            var p = b.parentNode;
>            while ( p !== c ) {
>                b = p;
>                p = p.parentNode;
>            }
>
>            var nodes = c.childNodes;
>            for ( var i = 0, node = nodes[0]; node; node = nodes[+
> +i] ) {
>                if ( node === a )
>                    return true;
>                if ( node === b )
>                    return false;
>            }
>
>            return false;
>        };
>
>
> function joinResultsHelper( res, a, i, b, j ) {
>    if ( b[j] ) {
>        for ( var l = a.length; i < l; ++i ) {
>            if ( a[i] === b[j] )
>                ++j;
>
>            else if ( precedes(a[i], b[j]) )
>                res.push( a[i] );
>
>            else {
>                res.push( b[j++] );
>                joinResultsHelper( res, b, j, a, i );
>                break;
>            }
>        }
>    } else {
>        for ( var l = a.length; i < l; ++i )
>            res.push( a[i] );
>    }
>
>    return res;
> }
>
> function joinResults( a, b ) {
>    if ( a.length === 0 )
>        return b;
>
>    if ( b.length === 0 )
>        return a;
>
>    return joinResultsHelper( [], a, 0, b, 0 );
> }
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to