Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(selector): Collect bind- attributes. More tests. Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Apr 8, 2014
1 parent 9c03a3d commit 4707826
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
7 changes: 4 additions & 3 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class TemplateElementBinder extends ElementBinder {
}

TemplateElementBinder(_perf, _expando, this.template, this.templateBinder,
onEvents, childMode)
: super(_perf, _expando, null, null, onEvents, childMode);
onEvents, bindAttrs, childMode)
: super(_perf, _expando, null, null, onEvents, bindAttrs, childMode);

String toString() => "[TemplateElementBinder template:$template]";

Expand All @@ -40,6 +40,7 @@ class ElementBinder {
final Profiler _perf;
final Expando _expando;
final Map onEvents;
final Map bindAttrs;

// Member fields
final decorators;
Expand All @@ -50,7 +51,7 @@ class ElementBinder {
final String childMode;

ElementBinder(this._perf, this._expando, this.component, this.decorators, this.onEvents,
this.childMode);
this.bindAttrs, this.childMode);

final bool hasTemplate = false;

Expand Down
17 changes: 10 additions & 7 deletions lib/core_dom/element_binder_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class ElementBinderFactory {
// TODO: Optimize this to re-use a builder.
ElementBinderBuilder builder() => new ElementBinderBuilder(this, _parser);

ElementBinder binder(component, decorators, onEvents, childMode) =>
new ElementBinder(_perf, _expando, component, decorators, onEvents, childMode);
TemplateElementBinder templateBinder(template, transclude, onEvents, childMode) =>
new TemplateElementBinder(_perf, _expando, template, transclude, onEvents, childMode);
ElementBinder binder(ElementBinderBuilder b) =>
new ElementBinder(_perf, _expando,
b.component, b.decorators, b.onEvents, b.bindAttrs, b.childMode);
TemplateElementBinder templateBinder(ElementBinderBuilder b, ElementBinder transclude) =>
new TemplateElementBinder(_perf, _expando,
b.template, transclude, b.onEvents, b.bindAttrs, b.childMode);
}

/**
Expand All @@ -26,6 +28,7 @@ class ElementBinderBuilder {
final Parser _parser;

final onEvents = <String, String>{};
final bindAttrs = <String, String>{};

var decorators = <DirectiveRef>[];
DirectiveRef template;
Expand Down Expand Up @@ -166,11 +169,11 @@ class ElementBinderBuilder {

ElementBinder get binder {
if (template != null) {
var transclude = _factory.binder(component, decorators, onEvents, childMode);
return _factory.templateBinder(template, transclude, onEvents, childMode);
var transclude = _factory.binder(this);
return _factory.templateBinder(this, transclude);

} else {
return _factory.binder(component, decorators, onEvents, childMode);
return _factory.binder(this);
}

}
Expand Down
6 changes: 6 additions & 0 deletions lib/core_dom/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,15 @@ class DirectiveSelector {

// Select [attributes]
element.attributes.forEach((attrName, value) {

if (attrName.startsWith("on-")) {
builder.onEvents[attrName] = value;
}

if (attrName.startsWith("bind-")) {
builder.bindAttrs[attrName] = value;
}

attrs[attrName] = value;
for (var k = 0; k < attrSelector.length; k++) {
_ContainsSelector selectorRegExp = attrSelector[k];
Expand Down
16 changes: 10 additions & 6 deletions test/core_dom/selector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ main() {
]));
});


it('should match directive on [attribute]', () {
expect(selector(element = e('<div directive=abc></div>')),
toEqualsDirectiveInfos([
Expand All @@ -93,7 +92,6 @@ main() {
"name": 'directive' }]));
});


it('should match directive on element[attribute]', () {
expect(selector(element = e('<b directive=abc></b>')),
toEqualsDirectiveInfos([
Expand All @@ -103,7 +101,6 @@ main() {
]));
});


it('should match directive on [attribute=value]', () {
expect(selector(element = e('<div directive=value></div>')),
toEqualsDirectiveInfos([
Expand All @@ -112,7 +109,6 @@ main() {
]));
});


it('should match directive on element[attribute=value]', () {
expect(selector(element = e('<b directive=value></div>')),
toEqualsDirectiveInfos([
Expand Down Expand Up @@ -140,8 +136,6 @@ main() {
]));
});



it('should sort by priority', () {
TemplateElementBinder eb = selector(element = e(
'<component attribute ignore-children structural></component>'));
Expand Down Expand Up @@ -195,6 +189,16 @@ main() {
{ "selector": '[two-directives]', "value": '', "element": element}
]));
});

it('should collect on-* attributes', () {
ElementBinder binder = selector(e('<input on-click="foo" on-blah="fad"></input>'));
expect(binder.onEvents).toEqual({'on-click': 'foo', 'on-blah': 'fad'});
});

it('should collect bind-* attributes', () {
ElementBinder binder = selector(e('<input bind-x="y" bind-z="yy"></input>'));
expect(binder.bindAttrs).toEqual({'bind-x': 'y', 'bind-z': 'yy'});
});
});

describe('matchText', () {
Expand Down

0 comments on commit 4707826

Please sign in to comment.