cfa97ee741cfcd40830cb40326116010ace422b5
[WebKit-https.git] / Tools / LayoutReloaded / FormattingContext / FloatingContext.js
1 /*
2  * Copyright (C) 2018 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 // All geometry here is absolute to the formatting context's root.
27 class FloatingContext {
28     constructor(floatingState) {
29         this.m_floatingState = floatingState;
30     }
31
32     computePosition(layoutBox) {
33         if (layoutBox.isOutOfFlowPositioned())
34             return;
35         let displayBox = this._formattingState().displayBox(layoutBox);
36         if (layoutBox.isFloatingPositioned()) {
37             displayBox.setTopLeft(this._positionForFloating(layoutBox));
38             this._addFloatingBox(layoutBox);
39             return;
40         }
41         if (Utils.hasClear(layoutBox))
42             return displayBox.setTopLeft(this._positionForClear(layoutBox));
43         // Intruding floats might force this box move.
44         displayBox.setTopLeft(this._computePositionToAvoidIntrudingFloats(layoutBox));
45     }
46
47     bottom() {
48         let leftBottom = this._bottom(this._leftFloatingStack());
49         let rightBottom = this._bottom(this._rightFloatingStack());
50         if (Number.isNaN(leftBottom) && Number.isNaN(rightBottom))
51             return Number.NaN;
52         if (!Number.isNaN(leftBottom) && !Number.isNaN(rightBottom))
53             return Math.max(leftBottom, rightBottom);
54         if (!Number.isNaN(leftBottom))
55             return leftBottom;
56         return rightBottom;
57     }
58
59     _positionForFloating(floatingBox) {
60         let absoluteFloatingBox = this._mapMarginBoxToFormattingRoot(floatingBox);
61         if (this._isEmpty())
62             return this._adjustedFloatingPosition(floatingBox, absoluteFloatingBox.top());
63         let verticalPosition = Math.max(absoluteFloatingBox.top(), this._mapDisplayMarginBoxToFormattingRoot(this._lastFloating()).top());
64         let spaceNeeded = absoluteFloatingBox.width();
65         while (true) {
66             let floatingPair = this._findInnerMostLeftAndRight(verticalPosition);
67             if (this._availableSpace(floatingBox.containingBlock(), floatingPair) >= spaceNeeded)
68                 return this._adjustedFloatingPosition(floatingBox, verticalPosition, floatingPair);
69             verticalPosition = this._moveToNextVerticalPosition(floatingPair);
70         }
71         return Math.Nan;
72     }
73
74     _positionForClear(layoutBox) {
75         ASSERT(Utils.hasClear(layoutBox));
76         let displayBox = this._formattingState().displayBox(layoutBox);
77         if (this._isEmpty())
78             return displayBox.topLeft();
79
80         let leftBottom = Number.NaN;
81         let rightBottom = Number.NaN;
82         if (Utils.hasClearLeft(layoutBox) || Utils.hasClearBoth(layoutBox))
83             leftBottom = this._bottom(this._leftFloatingStack());
84         if (Utils.hasClearRight(layoutBox) || Utils.hasClearBoth(layoutBox))
85             rightBottom = this._bottom(this._rightFloatingStack());
86
87         if (!Number.isNaN(leftBottom) && !Number.isNaN(rightBottom))
88             return new LayoutPoint(Math.max(leftBottom, rightBottom), displayBox.left());
89         if (!Number.isNaN(leftBottom))
90             return new LayoutPoint(leftBottom, displayBox.left());
91         if (!Number.isNaN(rightBottom))
92             return new LayoutPoint(rightBottom, displayBox.left());
93         return displayBox.topLeft();
94     }
95
96     _computePositionToAvoidIntrudingFloats(layoutBox) {
97         if (!layoutBox.establishesBlockFormattingContext() || this._isEmpty())
98             return this._formattingState().displayBox(layoutBox).topLeft();
99         // The border box of a table, a block-level replaced element, or an element in the normal flow that establishes
100         // a new block formatting context (such as an element with 'overflow' other than 'visible') must not overlap the
101         // margin box of any floats in the same block formatting context as the element itself.
102         // For some reason, we position this as if it was floating left.
103         return this._positionForFloating(layoutBox);
104     }
105
106     _findInnerMostLeftAndRight(verticalPosition) {
107         let leftFloating = this._findFloatingAtVerticalPosition(verticalPosition, this._leftFloatingStack());
108         let rightFloating = this._findFloatingAtVerticalPosition(verticalPosition, this._rightFloatingStack());
109         return { left: leftFloating, right: rightFloating };
110     }
111
112     _moveToNextVerticalPosition(floatingPair) {
113         if (!floatingPair.left && !floatingPair.right)
114             return Math.NaN;
115         let leftBottom = Number.POSITIVE_INFINITY;
116         let rightBottom = Number.POSITIVE_INFINITY;
117         if (floatingPair.left)
118             leftBottom = this._mapDisplayMarginBoxToFormattingRoot(floatingPair.left).bottom();
119         if (floatingPair.right)
120             rightBottom = this._mapDisplayMarginBoxToFormattingRoot(floatingPair.right).bottom();
121         return Math.min(leftBottom, rightBottom);
122     }
123
124     _availableSpace(containingBlock, floatingPair) {
125         let containingBlockContentBox = this._formattingState().displayBox(containingBlock);
126         if (floatingPair.left && floatingPair.right)
127             return floatingPair.right.left() - floatingPair.left.right();
128         if (floatingPair.left) {
129             return containingBlockContentBox.width() - (this._mapDisplayMarginBoxToFormattingRoot(floatingPair.left).right() - this._mapBorderBoxToFormattingRoot(containingBlock).left());
130         }
131         if (floatingPair.right)
132             return this._mapDisplayMarginBoxToFormattingRoot(floatingPair.right).left();
133         return containingBlockContentBox.width();
134     }
135
136     _findFloatingAtVerticalPosition(verticalPosition, floatingStack) {
137         let index = floatingStack.length;
138         while (--index >= 0 && this._mapDisplayMarginBoxToFormattingRoot(floatingStack[index]).bottom() <= verticalPosition);
139         return index >= 0 ? floatingStack[index] : null;
140     }
141
142     _isEmpty() {
143         return !this._leftFloatingStack().length && !this._rightFloatingStack().length;
144     }
145
146     _adjustedFloatingPosition(floatingBox, verticalPosition, leftRightFloatings) {
147         let containingBlock = floatingBox.containingBlock();
148         // Convert all coordinates relative to formatting context's root.
149         let left = this._mapContentBoxToFormattingRoot(containingBlock).left();
150         let right = this._mapContentBoxToFormattingRoot(containingBlock).right();
151         if (leftRightFloatings) {
152             if (leftRightFloatings.left) {
153                 let floatingBoxRight = this._mapDisplayMarginBoxToFormattingRoot(leftRightFloatings.left).right();
154                 if (floatingBoxRight > left)
155                     left = floatingBoxRight;
156             }
157
158             if (leftRightFloatings.right) {
159                 let floatingBoxLeft = this._mapDisplayMarginBoxToFormattingRoot(leftRightFloatings.right).left();
160                 if (floatingBoxLeft < right)
161                     right = floatingBoxLeft;
162             }
163         }
164         let floatingDisplayBox = this._formattingState().displayBox(floatingBox);
165         left += floatingDisplayBox.marginLeft();
166         right -= floatingDisplayBox.marginRight();
167         verticalPosition += floatingDisplayBox.marginTop();
168         // No convert them back relative to the floatingBox's containing block.
169         let containingBlockLeft = this._mapBorderBoxToFormattingRoot(containingBlock).left();
170         let containingBlockTop = this._mapBorderBoxToFormattingRoot(containingBlock).top();
171         left -= containingBlockLeft;
172         right -= containingBlockLeft;
173         verticalPosition -= containingBlockTop;
174
175         if (Utils.isFloatingLeft(floatingBox) || !Utils.isFloatingPositioned(floatingBox))
176             return new LayoutPoint(verticalPosition, left);
177         return new LayoutPoint(verticalPosition, right - floatingDisplayBox.rect().width());
178     }
179
180     _bottom(floatingStack) {
181         if (!floatingStack || !floatingStack.length)
182             return Number.NaN;
183         let max = Number.NEGATIVE_INFINITY;
184         for (let i = 0; i < floatingStack.length; ++i)
185             max = Math.max(this._mapDisplayMarginBoxToFormattingRoot(floatingStack[i]).bottom(), max);
186         return max;
187     }
188
189     _addFloatingBox(layoutBox) {
190         this._floatingState().addFloating(this._formattingState().displayBox(layoutBox), Utils.isFloatingLeft(layoutBox));
191     }
192
193     _mapMarginBoxToFormattingRoot(layoutBox) {
194         ASSERT(layoutBox instanceof Layout.Box);
195         return this._mapDisplayMarginBoxToFormattingRoot(this._formattingState().displayBox(layoutBox));
196     }
197
198     _mapDisplayMarginBoxToFormattingRoot(displayBox) {
199         ASSERT(displayBox instanceof Display.Box);
200         return Utils.marginBox(displayBox, this._formattingState().displayBox(this._formattingRoot()));
201     }
202
203     _mapBorderBoxToFormattingRoot(layoutBox) {
204         let displayBox = this._formattingState().displayBox(layoutBox);
205         let rootDisplayBox = this._formattingState().displayBox(this._formattingRoot());
206         return Utils.borderBox(displayBox, rootDisplayBox);
207     }
208
209     _mapContentBoxToFormattingRoot(layoutBox) {
210         let displayBox = this._formattingState().displayBox(layoutBox);
211         let rootDisplayBox = this._formattingState().displayBox(this._formattingRoot());
212         return Utils.contentBox(displayBox, rootDisplayBox);
213     }
214
215     _floatingState() {
216         return this.m_floatingState;
217     }
218
219     _formattingRoot() {
220         return this._formattingState().formattingRoot();
221     }
222
223     _formattingState() {
224         return this._floatingState().formattingState();
225     }
226
227     _lastFloating() {
228         return this._floatingState().lastFloating();
229     }
230
231     _leftFloatingStack() {
232         return this._floatingState().leftFloatingStack();
233     }
234
235     _rightFloatingStack() {
236         return this._floatingState().rightFloatingStack();
237     }
238 }