Skip to content

Commit edc3709

Browse files
fix(ElementBinderBuilder): properly bind CSS classes with "-" in their names
Fixes angular#1057 Closes angular#1059
1 parent e706f34 commit edc3709

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

modules/angular2/src/core/compiler/property_setter_factory.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function setterFactory(property: string): Function {
3232
if (DOM.hasProperty(receiver, property)) {
3333
return propertySetterFn(receiver, value);
3434
}
35-
}
35+
};
3636
StringMapWrapper.set(propertySettersCache, property, setterFn);
3737
}
3838
}
@@ -61,7 +61,7 @@ function attributeSetterFactory(attrName:string): Function {
6161
DOM.setAttribute(element, dashCasedAttributeName, stringify(value));
6262
} else {
6363
if (isPresent(value)) {
64-
throw new BaseException("Invalid " + dashCasedAttributeName +
64+
throw new BaseException("Invalid " + dashCasedAttributeName +
6565
" attribute, only string values are allowed, got '" + stringify(value) + "'");
6666
}
6767
DOM.removeAttribute(element, dashCasedAttributeName);
@@ -78,13 +78,14 @@ var classSettersCache = StringMapWrapper.create();
7878

7979
function classSetterFactory(className:string): Function {
8080
var setterFn = StringMapWrapper.get(classSettersCache, className);
81-
81+
var dashCasedClassName;
8282
if (isBlank(setterFn)) {
83+
dashCasedClassName = camelCaseToDashCase(className);
8384
setterFn = function(element, value) {
8485
if (value) {
85-
DOM.addClass(element, className);
86+
DOM.addClass(element, dashCasedClassName);
8687
} else {
87-
DOM.removeClass(element, className);
88+
DOM.removeClass(element, dashCasedClassName);
8889
}
8990
};
9091
StringMapWrapper.set(classSettersCache, className, setterFn);

modules/angular2/test/core/compiler/pipeline/element_binder_builder_spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,28 @@ export function main() {
349349
expect(view.nodes[0].className).toEqual('foo ng-binding');
350350
});
351351

352+
it('should properly bind to class containing "-" using the class. syntax', () => {
353+
var propertyBindings = MapWrapper.createFromStringMap({
354+
'class.foo-bar': 'prop1'
355+
});
356+
var pipeline = createPipeline({propertyBindings: propertyBindings});
357+
var results = pipeline.process(el('<input class="foo" viewroot prop-binding>'));
358+
var pv = results[0].inheritedProtoView;
359+
360+
expect(pv.elementBinders[0].hasElementPropertyBindings).toBe(true);
361+
362+
instantiateView(pv);
363+
364+
evalContext.prop1 = true;
365+
changeDetector.detectChanges();
366+
expect(view.nodes[0].className).toEqual('foo ng-binding foo-bar');
367+
368+
evalContext.prop1 = false;
369+
changeDetector.detectChanges();
370+
expect(view.nodes[0].className).toEqual('foo ng-binding');
371+
});
372+
373+
352374
it('should bind style with a dot', () => {
353375
var propertyBindings = MapWrapper.createFromStringMap({
354376
'style.color': 'prop1',

0 commit comments

Comments
 (0)