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

Unified Diff: sdk/lib/html/dartium/html_dartium.dart

Issue 14941002: Aggregate CSS manipulation functions in html lib. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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:
Download patch
Index: sdk/lib/html/dartium/html_dartium.dart
diff --git a/sdk/lib/html/dartium/html_dartium.dart b/sdk/lib/html/dartium/html_dartium.dart
index 40efe9a7430e0bf39a921474356c6834d81e778b..0b2f05022eee529fc463dde7d765012f3a1f55e9 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -6938,7 +6938,7 @@ class Document extends Node
* For details about CSS selector syntax, see the
* [CSS selector specification](http://www.w3.org/TR/css3-selectors/).
*/
- List<Element> queryAll(String selectors) {
+ ElementList queryAll(String selectors) {
return new _FrozenElementList._wrap($dom_querySelectorAll(selectors));
}
}
@@ -8401,11 +8401,32 @@ class _ChildrenElementList extends ListBase<Element> {
}
}
+/**
+ * An immutable list containing HTML elements. This list contains some
+ * additional methods for ease of CSS manipulation on a group of elements.
+ */
+abstract class ElementList extends ListBase {
+ /**
+ * The union of all CSS classes applied to the elements in this list.
+ *
+ * This set makes it easy to add, remove or toggle (add if not present, remove
+ * if present) the classes applied to a collection of elements.
+ *
+ * htmlList.classes.add('selected');
+ * htmlList.classes.toggle('isOnline');
+ * htmlList.classes.remove('selected');
+ */
+ CssClassSet get classes;
+
+ /** Replace the classes with `value` for every element in this list. */
+ set classes(Iterable<String> value);
+}
+
// TODO(jacobr): this is an inefficient implementation but it is hard to see
// a better option given that we cannot quite force NodeList to be an
// ElementList as there are valid cases where a NodeList JavaScript object
// contains Node objects that are not Elements.
-class _FrozenElementList<T extends Element> extends ListBase<T> {
+class _FrozenElementList<T extends Element> extends ListBase<T> implements ElementList {
final List<Node> _nodeList;
_FrozenElementList._wrap(this._nodeList);
@@ -8431,30 +8452,15 @@ class _FrozenElementList<T extends Element> extends ListBase<T> {
Element get last => _nodeList.last;
Element get single => _nodeList.single;
-}
-class _ElementCssClassSet extends CssClassSet {
+ CssClassSet get classes => new _MultiElementCssClassSet(
+ _nodeList.where((e) => e is Element));
- final Element _element;
-
- _ElementCssClassSet(this._element);
-
- Set<String> readClasses() {
- var s = new LinkedHashSet<String>();
- var classname = _element.$dom_className;
-
- for (String name in classname.split(' ')) {
- String trimmed = name.trim();
- if (!trimmed.isEmpty) {
- s.add(trimmed);
- }
- }
- return s;
- }
-
- void writeClasses(Set<String> s) {
- List list = new List.from(s);
- _element.$dom_className = s.join(' ');
+ void set classes(Iterable<String> value) {
+ _nodeList.where((e) => e is Element).forEach((e) {
+ e.classes.clear();
+ e.classes.addAll(value);
+ });
}
}
@@ -8554,7 +8560,7 @@ abstract class Element extends Node implements ElementTraversal {
*
* var items = element.query('.itemClassName');
*/
- List<Element> queryAll(String selectors) =>
+ ElementList queryAll(String selectors) =>
new _FrozenElementList._wrap($dom_querySelectorAll(selectors));
/**
@@ -28201,6 +28207,70 @@ abstract class HistoryBase {
abstract class CssClassSet implements Set<String> {
+ /**
+ * Adds the class [value] to the element if it is not on it, removes it if it
+ * is.
+ */
+ bool toggle(String value);
+
+ /**
+ * Returns [:true:] if classes cannot be added or removed from this
+ * [:CssClassSet:].
+ */
+ bool get frozen;
+
+ /**
+ * Determine if this element contains the class [value].
+ *
+ * This is the Dart equivalent of jQuery's
+ * [hasClass](http://api.jquery.com/hasClass/).
+ */
+ bool contains(String value);
+
+ /**
+ * Add the class [value] to element.
+ *
+ * This is the Dart equivalent of jQuery's
+ * [addClass](http://api.jquery.com/addClass/).
+ */
+ void add(String value);
+
+ /**
+ * Remove the class [value] from element, and return true on successful
+ * removal.
+ *
+ * This is the Dart equivalent of jQuery's
+ * [removeClass](http://api.jquery.com/removeClass/).
+ */
+ bool remove(Object value);
+
+ /**
+ * Add all classes specified in [iterable] to element.
+ *
+ * This is the Dart equivalent of jQuery's
+ * [addClass](http://api.jquery.com/addClass/).
+ */
+ void addAll(Iterable<String> iterable);
+
+ /**
+ * Remove all classes specified in [iterable] from element.
+ *
+ * This is the Dart equivalent of jQuery's
+ * [removeClass](http://api.jquery.com/removeClass/).
+ */
+ void removeAll(Iterable<String> iterable);
+
+ /**
+ * Toggles all classes specified in [iterable] on element.
+ *
+ * Iterate through [iterable]'s items, and add it if it is not on it, or
+ * remove it if it is. This is the Dart equivalent of jQuery's
+ * [toggleClass](http://api.jquery.com/toggleClass/).
+ */
+ void toggleAll(Iterable<String> iterable);
+}
+abstract class _CssClassSetImpl implements CssClassSet {
+
String toString() {
return readClasses().join(' ');
}
@@ -28409,6 +28479,90 @@ abstract class CssClassSet implements Set<String> {
*/
void writeClasses(Set<String> s);
}
+
+/**
+ * A set (union) of the CSS classes that are present in a set of elements.
+ * Implemented separately from _ElementCssClassSet for performance.
+ */
+class _MultiElementCssClassSet extends _CssClassSetImpl {
+ final Iterable<Element> _elementIterable;
+ Iterable<_ElementCssClassSet> _elementCssClassSetIterable;
+
+ _MultiElementCssClassSet(this._elementIterable) {
+ _elementCssClassSetIterable = new List.from(_elementIterable).map(
+ (e) => new _ElementCssClassSet(e));
+ }
+
+ Set<String> readClasses() {
+ var s = new LinkedHashSet<String>();
+ _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses()));
+ return s;
+ }
+
+ void writeClasses(Set<String> s) {
+ var classes = new List.from(s).join(' ');
+ for (Element e in _elementIterable) {
+ e.$dom_className = classes;
+ }
+ }
+
+ /**
+ * 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)) {
+ _elementCssClassSetIterable.forEach((e) => e._modify(f));
+ }
+
+ /**
+ * Adds the class [value] to the element if it is not on it, removes it if it
+ * is.
+ */
+ bool toggle(String value) =>
+ _modifyWithReturnValue((e) => e.toggle(value));
+
+ /**
+ * Remove the class [value] from element, and return true on successful
+ * removal.
+ *
+ * This is the Dart equivalent of jQuery's
+ * [removeClass](http://api.jquery.com/removeClass/).
+ */
+ bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value));
+
+ bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold(
+ false, (prevValue, element) => f(element) || prevValue);
+}
+
+class _ElementCssClassSet extends _CssClassSetImpl {
+
+ final Element _element;
+
+ _ElementCssClassSet(this._element);
+
+ Set<String> readClasses() {
+ var s = new LinkedHashSet<String>();
+ var classname = _element.$dom_className;
+
+ for (String name in classname.split(' ')) {
+ String trimmed = name.trim();
+ if (!trimmed.isEmpty) {
+ s.add(trimmed);
+ }
+ }
+ return s;
+ }
+
+ void writeClasses(Set<String> s) {
+ List list = new List.from(s);
+ _element.$dom_className = s.join(' ');
+ }
+}
// Copyright (c) 2011, 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.

Powered by Google App Engine
This is Rietveld 408576698