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/inline/ng_inline_node.h" | 7 #include "core/layout/ng/inline/ng_inline_node.h" |
8 #include "core/layout/ng/ng_absolute_utils.h" | 8 #include "core/layout/ng/ng_absolute_utils.h" |
9 #include "core/layout/ng/ng_block_child_iterator.h" | 9 #include "core/layout/ng/ng_block_child_iterator.h" |
10 #include "core/layout/ng/ng_box_fragment.h" | |
11 #include "core/layout/ng/ng_constraint_space.h" | 10 #include "core/layout/ng/ng_constraint_space.h" |
12 #include "core/layout/ng/ng_constraint_space_builder.h" | 11 #include "core/layout/ng/ng_constraint_space_builder.h" |
13 #include "core/layout/ng/ng_floats_utils.h" | 12 #include "core/layout/ng/ng_floats_utils.h" |
14 #include "core/layout/ng/ng_fragment.h" | 13 #include "core/layout/ng/ng_fragment.h" |
15 #include "core/layout/ng/ng_fragment_builder.h" | 14 #include "core/layout/ng/ng_fragment_builder.h" |
16 #include "core/layout/ng/ng_layout_opportunity_iterator.h" | 15 #include "core/layout/ng/ng_layout_opportunity_iterator.h" |
17 #include "core/layout/ng/ng_length_utils.h" | 16 #include "core/layout/ng/ng_length_utils.h" |
18 #include "core/layout/ng/ng_out_of_flow_layout_part.h" | 17 #include "core/layout/ng/ng_out_of_flow_layout_part.h" |
19 #include "core/layout/ng/ng_space_utils.h" | 18 #include "core/layout/ng/ng_space_utils.h" |
20 #include "core/style/ComputedStyle.h" | 19 #include "core/style/ComputedStyle.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 sizes.min_content = std::max(sizes.min_content, child_sizes.min_content); | 122 sizes.min_content = std::max(sizes.min_content, child_sizes.min_content); |
124 sizes.max_content = std::max(sizes.max_content, child_sizes.max_content); | 123 sizes.max_content = std::max(sizes.max_content, child_sizes.max_content); |
125 } | 124 } |
126 | 125 |
127 sizes.max_content = std::max(sizes.min_content, sizes.max_content); | 126 sizes.max_content = std::max(sizes.min_content, sizes.max_content); |
128 return sizes; | 127 return sizes; |
129 } | 128 } |
130 | 129 |
131 NGLogicalOffset NGBlockLayoutAlgorithm::CalculateLogicalOffset( | 130 NGLogicalOffset NGBlockLayoutAlgorithm::CalculateLogicalOffset( |
132 const WTF::Optional<NGLogicalOffset>& known_fragment_offset) { | 131 const WTF::Optional<NGLogicalOffset>& known_fragment_offset) { |
132 if (known_fragment_offset) | |
133 return known_fragment_offset.value() - ContainerBfcOffset(); | |
133 LayoutUnit inline_offset = | 134 LayoutUnit inline_offset = |
134 border_and_padding_.inline_start + curr_child_margins_.inline_start; | 135 border_and_padding_.inline_start + curr_child_margins_.inline_start; |
135 LayoutUnit block_offset = content_size_; | 136 return {inline_offset, content_size_}; |
136 if (known_fragment_offset) { | 137 } |
137 block_offset = known_fragment_offset.value().block_offset - | 138 |
138 ContainerBfcOffset().block_offset; | 139 NGLogicalOffset NGBlockLayoutAlgorithm::PositionNewFc( |
ikilpatrick
2017/04/17 08:17:24
.nit, so i like having these types of methods clos
ikilpatrick
2017/04/17 08:17:24
might be worth writing up here which case this cov
Gleb Lanbin
2017/04/17 21:50:09
Done.
Gleb Lanbin
2017/04/17 21:50:10
Done.
| |
139 } | 140 const NGBoxFragment& fragment, |
140 return {inline_offset, block_offset}; | 141 const NGConstraintSpace* child_space) { |
ikilpatrick
2017/04/17 08:17:24
you aren't modifying child_space right? so this sh
Gleb Lanbin
2017/04/17 21:50:10
Done.
| |
142 // 1. Position all pending floats to a temporary space. | |
143 RefPtr<NGConstraintSpace> tmp_space = | |
144 NGConstraintSpaceBuilder(child_space) | |
145 .SetIsNewFormattingContext(false) | |
146 .ToConstraintSpace(child_space->WritingMode()); | |
147 | |
148 NGFragmentBuilder tmp_builder(NGPhysicalFragment::kFragmentBox, | |
149 /* node */ nullptr); | |
150 tmp_builder.SetBfcOffset(curr_bfc_offset_); | |
151 tmp_builder.MutableUnpositionedFloats().AppendVector( | |
152 container_builder_.UnpositionedFloats()); | |
153 PositionPendingFloats(curr_bfc_offset_.block_offset, tmp_space.Get(), | |
154 &tmp_builder); | |
ikilpatrick
2017/04/17 08:17:24
is there an easy way we can avoid tmp_builder here
Gleb Lanbin
2017/04/17 21:50:10
Done.
| |
155 | |
156 // 2. Find an estimated layout opportunity for our fragment. | |
157 NGLayoutOpportunity opportunity = FindLayoutOpportunityForFragment( | |
158 tmp_space->Exclusions().get(), child_space->AvailableSize(), | |
159 curr_bfc_offset_, curr_child_margins_, fragment); | |
160 | |
161 // 3. If the found opportunity lies on the same line with our estimated | |
162 // child's BFC offset then merge fragment's margins with the current | |
163 // MarginStrut. | |
164 if (opportunity.offset.block_offset == curr_bfc_offset_.block_offset) | |
165 curr_margin_strut_.Append(curr_child_margins_.block_start); | |
166 curr_bfc_offset_.block_offset += curr_margin_strut_.Sum(); | |
167 curr_margin_strut_ = {}; | |
168 | |
169 // 4. Child's BFC block's offset is known here. | |
ikilpatrick
2017/04/17 08:17:24
4. The child's BFC block offset is known here.
?
Gleb Lanbin
2017/04/17 21:50:10
Done.
| |
170 MaybeUpdateFragmentBfcOffset(ConstraintSpace(), curr_bfc_offset_, | |
171 &container_builder_); | |
172 PositionPendingFloats(curr_bfc_offset_.block_offset, MutableConstraintSpace(), | |
173 &container_builder_); | |
174 | |
175 // 5. Find the final layout opportunity for the fragment after all pending | |
176 // floats are positioned at the correct BFC block's offset. | |
177 opportunity = FindLayoutOpportunityForFragment( | |
178 MutableConstraintSpace()->Exclusions().get(), | |
179 child_space->AvailableSize(), curr_bfc_offset_, curr_child_margins_, | |
180 fragment); | |
181 | |
182 curr_bfc_offset_ = opportunity.offset; | |
183 return curr_bfc_offset_; | |
184 } | |
185 | |
186 NGLogicalOffset NGBlockLayoutAlgorithm::PositionWithBfcOffset( | |
187 const NGBoxFragment& fragment) { | |
188 DCHECK(fragment.BfcOffset()); | |
189 curr_bfc_offset_.block_offset = fragment.BfcOffset().value().block_offset; | |
190 MaybeUpdateFragmentBfcOffset(ConstraintSpace(), curr_bfc_offset_, | |
191 &container_builder_); | |
192 PositionPendingFloats(curr_bfc_offset_.block_offset, MutableConstraintSpace(), | |
193 &container_builder_); | |
194 return fragment.BfcOffset().value(); | |
195 } | |
196 | |
197 NGLogicalOffset NGBlockLayoutAlgorithm::PositionWithParentBfc() { | |
198 curr_bfc_offset_ += | |
199 {curr_child_margins_.inline_start, curr_margin_strut_.Sum()}; | |
200 return curr_bfc_offset_; | |
141 } | 201 } |
142 | 202 |
143 RefPtr<NGLayoutResult> NGBlockLayoutAlgorithm::Layout() { | 203 RefPtr<NGLayoutResult> NGBlockLayoutAlgorithm::Layout() { |
144 WTF::Optional<MinMaxContentSize> min_max_size; | 204 WTF::Optional<MinMaxContentSize> min_max_size; |
145 if (NeedMinMaxContentSize(ConstraintSpace(), Style())) | 205 if (NeedMinMaxContentSize(ConstraintSpace(), Style())) |
146 min_max_size = ComputeMinMaxContentSize(); | 206 min_max_size = ComputeMinMaxContentSize(); |
147 | 207 |
148 border_and_padding_ = ComputeBorders(ConstraintSpace(), Style()) + | 208 border_and_padding_ = ComputeBorders(ConstraintSpace(), Style()) + |
149 ComputePadding(ConstraintSpace(), Style()); | 209 ComputePadding(ConstraintSpace(), Style()); |
150 | 210 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
214 content_size_ + curr_margin_strut_.Sum()}; | 274 content_size_ + curr_margin_strut_.Sum()}; |
215 container_builder_.AddOutOfFlowChildCandidate(ToNGBlockNode(child), | 275 container_builder_.AddOutOfFlowChildCandidate(ToNGBlockNode(child), |
216 offset); | 276 offset); |
217 NGBlockChildIterator::Entry entry = child_iterator.NextChild(); | 277 NGBlockChildIterator::Entry entry = child_iterator.NextChild(); |
218 child = entry.node; | 278 child = entry.node; |
219 child_break_token = entry.token; | 279 child_break_token = entry.token; |
220 continue; | 280 continue; |
221 } | 281 } |
222 } | 282 } |
223 | 283 |
224 PrepareChildLayout(child); | 284 NGLogicalOffset child_bfc_offset = PrepareChildLayout(child); |
225 RefPtr<NGConstraintSpace> child_space = | 285 RefPtr<NGConstraintSpace> child_space = |
226 CreateConstraintSpaceForChild(child); | 286 CreateConstraintSpaceForChild(child_bfc_offset, child); |
227 RefPtr<NGLayoutResult> layout_result = | 287 RefPtr<NGLayoutResult> layout_result = |
228 child->Layout(child_space.Get(), child_break_token); | 288 child->Layout(child_space.Get(), child_break_token); |
229 | 289 |
230 FinishChildLayout(child, child_space.Get(), layout_result); | 290 if (child->IsFloating()) |
291 FinishFloatChildLayout(child->Style(), *child_space, &*layout_result); | |
292 else | |
293 FinishChildLayout(&*child_space, &*layout_result); | |
231 | 294 |
232 entry = child_iterator.NextChild(); | 295 entry = child_iterator.NextChild(); |
233 child = entry.node; | 296 child = entry.node; |
234 child_break_token = entry.token; | 297 child_break_token = entry.token; |
235 | 298 |
236 if (IsOutOfSpace(ConstraintSpace(), content_size_)) | 299 if (IsOutOfSpace(ConstraintSpace(), content_size_)) |
237 break; | 300 break; |
238 } | 301 } |
239 | 302 |
240 // Margins collapsing: | 303 // Margins collapsing: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 | 339 |
277 container_builder_.SetOverflowSize( | 340 container_builder_.SetOverflowSize( |
278 NGLogicalSize(max_inline_size_, content_size_)); | 341 NGLogicalSize(max_inline_size_, content_size_)); |
279 | 342 |
280 if (ConstraintSpace().HasBlockFragmentation()) | 343 if (ConstraintSpace().HasBlockFragmentation()) |
281 FinalizeForFragmentation(); | 344 FinalizeForFragmentation(); |
282 | 345 |
283 return container_builder_.ToBoxFragment(); | 346 return container_builder_.ToBoxFragment(); |
284 } | 347 } |
285 | 348 |
286 void NGBlockLayoutAlgorithm::PrepareChildLayout(NGLayoutInputNode* child) { | 349 NGLogicalOffset NGBlockLayoutAlgorithm::PrepareChildLayout( |
350 NGLayoutInputNode* child) { | |
287 DCHECK(child); | 351 DCHECK(child); |
288 | 352 |
353 curr_bfc_offset_ = container_builder_.BfcOffset() | |
354 ? container_builder_.BfcOffset().value() | |
355 : ConstraintSpace().BfcOffset(); | |
356 curr_bfc_offset_ += {border_and_padding_.inline_start, content_size_}; | |
357 | |
289 // Calculate margins in parent's writing mode. | 358 // Calculate margins in parent's writing mode. |
290 curr_child_margins_ = CalculateMargins( | 359 curr_child_margins_ = CalculateMargins( |
291 child, *space_builder_.ToConstraintSpace( | 360 child, *space_builder_.ToConstraintSpace( |
292 FromPlatformWritingMode(Style().GetWritingMode()))); | 361 FromPlatformWritingMode(Style().GetWritingMode()))); |
293 | 362 |
294 // Set estimated BFC offset to the next child's constraint space. | |
295 curr_bfc_offset_ = container_builder_.BfcOffset() | |
296 ? container_builder_.BfcOffset().value() | |
297 : ConstraintSpace().BfcOffset(); | |
298 curr_bfc_offset_.block_offset += content_size_; | |
299 curr_bfc_offset_.inline_offset += border_and_padding_.inline_start; | |
300 | |
301 bool is_floating = child->IsBlock() && child->Style().IsFloating(); | |
302 | |
303 bool should_position_pending_floats = | 363 bool should_position_pending_floats = |
304 child->IsBlock() && !is_floating && | 364 !child->IsFloating() && |
305 !IsNewFormattingContextForBlockLevelChild(Style(), *child) && | 365 !IsNewFormattingContextForBlockLevelChild(Style(), *child) && |
306 ClearanceMayAffectLayout(ConstraintSpace(), | 366 ClearanceMayAffectLayout(ConstraintSpace(), |
307 container_builder_.UnpositionedFloats(), | 367 container_builder_.UnpositionedFloats(), |
308 child->Style()); | 368 child->Style()); |
309 | 369 |
310 // Children which may clear a float need to force all the pending floats to | 370 // Children which may clear a float need to force all the pending floats to |
311 // be positioned before layout. This also resolves the fragment's bfc offset. | 371 // be positioned before layout. This also resolves the fragment's bfc offset. |
312 if (should_position_pending_floats) { | 372 if (should_position_pending_floats) { |
313 LayoutUnit origin_point_block_offset = | 373 LayoutUnit origin_point_block_offset = |
314 curr_bfc_offset_.block_offset + curr_margin_strut_.Sum(); | 374 curr_bfc_offset_.block_offset + curr_margin_strut_.Sum(); |
315 MaybeUpdateFragmentBfcOffset( | 375 MaybeUpdateFragmentBfcOffset( |
316 ConstraintSpace(), | 376 ConstraintSpace(), |
317 {curr_bfc_offset_.inline_offset, origin_point_block_offset}, | 377 {curr_bfc_offset_.inline_offset, origin_point_block_offset}, |
318 &container_builder_); | 378 &container_builder_); |
319 PositionPendingFloats(origin_point_block_offset, MutableConstraintSpace(), | 379 PositionPendingFloats(origin_point_block_offset, MutableConstraintSpace(), |
320 &container_builder_); | 380 &container_builder_); |
321 } | 381 } |
322 | 382 |
323 bool is_inflow = child->IsInline() || !is_floating; | 383 bool is_inflow = child->IsInline() || !child->IsFloating(); |
324 | 384 |
385 NGLogicalOffset child_bfc_offset = curr_bfc_offset_; | |
325 // Only inflow children (e.g. not floats) are included in the child's margin | 386 // Only inflow children (e.g. not floats) are included in the child's margin |
326 // strut as they do not participate in margin collapsing. | 387 // strut as they do not participate in margin collapsing. |
327 if (is_inflow) { | 388 if (is_inflow) { |
328 curr_bfc_offset_.inline_offset += curr_child_margins_.inline_start; | 389 child_bfc_offset.inline_offset += curr_child_margins_.inline_start; |
329 // Append the current margin strut with child's block start margin. | 390 // Append the current margin strut with child's block start margin. |
330 // Non empty border/padding use cases are handled inside of the child's | 391 // Non empty border/padding, new FC use cases are handled inside of the |
331 // layout. | 392 // child's layout. |
332 curr_margin_strut_.Append(curr_child_margins_.block_start); | 393 if (!IsNewFormattingContextForBlockLevelChild(Style(), *child)) |
ikilpatrick
2017/04/17 08:17:24
you don't strictly need to do this right?
Gleb Lanbin
2017/04/17 21:50:09
the decision about whether we should append margin
| |
394 curr_margin_strut_.Append(curr_child_margins_.block_start); | |
333 } | 395 } |
334 | 396 |
335 // TODO(ikilpatrick): Children which establish new formatting contexts need | 397 if (bool should_collapse_margins_if_inline = child->IsInline()) { |
336 // to be placed using the opportunity iterator before we can collapse margins. | |
337 // If the child is placed at the block_start of this fragment, then its | |
338 // margins do impact the position of its parent, if not (its placed below a | |
339 // float for example) it doesn't. \o/ | |
340 | |
341 bool should_collapse_margins = | |
342 child->IsInline() || | |
343 (!is_floating && | |
344 IsNewFormattingContextForBlockLevelChild(Style(), *child)); | |
345 | |
346 // Inline children or children which establish a block formatting context | |
347 // collapse margins and position themselves immediately as they need to know | |
348 // their BFC offset for fragmentation purposes. | |
349 if (should_collapse_margins) { | |
350 curr_bfc_offset_.block_offset += curr_margin_strut_.Sum(); | 398 curr_bfc_offset_.block_offset += curr_margin_strut_.Sum(); |
351 MaybeUpdateFragmentBfcOffset(ConstraintSpace(), curr_bfc_offset_, | 399 MaybeUpdateFragmentBfcOffset(ConstraintSpace(), curr_bfc_offset_, |
352 &container_builder_); | 400 &container_builder_); |
353 PositionPendingFloats(curr_bfc_offset_.block_offset, | 401 PositionPendingFloats(curr_bfc_offset_.block_offset, |
354 MutableConstraintSpace(), &container_builder_); | 402 MutableConstraintSpace(), &container_builder_); |
355 curr_margin_strut_ = {}; | 403 curr_margin_strut_ = {}; |
356 } | 404 } |
405 child_bfc_offset.block_offset = curr_bfc_offset_.block_offset; | |
406 return child_bfc_offset; | |
357 } | 407 } |
358 | 408 |
359 void NGBlockLayoutAlgorithm::FinishChildLayout( | 409 void NGBlockLayoutAlgorithm::FinishChildLayout( |
360 NGLayoutInputNode* child, | 410 const NGConstraintSpace* child_space, |
361 NGConstraintSpace* child_space, | 411 NGLayoutResult* layout_result) { |
362 RefPtr<NGLayoutResult> layout_result) { | |
363 NGBoxFragment fragment( | |
364 ConstraintSpace().WritingMode(), | |
365 ToNGPhysicalBoxFragment(layout_result->PhysicalFragment().Get())); | |
366 | |
367 // Pull out unpositioned floats to the current fragment. This may needed if | 412 // Pull out unpositioned floats to the current fragment. This may needed if |
368 // for example the child fragment could not position its floats because it's | 413 // for example the child fragment could not position its floats because it's |
369 // empty and therefore couldn't determine its position in space. | 414 // empty and therefore couldn't determine its position in space. |
370 container_builder_.MutableUnpositionedFloats().AppendVector( | 415 container_builder_.MutableUnpositionedFloats().AppendVector( |
371 layout_result->UnpositionedFloats()); | 416 layout_result->UnpositionedFloats()); |
372 | 417 |
373 if (child->IsBlock() && child->Style().IsFloating()) { | 418 NGBoxFragment fragment( |
374 NGLogicalOffset origin_offset = constraint_space_->BfcOffset(); | 419 ConstraintSpace().WritingMode(), |
375 origin_offset.inline_offset += border_and_padding_.inline_start; | 420 ToNGPhysicalBoxFragment(layout_result->PhysicalFragment().Get())); |
376 RefPtr<NGFloatingObject> floating_object = NGFloatingObject::Create( | |
377 child->Style(), child_space->WritingMode(), | |
378 child_space->AvailableSize(), origin_offset, | |
379 constraint_space_->BfcOffset(), curr_child_margins_, | |
380 layout_result->PhysicalFragment().Get()); | |
381 container_builder_.AddUnpositionedFloat(floating_object); | |
382 // No need to postpone the positioning if we know the correct offset. | |
383 if (container_builder_.BfcOffset()) { | |
384 NGLogicalOffset origin_point = curr_bfc_offset_; | |
385 // Adjust origin point to the margins of the last child. | |
386 // Example: <div style="margin-bottom: 20px"><float></div> | |
387 // <div style="margin-bottom: 30px"></div> | |
388 origin_point.block_offset += curr_margin_strut_.Sum(); | |
389 PositionPendingFloats(origin_point.block_offset, MutableConstraintSpace(), | |
390 &container_builder_); | |
391 } | |
392 return; | |
393 } | |
394 | 421 |
395 // Determine the fragment's position in the parent space either by using | 422 // Determine the fragment's position in the parent space. |
ikilpatrick
2017/04/17 08:17:24
Yay! thanks for pulling these out.
Gleb Lanbin
2017/04/17 21:50:10
Acknowledged.
| |
396 // content_size_ or known fragment's BFC offset. | 423 WTF::Optional<NGLogicalOffset> child_bfc_offset; |
397 WTF::Optional<NGLogicalOffset> bfc_offset; | 424 if (child_space->IsNewFormattingContext()) |
398 if (child_space->IsNewFormattingContext()) { | 425 child_bfc_offset = PositionNewFc(fragment, child_space); |
399 bfc_offset = curr_bfc_offset_; | 426 else if (fragment.BfcOffset()) |
400 } else if (fragment.BfcOffset()) { | 427 child_bfc_offset = PositionWithBfcOffset(fragment); |
401 // Fragment that knows its offset can be used to set parent's BFC position. | 428 else if (container_builder_.BfcOffset()) |
402 curr_bfc_offset_.block_offset = fragment.BfcOffset().value().block_offset; | 429 child_bfc_offset = PositionWithParentBfc(); |
403 MaybeUpdateFragmentBfcOffset(ConstraintSpace(), curr_bfc_offset_, | 430 |
404 &container_builder_); | 431 NGLogicalOffset logical_offset = CalculateLogicalOffset(child_bfc_offset); |
405 PositionPendingFloats(curr_bfc_offset_.block_offset, | |
406 MutableConstraintSpace(), &container_builder_); | |
407 bfc_offset = curr_bfc_offset_; | |
408 } else if (container_builder_.BfcOffset()) { | |
409 // Fragment doesn't know its offset but we can still calculate its BFC | |
410 // position because the parent fragment's BFC is known. | |
411 // Example: | |
412 // BFC Offset is known here because of the padding. | |
413 // <div style="padding: 1px"> | |
414 // <div id="empty-div" style="margins: 1px"></div> | |
415 bfc_offset = curr_bfc_offset_; | |
416 bfc_offset.value().block_offset += curr_margin_strut_.Sum(); | |
417 } | |
418 NGLogicalOffset logical_offset = CalculateLogicalOffset(bfc_offset); | |
419 | 432 |
420 // Update margin strut. | 433 // Update margin strut. |
421 curr_margin_strut_ = fragment.EndMarginStrut(); | 434 curr_margin_strut_ = fragment.EndMarginStrut(); |
422 curr_margin_strut_.Append(curr_child_margins_.block_end); | 435 curr_margin_strut_.Append(curr_child_margins_.block_end); |
423 | 436 |
424 // Only modify content_size if BlockSize is not empty. It's needed to prevent | 437 // Only modify content_size if BlockSize is not empty. It's needed to prevent |
425 // the situation when logical_offset is included in content_size for empty | 438 // the situation when logical_offset is included in content_size for empty |
426 // blocks. Example: | 439 // blocks. Example: |
427 // <div style="overflow:hidden"> | 440 // <div style="overflow:hidden"> |
428 // <div style="margin-top: 8px"></div> | 441 // <div style="margin-top: 8px"></div> |
429 // <div style="margin-top: 10px"></div> | 442 // <div style="margin-top: 10px"></div> |
430 // </div> | 443 // </div> |
431 if (fragment.BlockSize()) | 444 if (fragment.BlockSize()) |
432 content_size_ = fragment.BlockSize() + logical_offset.block_offset; | 445 content_size_ = fragment.BlockSize() + logical_offset.block_offset; |
433 max_inline_size_ = | 446 max_inline_size_ = |
434 std::max(max_inline_size_, fragment.InlineSize() + | 447 std::max(max_inline_size_, fragment.InlineSize() + |
435 curr_child_margins_.InlineSum() + | 448 curr_child_margins_.InlineSum() + |
436 border_and_padding_.InlineSum()); | 449 border_and_padding_.InlineSum()); |
437 | 450 |
438 container_builder_.AddChild(layout_result, logical_offset); | 451 container_builder_.AddChild(layout_result, logical_offset); |
439 } | 452 } |
440 | 453 |
454 void NGBlockLayoutAlgorithm::FinishFloatChildLayout( | |
455 const ComputedStyle& child_style, | |
456 const NGConstraintSpace& child_space, | |
457 const NGLayoutResult* layout_result) { | |
458 NGLogicalOffset origin_offset = constraint_space_->BfcOffset(); | |
459 origin_offset.inline_offset += border_and_padding_.inline_start; | |
460 RefPtr<NGFloatingObject> floating_object = NGFloatingObject::Create( | |
461 child_style, child_space.WritingMode(), child_space.AvailableSize(), | |
462 origin_offset, constraint_space_->BfcOffset(), curr_child_margins_, | |
463 layout_result->PhysicalFragment().Get()); | |
464 container_builder_.AddUnpositionedFloat(floating_object); | |
465 // No need to postpone the positioning if we know the correct offset. | |
ikilpatrick
2017/04/17 08:17:24
can you add a newline above this line? (just havin
Gleb Lanbin
2017/04/17 21:50:10
Done.
| |
466 if (container_builder_.BfcOffset()) { | |
467 NGLogicalOffset origin_point = curr_bfc_offset_; | |
468 // Adjust origin point to the margins of the last child. | |
469 // Example: <div style="margin-bottom: 20px"><float></div> | |
470 // <div style="margin-bottom: 30px"></div> | |
471 origin_point.block_offset += curr_margin_strut_.Sum(); | |
472 PositionPendingFloats(origin_point.block_offset, MutableConstraintSpace(), | |
473 &container_builder_); | |
474 } | |
475 } | |
476 | |
441 void NGBlockLayoutAlgorithm::FinalizeForFragmentation() { | 477 void NGBlockLayoutAlgorithm::FinalizeForFragmentation() { |
442 LayoutUnit used_block_size = | 478 LayoutUnit used_block_size = |
443 BreakToken() ? BreakToken()->UsedBlockSize() : LayoutUnit(); | 479 BreakToken() ? BreakToken()->UsedBlockSize() : LayoutUnit(); |
444 LayoutUnit block_size = ComputeBlockSizeForFragment( | 480 LayoutUnit block_size = ComputeBlockSizeForFragment( |
445 ConstraintSpace(), Style(), used_block_size + content_size_); | 481 ConstraintSpace(), Style(), used_block_size + content_size_); |
446 | 482 |
447 block_size -= used_block_size; | 483 block_size -= used_block_size; |
448 DCHECK_GE(block_size, LayoutUnit()) | 484 DCHECK_GE(block_size, LayoutUnit()) |
449 << "Adding and subtracting the used_block_size shouldn't leave the " | 485 << "Adding and subtracting the used_block_size shouldn't leave the " |
450 "block_size for this fragment smaller than zero."; | 486 "block_size for this fragment smaller than zero."; |
(...skipping 22 matching lines...) Expand all Loading... | |
473 | 509 |
474 // The end of the block fits in the current fragmentainer. | 510 // The end of the block fits in the current fragmentainer. |
475 container_builder_.SetBlockSize(block_size); | 511 container_builder_.SetBlockSize(block_size); |
476 container_builder_.SetBlockOverflow(content_size_); | 512 container_builder_.SetBlockOverflow(content_size_); |
477 } | 513 } |
478 | 514 |
479 NGBoxStrut NGBlockLayoutAlgorithm::CalculateMargins( | 515 NGBoxStrut NGBlockLayoutAlgorithm::CalculateMargins( |
480 NGLayoutInputNode* child, | 516 NGLayoutInputNode* child, |
481 const NGConstraintSpace& space) { | 517 const NGConstraintSpace& space) { |
482 DCHECK(child); | 518 DCHECK(child); |
519 if (child->IsInline()) | |
520 return {}; | |
483 const ComputedStyle& child_style = child->Style(); | 521 const ComputedStyle& child_style = child->Style(); |
484 | 522 |
485 WTF::Optional<MinMaxContentSize> sizes; | 523 WTF::Optional<MinMaxContentSize> sizes; |
486 if (NeedMinMaxContentSize(space, child_style)) | 524 if (NeedMinMaxContentSize(space, child_style)) |
487 sizes = child->ComputeMinMaxContentSize(); | 525 sizes = child->ComputeMinMaxContentSize(); |
488 | 526 |
489 LayoutUnit child_inline_size = | 527 LayoutUnit child_inline_size = |
490 ComputeInlineSizeForFragment(space, child_style, sizes); | 528 ComputeInlineSizeForFragment(space, child_style, sizes); |
491 NGBoxStrut margins = ComputeMargins(space, child_style, space.WritingMode(), | 529 NGBoxStrut margins = ComputeMargins(space, child_style, space.WritingMode(), |
492 space.Direction()); | 530 space.Direction()); |
493 if (!child_style.IsFloating()) { | 531 if (!child->IsFloating()) { |
494 ApplyAutoMargins(space, child_style, child_inline_size, &margins); | 532 ApplyAutoMargins(space, child_style, child_inline_size, &margins); |
495 } | 533 } |
496 return margins; | 534 return margins; |
497 } | 535 } |
498 | 536 |
499 RefPtr<NGConstraintSpace> NGBlockLayoutAlgorithm::CreateConstraintSpaceForChild( | 537 RefPtr<NGConstraintSpace> NGBlockLayoutAlgorithm::CreateConstraintSpaceForChild( |
538 const NGLogicalOffset& child_bfc_offset, | |
500 NGLayoutInputNode* child) { | 539 NGLayoutInputNode* child) { |
501 DCHECK(child); | 540 DCHECK(child); |
502 | 541 |
503 const ComputedStyle& child_style = child->Style(); | 542 const ComputedStyle& child_style = child->Style(); |
504 bool is_new_bfc = IsNewFormattingContextForBlockLevelChild(Style(), *child); | 543 bool is_new_bfc = IsNewFormattingContextForBlockLevelChild(Style(), *child); |
505 space_builder_.SetIsNewFormattingContext(is_new_bfc) | 544 space_builder_.SetIsNewFormattingContext(is_new_bfc) |
506 .SetBfcOffset(curr_bfc_offset_); | 545 .SetBfcOffset(child_bfc_offset); |
507 | 546 |
508 if (child->IsInline()) { | 547 if (child->IsInline()) { |
509 // TODO(kojii): Setup space_builder_ appropriately for inline child. | 548 // TODO(kojii): Setup space_builder_ appropriately for inline child. |
510 space_builder_.SetBfcOffset(curr_bfc_offset_) | 549 space_builder_.SetClearanceOffset(ConstraintSpace().ClearanceOffset()); |
511 .SetClearanceOffset(ConstraintSpace().ClearanceOffset()); | |
512 return space_builder_.ToConstraintSpace( | 550 return space_builder_.ToConstraintSpace( |
513 FromPlatformWritingMode(Style().GetWritingMode())); | 551 FromPlatformWritingMode(Style().GetWritingMode())); |
514 } | 552 } |
515 | 553 |
516 space_builder_ | 554 space_builder_ |
517 .SetClearanceOffset( | 555 .SetClearanceOffset( |
518 GetClearanceOffset(constraint_space_->Exclusions(), child_style)) | 556 GetClearanceOffset(constraint_space_->Exclusions(), child_style)) |
519 .SetIsShrinkToFit(ShouldShrinkToFit(Style(), child_style)) | 557 .SetIsShrinkToFit(ShouldShrinkToFit(Style(), child_style)) |
520 .SetTextDirection(child_style.Direction()); | 558 .SetTextDirection(child_style.Direction()); |
521 | 559 |
522 // Float's margins are not included in child's space because: | 560 // Float's margins are not included in child's space because: |
523 // 1) Floats do not participate in margins collapsing. | 561 // 1) Floats do not participate in margins collapsing. |
524 // 2) Floats margins are used separately to calculate floating exclusions. | 562 // 2) Floats margins are used separately to calculate floating exclusions. |
525 space_builder_.SetMarginStrut(child_style.IsFloating() ? NGMarginStrut() | 563 space_builder_.SetMarginStrut(child->IsFloating() ? NGMarginStrut() |
526 : curr_margin_strut_); | 564 : curr_margin_strut_); |
527 | 565 |
528 LayoutUnit space_available; | 566 LayoutUnit space_available; |
529 if (constraint_space_->HasBlockFragmentation()) { | 567 if (constraint_space_->HasBlockFragmentation()) { |
530 space_available = ConstraintSpace().FragmentainerSpaceAvailable(); | 568 space_available = ConstraintSpace().FragmentainerSpaceAvailable(); |
531 // If a block establishes a new formatting context we must know our | 569 // If a block establishes a new formatting context we must know our |
532 // position in the formatting context, and are able to adjust the | 570 // position in the formatting context, and are able to adjust the |
533 // fragmentation line. | 571 // fragmentation line. |
534 if (is_new_bfc) { | 572 if (is_new_bfc) { |
535 space_available -= curr_bfc_offset_.block_offset; | 573 space_available -= child_bfc_offset.block_offset; |
536 } | 574 } |
537 } | 575 } |
538 space_builder_.SetFragmentainerSpaceAvailable(space_available); | 576 space_builder_.SetFragmentainerSpaceAvailable(space_available); |
539 | 577 |
540 return space_builder_.ToConstraintSpace( | 578 return space_builder_.ToConstraintSpace( |
541 FromPlatformWritingMode(child_style.GetWritingMode())); | 579 FromPlatformWritingMode(child_style.GetWritingMode())); |
542 } | 580 } |
543 } // namespace blink | 581 } // namespace blink |
OLD | NEW |