Skip to content

Commit 67d445a

Browse files
committed
A follow-up to [6578] (which stopped adding expandos to elements that didn't have data). That broke jQuery.unique() (so we're now using the unique from Sizzle). Using Sizzle's unique (which also sorts in document order) changed how add, andSelf, parents, nextAll, prevAll, and siblings work. after and before were changed to not use .add() (in order to guarantee their position in the jQuery set). Also, jQuery.data(elem) was updated to return that element's data object (instead of its ID).
$("<div/>").after("<span/>") => [ div, span ] (calling after on a disconnected DOM node adds the nodes to the end of the jQuery set) $("<div/>").before("<span/>") => [ span, div ] (calling before on a disconnected DOM node adds the nodes to the beginning of the jQuery set) $("div").add("span") => [ div, span, span, div, span ] (results now come out in document order) $("div").find("code").andSelf(); => [ div, code, code ] (results now come out in document order) Same goes for .parents(), .nextAll(), .prevAll(), and .siblings(). Exception: .parents() will still return the results in reverse document order. jQuery.data(elem) => { object of data } (no longer returns the unique ID assigned to the node)
1 parent 67089ee commit 67d445a

File tree

9 files changed

+73
-48
lines changed

9 files changed

+73
-48
lines changed

src/core.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -462,25 +462,6 @@ jQuery.extend({
462462
return first;
463463
},
464464

465-
unique: function( array ) {
466-
var ret = [], done = {}, id;
467-
468-
try {
469-
for ( var i = 0, length = array.length; i < length; i++ ) {
470-
id = jQuery.data( array[ i ] );
471-
472-
if ( !done[ id ] ) {
473-
done[ id ] = true;
474-
ret.push( array[ i ] );
475-
}
476-
}
477-
} catch( e ) {
478-
ret = array;
479-
}
480-
481-
return ret;
482-
},
483-
484465
grep: function( elems, callback, inv ) {
485466
var ret = [];
486467

src/data.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jQuery.extend({
1414
var id = elem[ expando ], cache = jQuery.cache, thisCache;
1515

1616
// Handle the case where there's no name immediately
17-
if ( !name ) {
18-
return id;
17+
if ( !name && !id ) {
18+
return null;
1919
}
2020

2121
// Compute a unique ID for the element
@@ -39,7 +39,7 @@ jQuery.extend({
3939
thisCache[ name ] = data;
4040
}
4141

42-
return name === true ? thisCache : thisCache[ name ];
42+
return name ? thisCache[ name ] : thisCache;
4343
},
4444

4545
removeData: function( elem, name ) {
@@ -116,7 +116,9 @@ jQuery.extend({
116116

117117
jQuery.fn.extend({
118118
data: function( key, value ){
119-
if(typeof key === "undefined" && this.length) return jQuery.data(this[0], true);
119+
if ( typeof key === "undefined" && this.length ) {
120+
return jQuery.data( this[0] );
121+
}
120122

121123
var parts = key.split(".");
122124
parts[1] = parts[1] ? "." + parts[1] : "";
@@ -165,4 +167,4 @@ jQuery.fn.extend({
165167
clearQueue: function(type){
166168
return this.queue( type || "fx", [] );
167169
}
168-
});
170+
});

src/manipulation.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,10 @@ jQuery.fn.extend({
111111
return this.domManip(arguments, false, function(elem){
112112
this.parentNode.insertBefore( elem, this );
113113
});
114-
} else {
115-
var set = jQuery.isFunction(arguments[0]) ?
116-
jQuery( arguments[0]() ) :
117-
jQuery.apply(jQuery, arguments);
118-
119-
return this.pushStack( set.add( this ), "before", arguments );
114+
} else if ( arguments.length ) {
115+
var set = jQuery(arguments[0]);
116+
set.push.apply( set, this.toArray() );
117+
return this.pushStack( set, "before", arguments );
120118
}
121119
},
122120

@@ -125,10 +123,10 @@ jQuery.fn.extend({
125123
return this.domManip(arguments, false, function(elem){
126124
this.parentNode.insertBefore( elem, this.nextSibling );
127125
});
128-
} else {
129-
return jQuery.isFunction(arguments[0]) ?
130-
this.add( arguments[0]() ) :
131-
this.add.apply( this, arguments );
126+
} else if ( arguments.length ) {
127+
var set = this.pushStack( this, "after", arguments );
128+
set.push.apply( set, jQuery(arguments[0]).toArray() );
129+
return set;
132130
}
133131
},
134132

src/selector.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ Sizzle.uniqueSort = function(results){
144144
}
145145
}
146146
}
147+
148+
return results;
147149
};
148150

149151
Sizzle.matches = function(expr, set){
@@ -703,6 +705,13 @@ var sortOrder;
703705

704706
if ( document.documentElement.compareDocumentPosition ) {
705707
sortOrder = function( a, b ) {
708+
if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) {
709+
if ( a == b ) {
710+
hasDuplicate = true;
711+
}
712+
return 0;
713+
}
714+
706715
var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
707716
if ( ret === 0 ) {
708717
hasDuplicate = true;
@@ -711,6 +720,13 @@ if ( document.documentElement.compareDocumentPosition ) {
711720
};
712721
} else if ( "sourceIndex" in document.documentElement ) {
713722
sortOrder = function( a, b ) {
723+
if ( !a.sourceIndex || !b.sourceIndex ) {
724+
if ( a == b ) {
725+
hasDuplicate = true;
726+
}
727+
return 0;
728+
}
729+
714730
var ret = a.sourceIndex - b.sourceIndex;
715731
if ( ret === 0 ) {
716732
hasDuplicate = true;
@@ -719,6 +735,13 @@ if ( document.documentElement.compareDocumentPosition ) {
719735
};
720736
} else if ( document.createRange ) {
721737
sortOrder = function( a, b ) {
738+
if ( !a.ownerDocument || !b.ownerDocument ) {
739+
if ( a == b ) {
740+
hasDuplicate = true;
741+
}
742+
return 0;
743+
}
744+
722745
var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
723746
aRange.selectNode(a);
724747
aRange.collapse(true);
@@ -1036,6 +1059,8 @@ jQuery.sibling = function(n, elem){
10361059
return r;
10371060
};
10381061

1062+
jQuery.unique = Sizzle.uniqueSort;
1063+
10391064
return;
10401065

10411066
window.Sizzle = Sizzle;

src/traversing.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ jQuery.fn.extend({
6969
},
7070

7171
add: function( selector ) {
72-
return this.pushStack( jQuery.unique( jQuery.merge(
73-
this.get(),
74-
typeof selector === "string" ?
72+
var set = typeof selector === "string" ?
7573
jQuery( selector ) :
76-
jQuery.makeArray( selector )
77-
)));
74+
jQuery.makeArray( selector ),
75+
all = jQuery.merge( this.get(), set );
76+
77+
return this.pushStack( set[0] && (set[0].setInterval || set[0].nodeType === 9 || (set[0].parentNode && set[0].parentNode.nodeType !== 11)) ?
78+
jQuery.unique( all ) :
79+
all );
7880
},
7981

8082
eq: function( i ) {
@@ -125,9 +127,16 @@ jQuery.each({
125127
jQuery.fn[ name ] = function( selector ) {
126128
var ret = jQuery.map( this, fn );
127129

128-
if ( selector && typeof selector == "string" )
130+
if ( selector && typeof selector === "string" ) {
129131
ret = jQuery.multiFilter( selector, ret );
132+
}
133+
134+
ret = this.length > 1 ? jQuery.unique( ret ) : ret;
135+
136+
if ( name === "parents" && this.length > 1 ) {
137+
ret = ret.reverse();
138+
}
130139

131-
return this.pushStack( jQuery.unique( ret ), name, selector );
140+
return this.pushStack( ret, name, selector );
132141
};
133142
});

test/unit/core.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ test("get(-Number)",function() {
399399
})
400400

401401
test("add(String|Element|Array|undefined)", function() {
402-
expect(12);
402+
expect(16);
403403
isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
404404
isSet( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
405405
ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
@@ -408,6 +408,16 @@ test("add(String|Element|Array|undefined)", function() {
408408
// use jQuery([]).add(form.elements) instead.
409409
//equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
410410

411+
var tmp = jQuery("<div/>");
412+
413+
var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
414+
equals( x[0].id, "x1", "Check on-the-fly element1" );
415+
equals( x[1].id, "x2", "Check on-the-fly element2" );
416+
417+
var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
418+
equals( x[0].id, "x1", "Check on-the-fly element1" );
419+
equals( x[1].id, "x2", "Check on-the-fly element2" );
420+
411421
var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
412422
equals( x[0].id, "x1", "Check on-the-fly element1" );
413423
equals( x[1].id, "x2", "Check on-the-fly element2" );

test/unit/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test("jQuery.data", function() {
3232
jQuery.data(div, "test", "success");
3333
equals( jQuery.data(div, "test"), "success", "Check for added data" );
3434

35-
var data = jQuery.data(div, true);
35+
var data = jQuery.data(div);
3636
same( data, { "test": "success" }, "Return complete data set" );
3737

3838
jQuery.data(div, "test", "overwritten");

test/unit/manipulation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ var testBefore = function(val) {
340340
jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
341341
equals( expected, jQuery('#en').text(), "Insert jQuery before" );
342342

343-
var set = jQuery("<div/>").before(val("<span>test</span>"));
343+
var set = jQuery("<div/>").before("<span>test</span>");
344344
equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
345345
equals( set.length, 2, "Insert the element before the disconnected node." );
346346
}
@@ -396,7 +396,7 @@ var testAfter = function(val) {
396396
jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
397397
equals( expected, jQuery('#en').text(), "Insert jQuery after" );
398398

399-
var set = jQuery("<div/>").after(val("<span>test</span>"));
399+
var set = jQuery("<div/>").after("<span>test</span>");
400400
equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
401401
equals( set.length, 2, "Insert the element after the disconnected node." );
402402
};

test/unit/traversing.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ test("not(jQuery)", function() {
145145

146146
test("andSelf()", function() {
147147
expect(4);
148-
isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
149-
isSet( jQuery("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
148+
isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "en", "sap"), "Check for siblings and self" );
149+
isSet( jQuery("#foo").children().andSelf().get(), q("foo", "sndp", "en", "sap"), "Check for children and self" );
150150
isSet( jQuery("#sndp, #en").parent().andSelf().get(), q("foo","sndp","en"), "Check for parent and self" );
151151
isSet( jQuery("#groups").parents("p, div").andSelf().get(), q("main", "ap", "groups"), "Check for parents and self" );
152152
});
@@ -157,7 +157,7 @@ test("siblings([String])", function() {
157157
isSet( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
158158
isSet( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
159159
isSet( jQuery("#foo").siblings("form, b").get(), q("form", "floatTest", "lengthtest", "name-tests", "testForm"), "Check for multiple filters" );
160-
var set = q("en", "sap", "sndp");
160+
var set = q("sndp", "en", "sap");
161161
isSet( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
162162
});
163163

0 commit comments

Comments
 (0)