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

Commit 3ea2416

Browse files
committed
Revert "fix(ng:class): preserve classes added post compilation"
This reverts commit 2428907. We decided to revert this because it is not bullet proof. The issue is that we can't reliably have both angular and non-angular code in charge of the DOM. We could work around some issues here and there, but we can't do it reliably, so it's better not to support DOM manipulation that happens outside of angular. There is a good chance that once we integrate with MDVs our possition will change, but until then our position is that only angular or angular widgets/directives can change change DOM that was compiled.
1 parent 9636160 commit 3ea2416

File tree

2 files changed

+10
-56
lines changed

2 files changed

+10
-56
lines changed

src/directives.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -565,25 +565,18 @@ angularDirective("ng:submit", function(expression, element) {
565565
});
566566
});
567567

568+
568569
function ngClass(selector) {
569570
return function(expression, element){
571+
var existing = element[0].className + ' ';
570572
return function(element){
571-
if(selector(this.$index)) {
572-
this.$watch(expression, function(newCls, oldCls) {
573-
var cls = element.attr('class');
574-
if (isArray(newCls)) newCls = newCls.join(' ');
575-
if (isArray(oldCls)) oldCls = oldCls.join(' ');
576-
577-
// The time when newCls == oldCLs is when $watch just started
578-
if (newCls == oldCls) {
579-
cls += ' ' + newCls;
580-
} else {
581-
cls = cls.replace(' ' + oldCls, ' ' + newCls);
582-
}
583-
584-
element.attr('class', cls);
585-
});
586-
}
573+
this.$onEval(function(){
574+
if (selector(this.$index)) {
575+
var value = this.$eval(expression);
576+
if (isArray(value)) value = value.join(' ');
577+
element[0].className = trim(existing + value);
578+
}
579+
}, element);
587580
};
588581
};
589582
}

test/directivesSpec.js

+1-40
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe("directive", function(){
179179

180180

181181
describe('ng:class', function() {
182-
it('should change current class or remove old classes dynamically', function() {
182+
it('should add new and remove old classes dynamically', function() {
183183
var scope = compile('<div class="existing" ng:class="dynClass"></div>');
184184
scope.dynClass = 'A';
185185
scope.$eval();
@@ -206,45 +206,6 @@ describe("directive", function(){
206206
expect(element.hasClass('A')).toBeTruthy();
207207
expect(element.hasClass('B')).toBeTruthy();
208208
});
209-
210-
it('should preserve class added post compilation', function() {
211-
var scope = compile('<div class="existing" ng:class="dynClass"></div>');
212-
scope.dynClass = 'A';
213-
scope.$eval();
214-
expect(element.hasClass('existing')).toBe(true);
215-
216-
// add extra class, change model and eval
217-
element.addClass('newClass');
218-
scope.dynClass = 'B';
219-
scope.$eval();
220-
221-
expect(element.hasClass('existing')).toBe(true);
222-
expect(element.hasClass('B')).toBe(true);
223-
expect(element.hasClass('newClass')).toBe(true);
224-
});
225-
226-
it('should preserve class added post compilation even without existing classes"', function() {
227-
var scope = compile('<div ng:class="dynClass"></div>');
228-
scope.dynClass = 'A';
229-
scope.$eval();
230-
expect(element.hasClass('A')).toBe(true);
231-
232-
// add extra class, change model and eval
233-
element.addClass('newClass');
234-
scope.dynClass = 'B';
235-
scope.$eval();
236-
237-
expect(element.hasClass('B')).toBe(true);
238-
expect(element.hasClass('newClass')).toBe(true);
239-
});
240-
241-
it('should preserve right classes"', function() {
242-
var scope = compile('<div class="ui-panel ui-selected" ng:class="dynCls"></div>');
243-
scope.dynCls = 'panel';
244-
scope.dynCls = 'foo';
245-
scope.$eval();
246-
expect(element.attr('class')).toBe('ui-panel ui-selected ng-directive foo');
247-
});
248209
});
249210

250211

0 commit comments

Comments
 (0)