feat(compiler): allow binding to className using class alias
Closes #2364
This commit is contained in:
parent
f2f4b905e5
commit
a7a1851c0f
|
@ -157,6 +157,7 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Map<String, String> get attrToPropMap => const <String, String>{
|
Map<String, String> get attrToPropMap => const <String, String>{
|
||||||
|
'class': 'className',
|
||||||
'innerHtml': 'innerHTML',
|
'innerHtml': 'innerHTML',
|
||||||
'readonly': 'readOnly',
|
'readonly': 'readOnly',
|
||||||
'tabindex': 'tabIndex',
|
'tabindex': 'tabIndex',
|
||||||
|
|
|
@ -3,7 +3,12 @@ import {isBlank, isPresent, global} from 'angular2/src/facade/lang';
|
||||||
import {setRootDomAdapter} from './dom_adapter';
|
import {setRootDomAdapter} from './dom_adapter';
|
||||||
import {GenericBrowserDomAdapter} from './generic_browser_adapter';
|
import {GenericBrowserDomAdapter} from './generic_browser_adapter';
|
||||||
|
|
||||||
var _attrToPropMap = {'innerHtml': 'innerHTML', 'readonly': 'readOnly', 'tabindex': 'tabIndex'};
|
var _attrToPropMap = {
|
||||||
|
'class': 'className',
|
||||||
|
'innerHtml': 'innerHTML',
|
||||||
|
'readonly': 'readOnly',
|
||||||
|
'tabindex': 'tabIndex'
|
||||||
|
};
|
||||||
|
|
||||||
const DOM_KEY_LOCATION_NUMPAD = 3;
|
const DOM_KEY_LOCATION_NUMPAD = 3;
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {BaseException, isPresent, isBlank, global} from 'angular2/src/facade/lan
|
||||||
import {SelectorMatcher, CssSelector} from 'angular2/src/render/dom/compiler/selector';
|
import {SelectorMatcher, CssSelector} from 'angular2/src/render/dom/compiler/selector';
|
||||||
|
|
||||||
var _attrToPropMap = {
|
var _attrToPropMap = {
|
||||||
|
'class': 'className',
|
||||||
'innerHtml': 'innerHTML',
|
'innerHtml': 'innerHTML',
|
||||||
'readonly': 'readOnly',
|
'readonly': 'readOnly',
|
||||||
'tabindex': 'tabIndex',
|
'tabindex': 'tabIndex',
|
||||||
|
@ -37,6 +38,8 @@ export class Parse5DomAdapter extends DomAdapter {
|
||||||
setProperty(el: /*element*/ any, name: string, value: any) {
|
setProperty(el: /*element*/ any, name: string, value: any) {
|
||||||
if (name === 'innerHTML') {
|
if (name === 'innerHTML') {
|
||||||
this.setInnerHTML(el, value);
|
this.setInnerHTML(el, value);
|
||||||
|
} else if (name === 'className') {
|
||||||
|
el.attribs["class"] = el.className = value;
|
||||||
} else {
|
} else {
|
||||||
el[name] = value;
|
el[name] = value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,6 +212,26 @@ export function main() {
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it('should consume binding to className using class alias',
|
||||||
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||||
|
tcb.overrideView(
|
||||||
|
MyComp,
|
||||||
|
new viewAnn.View({template: '<div class="initial" [class]="ctxProp"></div>'}))
|
||||||
|
|
||||||
|
.createAsync(MyComp)
|
||||||
|
.then((rootTC) => {
|
||||||
|
var nativeEl = rootTC.componentViewChildren[0].nativeElement;
|
||||||
|
rootTC.componentInstance.ctxProp = 'foo bar';
|
||||||
|
rootTC.detectChanges();
|
||||||
|
|
||||||
|
expect(nativeEl).toHaveCssClass('foo');
|
||||||
|
expect(nativeEl).toHaveCssClass('bar');
|
||||||
|
expect(nativeEl).not.toHaveCssClass('initial');
|
||||||
|
|
||||||
|
async.done();
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
it('should consume directive watch expression change.',
|
it('should consume directive watch expression change.',
|
||||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||||
var tpl = '<div>' +
|
var tpl = '<div>' +
|
||||||
|
|
|
@ -104,6 +104,12 @@ export function main() {
|
||||||
var pv = builder.build(new DomElementSchemaRegistry(), templateCloner);
|
var pv = builder.build(new DomElementSchemaRegistry(), templateCloner);
|
||||||
expect(pv.elementBinders[0].propertyBindings[0].property).toEqual('readOnly');
|
expect(pv.elementBinders[0].propertyBindings[0].property).toEqual('readOnly');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should normalize "class" to "className"', () => {
|
||||||
|
builder.bindElement(el('<div></div>')).bindProperty('class', emptyExpr());
|
||||||
|
var pv = builder.build(new DomElementSchemaRegistry(), templateCloner);
|
||||||
|
expect(pv.elementBinders[0].propertyBindings[0].property).toEqual('className');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('property binding', () => {
|
describe('property binding', () => {
|
||||||
|
|
Loading…
Reference in New Issue