Add Element data property

BUG=None
TEST=None

Review URL: https://chromereviews.googleplex.com/3512017

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@71 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
arv@google.com 2011-10-05 20:40:39 +00:00
parent 02fc5cc91f
commit 1e650a0e64
4 changed files with 104 additions and 2 deletions

View file

@ -549,6 +549,7 @@
#source('src/BodyElement.dart');
#source('src/BodyElementWrappingImplementation.dart');
#source('src/CssClassSet.dart');
#source('src/DataMap.dart');
#source('src/DocumentFragment.dart');
#source('src/DocumentFragmentWrappingImplementation.dart');
#source('src/DocumentWrappingImplementation.dart');

View file

@ -0,0 +1,82 @@
// 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 _DataMap implements Map<String, String> {
final Map<String, String> _attributes;
_DataMap(this._attributes);
// interface Map
// TODO: Use lazy iterator when it is available on Map.
bool containsValue(String value) => getValues().indexOf(value) != -1;
bool containsKey(String key) => _attributes.containsKey(_attr(key));
String operator [](String key) => _attributes[_attr(key)];
void operator []=(String key, String 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.substringToEnd(5);
}

View file

@ -84,6 +84,9 @@ interface Element extends Node /*, common.NodeSelector, common.ElementTraversal
// TODO: The type of value should be Collection<String>. See http://b/5392897
void set classes(value);
Map<String, String> get data();
void set data(Map<String, String> value);
int get clientHeight();
int get clientLeft();

View file

@ -283,8 +283,8 @@ class ElementAttributeMap implements Map<String, String> {
void clear() {
final attributes = _element.attributes;
for (int i = 0, len = attributes.length; i < len; i++) {
_element.removeAttribute(attributes[0].name);
for (int i = len = attributes.length - 1; i >= 0; i--) {
_element.removeAttribute(attributes.item(i).name);
}
}
@ -402,6 +402,7 @@ class ElementWrappingImplementation extends NodeWrappingImplementation implement
ElementWrappingImplementation._wrap(ptr) : super._wrap(ptr);
_CssClassSet _cssClassSet;
_DataMap _data;
Map<String, String> get attributes() {
return new ElementAttributeMap._wrap(_ptr);
@ -422,6 +423,21 @@ class ElementWrappingImplementation extends NodeWrappingImplementation implement
classSet.addAll(value);
}
Map<String, String> get data() {
if (_data === null) {
_data = new _DataMap(attributes);
}
return _data;
}
void set data(Map<String, String> value) {
Map<String, String> data = this.data;
data.clear();
for (String key in value.getKeys()) {
data[key] = value[key];
}
}
int get clientHeight() => _ptr.clientHeight;
int get clientLeft() => _ptr.clientLeft;