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

Side by Side Diff: observatory_pub_packages/html5lib/src/css_class_set.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 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) 2014, 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 // TODO(jmesserly): everything in this file is copied straight from "dart:html".
6 library html5lib.dom.src;
7
8 import 'dart:collection';
9 import 'package:html5lib/dom.dart';
10
11 class ElementCssClassSet extends CssClassSetImpl {
12
13 final Element _element;
14
15 ElementCssClassSet(this._element);
16
17 Set<String> readClasses() {
18 var s = new LinkedHashSet<String>();
19 var classname = _element.className;
20
21 for (String name in classname.split(' ')) {
22 String trimmed = name.trim();
23 if (!trimmed.isEmpty) {
24 s.add(trimmed);
25 }
26 }
27 return s;
28 }
29
30 void writeClasses(Set<String> s) {
31 List list = new List.from(s);
32 _element.className = s.join(' ');
33 }
34 }
35
36
37 /** A Set that stores the CSS class names for an element. */
38 abstract class CssClassSet implements Set<String> {
39
40 /**
41 * Adds the class [value] to the element if it is not on it, removes it if it
42 * is.
43 *
44 * If [shouldAdd] is true, then we always add that [value] to the element. If
45 * [shouldAdd] is false then we always remove [value] from the element.
46 */
47 bool toggle(String value, [bool shouldAdd]);
48
49 /**
50 * Returns [:true:] if classes cannot be added or removed from this
51 * [:CssClassSet:].
52 */
53 bool get frozen;
54
55 /**
56 * Determine if this element contains the class [value].
57 *
58 * This is the Dart equivalent of jQuery's
59 * [hasClass](http://api.jquery.com/hasClass/).
60 */
61 bool contains(String value);
62
63 /**
64 * Add the class [value] to element.
65 *
66 * This is the Dart equivalent of jQuery's
67 * [addClass](http://api.jquery.com/addClass/).
68 *
69 * If this corresponds to one element. Returns true if [value] was added to
70 * the set, otherwise false.
71 *
72 * If this corresponds to many elements, null is always returned.
73 */
74 bool add(String value);
75
76 /**
77 * Remove the class [value] from element, and return true on successful
78 * removal.
79 *
80 * This is the Dart equivalent of jQuery's
81 * [removeClass](http://api.jquery.com/removeClass/).
82 */
83 bool remove(Object value);
84
85 /**
86 * Add all classes specified in [iterable] to element.
87 *
88 * This is the Dart equivalent of jQuery's
89 * [addClass](http://api.jquery.com/addClass/).
90 */
91 void addAll(Iterable<String> iterable);
92
93 /**
94 * Remove all classes specified in [iterable] from element.
95 *
96 * This is the Dart equivalent of jQuery's
97 * [removeClass](http://api.jquery.com/removeClass/).
98 */
99 void removeAll(Iterable<String> iterable);
100
101 /**
102 * Toggles all classes specified in [iterable] on element.
103 *
104 * Iterate through [iterable]'s items, and add it if it is not on it, or
105 * remove it if it is. This is the Dart equivalent of jQuery's
106 * [toggleClass](http://api.jquery.com/toggleClass/).
107 * If [shouldAdd] is true, then we always add all the classes in [iterable]
108 * element. If [shouldAdd] is false then we always remove all the classes in
109 * [iterable] from the element.
110 */
111 void toggleAll(Iterable<String> iterable, [bool shouldAdd]);
112 }
113
114 abstract class CssClassSetImpl implements CssClassSet {
115
116 String toString() {
117 return readClasses().join(' ');
118 }
119
120 /**
121 * Adds the class [value] to the element if it is not on it, removes it if it
122 * is.
123 *
124 * If [shouldAdd] is true, then we always add that [value] to the element. If
125 * [shouldAdd] is false then we always remove [value] from the element.
126 */
127 bool toggle(String value, [bool shouldAdd]) {
128 Set<String> s = readClasses();
129 bool result = false;
130 if (shouldAdd == null) shouldAdd = !s.contains(value);
131 if (shouldAdd) {
132 s.add(value);
133 result = true;
134 } else {
135 s.remove(value);
136 }
137 writeClasses(s);
138 return result;
139 }
140
141 /**
142 * Returns [:true:] if classes cannot be added or removed from this
143 * [:CssClassSet:].
144 */
145 bool get frozen => false;
146
147 // interface Iterable - BEGIN
148 Iterator<String> get iterator => readClasses().iterator;
149 // interface Iterable - END
150
151 // interface Collection - BEGIN
152 void forEach(void f(String element)) {
153 readClasses().forEach(f);
154 }
155
156 String join([String separator = ""]) => readClasses().join(separator);
157
158 Iterable map(f(String element)) => readClasses().map(f);
159
160 Iterable<String> where(bool f(String element)) => readClasses().where(f);
161
162 Iterable expand(Iterable f(String element)) => readClasses().expand(f);
163
164 bool every(bool f(String element)) => readClasses().every(f);
165
166 bool any(bool f(String element)) => readClasses().any(f);
167
168 bool get isEmpty => readClasses().isEmpty;
169
170 bool get isNotEmpty => readClasses().isNotEmpty;
171
172 int get length => readClasses().length;
173
174 String reduce(String combine(String value, String element)) {
175 return readClasses().reduce(combine);
176 }
177
178 dynamic fold(dynamic initialValue,
179 dynamic combine(dynamic previousValue, String element)) {
180 return readClasses().fold(initialValue, combine);
181 }
182 // interface Collection - END
183
184 // interface Set - BEGIN
185 /**
186 * Determine if this element contains the class [value].
187 *
188 * This is the Dart equivalent of jQuery's
189 * [hasClass](http://api.jquery.com/hasClass/).
190 */
191 bool contains(String value) => readClasses().contains(value);
192
193 /** Lookup from the Set interface. Not interesting for a String set. */
194 String lookup(String value) => contains(value) ? value : null;
195
196 /**
197 * Add the class [value] to element.
198 *
199 * This is the Dart equivalent of jQuery's
200 * [addClass](http://api.jquery.com/addClass/).
201 */
202 bool add(String value) {
203 // TODO - figure out if we need to do any validation here
204 // or if the browser natively does enough.
205 return modify((s) => s.add(value));
206 }
207
208 /**
209 * Remove the class [value] from element, and return true on successful
210 * removal.
211 *
212 * This is the Dart equivalent of jQuery's
213 * [removeClass](http://api.jquery.com/removeClass/).
214 */
215 bool remove(Object value) {
216 if (value is! String) return false;
217 Set<String> s = readClasses();
218 bool result = s.remove(value);
219 writeClasses(s);
220 return result;
221 }
222
223 /**
224 * Add all classes specified in [iterable] to element.
225 *
226 * This is the Dart equivalent of jQuery's
227 * [addClass](http://api.jquery.com/addClass/).
228 */
229 void addAll(Iterable<String> iterable) {
230 // TODO - see comment above about validation.
231 modify((s) => s.addAll(iterable));
232 }
233
234 /**
235 * Remove all classes specified in [iterable] from element.
236 *
237 * This is the Dart equivalent of jQuery's
238 * [removeClass](http://api.jquery.com/removeClass/).
239 */
240 void removeAll(Iterable<String> iterable) {
241 modify((s) => s.removeAll(iterable));
242 }
243
244 /**
245 * Toggles all classes specified in [iterable] on element.
246 *
247 * Iterate through [iterable]'s items, and add it if it is not on it, or
248 * remove it if it is. This is the Dart equivalent of jQuery's
249 * [toggleClass](http://api.jquery.com/toggleClass/).
250 * If [shouldAdd] is true, then we always add all the classes in [iterable]
251 * element. If [shouldAdd] is false then we always remove all the classes in
252 * [iterable] from the element.
253 */
254 void toggleAll(Iterable<String> iterable, [bool shouldAdd]) {
255 iterable.forEach((e) => toggle(e, shouldAdd));
256 }
257
258 void retainAll(Iterable<String> iterable) {
259 modify((s) => s.retainAll(iterable));
260 }
261
262 void removeWhere(bool test(String name)) {
263 modify((s) => s.removeWhere(test));
264 }
265
266 void retainWhere(bool test(String name)) {
267 modify((s) => s.retainWhere(test));
268 }
269
270 bool containsAll(Iterable<String> collection) =>
271 readClasses().containsAll(collection);
272
273 Set<String> intersection(Set<String> other) =>
274 readClasses().intersection(other);
275
276 Set<String> union(Set<String> other) =>
277 readClasses().union(other);
278
279 Set<String> difference(Set<String> other) =>
280 readClasses().difference(other);
281
282 String get first => readClasses().first;
283 String get last => readClasses().last;
284 String get single => readClasses().single;
285 List<String> toList({ bool growable: true }) =>
286 readClasses().toList(growable: growable);
287 Set<String> toSet() => readClasses().toSet();
288 Iterable<String> take(int n) => readClasses().take(n);
289 Iterable<String> takeWhile(bool test(String value)) =>
290 readClasses().takeWhile(test);
291 Iterable<String> skip(int n) => readClasses().skip(n);
292 Iterable<String> skipWhile(bool test(String value)) =>
293 readClasses().skipWhile(test);
294 dynamic firstWhere(bool test(String value), { Object orElse() }) =>
295 readClasses().firstWhere(test, orElse: orElse);
296 dynamic lastWhere(bool test(String value), { Object orElse()}) =>
297 readClasses().lastWhere(test, orElse: orElse);
298 String singleWhere(bool test(String value)) =>
299 readClasses().singleWhere(test);
300 String elementAt(int index) => readClasses().elementAt(index);
301
302 void clear() {
303 modify((s) => s.clear());
304 }
305 // interface Set - END
306
307 /**
308 * Helper method used to modify the set of css classes on this element.
309 *
310 * f - callback with:
311 * s - a Set of all the css class name currently on this element.
312 *
313 * After f returns, the modified set is written to the
314 * className property of this element.
315 */
316 modify( f(Set<String> s)) {
317 Set<String> s = readClasses();
318 var ret = f(s);
319 writeClasses(s);
320 return ret;
321 }
322
323 /**
324 * Read the class names from the Element class property,
325 * and put them into a set (duplicates are discarded).
326 * This is intended to be overridden by specific implementations.
327 */
328 Set<String> readClasses();
329
330 /**
331 * Join all the elements of a set into one string and write
332 * back to the element.
333 * This is intended to be overridden by specific implementations.
334 */
335 void writeClasses(Set<String> s);
336 }
OLDNEW
« no previous file with comments | « observatory_pub_packages/html5lib/src/constants.dart ('k') | observatory_pub_packages/html5lib/src/encoding_parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698