summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/multicol/widows-and-orphans.html
blob: 380c31d171e1bb1f61ff2f3686dcf55696aae91e (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
<!DOCTYPE html>
<html>
<head>
<title>Testing Widows and Orphans</title>
<style>
body.hide-containers .container {
    display: none;
}

.container {
    width: 600px;
    height: 200px;
    -webkit-columns: 3;
    columns: 3;
    column-fill: auto;
    line-height: 20px; /* 10 lines per page */
    font-size: 16px;
    margin: 0 0 20px 0;
    padding: 0;
    overflow: hidden;
}

.block {
    margin: 0 0 15px 0;
    padding: 0;
}

.top {
    color: red;
}

.bottom {
    color: green;
}
</style>
<script>
var results;

function createTestContainer(id, description, blocks)
{
    var label = document.createElement("h3");
    label.textContent = id + " - " + description;
    document.body.appendChild(label);
    var element = document.createElement("div");
    element.className = "container";
    element.id = id;

    for (var i = 1; i <= blocks.length; i++) {
        var block = document.createElement("div");
        block.className = "block";
        var numLines = blocks[i-1];
        for (var j = 1; j <= numLines; j++) {
            var line = document.createElement("span");
            line.id = id + "-block-" + i + "-line-" + j;
            line.textContent = "Block " + i + " Line " + j;
            block.appendChild(line);
            block.appendChild(document.createElement("br"));
        }
        element.appendChild(block);
    }
    document.body.appendChild(element);
    return element;
}

function markTopLine(containerId, blockNumber, lineNumber)
{
    var element = document.getElementById(containerId + "-block-" + blockNumber + "-line-" + lineNumber);
    element.className = "top";
}

function markBottomLine(containerId, blockNumber, lineNumber)
{
    var element = document.getElementById(containerId + "-block-" + blockNumber + "-line-" + lineNumber);
    element.className = "bottom";
}

function logPass(msg) {
    log("PASS: " + msg);
}

function logFail(msg) {
    log("FAIL: " + msg);
}

function log(msg) {
    if (!results)
        results = document.createElement("div");

    var output = document.createElement("p");
    output.textContent = msg;
    results.appendChild(output);
}

function testIsFirstInColumn(containerId, blockNumber, lineNumber)
{
    // Get the upper bounds of the container and line.
    var topOfContainer = document.getElementById(containerId).getBoundingClientRect().top;
    var topOfLine = document.getElementById(containerId + "-block-" + blockNumber + "-line-" + lineNumber).getBoundingClientRect().top;

    if (Math.abs(topOfContainer - topOfLine) < 5) // Give 5 pixels to account for subpixel layout.
        logPass(containerId + " Block " + blockNumber + " Line " + lineNumber + " is correct.");
    else
        logFail(containerId + " Block " + blockNumber + " Line " + lineNumber + " wasn't at the top of the column.");
}

function runTest()
{
    var container;

    createTestContainer("test1", "Normal breaking", [5, 6, 5, 5]);

    markTopLine("test1", 1, 1);
    markBottomLine("test1", 2, 4);
    markTopLine("test1", 2, 5);
    markBottomLine("test1", 4, 1);
    markTopLine("test1", 4, 2);

    testIsFirstInColumn("test1", 1, 1);
    testIsFirstInColumn("test1", 2, 5);
    testIsFirstInColumn("test1", 4, 2);

    container = createTestContainer("test2", "Basic Orphan", [8, 6]);
    container.style.orphans = 2;

    markTopLine("test2", 1, 1);
    markBottomLine("test2", 1, 8); // Orphan break happens here.
    markTopLine("test2", 2, 1);

    testIsFirstInColumn("test2", 1, 1);
    testIsFirstInColumn("test2", 2, 1);

    container = createTestContainer("test3", "Basic Widow", [4, 6, 3]);
    container.style.widows = 2;

    markTopLine("test3", 1, 1);
    markBottomLine("test3", 2, 4); // Widow break happens here.
    markTopLine("test3", 2, 5);

    testIsFirstInColumn("test3", 1, 1);
    testIsFirstInColumn("test3", 2, 5);

    container = createTestContainer("test4", "Orphans causing Widows", [8, 6, 4, 4]);
    container.style.orphans = 2;
    container.style.widows = 2;

    markTopLine("test4", 1, 1);
    markBottomLine("test4", 1, 8); // Orphan break happens here.
    markTopLine("test4", 2, 1);
    markBottomLine("test4", 3, 2); // And that creates a widow forcing a break here.
    markTopLine("test4", 3, 3);

    testIsFirstInColumn("test4", 1, 1);
    testIsFirstInColumn("test4", 2, 1);
    testIsFirstInColumn("test4", 3, 3);

    container = createTestContainer("test5", "Widows blocked by Orphan rule", [7, 3, 4]);
    container.style.orphans = 2;
    container.style.widows = 2;

    markTopLine("test5", 1, 1);
    markBottomLine("test5", 2, 2); // This line should not move - protected by orphaning.
    markTopLine("test5", 2, 3); // This line won't be un-widowed - blocked by orphaning.

    testIsFirstInColumn("test5", 1, 1);
    testIsFirstInColumn("test5", 2, 3);

    container = createTestContainer("test6", "Ridiculous values", [7, 7, 7, 7]);
    container.style.orphans = 100;
    container.style.widows = 100;

    markTopLine("test6", 1, 1);
    markBottomLine("test6", 1, 7); // Orphan break happens here.
    markTopLine("test6", 2, 1); // Adopted.
    markBottomLine("test6", 2, 7); // Orphan break.
    markTopLine("test6", 3, 1); // Adopted.

    testIsFirstInColumn("test6", 1, 1);
    testIsFirstInColumn("test6", 2, 1);
    testIsFirstInColumn("test6", 3, 1);

    if (results)
        document.body.appendChild(results);

    if (window.testRunner) {
        // Hide all the containers and leave just the test results for text output.
        document.body.className = "hide-containers";
        testRunner.notifyDone();
    }
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

window.addEventListener("load", runTest, false);
</script>
</head>
<body>
    <p>
        Testing widows and orphans. Any green lines
        should be at the bottom of pages/columns, and any red lines
        should be at the top of pages/columns.
    </p>
</body>
</html>