OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 // WARNING: Do not edit - generated code. | |
6 | |
7 /** | |
8 * Provides a Map abstraction on top of data-* attributes, similar to the | |
9 * dataSet in the old DOM. | |
10 */ | |
11 class _DataAttributeMap implements Map<String, String> { | |
12 | |
13 final Map<String, String> _attributes; | |
14 | |
15 _DataAttributeMap(this._attributes); | |
16 | |
17 // interface Map | |
18 | |
19 // TODO: Use lazy iterator when it is available on Map. | |
20 bool containsValue(String value) => getValues().some((v) => v == value); | |
21 | |
22 bool containsKey(String key) => _attributes.containsKey(_attr(key)); | |
23 | |
24 String operator [](String key) => _attributes[_attr(key)]; | |
25 | |
26 void operator []=(String key, String value) { | |
27 _attributes[_attr(key)] = value; | |
28 } | |
29 | |
30 String putIfAbsent(String key, String ifAbsent()) { | |
31 if (!containsKey(key)) { | |
32 return this[key] = ifAbsent(); | |
33 } | |
34 return this[key]; | |
35 } | |
36 | |
37 String remove(String key) => _attributes.remove(_attr(key)); | |
38 | |
39 void clear() { | |
40 // Needs to operate on a snapshot since we are mutatiting the collection. | |
41 for (String key in getKeys()) { | |
42 remove(key); | |
43 } | |
44 } | |
45 | |
46 void forEach(void f(String key, String value)) { | |
47 _attributes.forEach((String key, String value) { | |
48 if (_matches(key)) { | |
49 f(_strip(key), value); | |
50 } | |
51 }); | |
52 } | |
53 | |
54 Collection<String> getKeys() { | |
55 final keys = new List<String>(); | |
56 _attributes.forEach((String key, String value) { | |
57 if (_matches(key)) { | |
58 keys.add(_strip(key)); | |
59 } | |
60 }); | |
61 return keys; | |
62 } | |
63 | |
64 Collection<String> getValues() { | |
65 final values = new List<String>(); | |
66 _attributes.forEach((String key, String value) { | |
67 if (_matches(key)) { | |
68 values.add(value); | |
69 } | |
70 }); | |
71 return values; | |
72 } | |
73 | |
74 int get length() => getKeys().length; | |
75 | |
76 // TODO: Use lazy iterator when it is available on Map. | |
77 bool isEmpty() => length == 0; | |
78 | |
79 // Helpers. | |
80 String _attr(String key) => 'data-$key'; | |
81 bool _matches(String key) => key.startsWith('data-'); | |
82 String _strip(String key) => key.substring(5); | |
83 } | |
84 | |
85 class _CssClassSet implements Set<String> { | |
86 | |
87 final _ElementImpl _element; | |
88 | |
89 _CssClassSet(this._element); | |
90 | |
91 String toString() { | |
92 return _formatSet(_read()); | |
93 } | |
94 | |
95 // interface Iterable - BEGIN | |
96 Iterator<String> iterator() { | |
97 return _read().iterator(); | |
98 } | |
99 // interface Iterable - END | |
100 | |
101 // interface Collection - BEGIN | |
102 void forEach(void f(String element)) { | |
103 _read().forEach(f); | |
104 } | |
105 | |
106 Collection map(f(String element)) { | |
107 return _read().map(f); | |
108 } | |
109 | |
110 Collection<String> filter(bool f(String element)) { | |
111 return _read().filter(f); | |
112 } | |
113 | |
114 bool every(bool f(String element)) { | |
115 return _read().every(f); | |
116 } | |
117 | |
118 bool some(bool f(String element)) { | |
119 return _read().some(f); | |
120 } | |
121 | |
122 bool isEmpty() { | |
123 return _read().isEmpty(); | |
124 } | |
125 | |
126 int get length() { | |
127 return _read().length; | |
128 } | |
129 // interface Collection - END | |
130 | |
131 // interface Set - BEGIN | |
132 bool contains(String value) { | |
133 return _read().contains(value); | |
134 } | |
135 | |
136 void add(String value) { | |
137 // TODO - figure out if we need to do any validation here | |
138 // or if the browser natively does enough | |
139 _modify((s) => s.add(value)); | |
140 } | |
141 | |
142 bool remove(String value) { | |
143 Set<String> s = _read(); | |
144 bool result = s.remove(value); | |
145 _write(s); | |
146 return result; | |
147 } | |
148 | |
149 void addAll(Collection<String> collection) { | |
150 // TODO - see comment above about validation | |
151 _modify((s) => s.addAll(collection)); | |
152 } | |
153 | |
154 void removeAll(Collection<String> collection) { | |
155 _modify((s) => s.removeAll(collection)); | |
156 } | |
157 | |
158 bool isSubsetOf(Collection<String> collection) { | |
159 return _read().isSubsetOf(collection); | |
160 } | |
161 | |
162 bool containsAll(Collection<String> collection) { | |
163 return _read().containsAll(collection); | |
164 } | |
165 | |
166 Set<String> intersection(Collection<String> other) { | |
167 return _read().intersection(other); | |
168 } | |
169 | |
170 void clear() { | |
171 _modify((s) => s.clear()); | |
172 } | |
173 // interface Set - END | |
174 | |
175 /** | |
176 * Helper method used to modify the set of css classes on this element. | |
177 * | |
178 * f - callback with: | |
179 * s - a Set of all the css class name currently on this element. | |
180 * | |
181 * After f returns, the modified set is written to the | |
182 * className property of this element. | |
183 */ | |
184 void _modify( f(Set<String> s)) { | |
185 Set<String> s = _read(); | |
186 f(s); | |
187 _write(s); | |
188 } | |
189 | |
190 /** | |
191 * Read the class names from the Element class property, | |
192 * and put them into a set (duplicates are discarded). | |
193 */ | |
194 Set<String> _read() { | |
195 // TODO(mattsh) simplify this once split can take regex. | |
196 Set<String> s = new Set<String>(); | |
197 for (String name in _className().split(' ')) { | |
198 String trimmed = name.trim(); | |
199 if (!trimmed.isEmpty()) { | |
200 s.add(trimmed); | |
201 } | |
202 } | |
203 return s; | |
204 } | |
205 | |
206 /** | |
207 * Read the class names as a space-separated string. This is meant to be | |
208 * overridden by subclasses. | |
209 */ | |
210 String _className() => _element._className; | |
211 | |
212 /** | |
213 * Join all the elements of a set into one string and write | |
214 * back to the element. | |
215 */ | |
216 void _write(Set s) { | |
217 _element._className = _formatSet(s); | |
218 } | |
219 | |
220 String _formatSet(Set<String> s) { | |
221 // TODO(mattsh) should be able to pass Set to String.joins http:/b/5398605 | |
222 List list = new List.from(s); | |
223 return Strings.join(list, ' '); | |
224 } | |
225 } | |
226 | |
227 interface ElementList extends List<Element> { | |
228 // TODO(jacobr): add element batch manipulation methods. | |
229 ElementList filter(bool f(Element element)); | |
230 | |
231 ElementList getRange(int start, int length); | |
232 | |
233 Element get first(); | |
234 // TODO(jacobr): add insertAt | |
235 } | |
236 | |
237 /** | |
238 * All your element measurement needs in one place | |
239 */ | |
240 interface ElementRect { | |
241 // Relative to offsetParent | |
242 ClientRect get client(); | |
243 ClientRect get offset(); | |
244 ClientRect get scroll(); | |
245 // In global coords | |
246 ClientRect get bounding(); | |
247 // In global coords | |
248 List<ClientRect> get clientRects(); | |
249 } | |
250 | |
251 interface Element extends Node, NodeSelector default _$(ID)FactoryProvider { | |
252 // TODO(jacobr): switch back to: | |
253 // interface $ID$EXTENDS default _ElementImpl { | |
254 Element.html(String html); | |
255 Element.tag(String tag); | |
256 | |
257 Map<String, String> get attributes(); | |
258 void set attributes(Map<String, String> value); | |
259 | |
260 /** | |
261 * @domName querySelectorAll, getElementsByClassName, getElementsByTagName, | |
262 * getElementsByTagNameNS | |
263 */ | |
264 ElementList queryAll(String selectors); | |
265 | |
266 /** | |
267 * @domName childElementCount, firstElementChild, lastElementChild, | |
268 * children, Node.nodes.add | |
269 */ | |
270 ElementList get elements(); | |
271 | |
272 void set elements(Collection<Element> value); | |
273 | |
274 /** @domName className, classList */ | |
275 Set<String> get classes(); | |
276 | |
277 void set classes(Collection<String> value); | |
278 | |
279 Map<String, String> get dataAttributes(); | |
280 void set dataAttributes(Map<String, String> value); | |
281 | |
282 /** | |
283 * @domName getClientRects, getBoundingClientRect, clientHeight, clientWidth, | |
284 * clientTop, clientLeft, offsetHeight, offsetWidth, offsetTop, offsetLeft, | |
285 * scrollHeight, scrollWidth, scrollTop, scrollLeft | |
286 */ | |
287 Future<ElementRect> get rect(); | |
288 | |
289 /** @domName Window.getComputedStyle */ | |
290 Future<CSSStyleDeclaration> get computedStyle(); | |
291 | |
292 /** @domName Window.getComputedStyle */ | |
293 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement); | |
294 | |
295 Element clone(bool deep); | |
296 | |
297 Element get parent(); | |
298 | |
299 $!MEMBERS | |
300 } | |
OLD | NEW |