Index: sdk/lib/html/html_common/css_class_set.dart |
diff --git a/sdk/lib/html/html_common/css_class_set.dart b/sdk/lib/html/html_common/css_class_set.dart |
index 0380d2d82e21e977a15da7c8731f5ecc337b6400..9c4b4430c0c0f59ccf92bdf2d9a941d16943b6a3 100644 |
--- a/sdk/lib/html/html_common/css_class_set.dart |
+++ b/sdk/lib/html/html_common/css_class_set.dart |
@@ -6,6 +6,13 @@ part of html_common; |
abstract class CssClassSetImpl implements CssClassSet { |
+ static final RegExp _validTokenRE = new RegExp(r'^\S+$'); |
+ |
+ String _validateToken(String value) { |
+ if (_validTokenRE.hasMatch(value)) return value; |
+ throw new ArgumentError.value(value, 'value', 'Not a valid class token'); |
+ } |
+ |
String toString() { |
return readClasses().join(' '); |
} |
@@ -18,6 +25,7 @@ abstract class CssClassSetImpl implements CssClassSet { |
* [shouldAdd] is false then we always remove [value] from the element. |
*/ |
bool toggle(String value, [bool shouldAdd]) { |
+ _validateToken(value); |
Set<String> s = readClasses(); |
bool result = false; |
if (shouldAdd == null) shouldAdd = !s.contains(value); |
@@ -81,7 +89,11 @@ abstract class CssClassSetImpl implements CssClassSet { |
* This is the Dart equivalent of jQuery's |
* [hasClass](http://api.jquery.com/hasClass/). |
*/ |
- bool contains(Object value) => readClasses().contains(value); |
+ bool contains(Object value) { |
+ if (value is! String) return false; |
+ _validateToken(value); |
+ return readClasses().contains(value); |
+ } |
/** Lookup from the Set interface. Not interesting for a String set. */ |
String lookup(Object value) => contains(value) ? value : null; |
@@ -93,6 +105,7 @@ abstract class CssClassSetImpl implements CssClassSet { |
* [addClass](http://api.jquery.com/addClass/). |
*/ |
bool add(String value) { |
+ _validateToken(value); |
// TODO - figure out if we need to do any validation here |
// or if the browser natively does enough. |
return modify((s) => s.add(value)); |
@@ -106,6 +119,7 @@ abstract class CssClassSetImpl implements CssClassSet { |
* [removeClass](http://api.jquery.com/removeClass/). |
*/ |
bool remove(Object value) { |
+ _validateToken(value); |
if (value is! String) return false; |
Set<String> s = readClasses(); |
bool result = s.remove(value); |
@@ -121,7 +135,7 @@ abstract class CssClassSetImpl implements CssClassSet { |
*/ |
void addAll(Iterable<String> iterable) { |
// TODO - see comment above about validation. |
- modify((s) => s.addAll(iterable)); |
+ modify((s) => s.addAll(iterable.map(_validateToken))); |
} |
/** |
@@ -131,7 +145,7 @@ abstract class CssClassSetImpl implements CssClassSet { |
* [removeClass](http://api.jquery.com/removeClass/). |
*/ |
void removeAll(Iterable<Object> iterable) { |
- modify((s) => s.removeAll(iterable)); |
+ modify((s) => s.removeAll(iterable.map(_validateToken))); |
} |
/** |