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

Commit 6a8edc1

Browse files
jankucajeffbcross
authored andcommitted
fix($compile): collect ranges on multiple directives on one element
The problem was in keeping the values of `attrNameStart` and `attrNameEnd` between directive loop iterations which lead to the compiler looking for multi-element ranges for any directives that happened to be in the directive list after one that was applied on a range. For instance, having a ng-repeat-start and ng-class on a single element with ng-repeat being resolved first made the compiler look for an ng-repeat-end for both ng-repeat and ng-class because the `attrNameEnd` was not reset to a falsy value before the second iteration. As the result, an exception saying the block end element could not be found and the second directive was not actually applied. Closes #4002
1 parent 6cadabf commit 6a8edc1

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/ng/compile.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ function $CompileProvider($provide) {
621621
// iterate over the attributes
622622
for (var attr, name, nName, ngAttrName, value, nAttrs = node.attributes,
623623
j = 0, jj = nAttrs && nAttrs.length; j < jj; j++) {
624-
var attrStartName;
625-
var attrEndName;
624+
var attrStartName = false;
625+
var attrEndName = false;
626626
var index;
627627

628628
attr = nAttrs[j];
@@ -633,11 +633,14 @@ function $CompileProvider($provide) {
633633
if (NG_ATTR_BINDING.test(ngAttrName)) {
634634
name = ngAttrName.substr(6).toLowerCase();
635635
}
636-
if ((index = ngAttrName.lastIndexOf('Start')) != -1 && index == ngAttrName.length - 5) {
636+
637+
var directiveNName = ngAttrName.replace(/(Start|End)$/, '');
638+
if (ngAttrName === directiveNName + 'Start') {
637639
attrStartName = name;
638640
attrEndName = name.substr(0, name.length - 5) + 'end';
639641
name = name.substr(0, name.length - 6);
640642
}
643+
641644
nName = directiveNormalize(name.toLowerCase());
642645
attrsMap[nName] = name;
643646
attrs[nName] = value = trim((msie && name == 'href')
@@ -712,6 +715,7 @@ function $CompileProvider($provide) {
712715
} else {
713716
nodes.push(node);
714717
}
718+
715719
return jqLite(nodes);
716720
}
717721

test/ng/compileSpec.js

+32
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,38 @@ describe('$compile', function() {
33823382
});
33833383

33843384

3385+
it('should correctly collect ranges on multiple directives on a single element', function () {
3386+
module(function($compileProvider) {
3387+
$compileProvider.directive('emptyDirective', function() {
3388+
return function (scope, element) {
3389+
element.data('x', 'abc');
3390+
};
3391+
});
3392+
$compileProvider.directive('rangeDirective', function() {
3393+
return {
3394+
link: function (scope) {
3395+
scope.x = 'X';
3396+
scope.y = 'Y';
3397+
}
3398+
};
3399+
});
3400+
});
3401+
3402+
inject(function ($compile, $rootScope) {
3403+
element = $compile(
3404+
'<div>' +
3405+
'<div range-directive-start empty-directive>{{x}}</div>' +
3406+
'<div range-directive-end>{{y}}</div>' +
3407+
'</div>'
3408+
)($rootScope);
3409+
3410+
$rootScope.$digest();
3411+
expect(element.text()).toBe('XY');
3412+
expect(angular.element(element[0].firstChild).data('x')).toBe('abc');
3413+
});
3414+
});
3415+
3416+
33853417
it('should throw error if unterminated (containing termination as a child)', function () {
33863418
module(function($compileProvider) {
33873419
$compileProvider.directive('foo', function() {

0 commit comments

Comments
 (0)