f68c5edb14
BUG=1877 TEST= Review URL: https://chromiumcodereview.appspot.com//9930008 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6015 260f80e4-7a28-3924-810f-c04153c831b5
83 lines
2.1 KiB
Dart
83 lines
2.1 KiB
Dart
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
/**
|
|
* Provides a Map abstraction on top of data-* attributes, similar to the
|
|
* dataSet in the old DOM.
|
|
*/
|
|
class _DataAttributeMap implements AttributeMap {
|
|
|
|
final Map<String, String> _attributes;
|
|
|
|
_DataAttributeMap(this._attributes);
|
|
|
|
// interface Map
|
|
|
|
// TODO: Use lazy iterator when it is available on Map.
|
|
bool containsValue(String value) => getValues().some((v) => v == value);
|
|
|
|
bool containsKey(String key) => _attributes.containsKey(_attr(key));
|
|
|
|
String operator [](String key) => _attributes[_attr(key)];
|
|
|
|
void operator []=(String key, value) {
|
|
_attributes[_attr(key)] = '$value';
|
|
}
|
|
|
|
String putIfAbsent(String key, String ifAbsent()) {
|
|
if (!containsKey(key)) {
|
|
return this[key] = ifAbsent();
|
|
}
|
|
return this[key];
|
|
}
|
|
|
|
String remove(String key) => _attributes.remove(_attr(key));
|
|
|
|
void clear() {
|
|
// Needs to operate on a snapshot since we are mutatiting the collection.
|
|
for (String key in getKeys()) {
|
|
remove(key);
|
|
}
|
|
}
|
|
|
|
void forEach(void f(String key, String value)) {
|
|
_attributes.forEach((String key, String value) {
|
|
if (_matches(key)) {
|
|
f(_strip(key), value);
|
|
}
|
|
});
|
|
}
|
|
|
|
Collection<String> getKeys() {
|
|
final keys = new List<String>();
|
|
_attributes.forEach((String key, String value) {
|
|
if (_matches(key)) {
|
|
keys.add(_strip(key));
|
|
}
|
|
});
|
|
return keys;
|
|
}
|
|
|
|
Collection<String> getValues() {
|
|
final values = new List<String>();
|
|
_attributes.forEach((String key, String value) {
|
|
if (_matches(key)) {
|
|
values.add(value);
|
|
}
|
|
});
|
|
return values;
|
|
}
|
|
|
|
int get length() => getKeys().length;
|
|
|
|
// TODO: Use lazy iterator when it is available on Map.
|
|
bool isEmpty() => length == 0;
|
|
|
|
// Helpers.
|
|
String _attr(String key) => 'data-$key';
|
|
bool _matches(String key) => key.startsWith('data-');
|
|
String _strip(String key) => key.substring(5);
|
|
}
|
|
|