diff --git a/src/core.js b/src/core.js index 771a30f80..72f4de2a0 100644 --- a/src/core.js +++ b/src/core.js @@ -42,6 +42,9 @@ jQuery.fn = jQuery.prototype = { constructor: jQuery, + // Start with an empty selector + selector: "", + // The default length of a jQuery object is 0 length: 0, @@ -70,6 +73,7 @@ jQuery.fn = jQuery.prototype = { // Add the old object onto the stack (as a reference) ret.prevObject = this; + ret.context = this.context; // Return the newly-formed element set return ret; diff --git a/src/core/init.js b/src/core/init.js index a00d58791..915d992b3 100644 --- a/src/core/init.js +++ b/src/core/init.js @@ -79,9 +79,12 @@ var rootjQuery, if ( elem ) { // Inject the element directly into the jQuery object - this[ 0 ] = elem; this.length = 1; + this[0] = elem; } + + this.context = document; + this.selector = selector; return this; } @@ -97,7 +100,7 @@ var rootjQuery, // HANDLE: $(DOMElement) } else if ( selector.nodeType ) { - this[ 0 ] = selector; + this.context = this[0] = selector; this.length = 1; return this; @@ -111,6 +114,11 @@ var rootjQuery, selector( jQuery ); } + if ( selector.selector !== undefined ) { + this.selector = selector.selector; + this.context = selector.context; + } + return jQuery.makeArray( selector, this ); }; diff --git a/src/traversing/findFilter.js b/src/traversing/findFilter.js index ee76a57a7..7d4b2cab9 100644 --- a/src/traversing/findFilter.js +++ b/src/traversing/findFilter.js @@ -72,7 +72,10 @@ jQuery.fn.extend( { jQuery.find( selector, self[ i ], ret ); } - return this.pushStack( len > 1 ? jQuery.uniqueSort( ret ) : ret ); + // Needed because $( selector, context ) becomes $( context ).find( selector ) + ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret ); + ret.selector = this.selector ? this.selector + " " + selector : selector; + return ret; }, filter: function( selector ) { return this.pushStack( winnow( this, selector || [], false ) ); diff --git a/test/unit/core.js b/test/unit/core.js index 49ad24023..528d5f593 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -53,10 +53,10 @@ QUnit.test( "jQuery()", function( assert ) { // Basic constructor's behavior assert.equal( jQuery().length, 0, "jQuery() === jQuery([])" ); - assert.equal( jQuery( undefined ).length, 0, "jQuery(undefined) === jQuery([])" ); - assert.equal( jQuery( null ).length, 0, "jQuery(null) === jQuery([])" ); - assert.equal( jQuery( "" ).length, 0, "jQuery('') === jQuery([])" ); - assert.deepEqual( jQuery( obj ).get(), obj.get(), "jQuery(jQueryObj) == jQueryObj" ); + assert.equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" ); + assert.equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" ); + assert.equal( jQuery("").length, 0, "jQuery('') === jQuery([])" ); + assert.equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" ); // Invalid #id goes to Sizzle which will throw an error (gh-1682) try { @@ -166,6 +166,7 @@ QUnit.test( "globalEval", function( assert ) { assert.equal( window.globalEvalTest, 3, "Test context (this) is the window object" ); } ); + QUnit.test( "globalEval with 'use strict'", function( assert ) { assert.expect( 1 ); Globals.register( "strictEvalTest" ); diff --git a/test/unit/offset.js b/test/unit/offset.js index 88e75950b..2ee0c12ea 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -496,10 +496,10 @@ testIframe( "offset/body", "body", function( $, window, document, assert ) { QUnit.test( "chaining", function( assert ) { assert.expect( 3 ); var coords = { "top": 1, "left": 1 }; - assert.equal( jQuery( "#absolute-1" ).offset( coords ).jquery, jQuery.fn.jquery, "offset(coords) returns jQuery object" ); - assert.equal( jQuery( "#non-existent" ).offset( coords ).jquery, jQuery.fn.jquery, "offset(coords) with empty jQuery set returns jQuery object" ); - assert.equal( jQuery( "#absolute-1" ).offset( undefined ).jquery, jQuery.fn.jquery, "offset(undefined) returns jQuery object (#5571)" ); -} ); + assert.equal( jQuery("#absolute-1").offset(coords).selector, "#absolute-1", "offset(coords) returns jQuery object" ); + assert.equal( jQuery("#non-existent").offset(coords).selector, "#non-existent", "offset(coords) with empty jQuery set returns jQuery object" ); + assert.equal( jQuery("#absolute-1").offset(undefined).selector, "#absolute-1", "offset(undefined) returns jQuery object (#5571)" ); +}); QUnit.test( "offsetParent", function( assert ) { assert.expect( 13 );