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 /** Extensions to the [Element] API. */ | 7 /** Extensions to the [Element] API. */ |
8 class _ElementExtension extends NodeBindExtension { | 8 class _ElementExtension extends NodeBindExtension { |
9 _ElementExtension(Element node) : super._(node); | 9 _ElementExtension(Element node) : super._(node); |
10 | 10 |
11 NodeBinding bind(String name, model, [String path]) { | 11 bind(String name, value, {bool oneTime: false}) { |
justinfagnani
2014/02/04 20:16:08
what's the point of returning the value? can we an
Jennifer Messerly
2014/02/04 23:00:31
This is a question for https://github.com/polymer/
justinfagnani
2014/02/04 23:34:49
Yeah... I'm not sure I like that part of the style
Jennifer Messerly
2014/02/04 23:40:41
yeah, I try to use "void" for that, at least in pu
| |
12 _self.unbind(name); | 12 _self.unbind(name); |
13 | 13 |
14 var binding; | 14 Element node = _node; |
justinfagnani
2014/02/04 20:16:08
This won't be necessary if you also add a check fo
Jennifer Messerly
2014/02/04 23:00:31
sure, but since node is always guaranteed to be an
| |
15 if (_node is OptionElement && name == 'value') { | 15 |
16 if (node is OptionElement && name == 'value') { | |
16 // Note: because <option> can be a semantic template, <option> will be | 17 // Note: because <option> can be a semantic template, <option> will be |
17 // a TemplateBindExtension sometimes. So we need to handle it here. | 18 // a TemplateBindExtension sometimes. So we need to handle it here. |
18 (_node as OptionElement).attributes.remove(name); | 19 node.attributes.remove(name); |
19 binding = new _OptionValueBinding(_node, model, path); | 20 |
21 if (oneTime) return _updateOption(value); | |
22 _open(value, _updateOption); | |
20 } else { | 23 } else { |
21 binding = new _AttributeBinding(_node, name, model, path); | 24 bool conditional = name.endsWith('?'); |
25 if (conditional) { | |
26 node.attributes.remove(name); | |
justinfagnani
2014/02/04 20:16:08
.attributes will fail if Node isn't an Element. Ad
Jennifer Messerly
2014/02/04 23:00:31
it should not be possible. See the type check on t
| |
27 name = name.substring(0, name.length - 1); | |
28 } | |
29 | |
30 if (oneTime) return _updateAttribute(_node, name, conditional, value); | |
31 | |
32 _open(value, (x) => _updateAttribute(_node, name, conditional, x)); | |
22 } | 33 } |
23 return bindings[name] = binding; | 34 return bindings[name] = value; |
24 } | |
25 } | |
26 | |
27 class _AttributeBinding extends NodeBinding { | |
28 final bool conditional; | |
29 | |
30 _AttributeBinding._(node, name, model, path, this.conditional) | |
31 : super(node, name, model, path); | |
32 | |
33 factory _AttributeBinding(Element node, name, model, path) { | |
34 bool conditional = name.endsWith('?'); | |
35 if (conditional) { | |
36 node.attributes.remove(name); | |
37 name = name.substring(0, name.length - 1); | |
38 } | |
39 return new _AttributeBinding._(node, name, model, path, conditional); | |
40 } | 35 } |
41 | 36 |
42 Element get node => super.node; | 37 void _updateOption(newValue) { |
43 | 38 OptionElement node = _node; |
44 void valueChanged(value) { | |
45 if (conditional) { | |
46 if (_toBoolean(value)) { | |
47 node.attributes[property] = ''; | |
48 } else { | |
49 node.attributes.remove(property); | |
50 } | |
51 } else { | |
52 // TODO(jmesserly): escape value if needed to protect against XSS. | |
53 // See https://github.com/polymer-project/mdv/issues/58 | |
54 node.attributes[property] = sanitizeBoundValue(value); | |
55 } | |
56 } | |
57 } | |
58 | |
59 class _OptionValueBinding extends _ValueBinding { | |
60 _OptionValueBinding(node, model, path) : super(node, model, path); | |
61 | |
62 OptionElement get node => super.node; | |
63 | |
64 void valueChanged(newValue) { | |
65 var oldValue = null; | 39 var oldValue = null; |
66 var selectBinding = null; | 40 var selectBinding = null; |
67 var select = node.parent; | 41 var select = node.parentNode; |
68 if (select is SelectElement) { | 42 if (select is SelectElement) { |
69 var valueBinding = nodeBind(select).bindings['value']; | 43 var valueBinding = nodeBind(select).bindings['value']; |
70 if (valueBinding is _SelectBinding) { | 44 if (valueBinding is _InputBinding) { |
71 selectBinding = valueBinding; | 45 selectBinding = valueBinding; |
72 oldValue = select.value; | 46 oldValue = select.value; |
73 } | 47 } |
74 } | 48 } |
75 | 49 |
76 super.valueChanged(newValue); | 50 node.value = _sanitizeValue(newValue); |
77 | 51 |
78 if (selectBinding != null && !selectBinding.closed && | 52 if (selectBinding != null && select.value != oldValue) { |
79 select.value != oldValue) { | 53 selectBinding.value = select.value; |
80 selectBinding.nodeValueChanged(null); | |
81 } | 54 } |
82 } | 55 } |
83 } | 56 } |
57 | |
58 void _updateAttribute(Element node, String name, bool conditional, value) { | |
59 if (conditional) { | |
60 if (_toBoolean(value)) { | |
61 node.attributes[name] = ''; | |
62 } else { | |
63 node.attributes.remove(name); | |
64 } | |
65 } else { | |
66 // TODO(jmesserly): escape value if needed to protect against XSS. | |
67 // See https://github.com/polymer-project/mdv/issues/58 | |
68 node.attributes[name] = _sanitizeValue(value); | |
69 } | |
70 } | |
OLD | NEW |