Skip to content

Commit bb0828d

Browse files
committed
Removed the browser-specific sourceIndex and createRange sorting techniques and replace it with a generic sorting routine. Most browsers are either providing querySelectorAll or compareDocumentPosition - it's better to have just one alternative branch to work against.
1 parent 6fd052b commit bb0828d

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

sizzle.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -756,65 +756,62 @@ var sortOrder;
756756

757757
if ( document.documentElement.compareDocumentPosition ) {
758758
sortOrder = function( a, b ) {
759+
if ( a === b ) {
760+
hasDuplicate = true;
761+
return 0;
762+
}
763+
759764
if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) {
760-
if ( a == b ) {
761-
hasDuplicate = true;
762-
}
763765
return a.compareDocumentPosition ? -1 : 1;
764766
}
765767

766-
var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
767-
if ( ret === 0 ) {
768-
hasDuplicate = true;
769-
}
770-
return ret;
768+
return a.compareDocumentPosition(b) & 4 ? -1 : 1;
771769
};
772-
} else if ( "sourceIndex" in document.documentElement ) {
770+
} else {
773771
sortOrder = function( a, b ) {
774-
if ( !a.sourceIndex || !b.sourceIndex ) {
775-
if ( a == b ) {
776-
hasDuplicate = true;
777-
}
778-
return a.sourceIndex ? -1 : 1;
772+
if ( a === b ) {
773+
hasDuplicate = true;
774+
return 0;
779775
}
780776

781-
var ret = a.sourceIndex - b.sourceIndex;
782-
if ( ret === 0 ) {
783-
hasDuplicate = true;
777+
var ap = [ a ], bp = [ b ], cur = a.parentNode, bcur, al, bl;
778+
779+
while ( cur ) {
780+
ap.unshift( cur );
781+
cur = cur.parentNode;
784782
}
785-
return ret;
786-
};
787-
} else if ( document.createRange ) {
788-
sortOrder = function( a, b ) {
789-
if ( !a.ownerDocument || !b.ownerDocument ) {
790-
if ( a == b ) {
791-
hasDuplicate = true;
792-
}
793-
return a.ownerDocument ? -1 : 1;
783+
784+
cur = b.parentNode;
785+
786+
while ( cur ) {
787+
bp.unshift( cur );
788+
cur = cur.parentNode;
794789
}
795790

796-
// Blackberry 4.6 has a createRange method but throws an exception #6952
797-
// No sorting alternative exists so punt and leave the elements alone
798-
try {
799-
var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
800-
aRange.setStart(a, 0);
801-
aRange.setEnd(a, 0);
802-
bRange.setStart(b, 0);
803-
bRange.setEnd(b, 0);
804-
805-
var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
806-
if ( ret === 0 ) {
807-
hasDuplicate = true;
808-
}
791+
al = ap.length;
792+
bl = bp.length;
809793

810-
return ret;
794+
if ( !al ) {
795+
return -1;
811796

812-
} catch(e) {
813-
if ( a == b ) {
814-
hasDuplicate = true;
815-
}
797+
} else if ( !bl ) {
798+
return 1;
799+
}
816800

817-
return 0;
801+
for ( var i = 0; i < al && i < bl; i++ ) {
802+
if ( ap[i] !== bp[i] ) {
803+
cur = ap[i].nextSibling;
804+
bcur = bp[i];
805+
806+
while ( cur ) {
807+
if ( cur === bcur ) {
808+
return -1;
809+
}
810+
cur = cur.nextSibling;
811+
}
812+
813+
return 1;
814+
}
818815
}
819816
};
820817
}

0 commit comments

Comments
 (0)