Skip to content

Commit 0b5e8db

Browse files
committed
Revert "Core: Remove deprecated context and selector properties"
This reverts commit e2ec5da.
1 parent fd5858f commit 0b5e8db

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

src/core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jQuery.fn = jQuery.prototype = {
4343

4444
constructor: jQuery,
4545

46+
// Start with an empty selector
47+
selector: "",
48+
4649
// The default length of a jQuery object is 0
4750
length: 0,
4851

@@ -71,6 +74,7 @@ jQuery.fn = jQuery.prototype = {
7174

7275
// Add the old object onto the stack (as a reference)
7376
ret.prevObject = this;
77+
ret.context = this.context;
7478

7579
// Return the newly-formed element set
7680
return ret;

src/core/init.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ var rootjQuery,
7878
if ( elem ) {
7979

8080
// Inject the element directly into the jQuery object
81-
this[ 0 ] = elem;
8281
this.length = 1;
82+
this[0] = elem;
8383
}
84+
85+
this.context = document;
86+
this.selector = selector;
8487
return this;
8588
}
8689

@@ -96,7 +99,7 @@ var rootjQuery,
9699

97100
// HANDLE: $(DOMElement)
98101
} else if ( selector.nodeType ) {
99-
this[ 0 ] = selector;
102+
this.context = this[0] = selector;
100103
this.length = 1;
101104
return this;
102105

@@ -110,6 +113,11 @@ var rootjQuery,
110113
selector( jQuery );
111114
}
112115

116+
if ( selector.selector !== undefined ) {
117+
this.selector = selector.selector;
118+
this.context = selector.context;
119+
}
120+
113121
return jQuery.makeArray( selector, this );
114122
};
115123

src/traversing/findFilter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ jQuery.fn.extend( {
7272
jQuery.find( selector, self[ i ], ret );
7373
}
7474

75-
return this.pushStack( len > 1 ? jQuery.uniqueSort( ret ) : ret );
75+
// Needed because $( selector, context ) becomes $( context ).find( selector )
76+
ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret );
77+
ret.selector = this.selector ? this.selector + " " + selector : selector;
78+
return ret;
7679
},
7780
filter: function( selector ) {
7881
return this.pushStack( winnow( this, selector || [], false ) );

test/unit/core.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ QUnit.test( "jQuery()", function( assert ) {
6262
equal( jQuery("").length, 0, "jQuery('') === jQuery([])" );
6363
equal( jQuery("#").length, 0, "jQuery('#') === jQuery([])" );
6464

65+
equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
66+
6567
// can actually yield more than one, when iframes are included, the window is an array as well
6668
assert.equal( jQuery( window ).length, 1, "Correct number of elements generated for jQuery(window)" );
6769

@@ -153,12 +155,52 @@ QUnit.test( "jQuery(selector, context)", function( assert ) {
153155
assert.deepEqual( jQuery( "div p", jQuery( "#qunit-fixture" ) ).get(), q( "sndp", "en", "sap" ), "Basic selector with jQuery object as context" );
154156
} );
155157

156-
QUnit.test( "globalEval", function( assert ) {
157-
assert.expect( 3 );
158-
Globals.register( "globalEvalTest" );
158+
test( "selector state", function() {
159+
expect( 18 );
160+
161+
var test;
162+
163+
test = jQuery( undefined );
164+
equal( test.selector, "", "Empty jQuery Selector" );
165+
equal( test.context, undefined, "Empty jQuery Context" );
166+
167+
test = jQuery( document );
168+
equal( test.selector, "", "Document Selector" );
169+
equal( test.context, document, "Document Context" );
170+
171+
test = jQuery( document.body );
172+
equal( test.selector, "", "Body Selector" );
173+
equal( test.context, document.body, "Body Context" );
174+
175+
test = jQuery("#qunit-fixture");
176+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
177+
equal( test.context, document, "#qunit-fixture Context" );
159178

160-
jQuery.globalEval( "globalEvalTest = 1;" );
161-
assert.equal( window.globalEvalTest, 1, "Test variable assignments are global" );
179+
test = jQuery("#notfoundnono");
180+
equal( test.selector, "#notfoundnono", "#notfoundnono Selector" );
181+
equal( test.context, document, "#notfoundnono Context" );
182+
183+
test = jQuery( "#qunit-fixture", document );
184+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
185+
equal( test.context, document, "#qunit-fixture Context" );
186+
187+
test = jQuery( "#qunit-fixture", document.body );
188+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
189+
equal( test.context, document.body, "#qunit-fixture Context" );
190+
191+
// Test cloning
192+
test = jQuery( test );
193+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
194+
equal( test.context, document.body, "#qunit-fixture Context" );
195+
196+
test = jQuery( document.body ).find("#qunit-fixture");
197+
equal( test.selector, "#qunit-fixture", "#qunit-fixture find Selector" );
198+
equal( test.context, document.body, "#qunit-fixture find Context" );
199+
});
200+
201+
QUnit.test( "globalEval", function( assert ) {
202+
expect( 2 );
203+
Globals.register("globalEvalTest");
162204

163205
jQuery.globalEval( "var globalEvalTest = 2;" );
164206
assert.equal( window.globalEvalTest, 2, "Test variable declarations are global" );

test/unit/offset.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,10 @@ testIframe( "offset/body", "body", function( $, window, document, assert ) {
497497
QUnit.test( "chaining", function( assert ) {
498498
assert.expect( 3 );
499499
var coords = { "top": 1, "left": 1 };
500-
assert.equal( jQuery( "#absolute-1" ).offset( coords ).jquery, jQuery.fn.jquery, "offset(coords) returns jQuery object" );
501-
assert.equal( jQuery( "#non-existent" ).offset( coords ).jquery, jQuery.fn.jquery, "offset(coords) with empty jQuery set returns jQuery object" );
502-
assert.equal( jQuery( "#absolute-1" ).offset( undefined ).jquery, jQuery.fn.jquery, "offset(undefined) returns jQuery object (#5571)" );
503-
} );
500+
equal( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" );
501+
equal( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" );
502+
equal( jQuery("#absolute-1").offset(undefined).selector, "#absolute-1", "offset(undefined) returns jQuery object (#5571)" );
503+
});
504504

505505
QUnit.test( "offsetParent", function( assert ) {
506506
assert.expect( 13 );

0 commit comments

Comments
 (0)