OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/layout/ng/ng_block_layout_algorithm.h" | 5 #include "core/layout/ng/ng_block_layout_algorithm.h" |
6 | 6 |
7 #include "core/layout/ng/ng_constraint_space.h" | 7 #include "core/layout/ng/ng_constraint_space.h" |
8 #include "core/layout/ng/ng_fragment_builder.h" | 8 #include "core/layout/ng/ng_fragment_builder.h" |
9 #include "core/layout/ng/ng_fragment.h" | 9 #include "core/layout/ng/ng_fragment.h" |
10 #include "core/layout/ng/ng_length_utils.h" | 10 #include "core/layout/ng/ng_length_utils.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 case kStateChildLayout: { | 63 case kStateChildLayout: { |
64 if (current_child_) { | 64 if (current_child_) { |
65 NGFragment* fragment; | 65 NGFragment* fragment; |
66 if (!current_child_->Layout(constraint_space_for_children_, &fragment)) | 66 if (!current_child_->Layout(constraint_space_for_children_, &fragment)) |
67 return false; | 67 return false; |
68 NGBoxStrut child_margins = computeMargins( | 68 NGBoxStrut child_margins = computeMargins( |
69 *constraint_space_for_children_, *current_child_->Style(), | 69 *constraint_space_for_children_, *current_child_->Style(), |
70 constraint_space_for_children_->WritingMode(), | 70 constraint_space_for_children_->WritingMode(), |
71 constraint_space_for_children_->Direction()); | 71 constraint_space_for_children_->Direction()); |
72 | 72 |
73 LayoutUnit margin_block_start = CollapseMargins( | 73 LayoutUnit margin_block_start = |
74 *constraint_space, child_margins, fragment->MarginStrut()); | 74 CollapseMargins(*constraint_space, child_margins, *fragment); |
75 | 75 |
76 // TODO(layout-ng): Support auto margins | 76 // TODO(layout-ng): Support auto margins |
77 builder_->AddChild(fragment, | 77 builder_->AddChild(fragment, |
78 NGLogicalOffset(border_and_padding_.inline_start + | 78 NGLogicalOffset(border_and_padding_.inline_start + |
79 child_margins.inline_start, | 79 child_margins.inline_start, |
80 content_size_ + margin_block_start)); | 80 content_size_ + margin_block_start)); |
81 | 81 |
82 content_size_ += fragment->BlockSize() + margin_block_start; | 82 content_size_ += fragment->BlockSize() + margin_block_start; |
83 max_inline_size_ = | 83 max_inline_size_ = |
84 std::max(max_inline_size_, fragment->InlineSize() + | 84 std::max(max_inline_size_, fragment->InlineSize() + |
(...skipping 21 matching lines...) Expand all Loading... |
106 } | 106 } |
107 }; | 107 }; |
108 NOTREACHED(); | 108 NOTREACHED(); |
109 *out = nullptr; | 109 *out = nullptr; |
110 return true; | 110 return true; |
111 } | 111 } |
112 | 112 |
113 LayoutUnit NGBlockLayoutAlgorithm::CollapseMargins( | 113 LayoutUnit NGBlockLayoutAlgorithm::CollapseMargins( |
114 const NGConstraintSpace& space, | 114 const NGConstraintSpace& space, |
115 const NGBoxStrut& margins, | 115 const NGBoxStrut& margins, |
116 const NGMarginStrut& children_margin_strut) { | 116 const NGFragment& fragment) { |
117 // Calculate margin strut for the current child. | 117 // TODO(chrome-layout-team): Do not collapse margins for elements that |
118 NGMarginStrut curr_margin_strut = children_margin_strut; | 118 // establish new block formatting contexts |
| 119 |
| 120 // Zero-height boxes are ignored and do not participate in margin collapsing. |
| 121 bool is_zero_height_box = !fragment.BlockSize() && margins.IsEmpty(); |
| 122 if (is_zero_height_box) |
| 123 return LayoutUnit(); |
| 124 |
| 125 // Create the current child's margin strut from its children's margin strut. |
| 126 NGMarginStrut curr_margin_strut = fragment.MarginStrut(); |
119 | 127 |
120 // Calculate borders and padding for the current child. | 128 // Calculate borders and padding for the current child. |
121 NGBoxStrut borders = computeBorders(*current_child_->Style()); | 129 NGBoxStrut border_and_padding = |
122 NGBoxStrut paddings = computePadding(space, *current_child_->Style()); | 130 computeBorders(*current_child_->Style()) + |
123 LayoutUnit border_and_padding_before = | 131 computePadding(space, *current_child_->Style()); |
124 borders.block_start + paddings.block_start; | |
125 LayoutUnit border_and_padding_after = borders.block_end + paddings.block_end; | |
126 | 132 |
127 // Collapse BLOCK-START margins if there is no padding or border between | 133 // Collapse BLOCK-START margins if there is no padding or border between |
128 // parent (current child) and its first in-flow child. | 134 // parent (current child) and its first in-flow child. |
129 if (border_and_padding_before) { | 135 if (border_and_padding.block_start) { |
130 curr_margin_strut.SetMarginBlockStart(margins.block_start); | 136 curr_margin_strut.SetMarginBlockStart(margins.block_start); |
131 } else { | 137 } else { |
132 curr_margin_strut.AppendMarginBlockStart(margins.block_start); | 138 curr_margin_strut.AppendMarginBlockStart(margins.block_start); |
133 } | 139 } |
134 | 140 |
135 // Collapse BLOCK-END margins if | 141 // Collapse BLOCK-END margins if |
136 // 1) there is no padding or border between parent (current child) and its | 142 // 1) there is no padding or border between parent (current child) and its |
137 // first/last in-flow child | 143 // first/last in-flow child |
138 // 2) parent's logical height is auto. | 144 // 2) parent's logical height is auto. |
139 if (current_child_->Style()->logicalHeight().isAuto() && | 145 if (current_child_->Style()->logicalHeight().isAuto() && |
140 !border_and_padding_after) { | 146 !border_and_padding.block_end) { |
141 curr_margin_strut.AppendMarginBlockEnd(margins.block_end); | 147 curr_margin_strut.AppendMarginBlockEnd(margins.block_end); |
142 } else { | 148 } else { |
143 curr_margin_strut.SetMarginBlockEnd(margins.block_end); | 149 curr_margin_strut.SetMarginBlockEnd(margins.block_end); |
144 } | 150 } |
145 | 151 |
146 // Set the margin strut for the resultant fragment if this is the first or | 152 // Update the parent fragment's margin strut and calculate the margin |
147 // last child fragment. | 153 // block start position. |
148 if (current_child_ == first_child_) | 154 builder_->UpdateMarginStrut(curr_margin_strut); |
149 builder_->SetMarginStrutBlockStart(curr_margin_strut); | 155 LayoutUnit margin_block_start = ComputeCollapsedMarginBlockStart( |
150 if (!current_child_->NextSibling()) | 156 prev_child_margin_strut_, curr_margin_strut); |
151 builder_->SetMarginStrutBlockEnd(curr_margin_strut); | |
152 | |
153 // Compute the margin block start for adjoining blocks. | |
154 LayoutUnit margin_block_start; | |
155 if (current_child_ != first_child_) | |
156 margin_block_start = ComputeCollapsedMarginBlockStart( | |
157 prev_child_margin_strut_, curr_margin_strut); | |
158 | |
159 prev_child_margin_strut_ = curr_margin_strut; | 157 prev_child_margin_strut_ = curr_margin_strut; |
160 // TODO(layout-ng): support other Margin Collapsing use cases, | |
161 // i.e. support 0 height elements etc. | |
162 return margin_block_start; | 158 return margin_block_start; |
163 } | 159 } |
164 | 160 |
165 } // namespace blink | 161 } // namespace blink |
OLD | NEW |