| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(jmesserly): everything in this file is copied straight from "dart:html". | 5 // TODO(jmesserly): everything in this file is copied straight from "dart:html". |
| 6 library html.dom.src; | 6 library html.dom.src; |
| 7 | 7 |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 |
| 9 import 'package:html/dom.dart'; | 10 import 'package:html/dom.dart'; |
| 10 | 11 |
| 11 class ElementCssClassSet extends CssClassSetImpl { | 12 class ElementCssClassSet extends CssClassSetImpl { |
| 12 final Element _element; | 13 final Element _element; |
| 13 | 14 |
| 14 ElementCssClassSet(this._element); | 15 ElementCssClassSet(this._element); |
| 15 | 16 |
| 16 Set<String> readClasses() { | 17 Set<String> readClasses() { |
| 17 var s = new LinkedHashSet<String>(); | 18 var s = new LinkedHashSet<String>(); |
| 18 var classname = _element.className; | 19 var classname = _element.className; |
| 19 | 20 |
| 20 for (String name in classname.split(' ')) { | 21 for (String name in classname.split(' ')) { |
| 21 String trimmed = name.trim(); | 22 String trimmed = name.trim(); |
| 22 if (!trimmed.isEmpty) { | 23 if (!trimmed.isEmpty) { |
| 23 s.add(trimmed); | 24 s.add(trimmed); |
| 24 } | 25 } |
| 25 } | 26 } |
| 26 return s; | 27 return s; |
| 27 } | 28 } |
| 28 | 29 |
| 29 void writeClasses(Set<String> s) { | 30 void writeClasses(Set<String> s) { |
| 30 _element.className = s.join(' '); | 31 _element.className = s.join(' '); |
| 31 } | 32 } |
| 32 } | 33 } |
| 33 | 34 |
| 34 /** A Set that stores the CSS class names for an element. */ | 35 /** A Set that stores the CSS class names for an element. */ |
| 35 abstract class CssClassSet implements Set<String> { | 36 abstract class CssClassSet implements Set<String> { |
| 36 | |
| 37 /** | 37 /** |
| 38 * Adds the class [value] to the element if it is not on it, removes it if it | 38 * Adds the class [value] to the element if it is not on it, removes it if it |
| 39 * is. | 39 * is. |
| 40 * | 40 * |
| 41 * If [shouldAdd] is true, then we always add that [value] to the element. If | 41 * If [shouldAdd] is true, then we always add that [value] to the element. If |
| 42 * [shouldAdd] is false then we always remove [value] from the element. | 42 * [shouldAdd] is false then we always remove [value] from the element. |
| 43 */ | 43 */ |
| 44 bool toggle(String value, [bool shouldAdd]); | 44 bool toggle(String value, [bool shouldAdd]); |
| 45 | 45 |
| 46 /** | 46 /** |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 Iterator<String> get iterator => readClasses().iterator; | 144 Iterator<String> get iterator => readClasses().iterator; |
| 145 // interface Iterable - END | 145 // interface Iterable - END |
| 146 | 146 |
| 147 // interface Collection - BEGIN | 147 // interface Collection - BEGIN |
| 148 void forEach(void f(String element)) { | 148 void forEach(void f(String element)) { |
| 149 readClasses().forEach(f); | 149 readClasses().forEach(f); |
| 150 } | 150 } |
| 151 | 151 |
| 152 String join([String separator = ""]) => readClasses().join(separator); | 152 String join([String separator = ""]) => readClasses().join(separator); |
| 153 | 153 |
| 154 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(String e)) => readClasses().map(f); | 154 Iterable<T> map<T>(T f(String e)) => readClasses().map(f); |
| 155 | 155 |
| 156 Iterable<String> where(bool f(String element)) => readClasses().where(f); | 156 Iterable<String> where(bool f(String element)) => readClasses().where(f); |
| 157 | 157 |
| 158 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(String element)) => readClasse
s().expand(f); | 158 Iterable<T> expand<T>(Iterable<T> f(String element)) => |
| 159 readClasses().expand(f); |
| 159 | 160 |
| 160 bool every(bool f(String element)) => readClasses().every(f); | 161 bool every(bool f(String element)) => readClasses().every(f); |
| 161 | 162 |
| 162 bool any(bool f(String element)) => readClasses().any(f); | 163 bool any(bool f(String element)) => readClasses().any(f); |
| 163 | 164 |
| 164 bool get isEmpty => readClasses().isEmpty; | 165 bool get isEmpty => readClasses().isEmpty; |
| 165 | 166 |
| 166 bool get isNotEmpty => readClasses().isNotEmpty; | 167 bool get isNotEmpty => readClasses().isNotEmpty; |
| 167 | 168 |
| 168 int get length => readClasses().length; | 169 int get length => readClasses().length; |
| 169 | 170 |
| 170 String reduce(String combine(String value, String element)) { | 171 String reduce(String combine(String value, String element)) { |
| 171 return readClasses().reduce(combine); | 172 return readClasses().reduce(combine); |
| 172 } | 173 } |
| 173 | 174 |
| 174 dynamic/*=T*/ fold/*<T>*/(var/*=T*/ initialValue, | 175 T fold<T>(T initialValue, T combine(T previousValue, String element)) { |
| 175 dynamic/*=T*/ combine(var/*=T*/ previousValue, String element)) { | |
| 176 return readClasses().fold(initialValue, combine); | 176 return readClasses().fold(initialValue, combine); |
| 177 } | 177 } |
| 178 // interface Collection - END | 178 // interface Collection - END |
| 179 | 179 |
| 180 // interface Set - BEGIN | 180 // interface Set - BEGIN |
| 181 /** | 181 /** |
| 182 * Determine if this element contains the class [value]. | 182 * Determine if this element contains the class [value]. |
| 183 * | 183 * |
| 184 * This is the Dart equivalent of jQuery's | 184 * This is the Dart equivalent of jQuery's |
| 185 * [hasClass](http://api.jquery.com/hasClass/). | 185 * [hasClass](http://api.jquery.com/hasClass/). |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 */ | 321 */ |
| 322 Set<String> readClasses(); | 322 Set<String> readClasses(); |
| 323 | 323 |
| 324 /** | 324 /** |
| 325 * Join all the elements of a set into one string and write | 325 * Join all the elements of a set into one string and write |
| 326 * back to the element. | 326 * back to the element. |
| 327 * This is intended to be overridden by specific implementations. | 327 * This is intended to be overridden by specific implementations. |
| 328 */ | 328 */ |
| 329 void writeClasses(Set<String> s); | 329 void writeClasses(Set<String> s); |
| 330 } | 330 } |
| OLD | NEW |