OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of html; | 5 part of html; |
6 | 6 |
7 abstract class _AttributeMap implements Map<String, String> { | 7 abstract class _AttributeMap implements Map<String, String> { |
8 final Element _element; | 8 final Element _element; |
9 | 9 |
10 _AttributeMap(this._element); | 10 _AttributeMap(this._element); |
11 | 11 |
12 void addAll(Map<String, String> other) { | 12 void addAll(Map<String, String> other) { |
13 other.forEach((k, v) { this[k] = v; }); | 13 other.forEach((k, v) { this[k] = v; }); |
14 } | 14 } |
15 | 15 |
16 bool containsValue(String value) { | 16 bool containsValue(Object value) { |
17 for (var v in this.values) { | 17 for (var v in this.values) { |
18 if (value == v) { | 18 if (value == v) { |
19 return true; | 19 return true; |
20 } | 20 } |
21 } | 21 } |
22 return false; | 22 return false; |
23 } | 23 } |
24 | 24 |
25 String putIfAbsent(String key, String ifAbsent()) { | 25 String putIfAbsent(String key, String ifAbsent()) { |
26 if (!containsKey(key)) { | 26 if (!containsKey(key)) { |
(...skipping 11 matching lines...) Expand all Loading... |
38 void forEach(void f(String key, String value)) { | 38 void forEach(void f(String key, String value)) { |
39 for (var key in keys) { | 39 for (var key in keys) { |
40 var value = this[key]; | 40 var value = this[key]; |
41 f(key, value); | 41 f(key, value); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 Iterable<String> get keys { | 45 Iterable<String> get keys { |
46 // TODO: generate a lazy collection instead. | 46 // TODO: generate a lazy collection instead. |
47 var attributes = _element._attributes; | 47 var attributes = _element._attributes; |
48 var keys = new List<String>(); | 48 var keys = <String>[]; |
49 for (int i = 0, len = attributes.length; i < len; i++) { | 49 for (int i = 0, len = attributes.length; i < len; i++) { |
50 if (_matches(attributes[i])) { | 50 _Attr attr = attributes[i]; |
51 keys.add(attributes[i].name); | 51 if (_matches(attr)) { |
| 52 keys.add(attr.name); |
52 } | 53 } |
53 } | 54 } |
54 return keys; | 55 return keys; |
55 } | 56 } |
56 | 57 |
57 Iterable<String> get values { | 58 Iterable<String> get values { |
58 // TODO: generate a lazy collection instead. | 59 // TODO: generate a lazy collection instead. |
59 var attributes = _element._attributes; | 60 var attributes = _element._attributes; |
60 var values = new List<String>(); | 61 var values = <String>[]; |
61 for (int i = 0, len = attributes.length; i < len; i++) { | 62 for (int i = 0, len = attributes.length; i < len; i++) { |
62 if (_matches(attributes[i])) { | 63 _Attr attr = attributes[i]; |
63 values.add(attributes[i].value); | 64 if (_matches(attr)) { |
| 65 values.add(attr.value); |
64 } | 66 } |
65 } | 67 } |
66 return values; | 68 return values; |
67 } | 69 } |
68 | 70 |
69 /** | 71 /** |
70 * Returns true if there is no {key, value} pair in the map. | 72 * Returns true if there is no {key, value} pair in the map. |
71 */ | 73 */ |
72 bool get isEmpty { | 74 bool get isEmpty { |
73 return length == 0; | 75 return length == 0; |
(...skipping 10 matching lines...) Expand all Loading... |
84 bool _matches(Node node); | 86 bool _matches(Node node); |
85 } | 87 } |
86 | 88 |
87 /** | 89 /** |
88 * Wrapper to expose [Element.attributes] as a typed map. | 90 * Wrapper to expose [Element.attributes] as a typed map. |
89 */ | 91 */ |
90 class _ElementAttributeMap extends _AttributeMap { | 92 class _ElementAttributeMap extends _AttributeMap { |
91 | 93 |
92 _ElementAttributeMap(Element element): super(element); | 94 _ElementAttributeMap(Element element): super(element); |
93 | 95 |
94 bool containsKey(String key) { | 96 bool containsKey(Object key) { |
95 return _element._hasAttribute(key); | 97 return _element._hasAttribute(key); |
96 } | 98 } |
97 | 99 |
98 String operator [](String key) { | 100 String operator [](Object key) { |
99 return _element.getAttribute(key); | 101 return _element.getAttribute(key); |
100 } | 102 } |
101 | 103 |
102 void operator []=(String key, String value) { | 104 void operator []=(String key, String value) { |
103 _element.setAttribute(key, value); | 105 _element.setAttribute(key, value); |
104 } | 106 } |
105 | 107 |
106 String remove(String key) { | 108 String remove(Object key) { |
107 String value = _element.getAttribute(key); | 109 String value = _element.getAttribute(key); |
108 _element._removeAttribute(key); | 110 _element._removeAttribute(key); |
109 return value; | 111 return value; |
110 } | 112 } |
111 | 113 |
112 /** | 114 /** |
113 * The number of {key, value} pairs in the map. | 115 * The number of {key, value} pairs in the map. |
114 */ | 116 */ |
115 int get length { | 117 int get length { |
116 return keys.length; | 118 return keys.length; |
117 } | 119 } |
118 | 120 |
119 bool _matches(Node node) => node._namespaceUri == null; | 121 bool _matches(Node node) => node._namespaceUri == null; |
120 } | 122 } |
121 | 123 |
122 /** | 124 /** |
123 * Wrapper to expose namespaced attributes as a typed map. | 125 * Wrapper to expose namespaced attributes as a typed map. |
124 */ | 126 */ |
125 class _NamespacedAttributeMap extends _AttributeMap { | 127 class _NamespacedAttributeMap extends _AttributeMap { |
126 | 128 |
127 final String _namespace; | 129 final String _namespace; |
128 | 130 |
129 _NamespacedAttributeMap(Element element, this._namespace): super(element); | 131 _NamespacedAttributeMap(Element element, this._namespace): super(element); |
130 | 132 |
131 bool containsKey(String key) { | 133 bool containsKey(Object key) { |
132 return _element._hasAttributeNS(_namespace, key); | 134 return _element._hasAttributeNS(_namespace, key); |
133 } | 135 } |
134 | 136 |
135 String operator [](String key) { | 137 String operator [](Object key) { |
136 return _element.getAttributeNS(_namespace, key); | 138 return _element.getAttributeNS(_namespace, key); |
137 } | 139 } |
138 | 140 |
139 void operator []=(String key, String value) { | 141 void operator []=(String key, String value) { |
140 _element.setAttributeNS(_namespace, key, value); | 142 _element.setAttributeNS(_namespace, key, value); |
141 } | 143 } |
142 | 144 |
143 String remove(String key) { | 145 String remove(Object key) { |
144 String value = this[key]; | 146 String value = this[key]; |
145 _element._removeAttributeNS(_namespace, key); | 147 _element._removeAttributeNS(_namespace, key); |
146 return value; | 148 return value; |
147 } | 149 } |
148 | 150 |
149 /** | 151 /** |
150 * The number of {key, value} pairs in the map. | 152 * The number of {key, value} pairs in the map. |
151 */ | 153 */ |
152 int get length { | 154 int get length { |
153 return keys.length; | 155 return keys.length; |
(...skipping 13 matching lines...) Expand all Loading... |
167 | 169 |
168 _DataAttributeMap(this._attributes); | 170 _DataAttributeMap(this._attributes); |
169 | 171 |
170 // interface Map | 172 // interface Map |
171 | 173 |
172 void addAll(Map<String, String> other) { | 174 void addAll(Map<String, String> other) { |
173 other.forEach((k, v) { this[k] = v; }); | 175 other.forEach((k, v) { this[k] = v; }); |
174 } | 176 } |
175 | 177 |
176 // TODO: Use lazy iterator when it is available on Map. | 178 // TODO: Use lazy iterator when it is available on Map. |
177 bool containsValue(String value) => values.any((v) => v == value); | 179 bool containsValue(Object value) => values.any((v) => v == value); |
178 | 180 |
179 bool containsKey(String key) => _attributes.containsKey(_attr(key)); | 181 bool containsKey(Object key) => _attributes.containsKey(_attr(key)); |
180 | 182 |
181 String operator [](String key) => _attributes[_attr(key)]; | 183 String operator [](Object key) => _attributes[_attr(key)]; |
182 | 184 |
183 void operator []=(String key, String value) { | 185 void operator []=(String key, String value) { |
184 _attributes[_attr(key)] = value; | 186 _attributes[_attr(key)] = value; |
185 } | 187 } |
186 | 188 |
187 String putIfAbsent(String key, String ifAbsent()) => | 189 String putIfAbsent(String key, String ifAbsent()) => |
188 _attributes.putIfAbsent(_attr(key), ifAbsent); | 190 _attributes.putIfAbsent(_attr(key), ifAbsent); |
189 | 191 |
190 String remove(String key) => _attributes.remove(_attr(key)); | 192 String remove(Object key) => _attributes.remove(_attr(key)); |
191 | 193 |
192 void clear() { | 194 void clear() { |
193 // Needs to operate on a snapshot since we are mutating the collection. | 195 // Needs to operate on a snapshot since we are mutating the collection. |
194 for (String key in keys) { | 196 for (String key in keys) { |
195 remove(key); | 197 remove(key); |
196 } | 198 } |
197 } | 199 } |
198 | 200 |
199 void forEach(void f(String key, String value)) { | 201 void forEach(void f(String key, String value)) { |
200 _attributes.forEach((String key, String value) { | 202 _attributes.forEach((String key, String value) { |
201 if (_matches(key)) { | 203 if (_matches(key)) { |
202 f(_strip(key), value); | 204 f(_strip(key), value); |
203 } | 205 } |
204 }); | 206 }); |
205 } | 207 } |
206 | 208 |
207 Iterable<String> get keys { | 209 Iterable<String> get keys { |
208 final keys = new List<String>(); | 210 final keys = <String>[]; |
209 _attributes.forEach((String key, String value) { | 211 _attributes.forEach((String key, String value) { |
210 if (_matches(key)) { | 212 if (_matches(key)) { |
211 keys.add(_strip(key)); | 213 keys.add(_strip(key)); |
212 } | 214 } |
213 }); | 215 }); |
214 return keys; | 216 return keys; |
215 } | 217 } |
216 | 218 |
217 Iterable<String> get values { | 219 Iterable<String> get values { |
218 final values = new List<String>(); | 220 final values = <String>[]; |
219 _attributes.forEach((String key, String value) { | 221 _attributes.forEach((String key, String value) { |
220 if (_matches(key)) { | 222 if (_matches(key)) { |
221 values.add(value); | 223 values.add(value); |
222 } | 224 } |
223 }); | 225 }); |
224 return values; | 226 return values; |
225 } | 227 } |
226 | 228 |
227 int get length => keys.length; | 229 int get length => keys.length; |
228 | 230 |
(...skipping 29 matching lines...) Expand all Loading... |
258 String _toHyphenedName(String word) { | 260 String _toHyphenedName(String word) { |
259 var sb = new StringBuffer(); | 261 var sb = new StringBuffer(); |
260 for (int i = 0; i < word.length; i++) { | 262 for (int i = 0; i < word.length; i++) { |
261 var lower = word[i].toLowerCase(); | 263 var lower = word[i].toLowerCase(); |
262 if (word[i] != lower && i > 0) sb.write('-'); | 264 if (word[i] != lower && i > 0) sb.write('-'); |
263 sb.write(lower); | 265 sb.write(lower); |
264 } | 266 } |
265 return sb.toString(); | 267 return sb.toString(); |
266 } | 268 } |
267 } | 269 } |
OLD | NEW |