Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / svg / resources / whitespace-helper.js
blob61eaf0bbfeacb51b152fe5255962dfad003ae35e
1 /**
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');
22 test(function() {
23 try {
24 target.setAttribute(attribute, valueStr);
25 assert_equals(target.getAttribute(attribute), valueStr);
26 assert_valid_custom(target, value);
28 finally {
29 target.removeAttribute(attribute);
31 }, "Test " + type + " valid value: " + escapedValueStr );
32 });
33 });
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');
40 test(function() {
41 try {
42 target.setAttribute(attribute, valueStr);
43 assert_equals(target.getAttribute(attribute), valueStr);
44 assert_invalid_custom(target, expected);
46 finally {
47 target.removeAttribute(attribute);
49 }, "Test " + type + " invalid value: " + escapedValueStr);
50 });
51 });
52 });
54 // test whitespace between value and unit
55 validunits.forEach(function(unit) {
56 if (unit == "" || leading == "")
57 return;
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');
61 test(function() {
62 try {
63 target.setAttribute(attribute, valueStr);
64 assert_equals(target.getAttribute(attribute), valueStr);
65 assert_invalid_custom(target, expected);
67 finally {
68 target.removeAttribute(attribute);
70 }, "Test " + type + " WS invalid value: " + escapedValueStr);
71 });
72 });
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');
79 test(function() {
80 try {
81 target.setAttribute(attribute, valueStr);
82 assert_equals(target.getAttribute(attribute), valueStr);
83 assert_invalid_custom(target, expected);
85 finally {
86 target.removeAttribute(attribute);
88 }, "Test " + type + " trailing garbage, value: " + escapedValueStr);
89 });
90 });
91 });