Skip to content

Commit 4fcbc3c

Browse files
committed
feat(compiler): Pre-compile Scope.watch ASTs for attribute mustaches
For dart-archive#1086
1 parent 573eb28 commit 4fcbc3c

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

lib/core_dom/element_binder.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ class ElementBinder {
254254
if (nodesAttrsDirectives.isEmpty) {
255255
nodeModule.bind(AttrMustache, toFactory: (Injector injector) {
256256
var scope = injector.getByKey(_SCOPE_KEY);
257-
var interpolate = injector.getByKey(_INTERPOLATE_KEY);
258257
for (var ref in nodesAttrsDirectives) {
259-
new AttrMustache(nodeAttrs, ref.value, interpolate, scope,
260-
injector.getByKey(_FORMATTER_MAP_KEY));
258+
new AttrMustache(nodeAttrs, ref.value, ref.valueAST, scope);
261259
}
262260
});
263261
}

lib/core_dom/mustache.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,16 @@ class AttrMustache {
2626

2727
// This Directive is special and does not go through injection.
2828
AttrMustache(this._attrs,
29-
String template,
30-
Interpolate interpolate,
31-
Scope scope,
32-
FormatterMap formatters) {
33-
var eqPos = template.indexOf('=');
34-
_attrName = template.substring(0, eqPos);
35-
String expression = interpolate(template.substring(eqPos + 1));
36-
37-
_updateMarkup('', template);
29+
String this._attrName,
30+
AST valueAST,
31+
Scope scope) {
32+
_updateMarkup('', 'INITIAL-VALUE');
3833

3934
_attrs.listenObserverChanges(_attrName, (hasObservers) {
4035
if (_hasObservers != hasObservers) {
4136
_hasObservers = hasObservers;
4237
if (_watch != null) _watch.remove();
43-
_watch = scope.watch(expression, _updateMarkup, formatters: formatters,
38+
_watch = scope.watchAST(valueAST, _updateMarkup,
4439
canChangeModel: _hasObservers);
4540
}
4641
});

lib/core_dom/selector.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ class DirectiveSelector {
9898
// we need to pass the name to the directive by prefixing it to
9999
// the value. Yes it is a bit of a hack.
100100
_directives[selectorRegExp.annotation].forEach((type) {
101+
// Pre-compute the AST to watch this value.
102+
String expression = _interpolate(value);
103+
AST valueAST = _astParser(expression, formatters: _formatters);
104+
101105
builder.addDirective(new DirectiveRef(
102-
node, type, selectorRegExp.annotation, new Key(type), '$attrName=$value'));
106+
node, type, selectorRegExp.annotation, new Key(type), attrName, valueAST));
103107
});
104108
}
105109
}

test/core_dom/selector_spec.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ main() {
123123
it('should match attributes', () {
124124
expect(selector(element = e('<div attr="before-xyz-after"></div>')),
125125
toEqualsDirectiveInfos([
126-
{ "selector": '[*=/xyz/]', "value": 'attr=before-xyz-after',
126+
{ "selector": '[*=/xyz/]',
127+
"value": 'attr',
128+
"ast": '"before-xyz-after"',
127129
"element": element, "name": 'attr'}
128130
]));
129131
});

0 commit comments

Comments
 (0)