Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f02f7d9

Browse files
jbedardIgorMinar
authored andcommitted
fix($compile): update the jQuery .context when an element is replaced by replace:true directive
.context is a deprecated jQuery api still being used by at least live() queries, so we need to keep it in up to date during replacement. Because of the if check, we can be sure that we replace the context only when jQuery is being used and the context property is set to the element being replaced. Closes #8253 Closes #7900
1 parent 7eae29e commit f02f7d9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/ng/compile.js

+7
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20552055
}
20562056
}
20572057
$rootElement.length -= removeCount - 1;
2058+
2059+
// If the replaced element is also the jQuery .context then replace it
2060+
// .context is a deprecated jQuery api, so we should set it only when jQuery set it
2061+
// http://api.jquery.com/context/
2062+
if ($rootElement.context === firstElementToRemove) {
2063+
$rootElement.context = newNode;
2064+
}
20582065
break;
20592066
}
20602067
}

test/ng/compileSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,31 @@ describe('$compile', function() {
719719
expect(child).toHaveClass('log'); // merged from replace directive template
720720
}));
721721

722+
it('should update references to replaced jQuery context', function() {
723+
module(function($compileProvider) {
724+
$compileProvider.directive('foo', function() {
725+
return {
726+
replace: true,
727+
template: '<div></div>'
728+
};
729+
});
730+
});
731+
732+
inject(function($compile, $rootScope) {
733+
element = jqLite(document.createElement('span')).attr('foo', '');
734+
expect(nodeName_(element)).toBe('span');
735+
736+
var preCompiledNode = element[0];
737+
738+
var linked = $compile(element)($rootScope);
739+
expect(linked).toBe(element);
740+
expect(nodeName_(element)).toBe('div');
741+
if (element.context) {
742+
expect(element.context).toBe(element[0]);
743+
}
744+
});
745+
});
746+
722747
it("should fail if replacing and template doesn't have a single root element", function() {
723748
module(function() {
724749
directive('noRootElem', function() {

0 commit comments

Comments
 (0)