Reasons why maybe an joinResults would be better/faster instead of an
sort are this:

1) Single selector (without comma) will easily return nodes in
document order and with no duplicates, so we have not to worry about
single selector results order and duplicates.

2) If we sort the final array we have to remove duplicates too (?).

However I am not sure if all this is true, so here an cmp function to
sort nodes in document order:

var cmpOrder = ( document.documentElement.sourceIndex == 0 ) &&
        function( a, b ) {
            return a.sourceIndex - b.sourceIndex;
        }
    || ( document.documentElement.compareDocumentPosition ) &&
        function( a, b ) {
            var t = a.compareDocumentPosition(b);
            return t ? ( t & 4 ? -1 : 1 ) : 0;
        }
    ||  function( a, b ) {
            if ( a === b )
                return 0;

            if ( b.contains(a) )
                return 1;

            if ( a.contains(b) )
                return -1;

            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 -1;
                if ( node === b )
                    return 1;
            }

            return -1;
        };


I also corrected again the joinResultsHelper (hope this one is ok):

function joinResultsHelper( res, a, i, b, j ) {
    if ( b && b[j] ) {
        for ( var l = a.length; i < l; ++i ) {
            if ( a[i] === b[j] ) {
                if ( !b[++j] )
                    return joinResultsHelper( res, a, i );
            }

            else if ( precedes(a[i], b[j]) )
                res.push( a[i] );

            else {
                res.push( b[j++] );
                return joinResultsHelper( res, b, j, a, i );
            }
        }
    } else {
        for ( var l = a.length; i < l; ++i )
            res.push( a[i] );
    }

    return res;
}


I doesn't know that Sizzle has an separate discussion group, so I
apologize.

On Feb 14, 2:33 am, Diego Perini <diego.per...@gmail.com> wrote:
> Robert,
> I have also needed this bit of help from jQuery...for extracting texts
> from HTML code.
>
> I proposed this similar solution on the Sizzle group some time ago:
>
>    http://groups.google.com/group/sizzlejs/browse_thread/thread/44d2b3fd...
>
> which probably solves with a simpler sort() on the result set, maybe
> only done when a comma (group separator) is found in the selector.
>
> Maybe your code adds some way to do it faster or are there other
> reasons to not use .sort() ?
>
> Diego
>
> On 14 Feb, 01:47, Robert Katić <robert.ka...@gmail.com> wrote:
>
> > I corrected the joinResultsHelper:
>
> > function joinResultsHelper( res, a, i, b, j ) {
> >     if ( b && b[j] ) {
> >         for ( var l = a.length; i < l; ++i ) {
> >             if ( a[i] === b[j] && !b[++j] )
> >                 return joinResultsHelper( res, a, i )
>
> >             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;
>
> > }
>
> > On Feb 14, 12:26 am, Robert Katić <robert.ka...@gmail.com> 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 jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to