Skip to content

Commit

Permalink
feat(compiler): special-case class attribute in hostAttributes
Browse files Browse the repository at this point in the history
Closes #1774

Closes #1841
  • Loading branch information
pkozlowski-opensource authored and mhevery committed May 18, 2015
1 parent cfba38b commit 3011cd8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 11 additions & 3 deletions modules/angular2/src/render/dom/compiler/directive_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ export class DirectiveParser extends CompileStep {
}
if (isPresent(directive.hostAttributes)) {
MapWrapper.forEach(directive.hostAttributes, (hostAttrValue, hostAttrName) => {
if (!DOM.hasAttribute(current.element, hostAttrName)) {
DOM.setAttribute(current.element, hostAttrName, hostAttrValue);
}
this._addHostAttribute(hostAttrName, hostAttrValue, current);
});
}
if (isPresent(directive.readAttributes)) {
Expand Down Expand Up @@ -162,6 +160,16 @@ export class DirectiveParser extends CompileStep {
directiveBinderBuilder.bindHostProperty(hostPropertyName, ast);
}

_addHostAttribute(attrName, attrValue, compileElement) {
if (StringWrapper.equals(attrName, 'class')) {
ListWrapper.forEach(attrValue.split(' '), (className) => {
DOM.addClass(compileElement.element, className);
});
} else if (!DOM.hasAttribute(compileElement.element, attrName)) {
DOM.setAttribute(compileElement.element, attrName, attrValue);
}
}

_splitBindConfig(bindConfig:string) {
return ListWrapper.map(bindConfig.split('|'), (s) => s.trim());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ export function main() {
expect(DOM.getAttribute(results[0].element, 'attr_name')).toEqual('attr_val');
});

it('should add CSS classes if "class" specified in host element attributes', () => {
var element = el('<input class="foo baz" some-decor-with-host-attrs>');
var results = process(element);

expect(DOM.hasClass(results[0].element, 'foo')).toBeTruthy();
expect(DOM.hasClass(results[0].element, 'bar')).toBeTruthy();
expect(DOM.hasClass(results[0].element, 'baz')).toBeTruthy();
});

it('should read attribute values', () => {
var element = el('<input some-decor-props some-attr="someValue">');
var results = process(element);
Expand Down Expand Up @@ -283,7 +292,8 @@ var someDirectiveWithHostProperties = new DirectiveMetadata({
var someDirectiveWithHostAttributes = new DirectiveMetadata({
selector: '[some-decor-with-host-attrs]',
hostAttributes: MapWrapper.createFromStringMap({
'attr_name': 'attr_val'
'attr_name': 'attr_val',
'class': 'foo bar'
})
});

Expand Down

0 comments on commit 3011cd8

Please sign in to comment.