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

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..d3035abfcf1568aecb599a9d67df9bec4df505e0 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -6938,8 +6938,8 @@ 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) {
- return new _FrozenElementList._wrap($dom_querySelectorAll(selectors));
+ HtmlList<Element> queryAll(String selectors) {
+ return new HtmlList._wrap($dom_querySelectorAll(selectors));
}
}
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
@@ -8405,10 +8405,14 @@ class _ChildrenElementList extends ListBase<Element> {
// 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> {
+/**
+ * An immutable list containing HTML elements. This list contains some
+ * additional methods for ease of CSS manipulation on a group of elements.
+ */
+class HtmlList<T extends Element> extends ListBase<T> {
final List<Node> _nodeList;
- _FrozenElementList._wrap(this._nodeList);
+ HtmlList._wrap(this._nodeList);
int get length => _nodeList.length;
@@ -8431,6 +8435,86 @@ class _FrozenElementList<T extends Element> extends ListBase<T> {
Element get last => _nodeList.last;
Element get single => _nodeList.single;
+
+ /**
+ * 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 => new _MultiElementCssClassSet(
+ _nodeList.where((e) => e is Element));
+
+ /** Replace the classes with `value` for every element in this list. */
+ void set classes(Iterable<String> value) {
+ _nodeList.where((e) => e is Element).forEach((e) {
+ e.classes.clear();
+ e.classes.addAll(value);
+ });
+ }
+}
+
+/**
+ * A set (union) of the CSS classes that are present in a set of elements.
+ * Implemented separately from _ElementCssClassSet for performance.
+ */
+class _MultiElementCssClassSet extends CssClassSet {
+ 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 CssClassSet {
@@ -8554,8 +8638,8 @@ abstract class Element extends Node implements ElementTraversal {
*
* var items = element.query('.itemClassName');
*/
- List<Element> queryAll(String selectors) =>
- new _FrozenElementList._wrap($dom_querySelectorAll(selectors));
+ HtmlList<Element> queryAll(String selectors) =>
+ new HtmlList._wrap($dom_querySelectorAll(selectors));
/**
* The set of CSS classes applied to this element.
@@ -28281,7 +28365,7 @@ abstract class CssClassSet implements Set<String> {
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));
+ modify((s) => s.add(value));
}
/**
@@ -28307,7 +28391,7 @@ abstract class CssClassSet implements Set<String> {
*/
void addAll(Iterable<String> iterable) {
// TODO - see comment above about validation.
- _modify((s) => s.addAll(iterable));
+ modify((s) => s.addAll(iterable));
}
/**
@@ -28317,7 +28401,7 @@ abstract class CssClassSet implements Set<String> {
* [removeClass](http://api.jquery.com/removeClass/).
*/
void removeAll(Iterable<String> iterable) {
- _modify((s) => s.removeAll(iterable));
+ modify((s) => s.removeAll(iterable));
}
/**
@@ -28332,15 +28416,15 @@ abstract class CssClassSet implements Set<String> {
}
void retainAll(Iterable<String> iterable) {
- _modify((s) => s.retainAll(iterable));
+ modify((s) => s.retainAll(iterable));
}
void removeWhere(bool test(String name)) {
- _modify((s) => s.removeWhere(test));
+ modify((s) => s.removeWhere(test));
}
void retainWhere(bool test(String name)) {
- _modify((s) => s.retainWhere(test));
+ modify((s) => s.retainWhere(test));
}
bool containsAll(Iterable<String> collection) =>
@@ -28376,7 +28460,7 @@ abstract class CssClassSet implements Set<String> {
String elementAt(int index) => readClasses().elementAt(index);
void clear() {
- _modify((s) => s.clear());
+ modify((s) => s.clear());
}
// interface Set - END
@@ -28389,7 +28473,7 @@ abstract class CssClassSet implements Set<String> {
* After f returns, the modified set is written to the
* className property of this element.
*/
- void _modify( f(Set<String> s)) {
+ void modify( f(Set<String> s)) {
Set<String> s = readClasses();
f(s);
writeClasses(s);

Powered by Google App Engine
This is Rietveld 408576698