summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/css/table-border-spacing.html
blob: b40942711eb4b2aa6da4ec85570554482989f6e7 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
<style>
#computed-infer-vertical-border-spacing { border-spacing: 11px; }
#computed-infer-vertical-border-spacing-from-negative-horizontal-border-spacing { border-spacing: -11px; }
#computed-explicit-border-spacing { border-spacing: 13px 23px; }
#computed-explicit-negative-border-spacing { border-spacing: -5px -13px; }
#computed-explicit-border-spacing-with-negative-horizontal { border-spacing: -5px 13px; }
#computed-explicit-border-spacing-with-negative-vertical { border-spacing: 5px -13px; }
#missing-stylesheet-border-spacing { border-spacing: }
</style>
<script>

window.onload = runTests;

if (!String.prototype.trim) {
    // Assume this script is being executed in Internet Explorer.
    String.prototype.trim = function() {
        return this.replace(/^\s+/, '').replace(/\s+$/, '');
    }
}

function runTests()
{
    debug('Valid values:');
    testInferredVerticalBorderSpacing();
    testExplicitBorderSpacing();
    testComputedInferredVerticalBorderSpacing();
    testComputedExplicitBorderSpacing();

    debug('<br />Negative values:');
    testInferredVerticalBorderSpacingFromNegativeHorizontalSpacing();
    testExplicitNegativeBorderSpacing();
    testExplicitBorderSpacingWithNegativeHorizontal();
    testExplicitBorderSpacingWithNegativeVertical();
    testComputedInferredVerticalBorderSpacingFromNegativeHorizontalSpacing();
    testComputedExplicitNegativeBorderSpacing();
    testComputedExplicitBorderSpacingWithNegativeHorizontal();
    testComputedExplicitBorderSpacingWithNegativeVertical();

    debug('<br />Missing values:');
    testMissingBorderSpacing();
    testMissingStylesheetBorderSpacing();

    document.body.removeChild(document.getElementById("test-container"));
    debug('<br /><span class="pass">TEST COMPLETE</span>');
}

function testInferredVerticalBorderSpacing()
{
    var styleAttr = styleAttribute(document.getElementById("infer-vertical-border-spacing"));
    shouldBeEqualToString('document.getElementById("infer-vertical-border-spacing").style.borderSpacing', parseCSSTextForPropertyValue(styleAttr, "border-spacing"));
}

function testExplicitBorderSpacing()
{
    var styleAttr = styleAttribute(document.getElementById("explicit-border-spacing"));
    shouldBeEqualToString('document.getElementById("explicit-border-spacing").style.borderSpacing', parseCSSTextForPropertyValue(styleAttr, "border-spacing"));
}

function testComputedInferredVerticalBorderSpacing()
{
    shouldBeEqualToString('computedStyle(document.getElementById("computed-infer-vertical-border-spacing")).borderSpacing', "11px 11px");
    shouldBeEqualToString('cssRule("#computed-infer-vertical-border-spacing").style.borderSpacing', "11px"); // Must match border-spacing for #computed-infer-vertical-border-spacing.
}

function testComputedExplicitBorderSpacing()
{
    var expectedResult = "13px 23px"; // Must match border-spacing for #computed-explicit-border-spacing.
    shouldBeEqualToString('computedStyle(document.getElementById("computed-explicit-border-spacing")).borderSpacing', expectedResult);
    shouldBeEqualToString('cssRule("#computed-explicit-border-spacing").style.borderSpacing', expectedResult);
}

function testInferredVerticalBorderSpacingFromNegativeHorizontalSpacing()
{
    // The expected value of the empty string follows from <http://www.w3.org/TR/html5/elements.html#the-style-attribute>,
    // <http://www.w3.org/TR/css-style-attr/#syntax>, <http://www.w3.org/TR/CSS21/syndata.html#declaration>, <http://www.w3.org/TR/CSS21/syndata.html#length-units>,
    // and the definition of "ignore" <http://www.w3.org/TR/CSS21/syndata.html#ignore>.
    shouldBeEqualToString('document.getElementById("infer-vertical-border-spacing-from-negative-horizontal-spacing").style.borderSpacing', "");
}

function testExplicitNegativeBorderSpacing()
{
    // The expected value of the empty string follows from <http://www.w3.org/TR/html5/elements.html#the-style-attribute>,
    // <http://www.w3.org/TR/css-style-attr/#syntax>, <http://www.w3.org/TR/CSS21/syndata.html#declaration>, <http://www.w3.org/TR/CSS21/syndata.html#length-units>,
    // and the definition of "ignore" <http://www.w3.org/TR/CSS21/syndata.html#ignore>.
    shouldBeEqualToString('document.getElementById("explicit-negative-border-spacing").style.borderSpacing', "");
}

function testExplicitBorderSpacingWithNegativeHorizontal()
{
    // The expected value of the empty string follows from <http://www.w3.org/TR/html5/elements.html#the-style-attribute>,
    // <http://www.w3.org/TR/css-style-attr/#syntax>, <http://www.w3.org/TR/CSS21/syndata.html#declaration>, <http://www.w3.org/TR/CSS21/syndata.html#length-units>,
    // and the definition of "ignore" <http://www.w3.org/TR/CSS21/syndata.html#ignore>.
    shouldBeEqualToString('document.getElementById("explicit-border-spacing-with-negative-horizontal").style.borderSpacing', "");
}

function testExplicitBorderSpacingWithNegativeVertical()
{
    // The expected value of the empty string follows from <http://www.w3.org/TR/html5/elements.html#the-style-attribute>,
    // <http://www.w3.org/TR/css-style-attr/#syntax>, <http://www.w3.org/TR/CSS21/syndata.html#declaration>, <http://www.w3.org/TR/CSS21/syndata.html#length-units>,
    // and the definition of "ignore" <http://www.w3.org/TR/CSS21/syndata.html#ignore>.
    shouldBeEqualToString('document.getElementById("explicit-border-spacing-with-negative-vertical").style.borderSpacing', "");
}

function testComputedInferredVerticalBorderSpacingFromNegativeHorizontalSpacing()
{
    // Expected results follow from the explanation given in testInferredVerticalBorderSpacingFromNegativeHorizontalSpacing().
    shouldBeEqualToString('cssRule("#computed-infer-vertical-border-spacing-from-negative-horizontal-border-spacing").style.borderSpacing', "");
}

function testComputedExplicitNegativeBorderSpacing()
{
    // Expected results follow from the explanation given in testExplicitNegativeBorderSpacing().
    shouldBeEqualToString('cssRule("#computed-explicit-negative-border-spacing").style.borderSpacing', "");
}

function testComputedExplicitBorderSpacingWithNegativeHorizontal()
{
    // Expected results follow from the explanation given in testExplicitBorderSpacingWithNegativeHorizontal().
    shouldBeEqualToString('cssRule("#computed-explicit-border-spacing-with-negative-horizontal").style.borderSpacing', "");
}

function testComputedExplicitBorderSpacingWithNegativeVertical()
{
    // Expected results follow from the explanation given in testExplicitBorderSpacingWithNegativeVertical().
    shouldBeEqualToString('cssRule("#computed-explicit-border-spacing-with-negative-vertical").style.borderSpacing', "");
}

function testMissingBorderSpacing()
{
    // Notice, a property with a missing value is considered a malformed declaration by
    // <http://www.w3.org/TR/CSS21/syndata.html#parsing-errors>. Hence, we ignore the declaration.
    shouldBeEqualToString('document.getElementById("missing-border-spacing").style.borderSpacing', "");
}

function testMissingStylesheetBorderSpacing()
{
    // Expected results follow from the explanation given in testMissingBorderSpacing().
    shouldBeEqualToString('cssRule("#missing-stylesheet-border-spacing").style.borderSpacing', "");
}

function styleAttribute(element)
{
    var result = element.getAttribute("style");
    if (typeof(result) === "object") {
        // Assume this script is being executed in Internet Explorer.
        result = result.cssText;
    }
    return result;
}

function computedStyle(element)
{
    if (window.getComputedStyle)
        return window.getComputedStyle(element, null);
    return element.currentStyle; // Assume this script is being executed in Internet Explorer 8 or less.
}

function cssRule(ruleName)
{
    if (!ruleName)
        return;

    if (!document.styleSheets)
        return;

    var NotFound = -1; // We can't make this "const" since IE (as of 8.0) doesn't support it.

    // We normalize the name of the rule to be lowercase since it is case-insensitive by <http://www.w3.org/TR/CSS21/syndata.html#characters>.
    var ruleName = ruleName.toLowerCase();
    var lastRuleThatMatchedCriterion;
    for (var i = 0; i < document.styleSheets.length; ++i) {
        var rules = [];
        try {
            var rules = document.styleSheets[i].rules || document.styleSheets[i].cssRules; // IE has "rules" property; WebKit and Firefox have "cssRules". 
        } catch (e) {
            // Firefox's same-origin policy for file URLs prevents access to cssRules for stylesheets that aren't within the
            // same directory as the web page (e.g. ../js/resources/js-test-style.css). So, we ignore such exceptions that arise.
            continue;
        } 
        for (var r = 0; r < rules.length; ++r) {
            if (rules[r].selectorText.toLowerCase() !== ruleName)
                continue;
            lastRuleThatMatchedCriterion = rules[r];
        }
    }
    return lastRuleThatMatchedCriterion;
}

function parseCSSTextForPropertyValue(cssText, propertyName)
{
    if (!cssText)
        return String();

    if (!propertyName)
        return String();

    // Some constants. We can't use "const" since IE (as of 8.0) doesn't support it.
    var Colon = ':';
    var Semicolon = ';';
    var NotFound = -1;

    // Note, CSS property names are case-insensitive, but its corresponding value can be case-
    // sensitive (e.g. font-family) by section 4.1.3 of the CSS 2.1 spec <http://www.w3.org/TR/CSS21/syndata.html#characters>.
    var cssTextLowerCase = cssText.toLowerCase();
    var propertyName = propertyName.toLowerCase();

    var startOfPropertyName = cssTextLowerCase.indexOf(propertyName);
    if (startOfPropertyName === NotFound)
        return;
    var delimiter = cssTextLowerCase.indexOf(Colon, startOfPropertyName);
    if (delimiter === NotFound)
        return;
    if (cssTextLowerCase.substr(startOfPropertyName, delimiter - startOfPropertyName).trim() !== propertyName)
        return;
    var startOfPropertyValue = delimiter + 1;
    var endOfRule = cssTextLowerCase.indexOf(Semicolon, startOfPropertyValue);
    return cssText.substr(startOfPropertyValue, endOfRule === NotFound ? cssText.length : endOfRule - startOfPropertyValue).trim(); // Preserve case-sensitivity of the property value.
}
</script>
</head>
<body>
    <p id="description"></p>
    <div id="test-container">
        <table id="infer-vertical-border-spacing" style="border-spacing: 4px">
            <tr><td>Infer vertical border spacing</td></tr>
        </table>
        <table id="explicit-border-spacing" style="border-spacing: 4px 5px">
            <tr><td>Explicit horizontal and vertical border spacing</td></tr>
        </table>
        <table id="computed-infer-vertical-border-spacing">
            <tr><td>Infer vertical border spacing (uses computed style)</td></tr>
        </table>
        <table id="computed-explicit-border-spacing">
            <tr><td>Explicit horizontal and vertical border spacing (uses computed style)</td></tr>
        </table>

        <table id="infer-vertical-border-spacing-from-negative-horizontal-spacing" style="border-spacing: -5px">
            <tr><td>Infer vertical border spacing from negative horizontal spacing</td></tr>
        </table>
        <table id="explicit-negative-border-spacing" style="border-spacing: -5px -13px">
            <tr><td>Explicit negative horizontal and vertical border spacing</td></tr>
        </table>
        <table id="explicit-border-spacing-with-negative-horizontal" style="border-spacing: -5px 13px">
            <tr><td>Explicit vertical and horizontal border spacing with negative horizontal border spacing</td></tr>
        </table>
        <table id="explicit-border-spacing-with-negative-vertical" style="border-spacing: 5px -13px">
            <tr><td>Explicit vertical and horizontal border spacing with negative vertical border spacing</td></tr>
        </table>
        <table id="computed-infer-vertical-border-spacing-from-negative-horizontal-border-spacing">
            <tr><td>Infer vertical border spacing from negative horizontal border spacing (uses computed style)</td></tr>
        </table>
        <table id="computed-explicit-negative-border-spacing">
            <tr><td>Explicit negative horizontal and vertical border spacing (uses computed style)</td></tr>
        </table>
        <table id="computed-explicit-border-spacing-with-negative-horizontal">
            <tr><td>Explicit horizontal and vertical border spacing with negative horizontal border spacing (uses computed style)</td></tr>
        </table>
        <table id="computed-explicit-border-spacing-with-negative-vertical">
            <tr><td>Explicit horizontal and vertical border spacing with negative vertical border spacing (uses computed style)</td></tr>
        </table>

        <table id="missing-border-spacing" style="border-spacing: ">
            <tr><td>Missing border spacing</td></tr>
        </table>
        <table id="missing-stylesheet-border-spacing">
            <tr><td>Missing border-spacing value in stylesheet</td></tr>
        </table>
    </div>
    <div id="console"></div>
    <script>
        description("This test checks that style.borderSpacing returns the correct result for valid, negative, and missing border-spacing values.");
    </script>
</body>
</html>