2 * Tests attribute parsing and handling of whitespace in attribute values.
4 * @param type Name of the type being tested (only for test output)
5 * @param target The element that should be tested
6 * @param attribute The name of the attribute that should be tested
7 * @param expected The fallback/default value that is the expectation for invalid values
8 * @param whitespace An array of strings that are valid whitespace characters
9 * @param valid An array of strings containing valid attribute values
10 * @param invalid An array of strings containing invalid attribute values
11 * @param garbage An array of strings containing values that would make a valid value invalid when concatenated
12 * @param assert_valid_custom A function for asserting validity of a valid value, arguments passed to this function: the element and the string from valid values array
13 * @param assert_invalid_custom A function for asserting that an invalid value results in the expected default value, arguments passed to this function: the element and the expected value
15 function testType(type
, target
, attribute
, expected
, whitespace
, valid
, invalid
, validunits
, garbage
, assert_valid_custom
, assert_invalid_custom
) {
16 whitespace
.forEach(function(leading
) {
17 whitespace
.forEach(function(trailing
) {
18 valid
.forEach(function(value
) {
19 validunits
.forEach(function(unit
) {
20 var valueStr
= leading
+ value
+ unit
+ trailing
;
21 var escapedValueStr
= valueStr
.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
24 target
.setAttribute(attribute
, valueStr
);
25 assert_equals(target
.getAttribute(attribute
), valueStr
);
26 assert_valid_custom(target
, value
);
29 target
.removeAttribute(attribute
);
31 }, "Test " + type
+ " valid value: " + escapedValueStr
);
35 // test invalid values
36 invalid
.forEach(function(value
) {
37 validunits
.forEach(function(unit
) {
38 var valueStr
= leading
+ value
+ unit
+ trailing
;
39 var escapedValueStr
= valueStr
.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
42 target
.setAttribute(attribute
, valueStr
);
43 assert_equals(target
.getAttribute(attribute
), valueStr
);
44 assert_invalid_custom(target
, expected
);
47 target
.removeAttribute(attribute
);
49 }, "Test " + type
+ " invalid value: " + escapedValueStr
);
54 // test whitespace between value and unit
55 validunits
.forEach(function(unit
) {
56 if (unit
== "" || leading
== "")
58 valid
.forEach(function(value
) {
59 var valueStr
= value
+ leading
+ unit
;
60 var escapedValueStr
= valueStr
.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
63 target
.setAttribute(attribute
, valueStr
);
64 assert_equals(target
.getAttribute(attribute
), valueStr
);
65 assert_invalid_custom(target
, expected
);
68 target
.removeAttribute(attribute
);
70 }, "Test " + type
+ " WS invalid value: " + escapedValueStr
);
74 // test trailing garbage
75 garbage
.forEach(function(trailing
) {
76 valid
.forEach(function(value
) {
77 var valueStr
= leading
+ value
+ trailing
;
78 var escapedValueStr
= valueStr
.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
81 target
.setAttribute(attribute
, valueStr
);
82 assert_equals(target
.getAttribute(attribute
), valueStr
);
83 assert_invalid_custom(target
, expected
);
86 target
.removeAttribute(attribute
);
88 }, "Test " + type
+ " trailing garbage, value: " + escapedValueStr
);