| Index: sdk/lib/html/dart2js/html_dart2js.dart
|
| diff --git a/sdk/lib/html/dart2js/html_dart2js.dart b/sdk/lib/html/dart2js/html_dart2js.dart
|
| index d67a2c9ff3950b3d9a770804f89d73a70f63118f..f8b09a918f502812ceceedfb89adfac00a5d12bf 100644
|
| --- a/sdk/lib/html/dart2js/html_dart2js.dart
|
| +++ b/sdk/lib/html/dart2js/html_dart2js.dart
|
| @@ -6541,8 +6541,8 @@ class Document extends Node native "Document"
|
| * 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
|
| @@ -7939,10 +7939,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;
|
|
|
| @@ -7965,6 +7969,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 {
|
| @@ -8088,8 +8172,8 @@ abstract class Element extends Node implements ElementTraversal native "Element"
|
| *
|
| * 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.
|
| @@ -26572,7 +26656,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));
|
| }
|
|
|
| /**
|
| @@ -26598,7 +26682,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));
|
| }
|
|
|
| /**
|
| @@ -26608,7 +26692,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));
|
| }
|
|
|
| /**
|
| @@ -26623,15 +26707,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) =>
|
| @@ -26667,7 +26751,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
|
|
|
| @@ -26680,7 +26764,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);
|
|
|