OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of template_binding; | |
6 | |
7 /** Extensions to the [Element] API. */ | |
8 class _ElementExtension extends NodeBindExtension { | |
9 _ElementExtension(Element node) : super._(node); | |
10 | |
11 bind(String name, value, {bool oneTime: false}) { | |
12 Element node = _node; | |
13 | |
14 if (node is OptionElement && name == 'value') { | |
15 // Note: because <option> can be a semantic template, <option> will be | |
16 // a TemplateBindExtension sometimes. So we need to handle it here. | |
17 node.attributes.remove(name); | |
18 | |
19 if (oneTime) return _updateOption(value); | |
20 _open(value, _updateOption); | |
21 } else { | |
22 bool conditional = name.endsWith('?'); | |
23 if (conditional) { | |
24 node.attributes.remove(name); | |
25 name = name.substring(0, name.length - 1); | |
26 } | |
27 | |
28 if (oneTime) return _updateAttribute(_node, name, conditional, value); | |
29 | |
30 _open(value, (x) => _updateAttribute(_node, name, conditional, x)); | |
31 } | |
32 return _maybeUpdateBindings(name, value); | |
33 } | |
34 | |
35 void _updateOption(newValue) { | |
36 OptionElement node = _node; | |
37 var oldValue = null; | |
38 var selectBinding = null; | |
39 var select = node.parentNode; | |
40 if (select is SelectElement) { | |
41 var bindings = nodeBind(select).bindings; | |
42 if (bindings != null) { | |
43 var valueBinding = bindings['value']; | |
44 if (valueBinding is _InputBinding) { | |
45 selectBinding = valueBinding; | |
46 oldValue = select.value; | |
47 } | |
48 } | |
49 } | |
50 | |
51 node.value = _sanitizeValue(newValue); | |
52 | |
53 if (selectBinding != null && select.value != oldValue) { | |
54 selectBinding.value = select.value; | |
55 } | |
56 } | |
57 } | |
58 | |
59 void _updateAttribute(Element node, String name, bool conditional, value) { | |
60 if (conditional) { | |
61 if (_toBoolean(value)) { | |
62 node.attributes[name] = ''; | |
63 } else { | |
64 node.attributes.remove(name); | |
65 } | |
66 } else { | |
67 // TODO(jmesserly): escape value if needed to protect against XSS. | |
68 // See https://github.com/polymer-project/mdv/issues/58 | |
69 node.attributes[name] = _sanitizeValue(value); | |
70 } | |
71 } | |
OLD | NEW |