| 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 // TODO - figure out whether classList exists, and if so use that | |
| 6 // rather than the className property that is being used here. | |
| 7 | |
| 8 class _CssClassSet implements Set<String> { | |
| 9 | |
| 10 final _element; | |
| 11 | |
| 12 _CssClassSet(this._element); | |
| 13 | |
| 14 String toString() { | |
| 15 return _formatSet(_read()); | |
| 16 } | |
| 17 | |
| 18 // interface Iterable - BEGIN | |
| 19 Iterator<String> iterator() { | |
| 20 return _read().iterator(); | |
| 21 } | |
| 22 // interface Iterable - END | |
| 23 | |
| 24 // interface Collection - BEGIN | |
| 25 void forEach(void f(String element)) { | |
| 26 _read().forEach(f); | |
| 27 } | |
| 28 | |
| 29 Collection map(f(String element)) { | |
| 30 return _read().map(f); | |
| 31 } | |
| 32 | |
| 33 Collection<String> filter(bool f(String element)) { | |
| 34 return _read().filter(f); | |
| 35 } | |
| 36 | |
| 37 bool every(bool f(String element)) { | |
| 38 return _read().every(f); | |
| 39 } | |
| 40 | |
| 41 bool some(bool f(String element)) { | |
| 42 return _read().some(f); | |
| 43 } | |
| 44 | |
| 45 bool isEmpty() { | |
| 46 return _read().isEmpty(); | |
| 47 } | |
| 48 | |
| 49 int get length() { | |
| 50 return _read().length; | |
| 51 } | |
| 52 // interface Collection - END | |
| 53 | |
| 54 // interface Set - BEGIN | |
| 55 bool contains(String value) { | |
| 56 return _read().contains(value); | |
| 57 } | |
| 58 | |
| 59 void add(String value) { | |
| 60 // TODO - figure out if we need to do any validation here | |
| 61 // or if the browser natively does enough | |
| 62 _modify((s) => s.add(value)); | |
| 63 } | |
| 64 | |
| 65 bool remove(String value) { | |
| 66 Set<String> s = _read(); | |
| 67 bool result = s.remove(value); | |
| 68 _write(s); | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 void addAll(Collection<String> collection) { | |
| 73 // TODO - see comment above about validation | |
| 74 _modify((s) => s.addAll(collection)); | |
| 75 } | |
| 76 | |
| 77 void removeAll(Collection<String> collection) { | |
| 78 _modify((s) => s.removeAll(collection)); | |
| 79 } | |
| 80 | |
| 81 bool isSubsetOf(Collection<String> collection) { | |
| 82 return _read().isSubsetOf(collection); | |
| 83 } | |
| 84 | |
| 85 bool containsAll(Collection<String> collection) { | |
| 86 return _read().containsAll(collection); | |
| 87 } | |
| 88 | |
| 89 Set<String> intersection(Collection<String> other) { | |
| 90 return _read().intersection(other); | |
| 91 } | |
| 92 | |
| 93 void clear() { | |
| 94 _modify((s) => s.clear()); | |
| 95 } | |
| 96 // interface Set - END | |
| 97 | |
| 98 /** | |
| 99 * Helper method used to modify the set of css classes on this element. | |
| 100 * | |
| 101 * f - callback with: | |
| 102 * s - a Set of all the css class name currently on this element. | |
| 103 * | |
| 104 * After f returns, the modified set is written to the | |
| 105 * className property of this element. | |
| 106 */ | |
| 107 void _modify( f(Set<String> s)) { | |
| 108 Set<String> s = _read(); | |
| 109 f(s); | |
| 110 _write(s); | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Read the class names from the HTMLElement class property, | |
| 115 * and put them into a set (duplicates are discarded). | |
| 116 */ | |
| 117 Set<String> _read() { | |
| 118 // TODO(mattsh) simplify this once split can take regex. | |
| 119 Set<String> s = new Set<String>(); | |
| 120 for (String name in _className().split(' ')) { | |
| 121 String trimmed = name.trim(); | |
| 122 if (!trimmed.isEmpty()) { | |
| 123 s.add(trimmed); | |
| 124 } | |
| 125 } | |
| 126 return s; | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Read the class names as a space-separated string. This is meant to be | |
| 131 * overridden by subclasses. | |
| 132 */ | |
| 133 String _className() => _element.className; | |
| 134 | |
| 135 /** | |
| 136 * Join all the elements of a set into one string and write | |
| 137 * back to the element. | |
| 138 */ | |
| 139 void _write(Set s) { | |
| 140 _element.className = _formatSet(s); | |
| 141 } | |
| 142 | |
| 143 String _formatSet(Set<String> s) { | |
| 144 // TODO(mattsh) should be able to pass Set to String.joins http:/b/5398605 | |
| 145 List list = new List.from(s); | |
| 146 return Strings.join(list, ' '); | |
| 147 } | |
| 148 | |
| 149 } | |
| 150 | |
| OLD | NEW |