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

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

Issue 11691009: Moved most of html lib generating scripts into tools. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months 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
« no previous file with comments | « sdk/lib/html/scripts/templateloader_test.py ('k') | sdk/lib/html/src/CrossFrameTypes.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 final Element _element;
9
10 _AttributeMap(this._element);
11
12 bool containsValue(String value) {
13 for (var v in this.values) {
14 if (value == v) {
15 return true;
16 }
17 }
18 return false;
19 }
20
21 String putIfAbsent(String key, String ifAbsent()) {
22 if (!containsKey(key)) {
23 this[key] = ifAbsent();
24 }
25 return this[key];
26 }
27
28 void clear() {
29 for (var key in keys) {
30 remove(key);
31 }
32 }
33
34 void forEach(void f(String key, String value)) {
35 for (var key in keys) {
36 var value = this[key];
37 f(key, value);
38 }
39 }
40
41 Collection<String> get keys {
42 // TODO: generate a lazy collection instead.
43 var attributes = _element.$dom_attributes;
44 var keys = new List<String>();
45 for (int i = 0, len = attributes.length; i < len; i++) {
46 if (_matches(attributes[i])) {
47 keys.add(attributes[i].$dom_localName);
48 }
49 }
50 return keys;
51 }
52
53 Collection<String> get values {
54 // TODO: generate a lazy collection instead.
55 var attributes = _element.$dom_attributes;
56 var values = new List<String>();
57 for (int i = 0, len = attributes.length; i < len; i++) {
58 if (_matches(attributes[i])) {
59 values.add(attributes[i].value);
60 }
61 }
62 return values;
63 }
64
65 /**
66 * Returns true if there is no {key, value} pair in the map.
67 */
68 bool get isEmpty {
69 return length == 0;
70 }
71
72 /**
73 * Checks to see if the node should be included in this map.
74 */
75 bool _matches(Node node);
76 }
77
78 /**
79 * Wrapper to expose [Element.attributes] as a typed map.
80 */
81 class _ElementAttributeMap extends _AttributeMap {
82
83 _ElementAttributeMap(Element element): super(element);
84
85 bool containsKey(String key) {
86 return _element.$dom_hasAttribute(key);
87 }
88
89 String operator [](String key) {
90 return _element.$dom_getAttribute(key);
91 }
92
93 void operator []=(String key, value) {
94 _element.$dom_setAttribute(key, '$value');
95 }
96
97 String remove(String key) {
98 String value = _element.$dom_getAttribute(key);
99 _element.$dom_removeAttribute(key);
100 return value;
101 }
102
103 /**
104 * The number of {key, value} pairs in the map.
105 */
106 int get length {
107 return keys.length;
108 }
109
110 bool _matches(Node node) => node.$dom_namespaceUri == null;
111 }
112
113 /**
114 * Wrapper to expose namespaced attributes as a typed map.
115 */
116 class _NamespacedAttributeMap extends _AttributeMap {
117
118 final String _namespace;
119
120 _NamespacedAttributeMap(Element element, this._namespace): super(element);
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/templateloader_test.py ('k') | sdk/lib/html/src/CrossFrameTypes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698