Permalink
Browse files

feature: extra function for handling data-attributes added

  • Loading branch information...
MikeMitterer committed Sep 1, 2016
1 parent 67e9ba9 commit 613ad1a0234e2c35caef43ea776c89e4c1ddf55e
Showing with 74 additions and 4 deletions.
  1. +37 −4 lib/mdlutils.dart
  2. +37 −0 test/unit/utils/utils_test.dart
View
@@ -40,19 +40,52 @@ import "package:validate/validate.dart";
*/
class DataAttribute {
static _DataValue forValue(final dynamic value) {
/// Converts the given {value} to a {_DataValue} which returns e.g. a boolean
///
/// If value is null and onError-callback is set the return value that comes from
/// {onError} is used for conversion
static _DataValue forValue(final dynamic value, { dynamic onError() }) {
if(value == null && onError != null) {
return new _DataValue(onError());
}
return new _DataValue(value);
}
static _DataValue forAttribute(final dom.HtmlElement element,final String name) {
static _DataValue forAttribute(final dom.HtmlElement element,final String name,{ dynamic onError() }) {
Validate.notNull(element,"Element for attribute $name must not be null");
Validate.notNull(name,"Attribute-Name for $element must not be null");
Validate.notBlank(name,"Attribute-Name for $element must not be blank");
var value;
if(!element.attributes.containsKey(name)) {
throw new ArgumentError("$element has no attribute '$name' set!");
if(onError != null) {
value = onError();
} else {
throw new ArgumentError("$element has no attribute '$name' set!");
}
} else {
value = element.attributes[name];
}
final String value = element.attributes[name];
return new _DataValue(value);
}
static _DataValue forDataAttribute(final dom.HtmlElement element,final String name,{ dynamic onError() }) {
Validate.notNull(element,"Element for data attribute $name must not be null");
Validate.notNull(name,"Data-Attribute for $element must not be null");
Validate.notBlank(name,"Data-Attribute for $element must not be blank");
var value;
if(!element.dataset.containsKey(name)) {
if(onError != null) {
value = onError();
} else {
throw new ArgumentError("$element has no data-attribute '$name' set!");
}
} else {
value = element.dataset[name];
}
return new _DataValue(value);
}
}
@@ -18,6 +18,7 @@
*/
@TestOn("content-shell")
import 'dart:html' as dom;
import 'package:test/test.dart';
import "package:mdl/mdlutils.dart";
@@ -50,12 +51,48 @@ main() {
expect(DataAttribute.forValue("1.000").asInt(), 0);
expect(DataAttribute.forValue(null).asInt(), 0);
expect(DataAttribute.forValue(null,onError: () => 99).asInt(), 99);
expect(DataAttribute.forValue("true").asInt(), 0);
expect(DataAttribute.forValue("false").asInt(), 0);
expect(DataAttribute.forValue("").asInt(), 0);
}); // end of 'asInt' test
test('> forAttribute', () {
final dom.DivElement element = new dom.DivElement();
element.setAttribute("testvalue","10");
expect(element.attributes.containsKey("testvalue"),isTrue);
expect(element.dataset.containsKey("testvalue"),isFalse);
expect(element.dataset.containsKey("age"),isFalse);
expect(element.dataset["age"],isNull);
expect(DataAttribute.forAttribute(element,"testvalue").asInt(), 10);
expect(() => DataAttribute.forAttribute(element,"age").asInt(), throwsArgumentError);
expect(DataAttribute.forAttribute(element,"age",onError: () => "42").asInt(), 42);
expect(DataAttribute.forAttribute(element,"age",onError: () => 42).asInt(), 42);
}); // end of 'forAttribute' test
test('> forDataAttribute', () {
final dom.DivElement element = new dom.DivElement();
element.setAttribute("data-testvalue","10");
expect(element.dataset.containsKey("testvalue"),isTrue);
expect(element.dataset.containsKey("age"),isFalse);
expect(element.dataset["age"],isNull);
expect(DataAttribute.forDataAttribute(element,"testvalue").asInt(), 10);
expect(DataAttribute.forAttribute(element,"data-testvalue").asInt(), 10);
expect(() => DataAttribute.forDataAttribute(element,"age").asInt(), throwsArgumentError);
expect(DataAttribute.forDataAttribute(element,"age",onError: () => "42").asInt(), 42);
expect(DataAttribute.forDataAttribute(element,"age",onError: () => 42).asInt(), 42);
}); // end of 'forDataAttribute' test
});
// end 'DataAttribute' group
}

0 comments on commit 613ad1a

Please sign in to comment.