Skip to content

Commit 683b9b3

Browse files
committed
Core: Fill in context and selector properties for .live() support
Fixes #79 Ref jquery/jquery#1908
1 parent 81d61ac commit 683b9b3

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/core.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var matched, browser,
88

99
// $(html) "looks like html" rule change
1010
jQuery.fn.init = function( selector, context, rootjQuery ) {
11-
var match;
11+
var match, ret;
1212

1313
if ( selector && typeof selector === "string" && !jQuery.isPlainObject( context ) &&
1414
(match = rquickExpr.exec( jQuery.trim( selector ) )) && match[ 0 ] ) {
@@ -31,11 +31,28 @@ jQuery.fn.init = function( selector, context, rootjQuery ) {
3131
context = context.context;
3232
}
3333
if ( jQuery.parseHTML ) {
34-
return oldInit.call( this, jQuery.parseHTML( match[ 2 ], context, true ),
34+
return oldInit.call( this,
35+
jQuery.parseHTML( match[ 2 ], context && context.ownerDocument || context, true ),
3536
context, rootjQuery );
3637
}
3738
}
38-
return oldInit.apply( this, arguments );
39+
40+
ret = oldInit.apply( this, arguments );
41+
42+
// Fill in selector and context properties so .live() works
43+
if ( selector && selector.selector !== undefined ) {
44+
// A jQuery object, copy its properties
45+
ret.selector = selector.selector;
46+
ret.context = selector.context;
47+
48+
} else {
49+
ret.selector = typeof selector === "string" ? selector : "";
50+
if ( selector ) {
51+
ret.context = selector.nodeType? selector : context || document;
52+
}
53+
}
54+
55+
return ret;
3956
};
4057
jQuery.fn.init.prototype = jQuery.fn;
4158

src/traversing.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
var oldSelf = jQuery.fn.andSelf || jQuery.fn.addBack;
1+
var oldSelf = jQuery.fn.andSelf || jQuery.fn.addBack,
2+
oldFind = jQuery.fn.find;
23

34
jQuery.fn.andSelf = function() {
45
migrateWarn("jQuery.fn.andSelf() replaced by jQuery.fn.addBack()");
56
return oldSelf.apply( this, arguments );
67
};
8+
9+
jQuery.fn.find = function( selector ) {
10+
var ret = oldFind.apply( this, arguments );
11+
ret.context = this.context;
12+
ret.selector = this.selector ? this.selector + " " + selector : selector;
13+
return ret;
14+
};

test/core.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,49 @@ test( "jQuery(html) loose rules", function() {
4747
}
4848
});
4949

50+
test( "selector state", function() {
51+
expect( 18 );
52+
53+
var test;
54+
55+
test = jQuery( undefined );
56+
equal( test.selector, "", "Empty jQuery Selector" );
57+
equal( test.context, undefined, "Empty jQuery Context" );
58+
59+
test = jQuery( document );
60+
equal( test.selector, "", "Document Selector" );
61+
equal( test.context, document, "Document Context" );
62+
63+
test = jQuery( document.body );
64+
equal( test.selector, "", "Body Selector" );
65+
equal( test.context, document.body, "Body Context" );
66+
67+
test = jQuery("#qunit-fixture");
68+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
69+
equal( test.context, document, "#qunit-fixture Context" );
70+
71+
test = jQuery("#notfoundnono");
72+
equal( test.selector, "#notfoundnono", "#notfoundnono Selector" );
73+
equal( test.context, document, "#notfoundnono Context" );
74+
75+
test = jQuery( "#qunit-fixture", document );
76+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
77+
equal( test.context, document, "#qunit-fixture Context" );
78+
79+
test = jQuery( "#qunit-fixture", document.body );
80+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
81+
equal( test.context, document.body, "#qunit-fixture Context" );
82+
83+
// Test cloning
84+
test = jQuery( test );
85+
equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
86+
equal( test.context, document.body, "#qunit-fixture Context" );
87+
88+
test = jQuery( document.body ).find("#qunit-fixture");
89+
equal( test.selector, "#qunit-fixture", "#qunit-fixture find Selector" );
90+
equal( test.context, document.body, "#qunit-fixture find Context" );
91+
});
92+
5093
test( "XSS injection", function() {
5194
expect( 10 );
5295

0 commit comments

Comments
 (0)