| 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 // TODO(jmesserly): should path be optional, and default to empty path? | 11 NodeBinding bind(String name, model, [String path]) { |
| 12 // It is used that way in at least one path in JS TemplateElement tests | 12 _self.unbind(name); |
| 13 // (see "BindImperative" test in original JS code). | 13 |
| 14 NodeBinding createBinding(String name, model, String path) => | 14 var binding; |
| 15 new _AttributeBinding(_node, name, model, path); | 15 if (_node is OptionElement && name == 'value') { |
| 16 // Note: because <option> can be a semantic template, <option> will be |
| 17 // a TemplateBindExtension sometimes. So we need to handle it here. |
| 18 _node.attributes.remove(name); |
| 19 binding = new _OptionValueBinding(_node, model, path); |
| 20 } else { |
| 21 binding = new _AttributeBinding(_node, name, model, path); |
| 22 } |
| 23 return bindings[name] = binding; |
| 24 } |
| 16 } | 25 } |
| 17 | 26 |
| 18 class _AttributeBinding extends NodeBinding { | 27 class _AttributeBinding extends NodeBinding { |
| 19 final bool conditional; | 28 final bool conditional; |
| 20 | 29 |
| 21 _AttributeBinding._(node, name, model, path, this.conditional) | 30 _AttributeBinding._(node, name, model, path, this.conditional) |
| 22 : super(node, name, model, path); | 31 : super(node, name, model, path); |
| 23 | 32 |
| 24 factory _AttributeBinding(Element node, name, model, path) { | 33 factory _AttributeBinding(Element node, name, model, path) { |
| 25 bool conditional = name.endsWith('?'); | 34 bool conditional = name.endsWith('?'); |
| 26 if (conditional) { | 35 if (conditional) { |
| 27 node.attributes.remove(name); | 36 node.attributes.remove(name); |
| 28 name = name.substring(0, name.length - 1); | 37 name = name.substring(0, name.length - 1); |
| 29 } | 38 } |
| 30 return new _AttributeBinding._(node, name, model, path, conditional); | 39 return new _AttributeBinding._(node, name, model, path, conditional); |
| 31 } | 40 } |
| 32 | 41 |
| 33 Element get node => super.node; | 42 Element get node => super.node; |
| 34 | 43 |
| 35 void boundValueChanged(value) { | 44 void valueChanged(value) { |
| 36 if (conditional) { | 45 if (conditional) { |
| 37 if (_toBoolean(value)) { | 46 if (_toBoolean(value)) { |
| 38 node.attributes[property] = ''; | 47 node.attributes[property] = ''; |
| 39 } else { | 48 } else { |
| 40 node.attributes.remove(property); | 49 node.attributes.remove(property); |
| 41 } | 50 } |
| 42 } else { | 51 } else { |
| 43 // TODO(jmesserly): escape value if needed to protect against XSS. | 52 // TODO(jmesserly): escape value if needed to protect against XSS. |
| 44 // See https://github.com/polymer-project/mdv/issues/58 | 53 // See https://github.com/polymer-project/mdv/issues/58 |
| 45 node.attributes[property] = sanitizeBoundValue(value); | 54 node.attributes[property] = sanitizeBoundValue(value); |
| 46 } | 55 } |
| 47 } | 56 } |
| 48 } | 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; |
| 66 var selectBinding = null; |
| 67 var select = node.parent; |
| 68 if (select is SelectElement) { |
| 69 var valueBinding = nodeBind(select).bindings['value']; |
| 70 if (valueBinding is _SelectBinding) { |
| 71 selectBinding = valueBinding; |
| 72 oldValue = select.value; |
| 73 } |
| 74 } |
| 75 |
| 76 super.valueChanged(newValue); |
| 77 |
| 78 if (selectBinding != null && !selectBinding.closed && |
| 79 select.value != oldValue) { |
| 80 selectBinding.nodeValueChanged(null); |
| 81 } |
| 82 } |
| 83 } |
| OLD | NEW |