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

Side by Side Diff: lib/dom/templates/html/interface/interface_Element.darttemplate

Issue 9930008: Fix issue http://code.google.com/p/dart/issues/detail?id=1877 without degrading performance. Impro… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review fixes Created 8 years, 8 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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 // WARNING: Do not edit - generated code. 5 // WARNING: Do not edit - generated code.
6 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> $dom_attributes;
14
15 _DataAttributeMap(this.$dom_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) => $dom_attributes.containsKey(_attr(key));
23
24 String operator [](String key) => $dom_attributes[_attr(key)];
25
26 void operator []=(String key, String value) {
27 $dom_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) => $dom_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 $dom_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 $dom_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 $dom_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 $dom_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 $dom_className() => _element.$dom_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.$dom_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> { 7 interface ElementList extends List<Element> {
228 // TODO(jacobr): add element batch manipulation methods. 8 // TODO(jacobr): add element batch manipulation methods.
229 ElementList filter(bool f(Element element)); 9 ElementList filter(bool f(Element element));
230 10
231 ElementList getRange(int start, int length); 11 ElementList getRange(int start, int length);
232 12
233 Element get first(); 13 Element get first();
234 // TODO(jacobr): add insertAt 14 // TODO(jacobr): add insertAt
235 } 15 }
236 16
237 /** 17 /**
18 * All your attribute manipulation needs in one place.
19 * Extends the regular Map interface by automatically coercing non-string
20 * values to strings.
21 */
22 interface AttributeMap extends Map<String, String> {
23 void operator []=(String key, value);
24 }
25
26 /**
238 * All your element measurement needs in one place 27 * All your element measurement needs in one place
239 */ 28 */
240 interface ElementRect { 29 interface ElementRect {
241 // Relative to offsetParent 30 // Relative to offsetParent
242 ClientRect get client(); 31 ClientRect get client();
243 ClientRect get offset(); 32 ClientRect get offset();
244 ClientRect get scroll(); 33 ClientRect get scroll();
245 // In global coords 34 // In global coords
246 ClientRect get bounding(); 35 ClientRect get bounding();
247 // In global coords 36 // In global coords
248 List<ClientRect> get clientRects(); 37 List<ClientRect> get clientRects();
249 } 38 }
250 39
251 interface Element extends Node, NodeSelector default _$(ID)FactoryProvider { 40 interface Element extends Node, NodeSelector default _$(ID)FactoryProvider {
252 // TODO(jacobr): switch back to: 41 // TODO(jacobr): switch back to:
253 // interface $ID$EXTENDS default _ElementImpl { 42 // interface $ID$EXTENDS default _ElementImpl {
254 Element.html(String html); 43 Element.html(String html);
255 Element.tag(String tag); 44 Element.tag(String tag);
256 45
257 Map<String, String> get attributes(); 46 AttributeMap get attributes();
258 void set attributes(Map<String, String> value); 47 void set attributes(Map<String, String> value);
259 48
260 /** 49 /**
261 * @domName querySelectorAll, getElementsByClassName, getElementsByTagName, 50 * @domName querySelectorAll, getElementsByClassName, getElementsByTagName,
262 * getElementsByTagNameNS 51 * getElementsByTagNameNS
263 */ 52 */
264 ElementList queryAll(String selectors); 53 ElementList queryAll(String selectors);
265 54
266 /** 55 /**
267 * @domName childElementCount, firstElementChild, lastElementChild, 56 * @domName childElementCount, firstElementChild, lastElementChild,
268 * children, Node.nodes.add 57 * children, Node.nodes.add
269 */ 58 */
270 ElementList get elements(); 59 ElementList get elements();
271 60
272 void set elements(Collection<Element> value); 61 void set elements(Collection<Element> value);
273 62
274 /** @domName className, classList */ 63 /** @domName className, classList */
275 Set<String> get classes(); 64 Set<String> get classes();
276 65
277 void set classes(Collection<String> value); 66 void set classes(Collection<String> value);
278 67
279 Map<String, String> get dataAttributes(); 68 AttributeMap get dataAttributes();
280 void set dataAttributes(Map<String, String> value); 69 void set dataAttributes(Map<String, String> value);
281 70
282 /** 71 /**
283 * @domName getClientRects, getBoundingClientRect, clientHeight, clientWidth, 72 * @domName getClientRects, getBoundingClientRect, clientHeight, clientWidth,
284 * clientTop, clientLeft, offsetHeight, offsetWidth, offsetTop, offsetLeft, 73 * clientTop, clientLeft, offsetHeight, offsetWidth, offsetTop, offsetLeft,
285 * scrollHeight, scrollWidth, scrollTop, scrollLeft 74 * scrollHeight, scrollWidth, scrollTop, scrollLeft
286 */ 75 */
287 Future<ElementRect> get rect(); 76 Future<ElementRect> get rect();
288 77
289 /** @domName Window.getComputedStyle */ 78 /** @domName Window.getComputedStyle */
290 Future<CSSStyleDeclaration> get computedStyle(); 79 Future<CSSStyleDeclaration> get computedStyle();
291 80
292 /** @domName Window.getComputedStyle */ 81 /** @domName Window.getComputedStyle */
293 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement); 82 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement);
294 83
295 Element clone(bool deep); 84 Element clone(bool deep);
296 85
297 Element get parent(); 86 Element get parent();
298 87
299 $!MEMBERS 88 $!MEMBERS
300 } 89 }
OLDNEW
« no previous file with comments | « lib/dom/templates/html/impl/impl_Element.darttemplate ('k') | lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698