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

Side by Side Diff: sdk/lib/html/dart2js/html_dart2js.dart

Issue 14931008: Reapply "Aggregate CSS manipulation functions in html lib." (r22400) (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') | no next file with comments »
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 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
26496 /** A Set that stores the CSS class names for an element. */
26493 abstract class CssClassSet implements Set<String> { 26497 abstract class CssClassSet implements Set<String> {
26494 26498
26495 String toString() {
26496 return readClasses().join(' ');
26497 }
26498
26499 /** 26499 /**
26500 * Adds the class [value] to the element if it is not on it, removes it if it 26500 * Adds the class [value] to the element if it is not on it, removes it if it
26501 * is. 26501 * is.
26502 */ 26502 */
26503 bool toggle(String value) { 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 }
26515 26504
26516 /** 26505 /**
26517 * Returns [:true:] if classes cannot be added or removed from this 26506 * Returns [:true:] if classes cannot be added or removed from this
26518 * [:CssClassSet:]. 26507 * [:CssClassSet:].
26519 */ 26508 */
26520 bool get frozen => false; 26509 bool get frozen;
26521 26510
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 }
26530
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 /** 26511 /**
26559 * Determine if this element contains the class [value]. 26512 * Determine if this element contains the class [value].
26560 * 26513 *
26561 * This is the Dart equivalent of jQuery's 26514 * This is the Dart equivalent of jQuery's
26562 * [hasClass](http://api.jquery.com/hasClass/). 26515 * [hasClass](http://api.jquery.com/hasClass/).
26563 */ 26516 */
26564 bool contains(String value) => readClasses().contains(value); 26517 bool contains(String value);
26565 26518
26566 /** 26519 /**
26567 * Add the class [value] to element. 26520 * Add the class [value] to element.
26568 * 26521 *
26569 * This is the Dart equivalent of jQuery's 26522 * This is the Dart equivalent of jQuery's
26570 * [addClass](http://api.jquery.com/addClass/). 26523 * [addClass](http://api.jquery.com/addClass/).
26571 */ 26524 */
26572 void add(String value) { 26525 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 26526
26578 /** 26527 /**
26579 * Remove the class [value] from element, and return true on successful 26528 * Remove the class [value] from element, and return true on successful
26580 * removal. 26529 * removal.
26581 * 26530 *
26582 * This is the Dart equivalent of jQuery's 26531 * This is the Dart equivalent of jQuery's
26583 * [removeClass](http://api.jquery.com/removeClass/). 26532 * [removeClass](http://api.jquery.com/removeClass/).
26584 */ 26533 */
26585 bool remove(Object value) { 26534 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 26535
26593 /** 26536 /**
26594 * Add all classes specified in [iterable] to element. 26537 * Add all classes specified in [iterable] to element.
26595 * 26538 *
26596 * This is the Dart equivalent of jQuery's 26539 * This is the Dart equivalent of jQuery's
26597 * [addClass](http://api.jquery.com/addClass/). 26540 * [addClass](http://api.jquery.com/addClass/).
26598 */ 26541 */
26599 void addAll(Iterable<String> iterable) { 26542 void addAll(Iterable<String> iterable);
26600 // TODO - see comment above about validation.
26601 _modify((s) => s.addAll(iterable));
26602 }
26603 26543
26604 /** 26544 /**
26605 * Remove all classes specified in [iterable] from element. 26545 * Remove all classes specified in [iterable] from element.
26606 * 26546 *
26607 * This is the Dart equivalent of jQuery's 26547 * This is the Dart equivalent of jQuery's
26608 * [removeClass](http://api.jquery.com/removeClass/). 26548 * [removeClass](http://api.jquery.com/removeClass/).
26609 */ 26549 */
26610 void removeAll(Iterable<String> iterable) { 26550 void removeAll(Iterable<String> iterable);
26611 _modify((s) => s.removeAll(iterable));
26612 }
26613 26551
26614 /** 26552 /**
26615 * Toggles all classes specified in [iterable] on element. 26553 * Toggles all classes specified in [iterable] on element.
26616 * 26554 *
26617 * Iterate through [iterable]'s items, and add it if it is not on it, or 26555 * 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 26556 * remove it if it is. This is the Dart equivalent of jQuery's
26619 * [toggleClass](http://api.jquery.com/toggleClass/). 26557 * [toggleClass](http://api.jquery.com/toggleClass/).
26620 */ 26558 */
26621 void toggleAll(Iterable<String> iterable) { 26559 void toggleAll(Iterable<String> iterable);
26622 iterable.forEach(toggle); 26560 }
26561
26562 /**
26563 * A set (union) of the CSS classes that are present in a set of elements.
26564 * Implemented separately from _ElementCssClassSet for performance.
26565 */
26566 class _MultiElementCssClassSet extends CssClassSetImpl {
26567 final Iterable<Element> _elementIterable;
26568 Iterable<_ElementCssClassSet> _elementCssClassSetIterable;
26569
26570 _MultiElementCssClassSet(this._elementIterable) {
26571 _elementCssClassSetIterable = new List.from(_elementIterable).map(
26572 (e) => new _ElementCssClassSet(e));
26623 } 26573 }
26624 26574
26625 void retainAll(Iterable<String> iterable) { 26575 Set<String> readClasses() {
26626 _modify((s) => s.retainAll(iterable)); 26576 var s = new LinkedHashSet<String>();
26577 _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses()));
26578 return s;
26627 } 26579 }
26628 26580
26629 void removeWhere(bool test(String name)) { 26581 void writeClasses(Set<String> s) {
26630 _modify((s) => s.removeWhere(test)); 26582 var classes = new List.from(s).join(' ');
26583 for (Element e in _elementIterable) {
26584 e.$dom_className = classes;
26585 }
26631 } 26586 }
26632 26587
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 /** 26588 /**
26675 * Helper method used to modify the set of css classes on this element. 26589 * Helper method used to modify the set of css classes on this element.
26676 * 26590 *
26677 * f - callback with: 26591 * f - callback with:
26678 * s - a Set of all the css class name currently on this element. 26592 * s - a Set of all the css class name currently on this element.
26679 * 26593 *
26680 * After f returns, the modified set is written to the 26594 * After f returns, the modified set is written to the
26681 * className property of this element. 26595 * className property of this element.
26682 */ 26596 */
26683 void _modify( f(Set<String> s)) { 26597 void modify( f(Set<String> s)) {
26684 Set<String> s = readClasses(); 26598 _elementCssClassSetIterable.forEach((e) => e.modify(f));
26685 f(s);
26686 writeClasses(s);
26687 } 26599 }
26688 26600
26689 /** 26601 /**
26690 * Read the class names from the Element class property, 26602 * 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). 26603 * is.
26692 * This is intended to be overridden by specific implementations.
26693 */ 26604 */
26694 Set<String> readClasses(); 26605 bool toggle(String value) =>
26606 _modifyWithReturnValue((e) => e.toggle(value));
26695 26607
26696 /** 26608 /**
26697 * Join all the elements of a set into one string and write 26609 * Remove the class [value] from element, and return true on successful
26698 * back to the element. 26610 * removal.
26699 * This is intended to be overridden by specific implementations. 26611 *
26612 * This is the Dart equivalent of jQuery's
26613 * [removeClass](http://api.jquery.com/removeClass/).
26700 */ 26614 */
26701 void writeClasses(Set<String> s); 26615 bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value));
26616
26617 bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold(
26618 false, (prevValue, element) => f(element) || prevValue);
26619 }
26620
26621 class _ElementCssClassSet extends CssClassSetImpl {
26622
26623 final Element _element;
26624
26625 _ElementCssClassSet(this._element);
26626
26627 Set<String> readClasses() {
26628 var s = new LinkedHashSet<String>();
26629 var classname = _element.$dom_className;
26630
26631 for (String name in classname.split(' ')) {
26632 String trimmed = name.trim();
26633 if (!trimmed.isEmpty) {
26634 s.add(trimmed);
26635 }
26636 }
26637 return s;
26638 }
26639
26640 void writeClasses(Set<String> s) {
26641 List list = new List.from(s);
26642 _element.$dom_className = s.join(' ');
26643 }
26702 } 26644 }
26703 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 26645 // 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 26646 // 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. 26647 // BSD-style license that can be found in the LICENSE file.
26706 26648
26707 26649
26708 typedef EventListener(Event event); 26650 typedef EventListener(Event event);
26709 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 26651 // 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 26652 // 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. 26653 // 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; 29518 _position = nextPosition;
29577 return true; 29519 return true;
29578 } 29520 }
29579 _current = null; 29521 _current = null;
29580 _position = _array.length; 29522 _position = _array.length;
29581 return false; 29523 return false;
29582 } 29524 }
29583 29525
29584 T get current => _current; 29526 T get current => _current;
29585 } 29527 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698