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 /** | 5 /** |
6 * This library provides access to the Polymer project's | 6 * This library provides access to the Polymer project's |
7 * [Data Binding](http://www.polymer-project.org/docs/polymer/databinding.html) | 7 * [Data Binding](http://www.polymer-project.org/docs/polymer/databinding.html) |
8 * Find more information at the | 8 * Find more information at the |
9 * [Polymer.dart homepage](https://www.dartlang.org/polymer-dart/). | 9 * [Polymer.dart homepage](https://www.dartlang.org/polymer-dart/). |
10 * | 10 * |
11 * Extends the capabilities of the HTML Template Element by enabling it to | 11 * Extends the capabilities of the HTML Template Element by enabling it to |
12 * create, manage, and remove instances of content bound to data defined in | 12 * create, manage, and remove instances of content bound to data defined in |
13 * Dart. | 13 * Dart. |
14 * | 14 * |
15 * Node.bind() is a new method added to all DOM nodes which instructs them to | 15 * Node.bind() is a new method added to all DOM nodes which instructs them to |
16 * bind the named property to the data provided. These allows applications to | 16 * bind the named property to the data provided. These allows applications to |
17 * create a data model in Dart or JavaScript that DOM reacts to. | 17 * create a data model in Dart or JavaScript that DOM reacts to. |
18 */ | 18 */ |
19 library template_binding; | 19 library template_binding; |
20 | 20 |
21 import 'dart:async'; | 21 import 'dart:async'; |
22 import 'dart:collection'; | 22 import 'dart:collection'; |
23 import 'dart:html'; | 23 import 'dart:html'; |
24 import 'dart:svg' show SvgSvgElement; | 24 import 'dart:svg' show SvgSvgElement; |
25 import 'package:observe/observe.dart'; | 25 import 'package:observe/observe.dart'; |
26 | 26 |
27 import 'src/binding_delegate.dart'; | 27 import 'src/binding_delegate.dart'; |
28 import 'src/node_binding.dart'; | 28 import 'src/mustache_tokens.dart'; |
29 | 29 |
30 export 'src/binding_delegate.dart'; | 30 export 'src/binding_delegate.dart'; |
31 export 'src/node_binding.dart' show NodeBinding; | |
32 | 31 |
33 part 'src/element.dart'; | 32 part 'src/element.dart'; |
34 part 'src/input_bindings.dart'; | 33 part 'src/input_bindings.dart'; |
35 part 'src/input_element.dart'; | 34 part 'src/input_element.dart'; |
36 part 'src/instance_binding_map.dart'; | 35 part 'src/instance_binding_map.dart'; |
37 part 'src/node.dart'; | 36 part 'src/node.dart'; |
38 part 'src/select_element.dart'; | 37 part 'src/select_element.dart'; |
39 part 'src/template.dart'; | 38 part 'src/template.dart'; |
40 part 'src/template_iterator.dart'; | 39 part 'src/template_iterator.dart'; |
41 part 'src/text.dart'; | 40 part 'src/text.dart'; |
42 part 'src/text_area_element.dart'; | 41 part 'src/text_area_element.dart'; |
43 | 42 |
44 // TODO(jmesserly): ideally we would split TemplateBinding and Node.bind into | 43 // TODO(jmesserly): ideally we would split TemplateBinding and Node.bind into |
45 // two packages, but this is not easy when we are faking extension methods. | 44 // two packages, but this is not easy when we are faking extension methods. |
46 // Since TemplateElement needs to override Node.bind, it seems like the | 45 // Since TemplateElement needs to override Node.bind, it seems like the |
47 // Node.bind layer must have some innate knowledge of TemplateBinding. | 46 // Node.bind layer must have some innate knowledge of TemplateBinding. |
| 47 // NOTE: I've heard NodeBind might become an internal API, which is all the more |
| 48 // reason to have it in this package. |
48 | 49 |
49 /** | 50 /** |
50 * Provides access to the data binding APIs for the [node]. For example: | 51 * Provides access to the data binding APIs for the [node]. For example: |
51 * | 52 * |
52 * templateBind(node).model = new MyModel(); | 53 * templateBind(node).model = new MyModel(); |
53 * | 54 * |
54 * This is equivalent to [nodeBind], but provides access to | 55 * This is equivalent to [nodeBind], but provides access to |
55 * [TemplateBindExtension] APIs. [node] should be a [TemplateElement], or | 56 * [TemplateBindExtension] APIs. [node] should be a [TemplateElement], or |
56 * equivalent semantic template such as: | 57 * equivalent semantic template such as: |
57 * | 58 * |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } | 130 } |
130 | 131 |
131 _expando[node] = extension; | 132 _expando[node] = extension; |
132 return extension; | 133 return extension; |
133 } | 134 } |
134 | 135 |
135 | 136 |
136 bool _isAttributeTemplate(Element n) => n.attributes.containsKey('template') && | 137 bool _isAttributeTemplate(Element n) => n.attributes.containsKey('template') && |
137 _SEMANTIC_TEMPLATE_TAGS.containsKey(n.localName); | 138 _SEMANTIC_TEMPLATE_TAGS.containsKey(n.localName); |
138 | 139 |
| 140 bool _isSvgTemplate(Element el) => el.tagName == 'template' && |
| 141 el.namespaceUri == 'http://www.w3.org/2000/svg'; |
| 142 |
| 143 bool _isHtmlTemplate(Element el) => el.tagName == 'TEMPLATE' && |
| 144 el.namespaceUri == 'http://www.w3.org/1999/xhtml'; |
| 145 |
139 /** | 146 /** |
140 * Returns true if this node is semantically a template. | 147 * Returns true if this node is semantically a template. |
141 * | 148 * |
142 * A node is a template if [tagName] is TEMPLATE, or the node has the | 149 * A node is a template if [tagName] is TEMPLATE, or the node has the |
143 * 'template' attribute and this tag supports attribute form for backwards | 150 * 'template' attribute and this tag supports attribute form for backwards |
144 * compatibility with existing HTML parsers. The nodes that can use attribute | 151 * compatibility with existing HTML parsers. The nodes that can use attribute |
145 * form are table elments (THEAD, TBODY, TFOOT, TH, TR, TD, CAPTION, COLGROUP | 152 * form are table elments (THEAD, TBODY, TFOOT, TH, TR, TD, CAPTION, COLGROUP |
146 * and COL), OPTION, and OPTGROUP. | 153 * and COL), OPTION, and OPTGROUP. |
147 */ | 154 */ |
148 bool isSemanticTemplate(Node n) => n is Element && | 155 bool isSemanticTemplate(Node n) => n is Element && |
149 (n.localName == 'template' || _isAttributeTemplate(n)); | 156 (_isHtmlTemplate(n) || _isAttributeTemplate(n) || _isSvgTemplate(n)); |
150 | 157 |
151 // TODO(jmesserly): const set would be better | 158 // TODO(jmesserly): const set would be better |
152 const _SEMANTIC_TEMPLATE_TAGS = const { | 159 const _SEMANTIC_TEMPLATE_TAGS = const { |
153 'caption': null, | 160 'caption': null, |
154 'col': null, | 161 'col': null, |
155 'colgroup': null, | 162 'colgroup': null, |
156 'option': null, | 163 'option': null, |
157 'optgroup': null, | 164 'optgroup': null, |
158 'tbody': null, | 165 'tbody': null, |
159 'td': null, | 166 'td': null, |
160 'tfoot': null, | 167 'tfoot': null, |
161 'th': null, | 168 'th': null, |
162 'thead': null, | 169 'thead': null, |
163 'tr': null, | 170 'tr': null, |
164 }; | 171 }; |
165 | 172 |
166 | 173 |
167 // TODO(jmesserly): investigate if expandos give us enough performance. | 174 // TODO(jmesserly): investigate if expandos give us enough performance. |
168 | 175 |
169 // The expando for storing our MDV extensions. | 176 // The expando for storing our MDV extensions. |
170 // | 177 // |
171 // In general, we need state associated with the nodes. Rather than having a | 178 // In general, we need state associated with the nodes. Rather than having a |
172 // bunch of individual expandos, we keep one per node. | 179 // bunch of individual expandos, we keep one per node. |
173 // | 180 // |
174 // Aside from the potentially helping performance, it also keeps things simpler | 181 // Aside from the potentially helping performance, it also keeps things simpler |
175 // if we decide to integrate MDV into the DOM later, and means less code needs | 182 // if we decide to integrate MDV into the DOM later, and means less code needs |
176 // to worry about expandos. | 183 // to worry about expandos. |
177 final Expando _expando = new Expando('template_binding'); | 184 final Expando _expando = new Expando('template_binding'); |
OLD | NEW |