| 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 mdv; |
| 6 |
| 7 /** Extensions to the [Element] API. */ |
| 8 class _ElementExtension extends _NodeExtension { |
| 9 _ElementExtension(Element node) : super(node); |
| 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? |
| 16 // It is used that way in at least one path in JS TemplateElement tests |
| 17 // (see "BindImperative" test in original JS code). |
| 18 void bind(String name, model, String path) { |
| 19 if (_attributeBindings == null) { |
| 20 _attributeBindings = new Map<String, StreamSubscription>(); |
| 21 } |
| 22 |
| 23 node.xtag.attributes.remove(name); |
| 24 |
| 25 var changed; |
| 26 if (name.endsWith('?')) { |
| 27 name = name.substring(0, name.length - 1); |
| 28 |
| 29 changed = (value) { |
| 30 if (_Bindings._toBoolean(value)) { |
| 31 node.xtag.attributes[name] = ''; |
| 32 } else { |
| 33 node.xtag.attributes.remove(name); |
| 34 } |
| 35 }; |
| 36 } else { |
| 37 changed = (value) { |
| 38 // TODO(jmesserly): escape value if needed to protect against XSS. |
| 39 // See https://github.com/polymer-project/mdv/issues/58 |
| 40 node.xtag.attributes[name] = value == null ? '' : '$value'; |
| 41 }; |
| 42 } |
| 43 |
| 44 unbind(name); |
| 45 |
| 46 _attributeBindings[name] = |
| 47 new PathObserver(model, path).bindSync(changed); |
| 48 } |
| 49 |
| 50 void unbind(String name) { |
| 51 if (_attributeBindings != null) { |
| 52 var binding = _attributeBindings.remove(name); |
| 53 if (binding != null) binding.cancel(); |
| 54 } |
| 55 } |
| 56 |
| 57 void unbindAll() { |
| 58 if (_attributeBindings != null) { |
| 59 for (var binding in _attributeBindings.values) { |
| 60 binding.cancel(); |
| 61 } |
| 62 _attributeBindings = null; |
| 63 } |
| 64 } |
| 65 } |
| OLD | NEW |