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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "LayoutState.h" |
28 | |
29 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
30 | |
31 | #include "BlockFormattingContext.h" |
32 | #include "BlockFormattingState.h" |
33 | #include "BlockInvalidation.h" |
34 | #include "DisplayBox.h" |
35 | #include "InlineFormattingContext.h" |
36 | #include "InlineFormattingState.h" |
37 | #include "InlineInvalidation.h" |
38 | #include "Invalidation.h" |
39 | #include "LayoutBox.h" |
40 | #include "LayoutContainer.h" |
41 | #include <wtf/IsoMallocInlines.h> |
42 | |
43 | namespace WebCore { |
44 | namespace Layout { |
45 | |
46 | WTF_MAKE_ISO_ALLOCATED_IMPL(LayoutState); |
47 | |
48 | LayoutState::LayoutState(const Container& initialContainingBlock) |
49 | : m_initialContainingBlock(makeWeakPtr(initialContainingBlock)) |
50 | { |
51 | // LayoutState is always initiated with the ICB. |
52 | ASSERT(!initialContainingBlock.parent()); |
53 | ASSERT(initialContainingBlock.establishesBlockFormattingContext()); |
54 | |
55 | auto& displayBox = displayBoxForLayoutBox(initialContainingBlock); |
56 | displayBox.setHorizontalMargin({ }); |
57 | displayBox.setHorizontalComputedMargin({ }); |
58 | displayBox.setVerticalMargin({ }); |
59 | displayBox.setBorder({ }); |
60 | displayBox.setPadding({ }); |
61 | displayBox.setTopLeft({ }); |
62 | displayBox.setContentBoxHeight(initialContainingBlock.style().logicalHeight().value()); |
63 | displayBox.setContentBoxWidth(initialContainingBlock.style().logicalWidth().value()); |
64 | |
65 | m_formattingContextRootListForLayout.add(&initialContainingBlock); |
66 | } |
67 | |
68 | void LayoutState::updateLayout() |
69 | { |
70 | ASSERT(!m_formattingContextRootListForLayout.isEmpty()); |
71 | for (auto* layoutRoot : m_formattingContextRootListForLayout) |
72 | layoutFormattingContextSubtree(*layoutRoot); |
73 | m_formattingContextRootListForLayout.clear(); |
74 | } |
75 | |
76 | void LayoutState::layoutFormattingContextSubtree(const Box& layoutRoot) |
77 | { |
78 | RELEASE_ASSERT(layoutRoot.establishesFormattingContext()); |
79 | auto formattingContext = createFormattingContext(layoutRoot); |
80 | formattingContext->layout(); |
81 | formattingContext->layoutOutOfFlowDescendants(layoutRoot); |
82 | } |
83 | |
84 | Display::Box& LayoutState::displayBoxForLayoutBox(const Box& layoutBox) const |
85 | { |
86 | return *m_layoutToDisplayBox.ensure(&layoutBox, [&layoutBox] { |
87 | return std::make_unique<Display::Box>(layoutBox.style()); |
88 | }).iterator->value; |
89 | } |
90 | |
91 | void LayoutState::styleChanged(const Box& layoutBox, StyleDiff styleDiff) |
92 | { |
93 | auto& formattingState = formattingStateForBox(layoutBox); |
94 | const Container* invalidationRoot = nullptr; |
95 | if (is<BlockFormattingState>(formattingState)) |
96 | invalidationRoot = BlockInvalidation::invalidate(layoutBox, styleDiff, *this, downcast<BlockFormattingState>(formattingState)).root; |
97 | else if (is<InlineFormattingState>(formattingState)) |
98 | invalidationRoot = InlineInvalidation::invalidate(layoutBox, styleDiff, *this, downcast<InlineFormattingState>(formattingState)).root; |
99 | else |
100 | ASSERT_NOT_IMPLEMENTED_YET(); |
101 | ASSERT(invalidationRoot); |
102 | m_formattingContextRootListForLayout.addVoid(invalidationRoot); |
103 | } |
104 | |
105 | void LayoutState::markNeedsUpdate(const Box&, OptionSet<UpdateType>) |
106 | { |
107 | } |
108 | |
109 | FormattingState& LayoutState::formattingStateForBox(const Box& layoutBox) const |
110 | { |
111 | auto& root = layoutBox.formattingContextRoot(); |
112 | RELEASE_ASSERT(m_formattingStates.contains(&root)); |
113 | return *m_formattingStates.get(&root); |
114 | } |
115 | |
116 | FormattingState& LayoutState::establishedFormattingState(const Box& formattingRoot) const |
117 | { |
118 | ASSERT(formattingRoot.establishesFormattingContext()); |
119 | RELEASE_ASSERT(m_formattingStates.contains(&formattingRoot)); |
120 | return *m_formattingStates.get(&formattingRoot); |
121 | } |
122 | |
123 | FormattingState& LayoutState::createFormattingStateForFormattingRootIfNeeded(const Box& formattingRoot) |
124 | { |
125 | ASSERT(formattingRoot.establishesFormattingContext()); |
126 | |
127 | if (formattingRoot.establishesInlineFormattingContext()) { |
128 | return *m_formattingStates.ensure(&formattingRoot, [&] { |
129 | |
130 | // If the block container box that initiates this inline formatting context also establishes a block context, the floats outside of the formatting root |
131 | // should not interfere with the content inside. |
132 | // <div style="float: left"></div><div style="overflow: hidden"> <- is a non-intrusive float, because overflow: hidden triggers new block formatting context.</div> |
133 | if (formattingRoot.establishesBlockFormattingContext()) |
134 | return std::make_unique<InlineFormattingState>(FloatingState::create(*this, formattingRoot), *this); |
135 | |
136 | // Otherwise, the formatting context inherits the floats from the parent formatting context. |
137 | // Find the formatting state in which this formatting root lives, not the one it creates and use its floating state. |
138 | return std::make_unique<InlineFormattingState>(formattingStateForBox(formattingRoot).floatingState(), *this); |
139 | }).iterator->value; |
140 | } |
141 | |
142 | if (formattingRoot.establishesBlockFormattingContext()) { |
143 | return *m_formattingStates.ensure(&formattingRoot, [&] { |
144 | |
145 | // Block formatting context always establishes a new floating state. |
146 | return std::make_unique<BlockFormattingState>(FloatingState::create(*this, formattingRoot), *this); |
147 | }).iterator->value; |
148 | } |
149 | |
150 | CRASH(); |
151 | } |
152 | |
153 | std::unique_ptr<FormattingContext> LayoutState::createFormattingContext(const Box& formattingContextRoot) |
154 | { |
155 | ASSERT(formattingContextRoot.establishesFormattingContext()); |
156 | if (formattingContextRoot.establishesInlineFormattingContext()) { |
157 | auto& inlineFormattingState = downcast<InlineFormattingState>(createFormattingStateForFormattingRootIfNeeded(formattingContextRoot)); |
158 | return std::make_unique<InlineFormattingContext>(formattingContextRoot, inlineFormattingState); |
159 | } |
160 | |
161 | if (formattingContextRoot.establishesBlockFormattingContext()) { |
162 | ASSERT(formattingContextRoot.establishesBlockFormattingContextOnly()); |
163 | auto& blockFormattingState = downcast<BlockFormattingState>(createFormattingStateForFormattingRootIfNeeded(formattingContextRoot)); |
164 | return std::make_unique<BlockFormattingContext>(formattingContextRoot, blockFormattingState); |
165 | } |
166 | |
167 | CRASH(); |
168 | } |
169 | |
170 | } |
171 | } |
172 | |
173 | #endif |
174 | |