Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: sdk/lib/html/src/AttributeMap.dart

Issue 11348111: Adding support for accessing attributes in alternate namespaces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporating review feedback. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 html;
6
7 abstract class _AttributeMap implements Map<String, String> {
8
9 bool containsValue(String value) {
10 for (var v in this.values) {
11 if (value == v) {
12 return true;
13 }
14 }
15 return false;
16 }
17
18 String putIfAbsent(String key, String ifAbsent()) {
19 if (!containsKey(key)) {
20 this[key] = ifAbsent();
21 }
22 return this[key];
23 }
24
25 void clear() {
26 for (var key in keys) {
27 remove(key);
28 }
29 }
30
31 void forEach(void f(String key, String value)) {
32 for (var key in keys) {
33 var value = this[key];
34 f(key, value);
35 }
36 }
37
38 Collection<String> get keys {
39 // TODO: generate a lazy collection instead.
40 var attributes = _element.$dom_attributes;
41 var keys = new List<String>();
42 for (int i = 0, len = attributes.length; i < len; i++) {
43 if (_matches(attributes[i])) {
44 keys.add(attributes[i].$dom_localName);
45 }
46 }
47 return keys;
48 }
49
50 Collection<String> get values {
51 // TODO: generate a lazy collection instead.
52 var attributes = _element.$dom_attributes;
53 var values = new List<String>();
54 for (int i = 0, len = attributes.length; i < len; i++) {
55 if (_matches(attributes[i])) {
56 values.add(attributes[i].value);
57 }
58 }
59 return values;
60 }
61
62 /**
63 * Returns true if there is no {key, value} pair in the map.
64 */
65 bool get isEmpty {
66 return length == 0;
67 }
68
69 /**
70 * Checks to see if the node should be included in this map.
71 */
72 bool _matches(Node node);
73 }
74
75 /**
76 * Wrapper to expose [Element.attributes] as a typed map.
77 */
78 class _ElementAttributeMap extends _AttributeMap {
79
80 final Element _element;
81
82 _ElementAttributeMap(this._element);
83
84 bool containsKey(String key) {
85 return _element.$dom_hasAttribute(key);
86 }
87
88 String operator [](String key) {
89 return _element.$dom_getAttribute(key);
90 }
91
92 void operator []=(String key, value) {
93 _element.$dom_setAttribute(key, '$value');
94 }
95
96 String remove(String key) {
97 String value = _element.$dom_getAttribute(key);
98 _element.$dom_removeAttribute(key);
99 return value;
100 }
101
102 /**
103 * The number of {key, value} pairs in the map.
104 */
105 int get length {
106 return keys.length;
107 }
108
109 bool _matches(Node node) => node.$dom_namespaceURI == null;
110 }
111
112 /**
113 * Wrapper to expose namespaced attributes as a typed map.
114 */
115 class _NamespacedAttributeMap extends _AttributeMap {
116
117 final Element _element;
118 final String _namespace;
119
120 _NamespacedAttributeMap(this._element, this._namespace);
121
122 bool containsKey(String key) {
123 return _element.$dom_hasAttributeNS(_namespace, key);
124 }
125
126 String operator [](String key) {
127 return _element.$dom_getAttributeNS(_namespace, key);
128 }
129
130 void operator []=(String key, value) {
131 _element.$dom_setAttributeNS(_namespace, key, '$value');
132 }
133
134 String remove(String key) {
135 String value = this[key];
136 _element.$dom_removeAttributeNS(_namespace, key);
137 return value;
138 }
139
140 /**
141 * The number of {key, value} pairs in the map.
142 */
143 int get length {
144 return keys.length;
145 }
146
147 bool _matches(Node node) => node.$dom_namespaceURI == _namespace;
148 }
149
150
151 /**
152 * Provides a Map abstraction on top of data-* attributes, similar to the
153 * dataSet in the old DOM.
154 */
155 class _DataAttributeMap implements Map<String, String> {
156
157 final Map<String, String> $dom_attributes;
158
159 _DataAttributeMap(this.$dom_attributes);
160
161 // interface Map
162
163 // TODO: Use lazy iterator when it is available on Map.
164 bool containsValue(String value) => values.some((v) => v == value);
165
166 bool containsKey(String key) => $dom_attributes.containsKey(_attr(key));
167
168 String operator [](String key) => $dom_attributes[_attr(key)];
169
170 void operator []=(String key, value) {
171 $dom_attributes[_attr(key)] = '$value';
172 }
173
174 String putIfAbsent(String key, String ifAbsent()) =>
175 $dom_attributes.putIfAbsent(_attr(key), ifAbsent);
176
177 String remove(String key) => $dom_attributes.remove(_attr(key));
178
179 void clear() {
180 // Needs to operate on a snapshot since we are mutating the collection.
181 for (String key in keys) {
182 remove(key);
183 }
184 }
185
186 void forEach(void f(String key, String value)) {
187 $dom_attributes.forEach((String key, String value) {
188 if (_matches(key)) {
189 f(_strip(key), value);
190 }
191 });
192 }
193
194 Collection<String> get keys {
195 final keys = new List<String>();
196 $dom_attributes.forEach((String key, String value) {
197 if (_matches(key)) {
198 keys.add(_strip(key));
199 }
200 });
201 return keys;
202 }
203
204 Collection<String> get values {
205 final values = new List<String>();
206 $dom_attributes.forEach((String key, String value) {
207 if (_matches(key)) {
208 values.add(value);
209 }
210 });
211 return values;
212 }
213
214 int get length => keys.length;
215
216 // TODO: Use lazy iterator when it is available on Map.
217 bool get isEmpty => length == 0;
218
219 // Helpers.
220 String _attr(String key) => 'data-$key';
221 bool _matches(String key) => key.startsWith('data-');
222 String _strip(String key) => key.substring(5);
223 }
OLDNEW
« no previous file with comments | « sdk/lib/html/scripts/htmlrenamer.py ('k') | sdk/lib/html/templates/html/dart2js/html_dart2js.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698