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

Side by Side Diff: sdk/lib/html/dart2js/html_dart2js.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, 7 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:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | tools/dom/src/CssClassSet.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /// The Dart HTML library. 1 /// The Dart HTML library.
2 library dart.dom.html; 2 library dart.dom.html;
3 3
4 import 'dart:async'; 4 import 'dart:async';
5 import 'dart:collection'; 5 import 'dart:collection';
6 import 'dart:_collection-dev'; 6 import 'dart:_collection-dev';
7 import 'dart:html_common'; 7 import 'dart:html_common';
8 import 'dart:indexed_db'; 8 import 'dart:indexed_db';
9 import 'dart:isolate'; 9 import 'dart:isolate';
10 import 'dart:json' as json; 10 import 'dart:json' as json;
(...skipping 6523 matching lines...) Expand 10 before | Expand all | Expand 10 after
6534 * Unless your webpage contains multiple documents, the top-level queryAll 6534 * Unless your webpage contains multiple documents, the top-level queryAll
6535 * method behaves the same as this method, so you should use it instead to 6535 * method behaves the same as this method, so you should use it instead to
6536 * save typing a few characters. 6536 * save typing a few characters.
6537 * 6537 *
6538 * [selectors] should be a string using CSS selector syntax. 6538 * [selectors] should be a string using CSS selector syntax.
6539 * var items = document.queryAll('.itemClassName'); 6539 * var items = document.queryAll('.itemClassName');
6540 * 6540 *
6541 * For details about CSS selector syntax, see the 6541 * For details about CSS selector syntax, see the
6542 * [CSS selector specification](http://www.w3.org/TR/css3-selectors/). 6542 * [CSS selector specification](http://www.w3.org/TR/css3-selectors/).
6543 */ 6543 */
6544 List<Element> queryAll(String selectors) { 6544 HtmlList<Element> queryAll(String selectors) {
6545 return new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); 6545 return new HtmlList._wrap($dom_querySelectorAll(selectors));
6546 } 6546 }
6547 } 6547 }
6548 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 6548 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
6549 // for details. All rights reserved. Use of this source code is governed by a 6549 // for details. All rights reserved. Use of this source code is governed by a
6550 // BSD-style license that can be found in the LICENSE file. 6550 // BSD-style license that can be found in the LICENSE file.
6551 6551
6552 6552
6553 @DomName('DocumentFragment') 6553 @DomName('DocumentFragment')
6554 class DocumentFragment extends Node native "DocumentFragment" { 6554 class DocumentFragment extends Node native "DocumentFragment" {
6555 factory DocumentFragment() => _DocumentFragmentFactoryProvider.createDocumentF ragment(); 6555 factory DocumentFragment() => _DocumentFragmentFactoryProvider.createDocumentF ragment();
(...skipping 1376 matching lines...) Expand 10 before | Expand all | Expand 10 after
7932 Element get single { 7932 Element get single {
7933 if (length > 1) throw new StateError("More than one element"); 7933 if (length > 1) throw new StateError("More than one element");
7934 return first; 7934 return first;
7935 } 7935 }
7936 } 7936 }
7937 7937
7938 // TODO(jacobr): this is an inefficient implementation but it is hard to see 7938 // TODO(jacobr): this is an inefficient implementation but it is hard to see
7939 // a better option given that we cannot quite force NodeList to be an 7939 // a better option given that we cannot quite force NodeList to be an
7940 // ElementList as there are valid cases where a NodeList JavaScript object 7940 // ElementList as there are valid cases where a NodeList JavaScript object
7941 // contains Node objects that are not Elements. 7941 // contains Node objects that are not Elements.
7942 class _FrozenElementList<T extends Element> extends ListBase<T> { 7942 /**
7943 * An immutable list containing HTML elements. This list contains some
7944 * additional methods for ease of CSS manipulation on a group of elements.
7945 */
7946 class HtmlList<T extends Element> extends ListBase<T> {
7943 final List<Node> _nodeList; 7947 final List<Node> _nodeList;
7944 7948
7945 _FrozenElementList._wrap(this._nodeList); 7949 HtmlList._wrap(this._nodeList);
7946 7950
7947 int get length => _nodeList.length; 7951 int get length => _nodeList.length;
7948 7952
7949 Element operator [](int index) => _nodeList[index]; 7953 Element operator [](int index) => _nodeList[index];
7950 7954
7951 void operator []=(int index, Element value) { 7955 void operator []=(int index, Element value) {
7952 throw new UnsupportedError('Cannot modify list'); 7956 throw new UnsupportedError('Cannot modify list');
7953 } 7957 }
7954 7958
7955 void set length(int newLength) { 7959 void set length(int newLength) {
7956 throw new UnsupportedError('Cannot modify list'); 7960 throw new UnsupportedError('Cannot modify list');
7957 } 7961 }
7958 7962
7959 void sort([Comparator<Element> compare]) { 7963 void sort([Comparator<Element> compare]) {
7960 throw new UnsupportedError('Cannot sort list'); 7964 throw new UnsupportedError('Cannot sort list');
7961 } 7965 }
7962 7966
7963 Element get first => _nodeList.first; 7967 Element get first => _nodeList.first;
7964 7968
7965 Element get last => _nodeList.last; 7969 Element get last => _nodeList.last;
7966 7970
7967 Element get single => _nodeList.single; 7971 Element get single => _nodeList.single;
7972
7973 /**
7974 * The union of all CSS classes applied to the elements in this list.
7975 *
7976 * This set makes it easy to add, remove or toggle (add if not present, remove
7977 * if present) the classes applied to a collection of elements.
7978 *
7979 * htmlList.classes.add('selected');
7980 * htmlList.classes.toggle('isOnline');
7981 * htmlList.classes.remove('selected');
7982 */
7983 CssClassSet get classes => new _MultiElementCssClassSet(
7984 _nodeList.where((e) => e is Element));
7985
7986 /** Replace the classes with `value` for every element in this list. */
7987 void set classes(Iterable<String> value) {
7988 _nodeList.where((e) => e is Element).forEach((e) {
7989 e.classes.clear();
7990 e.classes.addAll(value);
7991 });
7992 }
7993 }
7994
7995 /**
7996 * A set (union) of the CSS classes that are present in a set of elements.
7997 * Implemented separately from _ElementCssClassSet for performance.
7998 */
7999 class _MultiElementCssClassSet extends CssClassSet {
8000 final Iterable<Element> _elementIterable;
8001 Iterable<_ElementCssClassSet> _elementCssClassSetIterable;
8002
8003 _MultiElementCssClassSet(this._elementIterable) {
8004 _elementCssClassSetIterable = new List.from(_elementIterable).map(
8005 (e) => new _ElementCssClassSet(e));
8006 }
8007
8008 Set<String> readClasses() {
8009 var s = new LinkedHashSet<String>();
8010 _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses()));
8011 return s;
8012 }
8013
8014 void writeClasses(Set<String> s) {
8015 var classes = new List.from(s).join(' ');
8016 for (Element e in _elementIterable) {
8017 e.$dom_className = classes;
8018 }
8019 }
8020
8021 /**
8022 * Helper method used to modify the set of css classes on this element.
8023 *
8024 * f - callback with:
8025 * s - a Set of all the css class name currently on this element.
8026 *
8027 * After f returns, the modified set is written to the
8028 * className property of this element.
8029 */
8030 void modify( f(Set<String> s)) {
8031 _elementCssClassSetIterable.forEach((e) => e.modify(f));
8032 }
8033
8034 /**
8035 * Adds the class [value] to the element if it is not on it, removes it if it
8036 * is.
8037 */
8038 bool toggle(String value) =>
8039 _modifyWithReturnValue((e) => e.toggle(value));
8040
8041 /**
8042 * Remove the class [value] from element, and return true on successful
8043 * removal.
8044 *
8045 * This is the Dart equivalent of jQuery's
8046 * [removeClass](http://api.jquery.com/removeClass/).
8047 */
8048 bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value));
8049
8050 bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold(
8051 false, (prevValue, element) => f(element) || prevValue);
7968 } 8052 }
7969 8053
7970 class _ElementCssClassSet extends CssClassSet { 8054 class _ElementCssClassSet extends CssClassSet {
7971 8055
7972 final Element _element; 8056 final Element _element;
7973 8057
7974 _ElementCssClassSet(this._element); 8058 _ElementCssClassSet(this._element);
7975 8059
7976 Set<String> readClasses() { 8060 Set<String> readClasses() {
7977 var s = new LinkedHashSet<String>(); 8061 var s = new LinkedHashSet<String>();
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
8081 } 8165 }
8082 8166
8083 /** 8167 /**
8084 * Finds all descendent elements of this element that match the specified 8168 * Finds all descendent elements of this element that match the specified
8085 * group of selectors. 8169 * group of selectors.
8086 * 8170 *
8087 * [selectors] should be a string using CSS selector syntax. 8171 * [selectors] should be a string using CSS selector syntax.
8088 * 8172 *
8089 * var items = element.query('.itemClassName'); 8173 * var items = element.query('.itemClassName');
8090 */ 8174 */
8091 List<Element> queryAll(String selectors) => 8175 HtmlList<Element> queryAll(String selectors) =>
8092 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); 8176 new HtmlList._wrap($dom_querySelectorAll(selectors));
8093 8177
8094 /** 8178 /**
8095 * The set of CSS classes applied to this element. 8179 * The set of CSS classes applied to this element.
8096 * 8180 *
8097 * This set makes it easy to add, remove or toggle the classes applied to 8181 * This set makes it easy to add, remove or toggle the classes applied to
8098 * this element. 8182 * this element.
8099 * 8183 *
8100 * element.classes.add('selected'); 8184 * element.classes.add('selected');
8101 * element.classes.toggle('isOnline'); 8185 * element.classes.toggle('isOnline');
8102 * element.classes.remove('selected'); 8186 * element.classes.remove('selected');
(...skipping 18462 matching lines...) Expand 10 before | Expand all | Expand 10 after
26565 26649
26566 /** 26650 /**
26567 * Add the class [value] to element. 26651 * Add the class [value] to element.
26568 * 26652 *
26569 * This is the Dart equivalent of jQuery's 26653 * This is the Dart equivalent of jQuery's
26570 * [addClass](http://api.jquery.com/addClass/). 26654 * [addClass](http://api.jquery.com/addClass/).
26571 */ 26655 */
26572 void add(String value) { 26656 void add(String value) {
26573 // TODO - figure out if we need to do any validation here 26657 // TODO - figure out if we need to do any validation here
26574 // or if the browser natively does enough. 26658 // or if the browser natively does enough.
26575 _modify((s) => s.add(value)); 26659 modify((s) => s.add(value));
26576 } 26660 }
26577 26661
26578 /** 26662 /**
26579 * Remove the class [value] from element, and return true on successful 26663 * Remove the class [value] from element, and return true on successful
26580 * removal. 26664 * removal.
26581 * 26665 *
26582 * This is the Dart equivalent of jQuery's 26666 * This is the Dart equivalent of jQuery's
26583 * [removeClass](http://api.jquery.com/removeClass/). 26667 * [removeClass](http://api.jquery.com/removeClass/).
26584 */ 26668 */
26585 bool remove(Object value) { 26669 bool remove(Object value) {
26586 if (value is! String) return false; 26670 if (value is! String) return false;
26587 Set<String> s = readClasses(); 26671 Set<String> s = readClasses();
26588 bool result = s.remove(value); 26672 bool result = s.remove(value);
26589 writeClasses(s); 26673 writeClasses(s);
26590 return result; 26674 return result;
26591 } 26675 }
26592 26676
26593 /** 26677 /**
26594 * Add all classes specified in [iterable] to element. 26678 * Add all classes specified in [iterable] to element.
26595 * 26679 *
26596 * This is the Dart equivalent of jQuery's 26680 * This is the Dart equivalent of jQuery's
26597 * [addClass](http://api.jquery.com/addClass/). 26681 * [addClass](http://api.jquery.com/addClass/).
26598 */ 26682 */
26599 void addAll(Iterable<String> iterable) { 26683 void addAll(Iterable<String> iterable) {
26600 // TODO - see comment above about validation. 26684 // TODO - see comment above about validation.
26601 _modify((s) => s.addAll(iterable)); 26685 modify((s) => s.addAll(iterable));
26602 } 26686 }
26603 26687
26604 /** 26688 /**
26605 * Remove all classes specified in [iterable] from element. 26689 * Remove all classes specified in [iterable] from element.
26606 * 26690 *
26607 * This is the Dart equivalent of jQuery's 26691 * This is the Dart equivalent of jQuery's
26608 * [removeClass](http://api.jquery.com/removeClass/). 26692 * [removeClass](http://api.jquery.com/removeClass/).
26609 */ 26693 */
26610 void removeAll(Iterable<String> iterable) { 26694 void removeAll(Iterable<String> iterable) {
26611 _modify((s) => s.removeAll(iterable)); 26695 modify((s) => s.removeAll(iterable));
26612 } 26696 }
26613 26697
26614 /** 26698 /**
26615 * Toggles all classes specified in [iterable] on element. 26699 * Toggles all classes specified in [iterable] on element.
26616 * 26700 *
26617 * Iterate through [iterable]'s items, and add it if it is not on it, or 26701 * Iterate through [iterable]'s items, and add it if it is not on it, or
26618 * remove it if it is. This is the Dart equivalent of jQuery's 26702 * remove it if it is. This is the Dart equivalent of jQuery's
26619 * [toggleClass](http://api.jquery.com/toggleClass/). 26703 * [toggleClass](http://api.jquery.com/toggleClass/).
26620 */ 26704 */
26621 void toggleAll(Iterable<String> iterable) { 26705 void toggleAll(Iterable<String> iterable) {
26622 iterable.forEach(toggle); 26706 iterable.forEach(toggle);
26623 } 26707 }
26624 26708
26625 void retainAll(Iterable<String> iterable) { 26709 void retainAll(Iterable<String> iterable) {
26626 _modify((s) => s.retainAll(iterable)); 26710 modify((s) => s.retainAll(iterable));
26627 } 26711 }
26628 26712
26629 void removeWhere(bool test(String name)) { 26713 void removeWhere(bool test(String name)) {
26630 _modify((s) => s.removeWhere(test)); 26714 modify((s) => s.removeWhere(test));
26631 } 26715 }
26632 26716
26633 void retainWhere(bool test(String name)) { 26717 void retainWhere(bool test(String name)) {
26634 _modify((s) => s.retainWhere(test)); 26718 modify((s) => s.retainWhere(test));
26635 } 26719 }
26636 26720
26637 bool containsAll(Iterable<String> collection) => 26721 bool containsAll(Iterable<String> collection) =>
26638 readClasses().containsAll(collection); 26722 readClasses().containsAll(collection);
26639 26723
26640 Set<String> intersection(Set<String> other) => 26724 Set<String> intersection(Set<String> other) =>
26641 readClasses().intersection(other); 26725 readClasses().intersection(other);
26642 26726
26643 Set<String> union(Set<String> other) => 26727 Set<String> union(Set<String> other) =>
26644 readClasses().union(other); 26728 readClasses().union(other);
(...skipping 15 matching lines...) Expand all
26660 readClasses().skipWhile(test); 26744 readClasses().skipWhile(test);
26661 String firstWhere(bool test(String value), { String orElse() }) => 26745 String firstWhere(bool test(String value), { String orElse() }) =>
26662 readClasses().firstWhere(test, orElse: orElse); 26746 readClasses().firstWhere(test, orElse: orElse);
26663 String lastWhere(bool test(String value), {String orElse()}) => 26747 String lastWhere(bool test(String value), {String orElse()}) =>
26664 readClasses().lastWhere(test, orElse: orElse); 26748 readClasses().lastWhere(test, orElse: orElse);
26665 String singleWhere(bool test(String value)) => 26749 String singleWhere(bool test(String value)) =>
26666 readClasses().singleWhere(test); 26750 readClasses().singleWhere(test);
26667 String elementAt(int index) => readClasses().elementAt(index); 26751 String elementAt(int index) => readClasses().elementAt(index);
26668 26752
26669 void clear() { 26753 void clear() {
26670 _modify((s) => s.clear()); 26754 modify((s) => s.clear());
26671 } 26755 }
26672 // interface Set - END 26756 // interface Set - END
26673 26757
26674 /** 26758 /**
26675 * Helper method used to modify the set of css classes on this element. 26759 * Helper method used to modify the set of css classes on this element.
26676 * 26760 *
26677 * f - callback with: 26761 * f - callback with:
26678 * s - a Set of all the css class name currently on this element. 26762 * s - a Set of all the css class name currently on this element.
26679 * 26763 *
26680 * After f returns, the modified set is written to the 26764 * After f returns, the modified set is written to the
26681 * className property of this element. 26765 * className property of this element.
26682 */ 26766 */
26683 void _modify( f(Set<String> s)) { 26767 void modify( f(Set<String> s)) {
26684 Set<String> s = readClasses(); 26768 Set<String> s = readClasses();
26685 f(s); 26769 f(s);
26686 writeClasses(s); 26770 writeClasses(s);
26687 } 26771 }
26688 26772
26689 /** 26773 /**
26690 * Read the class names from the Element class property, 26774 * Read the class names from the Element class property,
26691 * and put them into a set (duplicates are discarded). 26775 * and put them into a set (duplicates are discarded).
26692 * This is intended to be overridden by specific implementations. 26776 * This is intended to be overridden by specific implementations.
26693 */ 26777 */
(...skipping 2882 matching lines...) Expand 10 before | Expand all | Expand 10 after
29576 _position = nextPosition; 29660 _position = nextPosition;
29577 return true; 29661 return true;
29578 } 29662 }
29579 _current = null; 29663 _current = null;
29580 _position = _array.length; 29664 _position = _array.length;
29581 return false; 29665 return false;
29582 } 29666 }
29583 29667
29584 T get current => _current; 29668 T get current => _current;
29585 } 29669 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | tools/dom/src/CssClassSet.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698