Skip to content

Commit

Permalink
Switch to using createRange for element comparision instead of Array …
Browse files Browse the repository at this point in the history
…indexOf checks - thanks for the tip, Ioseb.
  • Loading branch information
jeresig committed Feb 17, 2009
1 parent 4b7e1c9 commit d8b3bd7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/selector.js
Expand Up @@ -692,20 +692,22 @@ if ( document.documentElement.compareDocumentPosition ) {
} }
return ret; return ret;
}; };
} else if ( Array.prototype.indexOf ) { } else if ( "sourceIndex" in document.documentElement ) {
var indexOf = Array.prototype.indexOf,
allSort = document.getElementsByTagName("*");

sortOrder = function( a, b ) { sortOrder = function( a, b ) {
var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b ); var ret = a.sourceIndex - b.sourceIndex;
if ( ret === 0 ) { if ( ret === 0 ) {
hasDuplicate = true; hasDuplicate = true;
} }
return ret; return ret;
}; };
} else if ( "sourceIndex" in document.documentElement ) { } else if ( document.createRange ) {
sortOrder = function( a, b ) { sortOrder = function( a, b ) {
var ret = a.sourceIndex - b.sourceIndex; var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
aRange.selectNode(a);
aRange.collapse(true);
bRange.selectNode(b);
bRange.collapse(true);
var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
if ( ret === 0 ) { if ( ret === 0 ) {
hasDuplicate = true; hasDuplicate = true;
} }
Expand Down

0 comments on commit d8b3bd7

Please sign in to comment.