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

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
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 ElementList queryAll(String selectors) {
6545 return new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); 6545 return new _FrozenElementList._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" {
(...skipping 1373 matching lines...) Expand 10 before | Expand all | Expand 10 after
7928 if (result == null) throw new StateError("No elements"); 7928 if (result == null) throw new StateError("No elements");
7929 return result; 7929 return result;
7930 } 7930 }
7931 7931
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 /**
7939 * An immutable list containing HTML elements. This list contains some
7940 * additional methods for ease of CSS manipulation on a group of elements.
7941 */
7942 abstract class ElementList<T extends Element> extends ListBase<T> {
7943 /**
7944 * The union of all CSS classes applied to the elements in this list.
7945 *
7946 * This set makes it easy to add, remove or toggle (add if not present, remove
7947 * if present) the classes applied to a collection of elements.
7948 *
7949 * htmlList.classes.add('selected');
7950 * htmlList.classes.toggle('isOnline');
7951 * htmlList.classes.remove('selected');
7952 */
7953 CssClassSet get classes;
7954
7955 /** Replace the classes with `value` for every element in this list. */
7956 set classes(Iterable<String> value);
7957 }
7958
7938 // TODO(jacobr): this is an inefficient implementation but it is hard to see 7959 // 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 7960 // 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 7961 // ElementList as there are valid cases where a NodeList JavaScript object
7941 // contains Node objects that are not Elements. 7962 // contains Node objects that are not Elements.
7942 class _FrozenElementList<T extends Element> extends ListBase<T> { 7963 class _FrozenElementList<T extends Element> extends ListBase<T> implements Eleme ntList {
7943 final List<Node> _nodeList; 7964 final List<Node> _nodeList;
7944 7965
7945 _FrozenElementList._wrap(this._nodeList); 7966 _FrozenElementList._wrap(this._nodeList);
7946 7967
7947 int get length => _nodeList.length; 7968 int get length => _nodeList.length;
7948 7969
7949 Element operator [](int index) => _nodeList[index]; 7970 Element operator [](int index) => _nodeList[index];
7950 7971
7951 void operator []=(int index, Element value) { 7972 void operator []=(int index, Element value) {
7952 throw new UnsupportedError('Cannot modify list'); 7973 throw new UnsupportedError('Cannot modify list');
7953 } 7974 }
7954 7975
7955 void set length(int newLength) { 7976 void set length(int newLength) {
7956 throw new UnsupportedError('Cannot modify list'); 7977 throw new UnsupportedError('Cannot modify list');
7957 } 7978 }
7958 7979
7959 void sort([Comparator<Element> compare]) { 7980 void sort([Comparator<Element> compare]) {
7960 throw new UnsupportedError('Cannot sort list'); 7981 throw new UnsupportedError('Cannot sort list');
7961 } 7982 }
7962 7983
7963 Element get first => _nodeList.first; 7984 Element get first => _nodeList.first;
7964 7985
7965 Element get last => _nodeList.last; 7986 Element get last => _nodeList.last;
7966 7987
7967 Element get single => _nodeList.single; 7988 Element get single => _nodeList.single;
7968 }
7969 7989
7970 class _ElementCssClassSet extends CssClassSet { 7990 CssClassSet get classes => new _MultiElementCssClassSet(
7991 _nodeList.where((e) => e is Element));
7971 7992
7972 final Element _element; 7993 void set classes(Iterable<String> value) {
7973 7994 _nodeList.where((e) => e is Element).forEach((e) => e.classes = value);
7974 _ElementCssClassSet(this._element);
7975
7976 Set<String> readClasses() {
7977 var s = new LinkedHashSet<String>();
7978 var classname = _element.$dom_className;
7979
7980 for (String name in classname.split(' ')) {
7981 String trimmed = name.trim();
7982 if (!trimmed.isEmpty) {
7983 s.add(trimmed);
7984 }
7985 }
7986 return s;
7987 }
7988
7989 void writeClasses(Set<String> s) {
7990 List list = new List.from(s);
7991 _element.$dom_className = s.join(' ');
7992 } 7995 }
7993 } 7996 }
7994 7997
7995 /** 7998 /**
7996 * An abstract class, which all HTML elements extend. 7999 * An abstract class, which all HTML elements extend.
7997 */ 8000 */
7998 @DomName('Element') 8001 @DomName('Element')
7999 abstract class Element extends Node implements ElementTraversal native "Element" { 8002 abstract class Element extends Node implements ElementTraversal native "Element" {
8000 8003
8001 /** 8004 /**
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
8081 } 8084 }
8082 8085
8083 /** 8086 /**
8084 * Finds all descendent elements of this element that match the specified 8087 * Finds all descendent elements of this element that match the specified
8085 * group of selectors. 8088 * group of selectors.
8086 * 8089 *
8087 * [selectors] should be a string using CSS selector syntax. 8090 * [selectors] should be a string using CSS selector syntax.
8088 * 8091 *
8089 * var items = element.query('.itemClassName'); 8092 * var items = element.query('.itemClassName');
8090 */ 8093 */
8091 List<Element> queryAll(String selectors) => 8094 ElementList queryAll(String selectors) =>
8092 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); 8095 new _FrozenElementList._wrap($dom_querySelectorAll(selectors));
8093 8096
8094 /** 8097 /**
8095 * The set of CSS classes applied to this element. 8098 * The set of CSS classes applied to this element.
8096 * 8099 *
8097 * This set makes it easy to add, remove or toggle the classes applied to 8100 * This set makes it easy to add, remove or toggle the classes applied to
8098 * this element. 8101 * this element.
8099 * 8102 *
8100 * element.classes.add('selected'); 8103 * element.classes.add('selected');
8101 * element.classes.toggle('isOnline'); 8104 * element.classes.toggle('isOnline');
(...skipping 18381 matching lines...) Expand 10 before | Expand all | Expand 10 after
26483 abstract class HistoryBase { 26486 abstract class HistoryBase {
26484 void back(); 26487 void back();
26485 void forward(); 26488 void forward();
26486 void go(int distance); 26489 void go(int distance);
26487 } 26490 }
26488 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 26491 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
26489 // for details. All rights reserved. Use of this source code is governed by a 26492 // for details. All rights reserved. Use of this source code is governed by a
26490 // BSD-style license that can be found in the LICENSE file. 26493 // BSD-style license that can be found in the LICENSE file.
26491 26494
26492 26495
26493 abstract class CssClassSet implements Set<String> { 26496 /**
26497 * A set (union) of the CSS classes that are present in a set of elements.
26498 * Implemented separately from _ElementCssClassSet for performance.
26499 */
26500 class _MultiElementCssClassSet extends CssClassSetImpl {
26501 final Iterable<Element> _elementIterable;
26502 Iterable<_ElementCssClassSet> _elementCssClassSetIterable;
26494 26503
26495 String toString() { 26504 _MultiElementCssClassSet(this._elementIterable) {
26496 return readClasses().join(' '); 26505 _elementCssClassSetIterable = new List.from(_elementIterable).map(
26506 (e) => new _ElementCssClassSet(e));
26497 } 26507 }
26498 26508
26499 /** 26509 Set<String> readClasses() {
26500 * Adds the class [value] to the element if it is not on it, removes it if it 26510 var s = new LinkedHashSet<String>();
26501 * is. 26511 _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses()));
26502 */ 26512 return s;
26503 bool toggle(String value) {
26504 Set<String> s = readClasses();
26505 bool result = false;
26506 if (s.contains(value)) {
26507 s.remove(value);
26508 } else {
26509 s.add(value);
26510 result = true;
26511 }
26512 writeClasses(s);
26513 return result;
26514 } 26513 }
26515 26514
26516 /** 26515 void writeClasses(Set<String> s) {
26517 * Returns [:true:] if classes cannot be added or removed from this 26516 var classes = new List.from(s).join(' ');
26518 * [:CssClassSet:]. 26517 for (Element e in _elementIterable) {
26519 */ 26518 e.$dom_className = classes;
26520 bool get frozen => false; 26519 }
26521
26522 // interface Iterable - BEGIN
26523 Iterator<String> get iterator => readClasses().iterator;
26524 // interface Iterable - END
26525
26526 // interface Collection - BEGIN
26527 void forEach(void f(String element)) {
26528 readClasses().forEach(f);
26529 } 26520 }
26530 26521
26531 String join([String separator = ""]) => readClasses().join(separator);
26532
26533 Iterable map(f(String element)) => readClasses().map(f);
26534
26535 Iterable<String> where(bool f(String element)) => readClasses().where(f);
26536
26537 Iterable expand(Iterable f(String element)) => readClasses().expand(f);
26538
26539 bool every(bool f(String element)) => readClasses().every(f);
26540
26541 bool any(bool f(String element)) => readClasses().any(f);
26542
26543 bool get isEmpty => readClasses().isEmpty;
26544
26545 int get length => readClasses().length;
26546
26547 String reduce(String combine(String value, String element)) {
26548 return readClasses().reduce(combine);
26549 }
26550
26551 dynamic fold(dynamic initialValue,
26552 dynamic combine(dynamic previousValue, String element)) {
26553 return readClasses().fold(initialValue, combine);
26554 }
26555 // interface Collection - END
26556
26557 // interface Set - BEGIN
26558 /**
26559 * Determine if this element contains the class [value].
26560 *
26561 * This is the Dart equivalent of jQuery's
26562 * [hasClass](http://api.jquery.com/hasClass/).
26563 */
26564 bool contains(String value) => readClasses().contains(value);
26565
26566 /**
26567 * Add the class [value] to element.
26568 *
26569 * This is the Dart equivalent of jQuery's
26570 * [addClass](http://api.jquery.com/addClass/).
26571 */
26572 void add(String value) {
26573 // TODO - figure out if we need to do any validation here
26574 // or if the browser natively does enough.
26575 _modify((s) => s.add(value));
26576 }
26577
26578 /**
26579 * Remove the class [value] from element, and return true on successful
26580 * removal.
26581 *
26582 * This is the Dart equivalent of jQuery's
26583 * [removeClass](http://api.jquery.com/removeClass/).
26584 */
26585 bool remove(Object value) {
26586 if (value is! String) return false;
26587 Set<String> s = readClasses();
26588 bool result = s.remove(value);
26589 writeClasses(s);
26590 return result;
26591 }
26592
26593 /**
26594 * Add all classes specified in [iterable] to element.
26595 *
26596 * This is the Dart equivalent of jQuery's
26597 * [addClass](http://api.jquery.com/addClass/).
26598 */
26599 void addAll(Iterable<String> iterable) {
26600 // TODO - see comment above about validation.
26601 _modify((s) => s.addAll(iterable));
26602 }
26603
26604 /**
26605 * Remove all classes specified in [iterable] from element.
26606 *
26607 * This is the Dart equivalent of jQuery's
26608 * [removeClass](http://api.jquery.com/removeClass/).
26609 */
26610 void removeAll(Iterable<String> iterable) {
26611 _modify((s) => s.removeAll(iterable));
26612 }
26613
26614 /**
26615 * Toggles all classes specified in [iterable] on element.
26616 *
26617 * 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
26619 * [toggleClass](http://api.jquery.com/toggleClass/).
26620 */
26621 void toggleAll(Iterable<String> iterable) {
26622 iterable.forEach(toggle);
26623 }
26624
26625 void retainAll(Iterable<String> iterable) {
26626 _modify((s) => s.retainAll(iterable));
26627 }
26628
26629 void removeWhere(bool test(String name)) {
26630 _modify((s) => s.removeWhere(test));
26631 }
26632
26633 void retainWhere(bool test(String name)) {
26634 _modify((s) => s.retainWhere(test));
26635 }
26636
26637 bool containsAll(Iterable<String> collection) =>
26638 readClasses().containsAll(collection);
26639
26640 Set<String> intersection(Set<String> other) =>
26641 readClasses().intersection(other);
26642
26643 Set<String> union(Set<String> other) =>
26644 readClasses().union(other);
26645
26646 Set<String> difference(Set<String> other) =>
26647 readClasses().difference(other);
26648
26649 String get first => readClasses().first;
26650 String get last => readClasses().last;
26651 String get single => readClasses().single;
26652 List<String> toList({ bool growable: true }) =>
26653 readClasses().toList(growable: growable);
26654 Set<String> toSet() => readClasses().toSet();
26655 Iterable<String> take(int n) => readClasses().take(n);
26656 Iterable<String> takeWhile(bool test(String value)) =>
26657 readClasses().takeWhile(test);
26658 Iterable<String> skip(int n) => readClasses().skip(n);
26659 Iterable<String> skipWhile(bool test(String value)) =>
26660 readClasses().skipWhile(test);
26661 String firstWhere(bool test(String value), { String orElse() }) =>
26662 readClasses().firstWhere(test, orElse: orElse);
26663 String lastWhere(bool test(String value), {String orElse()}) =>
26664 readClasses().lastWhere(test, orElse: orElse);
26665 String singleWhere(bool test(String value)) =>
26666 readClasses().singleWhere(test);
26667 String elementAt(int index) => readClasses().elementAt(index);
26668
26669 void clear() {
26670 _modify((s) => s.clear());
26671 }
26672 // interface Set - END
26673
26674 /** 26522 /**
26675 * Helper method used to modify the set of css classes on this element. 26523 * Helper method used to modify the set of css classes on this element.
26676 * 26524 *
26677 * f - callback with: 26525 * f - callback with:
26678 * s - a Set of all the css class name currently on this element. 26526 * s - a Set of all the css class name currently on this element.
26679 * 26527 *
26680 * After f returns, the modified set is written to the 26528 * After f returns, the modified set is written to the
26681 * className property of this element. 26529 * className property of this element.
26682 */ 26530 */
26683 void _modify( f(Set<String> s)) { 26531 void _modify( f(Set<String> s)) {
26684 Set<String> s = readClasses(); 26532 _elementCssClassSetIterable.forEach((e) => e._modify(f));
26685 f(s);
26686 writeClasses(s);
26687 } 26533 }
26688 26534
26689 /** 26535 /**
26690 * Read the class names from the Element class property, 26536 * Adds the class [value] to the element if it is not on it, removes it if it
26691 * and put them into a set (duplicates are discarded). 26537 * is.
26692 * This is intended to be overridden by specific implementations.
26693 */ 26538 */
26694 Set<String> readClasses(); 26539 bool toggle(String value) =>
26540 _modifyWithReturnValue((e) => e.toggle(value));
26695 26541
26696 /** 26542 /**
26697 * Join all the elements of a set into one string and write 26543 * Remove the class [value] from element, and return true on successful
26698 * back to the element. 26544 * removal.
26699 * This is intended to be overridden by specific implementations. 26545 *
26546 * This is the Dart equivalent of jQuery's
26547 * [removeClass](http://api.jquery.com/removeClass/).
26700 */ 26548 */
26701 void writeClasses(Set<String> s); 26549 bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value));
26550
26551 bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold(
26552 false, (prevValue, element) => f(element) || prevValue);
26553 }
26554
26555 class _ElementCssClassSet extends CssClassSetImpl {
26556
26557 final Element _element;
26558
26559 _ElementCssClassSet(this._element);
26560
26561 Set<String> readClasses() {
26562 var s = new LinkedHashSet<String>();
26563 var classname = _element.$dom_className;
26564
26565 for (String name in classname.split(' ')) {
26566 String trimmed = name.trim();
26567 if (!trimmed.isEmpty) {
26568 s.add(trimmed);
26569 }
26570 }
26571 return s;
26572 }
26573
26574 void writeClasses(Set<String> s) {
26575 List list = new List.from(s);
26576 _element.$dom_className = s.join(' ');
26577 }
26702 } 26578 }
26703 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 26579 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
26704 // for details. All rights reserved. Use of this source code is governed by a 26580 // for details. All rights reserved. Use of this source code is governed by a
26705 // BSD-style license that can be found in the LICENSE file. 26581 // BSD-style license that can be found in the LICENSE file.
26706 26582
26707 26583
26708 typedef EventListener(Event event); 26584 typedef EventListener(Event event);
26709 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 26585 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
26710 // for details. All rights reserved. Use of this source code is governed by a 26586 // for details. All rights reserved. Use of this source code is governed by a
26711 // BSD-style license that can be found in the LICENSE file. 26587 // BSD-style license that can be found in the LICENSE file.
(...skipping 2864 matching lines...) Expand 10 before | Expand all | Expand 10 after
29576 _position = nextPosition; 29452 _position = nextPosition;
29577 return true; 29453 return true;
29578 } 29454 }
29579 _current = null; 29455 _current = null;
29580 _position = _array.length; 29456 _position = _array.length;
29581 return false; 29457 return false;
29582 } 29458 }
29583 29459
29584 T get current => _current; 29460 T get current => _current;
29585 } 29461 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | sdk/lib/html/html_common/css_class_set.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698