| // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| part of html; |
| |
| abstract class CssClassSet implements Set<String> { |
| |
| String toString() { |
| return Strings.join(new List.from(readClasses()), ' '); |
| } |
| |
| /** |
| * Adds the class [token] to the element if it is not on it, removes it if it |
| * is. |
| */ |
| bool toggle(String value) { |
| Set<String> s = readClasses(); |
| bool result = false; |
| if (s.contains(value)) { |
| s.remove(value); |
| } else { |
| s.add(value); |
| result = true; |
| } |
| writeClasses(s); |
| return result; |
| } |
| |
| /** |
| * Returns [:true:] if classes cannot be added or removed from this |
| * [:CssClassSet:]. |
| */ |
| bool get frozen => false; |
| |
| // interface Iterable - BEGIN |
| Iterator<String> iterator() => readClasses().iterator(); |
| // interface Iterable - END |
| |
| // interface Collection - BEGIN |
| void forEach(void f(String element)) { |
| readClasses().forEach(f); |
| } |
| |
| Collection map(f(String element)) => readClasses().map(f); |
| |
| Collection<String> filter(bool f(String element)) => readClasses().filter(f); |
| |
| bool every(bool f(String element)) => readClasses().every(f); |
| |
| bool some(bool f(String element)) => readClasses().some(f); |
| |
| bool get isEmpty => readClasses().isEmpty; |
| |
| int get length =>readClasses().length; |
| // interface Collection - END |
| |
| // interface Set - BEGIN |
| bool contains(String value) => readClasses().contains(value); |
| |
| void add(String value) { |
| // TODO - figure out if we need to do any validation here |
| // or if the browser natively does enough |
| _modify((s) => s.add(value)); |
| } |
| |
| bool remove(String value) { |
| Set<String> s = readClasses(); |
| bool result = s.remove(value); |
| writeClasses(s); |
| return result; |
| } |
| |
| void addAll(Collection<String> collection) { |
| // TODO - see comment above about validation |
| _modify((s) => s.addAll(collection)); |
| } |
| |
| void removeAll(Collection<String> collection) { |
| _modify((s) => s.removeAll(collection)); |
| } |
| |
| bool isSubsetOf(Collection<String> collection) => |
| readClasses().isSubsetOf(collection); |
| |
| bool containsAll(Collection<String> collection) => |
| readClasses().containsAll(collection); |
| |
| Set<String> intersection(Collection<String> other) => |
| readClasses().intersection(other); |
| |
| void clear() { |
| _modify((s) => s.clear()); |
| } |
| // interface Set - END |
| |
| /** |
| * Helper method used to modify the set of css classes on this element. |
| * |
| * f - callback with: |
| * s - a Set of all the css class name currently on this element. |
| * |
| * After f returns, the modified set is written to the |
| * className property of this element. |
| */ |
| void _modify( f(Set<String> s)) { |
| Set<String> s = readClasses(); |
| f(s); |
| writeClasses(s); |
| } |
| |
| /** |
| * Read the class names from the Element class property, |
| * and put them into a set (duplicates are discarded). |
| * This is intended to be overridden by specific implementations. |
| */ |
| Set<String> readClasses(); |
| |
| /** |
| * Join all the elements of a set into one string and write |
| * back to the element. |
| * This is intended to be overridden by specific implementations. |
| */ |
| void writeClasses(Set<String> s); |
| } |