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

Side by Side Diff: tools/dom/src/dartium_CssClassSet.dart

Issue 2827793002: Format all files under tools and utils directory. (Closed)
Patch Set: Format all files under tools and utils directory. Created 3 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 /** 7 /**
8 * A set (union) of the CSS classes that are present in a set of elements. 8 * A set (union) of the CSS classes that are present in a set of elements.
9 * Implemented separately from _ElementCssClassSet for performance. 9 * Implemented separately from _ElementCssClassSet for performance.
10 */ 10 */
11 class _MultiElementCssClassSet extends CssClassSetImpl { 11 class _MultiElementCssClassSet extends CssClassSetImpl {
12 final Iterable<Element> _elementIterable; 12 final Iterable<Element> _elementIterable;
13 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; 13 Iterable<_ElementCssClassSet> _elementCssClassSetIterable;
14 14
15 _MultiElementCssClassSet(this._elementIterable) { 15 _MultiElementCssClassSet(this._elementIterable) {
16 _elementCssClassSetIterable = new List.from(_elementIterable).map( 16 _elementCssClassSetIterable =
17 (e) => new _ElementCssClassSet(e)); 17 new List.from(_elementIterable).map((e) => new _ElementCssClassSet(e));
18 } 18 }
19 19
20 Set<String> readClasses() { 20 Set<String> readClasses() {
21 var s = new LinkedHashSet<String>(); 21 var s = new LinkedHashSet<String>();
22 _elementCssClassSetIterable.forEach( 22 _elementCssClassSetIterable
23 (_ElementCssClassSet e) => s.addAll(e.readClasses())); 23 .forEach((_ElementCssClassSet e) => s.addAll(e.readClasses()));
24 return s; 24 return s;
25 } 25 }
26 26
27 void writeClasses(Set<String> s) { 27 void writeClasses(Set<String> s) {
28 var classes = s.join(' '); 28 var classes = s.join(' ');
29 for (Element e in _elementIterable) { 29 for (Element e in _elementIterable) {
30 e.className = classes; 30 e.className = classes;
31 } 31 }
32 } 32 }
33 33
34 /** 34 /**
35 * Helper method used to modify the set of css classes on this element. 35 * Helper method used to modify the set of css classes on this element.
36 * 36 *
37 * f - callback with: 37 * f - callback with:
38 * s - a Set of all the css class name currently on this element. 38 * s - a Set of all the css class name currently on this element.
39 * 39 *
40 * After f returns, the modified set is written to the 40 * After f returns, the modified set is written to the
41 * className property of this element. 41 * className property of this element.
42 */ 42 */
43 modify( f(Set<String> s)) { 43 modify(f(Set<String> s)) {
44 _elementCssClassSetIterable.forEach((_ElementCssClassSet e) => e.modify(f)); 44 _elementCssClassSetIterable.forEach((_ElementCssClassSet e) => e.modify(f));
45 } 45 }
46 46
47 /** 47 /**
48 * Adds the class [value] to the element if it is not on it, removes it if it 48 * Adds the class [value] to the element if it is not on it, removes it if it
49 * is. 49 * is.
50 */ 50 */
51 bool toggle(String value, [bool shouldAdd]) => 51 bool toggle(String value, [bool shouldAdd]) =>
52 _elementCssClassSetIterable.fold(false, 52 _elementCssClassSetIterable.fold(
53 false,
53 (bool changed, _ElementCssClassSet e) => 54 (bool changed, _ElementCssClassSet e) =>
54 e.toggle(value, shouldAdd) || changed); 55 e.toggle(value, shouldAdd) || changed);
55 56
56 /** 57 /**
57 * Remove the class [value] from element, and return true on successful 58 * Remove the class [value] from element, and return true on successful
58 * removal. 59 * removal.
59 * 60 *
60 * This is the Dart equivalent of jQuery's 61 * This is the Dart equivalent of jQuery's
61 * [removeClass](http://api.jquery.com/removeClass/). 62 * [removeClass](http://api.jquery.com/removeClass/).
62 */ 63 */
63 bool remove(Object value) => _elementCssClassSetIterable.fold(false, 64 bool remove(Object value) => _elementCssClassSetIterable.fold(false,
64 (bool changed, _ElementCssClassSet e) => e.remove(value) || changed); 65 (bool changed, _ElementCssClassSet e) => e.remove(value) || changed);
65 } 66 }
66 67
67 class _ElementCssClassSet extends CssClassSetImpl { 68 class _ElementCssClassSet extends CssClassSetImpl {
68
69 final Element _element; 69 final Element _element;
70 70
71 _ElementCssClassSet(this._element); 71 _ElementCssClassSet(this._element);
72 72
73 Set<String> readClasses() { 73 Set<String> readClasses() {
74 var s = new LinkedHashSet<String>(); 74 var s = new LinkedHashSet<String>();
75 var classname = _element.className; 75 var classname = _element.className;
76 76
77 for (String name in classname.split(' ')) { 77 for (String name in classname.split(' ')) {
78 String trimmed = name.trim(); 78 String trimmed = name.trim();
79 if (!trimmed.isEmpty) { 79 if (!trimmed.isEmpty) {
80 s.add(trimmed); 80 s.add(trimmed);
81 } 81 }
82 } 82 }
83 return s; 83 return s;
84 } 84 }
85 85
86 void writeClasses(Set<String> s) { 86 void writeClasses(Set<String> s) {
87 _element.className = s.join(' '); 87 _element.className = s.join(' ');
88 } 88 }
89 } 89 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698