OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of template_binding; | 5 part of template_binding; |
6 | 6 |
7 // This code is a port of what was formerly known as Model-Driven-Views, now | 7 // This code is a port of what was formerly known as Model-Driven-Views, now |
8 // located at: | 8 // located at: |
9 // https://github.com/polymer/TemplateBinding | 9 // https://github.com/polymer/TemplateBinding |
10 // https://github.com/polymer/NodeBind | 10 // https://github.com/polymer/NodeBind |
11 | 11 |
12 // TODO(jmesserly): not sure what kind of boolean conversion rules to | 12 // TODO(jmesserly): not sure what kind of boolean conversion rules to |
13 // apply for template data-binding. HTML attributes are true if they're | 13 // apply for template data-binding. HTML attributes are true if they're |
14 // present. However Dart only treats "true" as true. Since this is HTML we'll | 14 // present. However Dart only treats "true" as true. Since this is HTML we'll |
15 // use something closer to the HTML rules: null (missing) and false are false, | 15 // use something closer to the HTML rules: null (missing) and false are false, |
16 // everything else is true. | 16 // everything else is true. |
17 // See: https://github.com/polymer/TemplateBinding/issues/59 | 17 // See: https://github.com/polymer/TemplateBinding/issues/59 |
18 bool _toBoolean(value) => null != value && false != value; | 18 bool _toBoolean(value) => null != value && false != value; |
19 | 19 |
| 20 // Dart note: this was added to decouple the MustacheTokens.parse function from |
| 21 // the rest of tempalte_binding. |
| 22 _getDelegateFactory(name, node, delegate) { |
| 23 if (delegate == null) return null; |
| 24 return (pathString) => delegate.prepareBinding(pathString, name, node); |
| 25 } |
| 26 |
20 _InstanceBindingMap _getBindings(Node node, BindingDelegate delegate) { | 27 _InstanceBindingMap _getBindings(Node node, BindingDelegate delegate) { |
21 if (node is Element) { | 28 if (node is Element) { |
22 return _parseAttributeBindings(node, delegate); | 29 return _parseAttributeBindings(node, delegate); |
23 } | 30 } |
24 | 31 |
25 if (node is Text) { | 32 if (node is Text) { |
26 var tokens = MustacheTokens.parse(node.text, 'text', node, delegate); | 33 var tokens = MustacheTokens.parse(node.text, |
| 34 _getDelegateFactory('text', node, delegate)); |
27 if (tokens != null) return new _InstanceBindingMap(['text', tokens]); | 35 if (tokens != null) return new _InstanceBindingMap(['text', tokens]); |
28 } | 36 } |
29 | 37 |
30 return null; | 38 return null; |
31 } | 39 } |
32 | 40 |
33 void _addBindings(Node node, model, [BindingDelegate delegate]) { | 41 void _addBindings(Node node, model, [BindingDelegate delegate]) { |
34 final bindings = _getBindings(node, delegate); | 42 final bindings = _getBindings(node, delegate); |
35 if (bindings != null) { | 43 if (bindings != null) { |
36 _processBindings(node, bindings, model); | 44 _processBindings(node, bindings, model); |
37 } | 45 } |
38 | 46 |
39 for (var c = node.firstChild; c != null; c = c.nextNode) { | 47 for (var c = node.firstChild; c != null; c = c.nextNode) { |
40 _addBindings(c, model, delegate); | 48 _addBindings(c, model, delegate); |
41 } | 49 } |
42 } | 50 } |
43 | 51 |
44 MustacheTokens _parseWithDefault(Element element, String name, | 52 MustacheTokens _parseWithDefault(Element element, String name, |
45 BindingDelegate delegate) { | 53 BindingDelegate delegate) { |
46 | 54 |
47 var v = element.attributes[name]; | 55 var v = element.attributes[name]; |
48 if (v == '') v = '{{}}'; | 56 if (v == '') v = '{{}}'; |
49 return MustacheTokens.parse(v, name, element, delegate); | 57 return MustacheTokens.parse(v, _getDelegateFactory(name, element, delegate)); |
50 } | 58 } |
51 | 59 |
52 _InstanceBindingMap _parseAttributeBindings(Element element, | 60 _InstanceBindingMap _parseAttributeBindings(Element element, |
53 BindingDelegate delegate) { | 61 BindingDelegate delegate) { |
54 | 62 |
55 var bindings = null; | 63 var bindings = null; |
56 var ifFound = false; | 64 var ifFound = false; |
57 var bindFound = false; | 65 var bindFound = false; |
58 var isTemplateNode = isSemanticTemplate(element); | 66 var isTemplateNode = isSemanticTemplate(element); |
59 | 67 |
60 element.attributes.forEach((name, value) { | 68 element.attributes.forEach((name, value) { |
61 // Allow bindings expressed in attributes to be prefixed with underbars. | 69 // Allow bindings expressed in attributes to be prefixed with underbars. |
62 // We do this to allow correct semantics for browsers that don't implement | 70 // We do this to allow correct semantics for browsers that don't implement |
63 // <template> where certain attributes might trigger side-effects -- and | 71 // <template> where certain attributes might trigger side-effects -- and |
64 // for IE which sanitizes certain attributes, disallowing mustache | 72 // for IE which sanitizes certain attributes, disallowing mustache |
65 // replacements in their text. | 73 // replacements in their text. |
66 while (name[0] == '_') { | 74 while (name[0] == '_') { |
67 name = name.substring(1); | 75 name = name.substring(1); |
68 } | 76 } |
69 | 77 |
70 if (isTemplateNode && | 78 if (isTemplateNode && |
71 (name == 'bind' || name == 'if' || name == 'repeat')) { | 79 (name == 'bind' || name == 'if' || name == 'repeat')) { |
72 return; | 80 return; |
73 } | 81 } |
74 | 82 |
75 var tokens = MustacheTokens.parse(value, name, element, delegate); | 83 var tokens = MustacheTokens.parse(value, |
| 84 _getDelegateFactory(name, element, delegate)); |
76 if (tokens != null) { | 85 if (tokens != null) { |
77 if (bindings == null) bindings = []; | 86 if (bindings == null) bindings = []; |
78 bindings..add(name)..add(tokens); | 87 bindings..add(name)..add(tokens); |
79 } | 88 } |
80 }); | 89 }); |
81 | 90 |
82 if (isTemplateNode) { | 91 if (isTemplateNode) { |
83 if (bindings == null) bindings = []; | 92 if (bindings == null) bindings = []; |
84 var result = new _TemplateBindingMap(bindings) | 93 var result = new _TemplateBindingMap(bindings) |
85 .._if = _parseWithDefault(element, 'if', delegate) | 94 .._if = _parseWithDefault(element, 'if', delegate) |
86 .._bind = _parseWithDefault(element, 'bind', delegate) | 95 .._bind = _parseWithDefault(element, 'bind', delegate) |
87 .._repeat = _parseWithDefault(element, 'repeat', delegate); | 96 .._repeat = _parseWithDefault(element, 'repeat', delegate); |
88 | 97 |
89 // Treat <template if> as <template bind if> | 98 // Treat <template if> as <template bind if> |
90 if (result._if != null && result._bind == null && result._repeat == null) { | 99 if (result._if != null && result._bind == null && result._repeat == null) { |
91 result._bind = MustacheTokens.parse('{{}}', 'bind', element, delegate); | 100 result._bind = MustacheTokens.parse('{{}}', |
| 101 _getDelegateFactory('bind', element, delegate)); |
92 } | 102 } |
93 | 103 |
94 return result; | 104 return result; |
95 } | 105 } |
96 | 106 |
97 return bindings == null ? null : new _InstanceBindingMap(bindings); | 107 return bindings == null ? null : new _InstanceBindingMap(bindings); |
98 } | 108 } |
99 | 109 |
100 _processOneTimeBinding(String name, MustacheTokens tokens, Node node, model) { | 110 _processOneTimeBinding(String name, MustacheTokens tokens, Node node, model) { |
101 | 111 |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 _closed = true; | 543 _closed = true; |
534 } | 544 } |
535 } | 545 } |
536 | 546 |
537 // Dart note: the JavaScript version just puts an expando on the array. | 547 // Dart note: the JavaScript version just puts an expando on the array. |
538 class _BoundNodes { | 548 class _BoundNodes { |
539 final List<Node> nodes; | 549 final List<Node> nodes; |
540 final List<Bindable> instanceBindings; | 550 final List<Bindable> instanceBindings; |
541 _BoundNodes(this.nodes, this.instanceBindings); | 551 _BoundNodes(this.nodes, this.instanceBindings); |
542 } | 552 } |
OLD | NEW |