Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of html; | 5 part of html; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A set (union) of the CSS classes that are present in a set of elements. | 8 * A set (union) of the CSS classes that are present in a set of elements. |
| 9 * Implemented separately from _ElementCssClassSet for performance. | 9 * Implemented separately from _ElementCssClassSet for performance. |
| 10 */ | 10 */ |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 void retainWhere(bool test(String name)) { | 135 void retainWhere(bool test(String name)) { |
| 136 _removeWhere(_element, test, false); | 136 _removeWhere(_element, test, false); |
| 137 } | 137 } |
| 138 | 138 |
| 139 static bool _contains(Element _element, Object value) { | 139 static bool _contains(Element _element, Object value) { |
| 140 return value is String && _classListContains(_classListOf(_element), value); | 140 return value is String && _classListContains(_classListOf(_element), value); |
| 141 } | 141 } |
| 142 | 142 |
| 143 static bool _add(Element _element, String value) { | 143 static bool _add(Element _element, String value) { |
| 144 DomTokenList list = _classListOf(_element); | 144 DomTokenList list = _classListOf(_element); |
| 145 // Compute returned result independently of action upon the set. One day we | 145 // Compute returned result independently of action upon the set. |
| 146 // will be able to optimize it way if unused. | 146 bool added = !_classListContainsBeforeAddRemove(list, value); |
| 147 bool added = !_classListContains(list, value); | |
| 148 _classListAdd(list, value); | 147 _classListAdd(list, value); |
| 149 return added; | 148 return added; |
| 150 } | 149 } |
| 151 | 150 |
| 152 static bool _remove(Element _element, String value) { | 151 static bool _remove(Element _element, String value) { |
| 153 DomTokenList list = _classListOf(_element); | 152 DomTokenList list = _classListOf(_element); |
| 154 bool removed = _classListContains(list, value); | 153 bool removed = _classListContainsBeforeAddRemove(list, value); |
| 155 _classListRemove(list, value); | 154 _classListRemove(list, value); |
| 156 return removed; | 155 return removed; |
| 157 } | 156 } |
| 158 | 157 |
| 159 static bool _toggle(Element _element, String value, bool shouldAdd) { | 158 static bool _toggle(Element _element, String value, bool shouldAdd) { |
| 160 // There is no value that can be passed as the second argument of | 159 // There is no value that can be passed as the second argument of |
| 161 // DomTokenList.toggle that behaves the same as passing one argument. | 160 // DomTokenList.toggle that behaves the same as passing one argument. |
| 162 // `null` is seen as false, meaning 'remove'. | 161 // `null` is seen as false, meaning 'remove'. |
| 163 return shouldAdd == null | 162 return shouldAdd == null |
| 164 ? _toggleDefault(_element, value) | 163 ? _toggleDefault(_element, value) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 // the DomTokenList methods. | 217 // the DomTokenList methods. |
| 219 | 218 |
| 220 static DomTokenList _classListOf(Element e) => | 219 static DomTokenList _classListOf(Element e) => |
| 221 JS('returns:DomTokenList;creates:DomTokenList;effects:none;depends:all;', | 220 JS('returns:DomTokenList;creates:DomTokenList;effects:none;depends:all;', |
| 222 '#.classList', e); | 221 '#.classList', e); |
| 223 | 222 |
| 224 static int _classListLength(DomTokenList list) => | 223 static int _classListLength(DomTokenList list) => |
| 225 JS('returns:JSUInt31;effects:none;depends:all;', '#.length', list); | 224 JS('returns:JSUInt31;effects:none;depends:all;', '#.length', list); |
| 226 | 225 |
| 227 static bool _classListContains(DomTokenList list, String value) => | 226 static bool _classListContains(DomTokenList list, String value) => |
| 228 JS('returns:bool;effects:none;depends:all;', | 227 JS('returns:bool;effects:none;depends:all', |
| 228 '#.contains(#)', list, value); | |
| 229 | |
| 230 static bool _classListContainsBeforeAddRemove( | |
|
Siggi Cherem (dart-lang)
2015/04/21 05:07:25
optional minor nit: ...AddRemove => ...AddOrRemove
sra1
2015/04/21 19:38:08
Done.
| |
| 231 DomTokenList list, String value) => | |
| 232 // 'throws:never' is a lie, since 'contains' will throw on an illegal | |
|
Siggi Cherem (dart-lang)
2015/04/21 05:07:25
silly question just because I'm not used to these
sra1
2015/04/21 19:38:08
null(1) means throws iff first # is null.
It doesn
Siggi Cherem (dart-lang)
2015/04/21 22:32:50
since this is anyways lying, any reason not to jus
| |
| 233 // token. However, we always call this function immediately prior to | |
| 234 // add/remove/toggle with the same token. Often the result of 'contains' | |
| 235 // is unused and the lie makes it possible for the 'contains' instruction | |
| 236 // to be removed. | |
| 237 JS('returns:bool;effects:none;depends:all;throws:null(1)', | |
| 229 '#.contains(#)', list, value); | 238 '#.contains(#)', list, value); |
| 230 | 239 |
| 231 static void _classListAdd(DomTokenList list, String value) { | 240 static void _classListAdd(DomTokenList list, String value) { |
| 232 // list.add(value); | 241 // list.add(value); |
| 233 JS('', '#.add(#)', list, value); | 242 JS('', '#.add(#)', list, value); |
| 234 } | 243 } |
| 235 | 244 |
| 236 static void _classListRemove(DomTokenList list, String value) { | 245 static void _classListRemove(DomTokenList list, String value) { |
| 237 // list.remove(value); | 246 // list.remove(value); |
| 238 JS('', '#.remove(#)', list, value); | 247 JS('', '#.remove(#)', list, value); |
| 239 } | 248 } |
| 240 | 249 |
| 241 static bool _classListToggle1(DomTokenList list, String value) { | 250 static bool _classListToggle1(DomTokenList list, String value) { |
| 242 return JS('bool', '#.toggle(#)', list, value); | 251 return JS('bool', '#.toggle(#)', list, value); |
| 243 } | 252 } |
| 244 | 253 |
| 245 static bool _classListToggle2( | 254 static bool _classListToggle2( |
| 246 DomTokenList list, String value, bool shouldAdd) { | 255 DomTokenList list, String value, bool shouldAdd) { |
| 247 return JS('bool', '#.toggle(#, #)', list, value, shouldAdd); | 256 return JS('bool', '#.toggle(#, #)', list, value, shouldAdd); |
| 248 } | 257 } |
| 249 } | 258 } |
| OLD | NEW |