| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 extends ListBase { |
| 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) { |
| 7974 _ElementCssClassSet(this._element); | 7995 e.classes.clear(); |
| 7975 | 7996 e.classes.addAll(value); |
| 7976 Set<String> readClasses() { | 7997 }); |
| 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 } | 7998 } |
| 7993 } | 7999 } |
| 7994 | 8000 |
| 7995 /** | 8001 /** |
| 7996 * An abstract class, which all HTML elements extend. | 8002 * An abstract class, which all HTML elements extend. |
| 7997 */ | 8003 */ |
| 7998 @DomName('Element') | 8004 @DomName('Element') |
| 7999 abstract class Element extends Node implements ElementTraversal native "Element"
{ | 8005 abstract class Element extends Node implements ElementTraversal native "Element"
{ |
| 8000 | 8006 |
| 8001 /** | 8007 /** |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8081 } | 8087 } |
| 8082 | 8088 |
| 8083 /** | 8089 /** |
| 8084 * Finds all descendent elements of this element that match the specified | 8090 * Finds all descendent elements of this element that match the specified |
| 8085 * group of selectors. | 8091 * group of selectors. |
| 8086 * | 8092 * |
| 8087 * [selectors] should be a string using CSS selector syntax. | 8093 * [selectors] should be a string using CSS selector syntax. |
| 8088 * | 8094 * |
| 8089 * var items = element.query('.itemClassName'); | 8095 * var items = element.query('.itemClassName'); |
| 8090 */ | 8096 */ |
| 8091 List<Element> queryAll(String selectors) => | 8097 ElementList queryAll(String selectors) => |
| 8092 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); | 8098 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); |
| 8093 | 8099 |
| 8094 /** | 8100 /** |
| 8095 * The set of CSS classes applied to this element. | 8101 * The set of CSS classes applied to this element. |
| 8096 * | 8102 * |
| 8097 * This set makes it easy to add, remove or toggle the classes applied to | 8103 * This set makes it easy to add, remove or toggle the classes applied to |
| 8098 * this element. | 8104 * this element. |
| 8099 * | 8105 * |
| 8100 * element.classes.add('selected'); | 8106 * element.classes.add('selected'); |
| 8101 * element.classes.toggle('isOnline'); | 8107 * element.classes.toggle('isOnline'); |
| (...skipping 18383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 26485 void forward(); | 26491 void forward(); |
| 26486 void go(int distance); | 26492 void go(int distance); |
| 26487 } | 26493 } |
| 26488 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 26494 // 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 | 26495 // 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. | 26496 // BSD-style license that can be found in the LICENSE file. |
| 26491 | 26497 |
| 26492 | 26498 |
| 26493 abstract class CssClassSet implements Set<String> { | 26499 abstract class CssClassSet implements Set<String> { |
| 26494 | 26500 |
| 26501 /** |
| 26502 * Adds the class [value] to the element if it is not on it, removes it if it |
| 26503 * is. |
| 26504 */ |
| 26505 bool toggle(String value); |
| 26506 |
| 26507 /** |
| 26508 * Returns [:true:] if classes cannot be added or removed from this |
| 26509 * [:CssClassSet:]. |
| 26510 */ |
| 26511 bool get frozen; |
| 26512 |
| 26513 /** |
| 26514 * Determine if this element contains the class [value]. |
| 26515 * |
| 26516 * This is the Dart equivalent of jQuery's |
| 26517 * [hasClass](http://api.jquery.com/hasClass/). |
| 26518 */ |
| 26519 bool contains(String value); |
| 26520 |
| 26521 /** |
| 26522 * Add the class [value] to element. |
| 26523 * |
| 26524 * This is the Dart equivalent of jQuery's |
| 26525 * [addClass](http://api.jquery.com/addClass/). |
| 26526 */ |
| 26527 void add(String value); |
| 26528 |
| 26529 /** |
| 26530 * Remove the class [value] from element, and return true on successful |
| 26531 * removal. |
| 26532 * |
| 26533 * This is the Dart equivalent of jQuery's |
| 26534 * [removeClass](http://api.jquery.com/removeClass/). |
| 26535 */ |
| 26536 bool remove(Object value); |
| 26537 |
| 26538 /** |
| 26539 * Add all classes specified in [iterable] to element. |
| 26540 * |
| 26541 * This is the Dart equivalent of jQuery's |
| 26542 * [addClass](http://api.jquery.com/addClass/). |
| 26543 */ |
| 26544 void addAll(Iterable<String> iterable); |
| 26545 |
| 26546 /** |
| 26547 * Remove all classes specified in [iterable] from element. |
| 26548 * |
| 26549 * This is the Dart equivalent of jQuery's |
| 26550 * [removeClass](http://api.jquery.com/removeClass/). |
| 26551 */ |
| 26552 void removeAll(Iterable<String> iterable); |
| 26553 |
| 26554 /** |
| 26555 * Toggles all classes specified in [iterable] on element. |
| 26556 * |
| 26557 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 26558 * remove it if it is. This is the Dart equivalent of jQuery's |
| 26559 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 26560 */ |
| 26561 void toggleAll(Iterable<String> iterable); |
| 26562 } |
| 26563 abstract class _CssClassSetImpl implements CssClassSet { |
| 26564 |
| 26495 String toString() { | 26565 String toString() { |
| 26496 return readClasses().join(' '); | 26566 return readClasses().join(' '); |
| 26497 } | 26567 } |
| 26498 | 26568 |
| 26499 /** | 26569 /** |
| 26500 * Adds the class [value] to the element if it is not on it, removes it if it | 26570 * Adds the class [value] to the element if it is not on it, removes it if it |
| 26501 * is. | 26571 * is. |
| 26502 */ | 26572 */ |
| 26503 bool toggle(String value) { | 26573 bool toggle(String value) { |
| 26504 Set<String> s = readClasses(); | 26574 Set<String> s = readClasses(); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 26693 */ | 26763 */ |
| 26694 Set<String> readClasses(); | 26764 Set<String> readClasses(); |
| 26695 | 26765 |
| 26696 /** | 26766 /** |
| 26697 * Join all the elements of a set into one string and write | 26767 * Join all the elements of a set into one string and write |
| 26698 * back to the element. | 26768 * back to the element. |
| 26699 * This is intended to be overridden by specific implementations. | 26769 * This is intended to be overridden by specific implementations. |
| 26700 */ | 26770 */ |
| 26701 void writeClasses(Set<String> s); | 26771 void writeClasses(Set<String> s); |
| 26702 } | 26772 } |
| 26773 |
| 26774 /** |
| 26775 * A set (union) of the CSS classes that are present in a set of elements. |
| 26776 * Implemented separately from _ElementCssClassSet for performance. |
| 26777 */ |
| 26778 class _MultiElementCssClassSet extends _CssClassSetImpl { |
| 26779 final Iterable<Element> _elementIterable; |
| 26780 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; |
| 26781 |
| 26782 _MultiElementCssClassSet(this._elementIterable) { |
| 26783 _elementCssClassSetIterable = new List.from(_elementIterable).map( |
| 26784 (e) => new _ElementCssClassSet(e)); |
| 26785 } |
| 26786 |
| 26787 Set<String> readClasses() { |
| 26788 var s = new LinkedHashSet<String>(); |
| 26789 _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses())); |
| 26790 return s; |
| 26791 } |
| 26792 |
| 26793 void writeClasses(Set<String> s) { |
| 26794 var classes = new List.from(s).join(' '); |
| 26795 for (Element e in _elementIterable) { |
| 26796 e.$dom_className = classes; |
| 26797 } |
| 26798 } |
| 26799 |
| 26800 /** |
| 26801 * Helper method used to modify the set of css classes on this element. |
| 26802 * |
| 26803 * f - callback with: |
| 26804 * s - a Set of all the css class name currently on this element. |
| 26805 * |
| 26806 * After f returns, the modified set is written to the |
| 26807 * className property of this element. |
| 26808 */ |
| 26809 void _modify( f(Set<String> s)) { |
| 26810 _elementCssClassSetIterable.forEach((e) => e._modify(f)); |
| 26811 } |
| 26812 |
| 26813 /** |
| 26814 * Adds the class [value] to the element if it is not on it, removes it if it |
| 26815 * is. |
| 26816 */ |
| 26817 bool toggle(String value) => |
| 26818 _modifyWithReturnValue((e) => e.toggle(value)); |
| 26819 |
| 26820 /** |
| 26821 * Remove the class [value] from element, and return true on successful |
| 26822 * removal. |
| 26823 * |
| 26824 * This is the Dart equivalent of jQuery's |
| 26825 * [removeClass](http://api.jquery.com/removeClass/). |
| 26826 */ |
| 26827 bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value)); |
| 26828 |
| 26829 bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold( |
| 26830 false, (prevValue, element) => f(element) || prevValue); |
| 26831 } |
| 26832 |
| 26833 class _ElementCssClassSet extends _CssClassSetImpl { |
| 26834 |
| 26835 final Element _element; |
| 26836 |
| 26837 _ElementCssClassSet(this._element); |
| 26838 |
| 26839 Set<String> readClasses() { |
| 26840 var s = new LinkedHashSet<String>(); |
| 26841 var classname = _element.$dom_className; |
| 26842 |
| 26843 for (String name in classname.split(' ')) { |
| 26844 String trimmed = name.trim(); |
| 26845 if (!trimmed.isEmpty) { |
| 26846 s.add(trimmed); |
| 26847 } |
| 26848 } |
| 26849 return s; |
| 26850 } |
| 26851 |
| 26852 void writeClasses(Set<String> s) { |
| 26853 List list = new List.from(s); |
| 26854 _element.$dom_className = s.join(' '); |
| 26855 } |
| 26856 } |
| 26703 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 26857 // 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 | 26858 // 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. | 26859 // BSD-style license that can be found in the LICENSE file. |
| 26706 | 26860 |
| 26707 | 26861 |
| 26708 typedef EventListener(Event event); | 26862 typedef EventListener(Event event); |
| 26709 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 26863 // 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 | 26864 // 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. | 26865 // BSD-style license that can be found in the LICENSE file. |
| 26712 | 26866 |
| (...skipping 2863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 29576 _position = nextPosition; | 29730 _position = nextPosition; |
| 29577 return true; | 29731 return true; |
| 29578 } | 29732 } |
| 29579 _current = null; | 29733 _current = null; |
| 29580 _position = _array.length; | 29734 _position = _array.length; |
| 29581 return false; | 29735 return false; |
| 29582 } | 29736 } |
| 29583 | 29737 |
| 29584 T get current => _current; | 29738 T get current => _current; |
| 29585 } | 29739 } |
| OLD | NEW |