summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/svg/resources/whitespace-helper.js
blob: 61eaf0bbfeacb51b152fe5255962dfad003ae35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Tests attribute parsing and handling of whitespace in attribute values.
 *
 * @param type Name of the type being tested (only for test output)
 * @param target The element that should be tested
 * @param attribute The name of the attribute that should be tested
 * @param expected The fallback/default value that is the expectation for invalid values
 * @param whitespace An array of strings that are valid whitespace characters
 * @param valid An array of strings containing valid attribute values
 * @param invalid An array of strings containing invalid attribute values
 * @param garbage An array of strings containing values that would make a valid value invalid when concatenated
 * @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
 * @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
 */
function testType(type, target, attribute, expected, whitespace, valid, invalid, validunits, garbage, assert_valid_custom, assert_invalid_custom) {
    whitespace.forEach(function(leading) {
        whitespace.forEach(function(trailing) {
            valid.forEach(function(value) {
                validunits.forEach(function(unit) {
                    var valueStr = leading + value + unit + trailing;
                    var escapedValueStr = valueStr.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
                    test(function() {
                        try {
                            target.setAttribute(attribute, valueStr);
                            assert_equals(target.getAttribute(attribute), valueStr);
                            assert_valid_custom(target, value);
                        }
                        finally {
                            target.removeAttribute(attribute);
                        }
                    }, "Test " + type + " valid value: " + escapedValueStr );
                });
            });

            // test invalid values
            invalid.forEach(function(value) {
                validunits.forEach(function(unit) {
                    var valueStr = leading + value + unit + trailing;
                    var escapedValueStr = valueStr.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
                    test(function() {
                        try {
                            target.setAttribute(attribute, valueStr);
                            assert_equals(target.getAttribute(attribute), valueStr);
                            assert_invalid_custom(target, expected);
                        }
                        finally {
                            target.removeAttribute(attribute);
                        }
                    }, "Test " + type + " invalid value: " + escapedValueStr);
                });
            });
        });

        // test whitespace between value and unit
        validunits.forEach(function(unit) {
            if (unit == "" || leading == "")
                return;
            valid.forEach(function(value) {
                var valueStr = value + leading + unit;
                var escapedValueStr = valueStr.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
                test(function() {
                    try {
                        target.setAttribute(attribute, valueStr);
                        assert_equals(target.getAttribute(attribute), valueStr);
                        assert_invalid_custom(target, expected);
                    }
                    finally {
                        target.removeAttribute(attribute);
                    }
                }, "Test " + type + " WS invalid value: " + escapedValueStr);
            });
        });

        // test trailing garbage
        garbage.forEach(function(trailing) {
            valid.forEach(function(value) {
                var valueStr = leading + value + trailing;
                var escapedValueStr = valueStr.replace(/(\r)/g, '\\r').replace(/(\n)/g, '\\n').replace(/(\t)/g, '\\t').replace(/(\f)/g, '\\f');
                test(function() {
                    try {
                        target.setAttribute(attribute, valueStr);
                        assert_equals(target.getAttribute(attribute), valueStr);
                        assert_invalid_custom(target, expected);
                    }
                    finally {
                        target.removeAttribute(attribute);
                    }
                }, "Test " + type + " trailing garbage, value: " + escapedValueStr);
            });
        });
    });
}