| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 /** | |
| 6 * Provides a Map abstraction on top of data-* attributes, similar to the | |
| 7 * dataSet in the old DOM. | |
| 8 */ | |
| 9 class _DataAttributeMap implements AttributeMap { | |
| 10 | |
| 11 final Map<String, String> _attributes; | |
| 12 | |
| 13 _DataAttributeMap(this._attributes); | |
| 14 | |
| 15 // interface Map | |
| 16 | |
| 17 // TODO: Use lazy iterator when it is available on Map. | |
| 18 bool containsValue(String value) => getValues().some((v) => v == value); | |
| 19 | |
| 20 bool containsKey(String key) => _attributes.containsKey(_attr(key)); | |
| 21 | |
| 22 String operator [](String key) => _attributes[_attr(key)]; | |
| 23 | |
| 24 void operator []=(String key, value) { | |
| 25 _attributes[_attr(key)] = '$value'; | |
| 26 } | |
| 27 | |
| 28 String putIfAbsent(String key, String ifAbsent()) { | |
| 29 if (!containsKey(key)) { | |
| 30 return this[key] = ifAbsent(); | |
| 31 } | |
| 32 return this[key]; | |
| 33 } | |
| 34 | |
| 35 String remove(String key) => _attributes.remove(_attr(key)); | |
| 36 | |
| 37 void clear() { | |
| 38 // Needs to operate on a snapshot since we are mutatiting the collection. | |
| 39 for (String key in getKeys()) { | |
| 40 remove(key); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void forEach(void f(String key, String value)) { | |
| 45 _attributes.forEach((String key, String value) { | |
| 46 if (_matches(key)) { | |
| 47 f(_strip(key), value); | |
| 48 } | |
| 49 }); | |
| 50 } | |
| 51 | |
| 52 Collection<String> getKeys() { | |
| 53 final keys = new List<String>(); | |
| 54 _attributes.forEach((String key, String value) { | |
| 55 if (_matches(key)) { | |
| 56 keys.add(_strip(key)); | |
| 57 } | |
| 58 }); | |
| 59 return keys; | |
| 60 } | |
| 61 | |
| 62 Collection<String> getValues() { | |
| 63 final values = new List<String>(); | |
| 64 _attributes.forEach((String key, String value) { | |
| 65 if (_matches(key)) { | |
| 66 values.add(value); | |
| 67 } | |
| 68 }); | |
| 69 return values; | |
| 70 } | |
| 71 | |
| 72 int get length() => getKeys().length; | |
| 73 | |
| 74 // TODO: Use lazy iterator when it is available on Map. | |
| 75 bool isEmpty() => length == 0; | |
| 76 | |
| 77 // Helpers. | |
| 78 String _attr(String key) => 'data-$key'; | |
| 79 bool _matches(String key) => key.startsWith('data-'); | |
| 80 String _strip(String key) => key.substring(5); | |
| 81 } | |
| 82 | |
| OLD | NEW |