Skip to content

Commit a7a1851

Browse files
feat(compiler): allow binding to className using class alias
Closes angular#2364
1 parent f2f4b90 commit a7a1851

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

modules/angular2/src/dom/browser_adapter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
157157

158158
@override
159159
Map<String, String> get attrToPropMap => const <String, String>{
160+
'class': 'className',
160161
'innerHtml': 'innerHTML',
161162
'readonly': 'readOnly',
162163
'tabindex': 'tabIndex',

modules/angular2/src/dom/browser_adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {isBlank, isPresent, global} from 'angular2/src/facade/lang';
33
import {setRootDomAdapter} from './dom_adapter';
44
import {GenericBrowserDomAdapter} from './generic_browser_adapter';
55

6-
var _attrToPropMap = {'innerHtml': 'innerHTML', 'readonly': 'readOnly', 'tabindex': 'tabIndex'};
6+
var _attrToPropMap = {
7+
'class': 'className',
8+
'innerHtml': 'innerHTML',
9+
'readonly': 'readOnly',
10+
'tabindex': 'tabIndex'
11+
};
712

813
const DOM_KEY_LOCATION_NUMPAD = 3;
914

modules/angular2/src/dom/parse5_adapter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {BaseException, isPresent, isBlank, global} from 'angular2/src/facade/lan
1313
import {SelectorMatcher, CssSelector} from 'angular2/src/render/dom/compiler/selector';
1414

1515
var _attrToPropMap = {
16+
'class': 'className',
1617
'innerHtml': 'innerHTML',
1718
'readonly': 'readOnly',
1819
'tabindex': 'tabIndex',
@@ -37,6 +38,8 @@ export class Parse5DomAdapter extends DomAdapter {
3738
setProperty(el: /*element*/ any, name: string, value: any) {
3839
if (name === 'innerHTML') {
3940
this.setInnerHTML(el, value);
41+
} else if (name === 'className') {
42+
el.attribs["class"] = el.className = value;
4043
} else {
4144
el[name] = value;
4245
}

modules/angular2/test/core/compiler/integration_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,26 @@ export function main() {
212212
});
213213
}));
214214

215+
it('should consume binding to className using class alias',
216+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
217+
tcb.overrideView(
218+
MyComp,
219+
new viewAnn.View({template: '<div class="initial" [class]="ctxProp"></div>'}))
220+
221+
.createAsync(MyComp)
222+
.then((rootTC) => {
223+
var nativeEl = rootTC.componentViewChildren[0].nativeElement;
224+
rootTC.componentInstance.ctxProp = 'foo bar';
225+
rootTC.detectChanges();
226+
227+
expect(nativeEl).toHaveCssClass('foo');
228+
expect(nativeEl).toHaveCssClass('bar');
229+
expect(nativeEl).not.toHaveCssClass('initial');
230+
231+
async.done();
232+
});
233+
}));
234+
215235
it('should consume directive watch expression change.',
216236
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
217237
var tpl = '<div>' +

modules/angular2/test/render/dom/view/proto_view_builder_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ export function main() {
104104
var pv = builder.build(new DomElementSchemaRegistry(), templateCloner);
105105
expect(pv.elementBinders[0].propertyBindings[0].property).toEqual('readOnly');
106106
});
107+
108+
it('should normalize "class" to "className"', () => {
109+
builder.bindElement(el('<div></div>')).bindProperty('class', emptyExpr());
110+
var pv = builder.build(new DomElementSchemaRegistry(), templateCloner);
111+
expect(pv.elementBinders[0].propertyBindings[0].property).toEqual('className');
112+
});
107113
});
108114

109115
describe('property binding', () => {

0 commit comments

Comments
 (0)