2 * Copyright (C) 2018 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 Layout.Box = class Box {
29 constructor(node, id) {
31 this.m_rendererName = null;
34 this.m_nextSibling = null;
35 this.m_previousSibling = null;
36 this.m_isAnonymous = false;
37 this.m_establishedFormattingContext = null;
38 this.m_displayBox = null;
45 setRendererName(name) {
46 this.m_rendererName = name;
50 return this.m_rendererName;
62 return this.m_nextSibling;
66 let nextInFlowSibling = this.nextSibling();
67 while (nextInFlowSibling) {
68 if (nextInFlowSibling.isInFlow())
69 return nextInFlowSibling;
70 nextInFlowSibling = nextInFlowSibling.nextSibling();
75 nextInFlowOrFloatSibling() {
76 let nextInFlowSibling = this.nextSibling();
77 while (nextInFlowSibling) {
78 if (nextInFlowSibling.isInFlow() || nextInFlowSibling.isFloatingPositioned())
79 return nextInFlowSibling;
80 nextInFlowSibling = nextInFlowSibling.nextSibling();
86 return this.m_previousSibling;
89 previousInFlowSibling() {
90 let previousInFlowSibling = this.previousSibling();
91 while (previousInFlowSibling) {
92 if (previousInFlowSibling.isInFlow())
93 return previousInFlowSibling;
94 previousInFlowSibling = previousInFlowSibling.previousSibling();
100 this.m_parent = parent;
103 setNextSibling(nextSibling) {
104 this.m_nextSibling = nextSibling;
107 setPreviousSibling(previousSibling) {
108 this.m_previousSibling = previousSibling;
111 setDisplayBox(displayBox) {
112 ASSERT(!this.m_displayBox);
113 this.m_displayBox = displayBox;
117 ASSERT(this.m_displayBox);
118 return this.m_displayBox;
122 return this.displayBox().rect();
126 return this.rect().topLeft();
130 return this.rect().bottomRight();
138 return Utils.isBlockLevelElement(this.node());
141 isBlockContainerBox() {
142 return Utils.isBlockContainerElement(this.node());
146 return Utils.isInlineLevelElement(this.node());
150 this.m_isAnonymous = true;
154 return this.m_isAnonymous;
157 establishesFormattingContext() {
158 if (this.isAnonymous())
160 return this.establishesBlockFormattingContext() || this.establishesInlineFormattingContext();
163 establishedFormattingContext() {
164 if (this.establishesFormattingContext() && !this.m_establishedFormattingContext)
165 this.m_establishedFormattingContext = this.establishesBlockFormattingContext() ? new BlockFormattingContext(this) : new InlineFormattingContext(this);
166 return this.m_establishedFormattingContext;
169 establishesBlockFormattingContext() {
170 // 9.4.1 Block formatting contexts
171 // Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions)
172 // that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport)
173 // establish new block formatting contexts for their contents.
174 return this.isFloatingPositioned() || this.isAbsolutelyPositioned() || (this.isBlockContainerBox() && !this.isBlockLevelBox())
175 || (this.isBlockLevelBox() && !Utils.isOverflowVisible(this));
178 establishesInlineFormattingContext() {
183 return this.isOutOfFlowPositioned() || this.isRelativelyPositioned();
186 isRelativelyPositioned() {
187 return Utils.isRelativelyPositioned(this);
190 isAbsolutelyPositioned() {
191 return Utils.isAbsolutelyPositioned(this) || this.isFixedPositioned();
194 isFixedPositioned() {
195 return Utils.isFixedPositioned(this);
199 if (this.isAnonymous())
201 return !this.isFloatingOrOutOfFlowPositioned();
204 isOutOfFlowPositioned() {
205 return this.isAbsolutelyPositioned() || this.isFixedPositioned();
208 isInFlowPositioned() {
209 return this.isPositioned() && !this.isOutOfFlowPositioned();
212 isFloatingPositioned() {
213 return Utils.isFloatingPositioned(this);
216 isFloatingOrOutOfFlowPositioned() {
217 return this.isFloatingPositioned() || this.isOutOfFlowPositioned();
221 // FIXME: This should just be a simple instanceof check, but we are in the mainframe while the test document is in an iframe
222 // Let's just return root for both the RenderView and the <html> element.
223 return !this.parent() || !this.parent().parent();
229 if (!this.isPositioned() || this.isInFlowPositioned())
230 return this.parent();
231 if (this.isAbsolutelyPositioned() && !this.isFixedPositioned()) {
232 let ascendant = this.parent();
233 while (ascendant.parent() && !ascendant.isPositioned())
234 ascendant = ascendant.parent();
237 if (this.isFixedPositioned()) {
238 let ascendant = this.parent();
239 while (ascendant.parent())
240 ascendant = ascendant.parent();
243 ASSERT_NOT_REACHED();
247 isDescendantOf(container) {
249 let ascendant = this.parent();
250 while (ascendant && ascendant != container)
251 ascendant = ascendant.parent();
256 return this.displayBox().borderBox();
260 return this.displayBox().paddingBox();
264 return this.displayBox().contentBox();