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

fix($compile): updating the jQuery .context when it is replaced #8253

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}
}
$rootElement.length -= removeCount - 1;

//If the replaced element is also the jQuery .context then replace it
if ($rootElement.context === firstElementToRemove) {
$rootElement.context = newNode;
}
break;
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,29 @@ describe('$compile', function() {
expect(child).toHaveClass('log'); // merged from replace directive template
}));

it('should update references to replaced jQuery context', function() {
module(function($compileProvider) {
$compileProvider.directive('foo', function() {
return {
replace: true,
template: '<div></div>'
};
});
});

inject(function($compile, $rootScope) {
element = jqLite(document.createElement('span')).attr('foo', '');
expect(nodeName_(element)).toBe('span');

var preCompiledNode = element[0];

var linked = $compile(element)($rootScope);
expect(linked).toBe(element);
expect(nodeName_(element)).toBe('div');
expect(element.context).not.toBe(preCompiledNode);
});
});

it("should fail if replacing and template doesn't have a single root element", function() {
module(function() {
directive('noRootElem', function() {
Expand Down