Chromium Code Reviews| 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 mdv; | 5 part of mdv; |
| 6 | 6 |
| 7 /** Extensions to the [Element] API. */ | 7 /** Extensions to the [Element] API. */ |
| 8 class _ElementExtension extends _NodeExtension { | 8 class _ElementExtension extends _NodeExtension { |
| 9 _ElementExtension(Element node) : super(node); | 9 _ElementExtension(Element node) : super(node); |
| 10 | 10 |
| 11 Element get node => super.node; | |
| 12 | |
| 13 Map<String, StreamSubscription> _attributeBindings; | |
| 14 | |
| 15 // TODO(jmesserly): should path be optional, and default to empty path? | 11 // TODO(jmesserly): should path be optional, and default to empty path? |
| 16 // It is used that way in at least one path in JS TemplateElement tests | 12 // It is used that way in at least one path in JS TemplateElement tests |
| 17 // (see "BindImperative" test in original JS code). | 13 // (see "BindImperative" test in original JS code). |
| 18 void bind(String name, model, String path) { | 14 NodeBinding createBinding(String name, model, String path) => |
| 19 if (_attributeBindings == null) { | 15 new _AttributeBinding(node, name, model, path); |
| 20 _attributeBindings = new Map<String, StreamSubscription>(); | 16 } |
| 21 } | |
| 22 | 17 |
| 23 var changed; | 18 class _AttributeBinding extends NodeBinding { |
| 24 if (name.endsWith('?')) { | 19 final bool conditional; |
| 20 | |
| 21 _AttributeBinding._(node, name, model, path, this.conditional) | |
| 22 : super(node, name, model, path); | |
| 23 | |
| 24 factory _AttributeBinding(Element node, name, model, path) { | |
| 25 bool conditional = name.endsWith('?'); | |
| 26 if (conditional) { | |
| 25 node.xtag.attributes.remove(name); | 27 node.xtag.attributes.remove(name); |
| 26 name = name.substring(0, name.length - 1); | 28 name = name.substring(0, name.length - 1); |
| 27 | |
| 28 changed = (value) { | |
| 29 if (_toBoolean(value)) { | |
| 30 node.xtag.attributes[name] = ''; | |
| 31 } else { | |
| 32 node.xtag.attributes.remove(name); | |
| 33 } | |
| 34 }; | |
| 35 } else { | |
| 36 changed = (value) { | |
| 37 // TODO(jmesserly): escape value if needed to protect against XSS. | |
| 38 // See https://github.com/polymer-project/mdv/issues/58 | |
| 39 node.xtag.attributes[name] = value == null ? '' : '$value'; | |
| 40 }; | |
| 41 } | 29 } |
| 42 | 30 return new _AttributeBinding._(node, name, model, path, conditional); |
| 43 unbind(name); | |
| 44 | |
| 45 _attributeBindings[name] = new PathObserver(model, path).bindSync(changed); | |
| 46 } | 31 } |
| 47 | 32 |
| 48 void unbind(String name) { | 33 Element get node => super.node; |
| 49 if (_attributeBindings != null) { | |
| 50 var binding = _attributeBindings.remove(name); | |
| 51 if (binding != null) binding.cancel(); | |
| 52 } | |
| 53 } | |
| 54 | 34 |
| 55 void unbindAll() { | 35 void boundValueChanged(value) { |
| 56 if (_attributeBindings != null) { | 36 if (conditional) { |
| 57 for (var binding in _attributeBindings.values) { | 37 if (_toBoolean(value)) { |
| 58 binding.cancel(); | 38 node.xtag.attributes[property] = ''; |
| 39 } else { | |
| 40 node.xtag.attributes.remove(property); | |
| 59 } | 41 } |
| 60 _attributeBindings = null; | 42 } else { |
| 43 // TODO(jmesserly): escape value if needed to protect against XSS. | |
| 44 // See https://github.com/polymer-project/mdv/issues/58 | |
| 45 node.xtag.attributes[property] = sanitizeBoundValue(value); | |
|
Siggi Cherem (dart-lang)
2013/07/25 16:24:12
should that escaping be defined in sanitizeBoundVa
| |
| 61 } | 46 } |
| 62 } | 47 } |
| 63 } | 48 } |
| OLD | NEW |