OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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_common; | 5 library html5lib.dom.src; |
6 | |
7 import 'dart:collection'; | |
8 import 'package:html5lib/dom.dart'; | |
9 | |
10 class ElementCssClassSet extends CssClassSetImpl { | |
11 | |
12 final Element _element; | |
13 | |
14 ElementCssClassSet(this._element); | |
15 | |
16 Set<String> readClasses() { | |
17 var s = new LinkedHashSet<String>(); | |
18 var classname = _element.className; | |
19 | |
20 for (String name in classname.split(' ')) { | |
21 String trimmed = name.trim(); | |
22 if (!trimmed.isEmpty) { | |
23 s.add(trimmed); | |
24 } | |
25 } | |
26 return s; | |
27 } | |
28 | |
29 void writeClasses(Set<String> s) { | |
30 List list = new List.from(s); | |
31 _element.className = s.join(' '); | |
32 } | |
33 } | |
34 | |
35 | |
36 /** A Set that stores the CSS class names for an element. */ | |
37 abstract class CssClassSet implements Set<String> { | |
38 | |
39 /** | |
40 * Adds the class [value] to the element if it is not on it, removes it if it | |
41 * is. | |
42 * | |
43 * If [shouldAdd] is true, then we always add that [value] to the element. If | |
44 * [shouldAdd] is false then we always remove [value] from the element. | |
45 */ | |
46 bool toggle(String value, [bool shouldAdd]); | |
47 | |
48 /** | |
49 * Returns [:true:] if classes cannot be added or removed from this | |
50 * [:CssClassSet:]. | |
51 */ | |
52 bool get frozen; | |
53 | |
54 /** | |
55 * Determine if this element contains the class [value]. | |
56 * | |
57 * This is the Dart equivalent of jQuery's | |
58 * [hasClass](http://api.jquery.com/hasClass/). | |
59 */ | |
60 bool contains(String value); | |
61 | |
62 /** | |
63 * Add the class [value] to element. | |
64 * | |
65 * This is the Dart equivalent of jQuery's | |
66 * [addClass](http://api.jquery.com/addClass/). | |
67 * | |
68 * If this corresponds to one element. Returns true if [value] was added to | |
69 * the set, otherwise false. | |
70 * | |
71 * If this corresponds to many elements, null is always returned. | |
72 */ | |
73 bool add(String value); | |
74 | |
75 /** | |
76 * Remove the class [value] from element, and return true on successful | |
77 * removal. | |
78 * | |
79 * This is the Dart equivalent of jQuery's | |
80 * [removeClass](http://api.jquery.com/removeClass/). | |
81 */ | |
82 bool remove(Object value); | |
83 | |
84 /** | |
85 * Add all classes specified in [iterable] to element. | |
86 * | |
87 * This is the Dart equivalent of jQuery's | |
88 * [addClass](http://api.jquery.com/addClass/). | |
89 */ | |
90 void addAll(Iterable<String> iterable); | |
91 | |
92 /** | |
93 * Remove all classes specified in [iterable] from element. | |
94 * | |
95 * This is the Dart equivalent of jQuery's | |
96 * [removeClass](http://api.jquery.com/removeClass/). | |
97 */ | |
98 void removeAll(Iterable<String> iterable); | |
99 | |
100 /** | |
101 * Toggles all classes specified in [iterable] on element. | |
102 * | |
103 * Iterate through [iterable]'s items, and add it if it is not on it, or | |
104 * remove it if it is. This is the Dart equivalent of jQuery's | |
105 * [toggleClass](http://api.jquery.com/toggleClass/). | |
106 * If [shouldAdd] is true, then we always add all the classes in [iterable] | |
107 * element. If [shouldAdd] is false then we always remove all the classes in | |
108 * [iterable] from the element. | |
109 */ | |
110 void toggleAll(Iterable<String> iterable, [bool shouldAdd]); | |
111 } | |
6 | 112 |
7 abstract class CssClassSetImpl implements CssClassSet { | 113 abstract class CssClassSetImpl implements CssClassSet { |
Siggi Cherem (dart-lang)
2014/05/02 17:46:10
was this and below copied from dart:html? should w
Jennifer Messerly
2014/05/08 21:15:41
yup. Done.
| |
8 | 114 |
9 String toString() { | 115 String toString() { |
10 return readClasses().join(' '); | 116 return readClasses().join(' '); |
11 } | 117 } |
12 | 118 |
13 /** | 119 /** |
14 * Adds the class [value] to the element if it is not on it, removes it if it | 120 * Adds the class [value] to the element if it is not on it, removes it if it |
15 * is. | 121 * is. |
16 * | 122 * |
17 * If [shouldAdd] is true, then we always add that [value] to the element. If | 123 * If [shouldAdd] is true, then we always add that [value] to the element. If |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 */ | 326 */ |
221 Set<String> readClasses(); | 327 Set<String> readClasses(); |
222 | 328 |
223 /** | 329 /** |
224 * Join all the elements of a set into one string and write | 330 * Join all the elements of a set into one string and write |
225 * back to the element. | 331 * back to the element. |
226 * This is intended to be overridden by specific implementations. | 332 * This is intended to be overridden by specific implementations. |
227 */ | 333 */ |
228 void writeClasses(Set<String> s); | 334 void writeClasses(Set<String> s); |
229 } | 335 } |
OLD | NEW |