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

fix($compile): allow multiple directives on a single element even if one works with a multi-element range #4160

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
10 changes: 7 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ function $CompileProvider($provide) {
// iterate over the attributes
for (var attr, name, nName, ngAttrName, value, nAttrs = node.attributes,
j = 0, jj = nAttrs && nAttrs.length; j < jj; j++) {
var attrStartName;
var attrEndName;
var attrStartName = false;
var attrEndName = false;
var index;

attr = nAttrs[j];
Expand All @@ -633,11 +633,14 @@ function $CompileProvider($provide) {
if (NG_ATTR_BINDING.test(ngAttrName)) {
name = ngAttrName.substr(6).toLowerCase();
}
if ((index = ngAttrName.lastIndexOf('Start')) != -1 && index == ngAttrName.length - 5) {

var directiveNName = ngAttrName.replace(/(Start|End)$/, '');
if (ngAttrName === directiveNName + 'Start') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that attr{Start,End}Name should be set back to false, but why change this condition and involve a regex?
What's wrong with using lastIndexOf here?

If this way is the way to go, then remove the index variable (L626) since it's no longer used, and also cache the regex

attrStartName = name;
attrEndName = name.substr(0, name.length - 5) + 'end';
name = name.substr(0, name.length - 6);
}

nName = directiveNormalize(name.toLowerCase());
attrsMap[nName] = name;
attrs[nName] = value = trim((msie && name == 'href')
Expand Down Expand Up @@ -712,6 +715,7 @@ function $CompileProvider($provide) {
} else {
nodes.push(node);
}

return jqLite(nodes);
}

Expand Down
32 changes: 32 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,38 @@ describe('$compile', function() {
});


it('should correctly collect ranges on multiple directives on a single element', function () {
module(function($compileProvider) {
$compileProvider.directive('emptyDirective', function() {
return function (scope, element) {
element.data('x', 'abc');
};
});
$compileProvider.directive('rangeDirective', function() {
return {
link: function (scope) {
scope.x = 'X';
scope.y = 'Y';
}
};
});
});

inject(function ($compile, $rootScope) {
element = $compile(
'<div>' +
'<div range-directive-start empty-directive>{{x}}</div>' +
'<div range-directive-end>{{y}}</div>' +
'</div>'
)($rootScope);

$rootScope.$digest();
expect(element.text()).toBe('XY');
expect(angular.element(element[0].firstChild).data('x')).toBe('abc');
});
});


it('should throw error if unterminated (containing termination as a child)', function () {
module(function($compileProvider) {
$compileProvider.directive('foo', function() {
Expand Down