-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
layout_block.cc
2565 lines (2256 loc) · 99.5 KB
/
layout_block.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2007 David Smith (catfish.man@gmail.com)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc.
* All rights reserved.
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/core/layout/layout_block.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/display_lock/display_lock_utilities.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/editing/drag_caret.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/ime/input_method_controller.h"
#include "third_party/blink/renderer/core/editing/text_affinity.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/html/html_marquee_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_box.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_item.h"
#include "third_party/blink/renderer/core/layout/box_layout_extra_input.h"
#include "third_party/blink/renderer/core/layout/hit_test_location.h"
#include "third_party/blink/renderer/core/layout/hit_test_result.h"
#include "third_party/blink/renderer/core/layout/layout_flexible_box.h"
#include "third_party/blink/renderer/core/layout/layout_flow_thread.h"
#include "third_party/blink/renderer/core/layout/layout_grid.h"
#include "third_party/blink/renderer/core/layout/layout_inline.h"
#include "third_party/blink/renderer/core/layout/layout_multi_column_flow_thread.h"
#include "third_party/blink/renderer/core/layout/layout_multi_column_spanner_placeholder.h"
#include "third_party/blink/renderer/core/layout/layout_object_factory.h"
#include "third_party/blink/renderer/core/layout/layout_object_inlines.h"
#include "third_party/blink/renderer/core/layout/layout_table_cell.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/layout/line/inline_text_box.h"
#include "third_party/blink/renderer/core/layout/ng/legacy_layout_tree_walking.h"
#include "third_party/blink/renderer/core/layout/ng/ng_constraint_space.h"
#include "third_party/blink/renderer/core/layout/ng/ng_length_utils.h"
#include "third_party/blink/renderer/core/layout/ng/ng_physical_box_fragment.h"
#include "third_party/blink/renderer/core/layout/ng/svg/layout_ng_svg_text.h"
#include "third_party/blink/renderer/core/layout/text_autosizer.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/page/scrolling/root_scroller_controller.h"
#include "third_party/blink/renderer/core/paint/block_paint_invalidator.h"
#include "third_party/blink/renderer/core/paint/block_painter.h"
#include "third_party/blink/renderer/core/paint/ng/ng_box_fragment_painter.h"
#include "third_party/blink/renderer/core/paint/object_paint_invalidator.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/size_assertions.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
namespace blink {
struct SameSizeAsLayoutBlock : public LayoutBox {
LayoutObjectChildList children;
uint32_t bitfields;
};
ASSERT_SIZE(LayoutBlock, SameSizeAsLayoutBlock);
// This map keeps track of the positioned objects associated with a containing
// block.
//
// This map is populated during layout. It is kept across layouts to handle
// that we skip unchanged sub-trees during layout, in such a way that we are
// able to lay out deeply nested out-of-flow descendants if their containing
// block got laid out. The map could be invalidated during style change but
// keeping track of containing blocks at that time is complicated (we are in
// the middle of recomputing the style so we can't rely on any of its
// information), which is why it's easier to just update it for every layout.
TrackedDescendantsMap& GetPositionedDescendantsMap() {
DEFINE_STATIC_LOCAL(Persistent<TrackedDescendantsMap>, map,
(MakeGarbageCollected<TrackedDescendantsMap>()));
return *map;
}
TrackedContainerMap& GetPositionedContainerMap() {
DEFINE_STATIC_LOCAL(Persistent<TrackedContainerMap>, map,
(MakeGarbageCollected<TrackedContainerMap>()));
return *map;
}
// This map keeps track of the descendants whose 'height' is percentage
// associated with a containing block. Like |gPositionedDescendantsMap|, it is
// also recomputed for every layout (see the comment above about why).
static TrackedDescendantsMap& GetPercentHeightDescendantsMap() {
DEFINE_STATIC_LOCAL(Persistent<TrackedDescendantsMap>, map,
(MakeGarbageCollected<TrackedDescendantsMap>()));
return *map;
}
LayoutBlock::LayoutBlock(ContainerNode* node)
: LayoutBox(node),
has_margin_before_quirk_(false),
has_margin_after_quirk_(false),
has_markup_truncation_(false),
width_available_to_children_changed_(false),
height_available_to_children_changed_(false),
is_self_collapsing_(false),
descendants_with_floats_marked_for_layout_(false),
has_positioned_objects_(false),
has_percent_height_descendants_(false),
has_svg_text_descendants_(false),
pagination_state_changed_(false),
is_legacy_initiated_out_of_flow_layout_(false) {
// LayoutBlockFlow calls setChildrenInline(true).
// By default, subclasses do not have inline children.
}
void LayoutBlock::Trace(Visitor* visitor) const {
visitor->Trace(children_);
LayoutBox::Trace(visitor);
}
void LayoutBlock::RemoveFromGlobalMaps() {
NOT_DESTROYED();
if (HasPositionedObjects()) {
TrackedLayoutBoxLinkedHashSet* descendants =
GetPositionedDescendantsMap().Take(this);
DCHECK(!descendants->empty());
for (LayoutBox* descendant : *descendants) {
DCHECK_EQ(GetPositionedContainerMap().at(descendant), this);
GetPositionedContainerMap().erase(descendant);
}
}
if (HasPercentHeightDescendants()) {
TrackedLayoutBoxLinkedHashSet* descendants =
GetPercentHeightDescendantsMap().Take(this);
DCHECK(!descendants->empty());
for (LayoutBox* descendant : *descendants) {
DCHECK_EQ(descendant->PercentHeightContainer(), this);
descendant->SetPercentHeightContainer(nullptr);
}
}
if (has_svg_text_descendants_) {
View()->SvgTextDescendantsMap().erase(this);
has_svg_text_descendants_ = false;
}
}
void LayoutBlock::WillBeDestroyed() {
NOT_DESTROYED();
if (!DocumentBeingDestroyed() && Parent())
Parent()->DirtyLinesFromChangedChild(this);
if (LocalFrame* frame = GetFrame()) {
frame->Selection().LayoutBlockWillBeDestroyed(*this);
frame->GetPage()->GetDragCaret().LayoutBlockWillBeDestroyed(*this);
}
if (TextAutosizer* text_autosizer = GetDocument().GetTextAutosizer())
text_autosizer->Destroy(this);
RemoveFromGlobalMaps();
LayoutBox::WillBeDestroyed();
}
void LayoutBlock::StyleWillChange(StyleDifference diff,
const ComputedStyle& new_style) {
NOT_DESTROYED();
SetIsAtomicInlineLevel(new_style.IsDisplayInlineType());
LayoutBox::StyleWillChange(diff, new_style);
}
enum LogicalExtent { kLogicalWidth, kLogicalHeight };
static bool BorderOrPaddingLogicalDimensionChanged(
const ComputedStyle& old_style,
const ComputedStyle& new_style,
LogicalExtent logical_extent) {
if (new_style.IsHorizontalWritingMode() ==
(logical_extent == kLogicalWidth)) {
return old_style.BorderLeftWidth() != new_style.BorderLeftWidth() ||
old_style.BorderRightWidth() != new_style.BorderRightWidth() ||
old_style.PaddingLeft() != new_style.PaddingLeft() ||
old_style.PaddingRight() != new_style.PaddingRight();
}
return old_style.BorderTopWidth() != new_style.BorderTopWidth() ||
old_style.BorderBottomWidth() != new_style.BorderBottomWidth() ||
old_style.PaddingTop() != new_style.PaddingTop() ||
old_style.PaddingBottom() != new_style.PaddingBottom();
}
// Compute a local version of the "font size scale factor" used by SVG
// <text>. Squared to avoid computing the square root. See
// SVGLayoutSupport::CalculateScreenFontSizeScalingFactor().
static double ComputeSquaredLocalFontSizeScalingFactor(
const gfx::Transform* transform) {
if (!transform)
return 1;
const auto affine = AffineTransform::FromTransform(*transform);
return affine.XScaleSquared() + affine.YScaleSquared();
}
void LayoutBlock::StyleDidChange(StyleDifference diff,
const ComputedStyle* old_style) {
NOT_DESTROYED();
// Computes old scaling factor before PaintLayer::UpdateTransform()
// updates Layer()->Transform().
double old_squared_scale = 1;
if (Layer() && diff.TransformChanged() && has_svg_text_descendants_) {
old_squared_scale =
ComputeSquaredLocalFontSizeScalingFactor(Layer()->Transform());
}
LayoutBox::StyleDidChange(diff, old_style);
const ComputedStyle& new_style = StyleRef();
if (old_style && Parent()) {
if (old_style->GetPosition() != new_style.GetPosition() &&
new_style.GetPosition() != EPosition::kStatic) {
// In LayoutObject::styleWillChange() we already removed ourself from our
// old containing block's positioned descendant list, and we will be
// inserted to the new containing block's list during layout. However the
// positioned descendant layout logic assumes layout objects to obey
// parent-child order in the list. Remove our descendants here so they
// will be re-inserted after us.
if (LayoutBlock* cb = ContainingBlock()) {
cb->RemovePositionedObjects(this, kNewContainingBlock);
if (IsOutOfFlowPositioned() && !cb->IsLayoutNGObject()) {
// Insert this object into containing block's positioned descendants
// list in case the parent won't layout. This is needed especially
// there are descendants scheduled for overflow recalc.
//
// Only do this if the containing block is a legacy object, to let
// LayoutNG decide when to insert positioned objects. In particular,
// we don't want that if the OOF participates in block fragmentation,
// since an OOF will then be laid out as a child of a fragmentainer,
// rather than its actual containing block.
cb->InsertPositionedObject(this);
}
}
}
}
if (TextAutosizer* text_autosizer = GetDocument().GetTextAutosizer())
text_autosizer->Record(this);
PropagateStyleToAnonymousChildren();
// It's possible for our border/padding to change, but for the overall logical
// width or height of the block to end up being the same. We keep track of
// this change so in layoutBlock, we can know to set relayoutChildren=true.
width_available_to_children_changed_ |=
old_style && NeedsLayout() &&
(diff.NeedsFullLayout() || BorderOrPaddingLogicalDimensionChanged(
*old_style, new_style, kLogicalWidth));
height_available_to_children_changed_ |=
old_style && diff.NeedsFullLayout() && NeedsLayout() &&
BorderOrPaddingLogicalDimensionChanged(*old_style, new_style,
kLogicalHeight);
if (diff.TransformChanged() && has_svg_text_descendants_) {
const double new_squared_scale = ComputeSquaredLocalFontSizeScalingFactor(
Layer() ? Layer()->Transform() : nullptr);
// Compare local scale before and after.
if (old_squared_scale != new_squared_scale) {
for (LayoutBox* box : *View()->SvgTextDescendantsMap().at(this)) {
To<LayoutNGSVGText>(box)->SetNeedsTextMetricsUpdate();
if (GetNode() == GetDocument().documentElement()) {
box->SetNeedsLayout(layout_invalidation_reason::kStyleChange);
}
}
}
}
}
bool LayoutBlock::RespectsCSSOverflow() const {
NOT_DESTROYED();
// If overflow has been propagated to the viewport, it has no effect here.
return GetNode() != GetDocument().ViewportDefiningElement();
}
void LayoutBlock::AddChildBeforeDescendant(LayoutObject* new_child,
LayoutObject* before_descendant) {
NOT_DESTROYED();
DCHECK_NE(before_descendant->Parent(), this);
LayoutObject* before_descendant_container = before_descendant->Parent();
while (before_descendant_container->Parent() != this)
before_descendant_container = before_descendant_container->Parent();
DCHECK(before_descendant_container);
// We really can't go on if what we have found isn't anonymous. We're not
// supposed to use some random non-anonymous object and put the child there.
// That's a recipe for security issues.
CHECK(before_descendant_container->IsAnonymous());
// If the requested insertion point is not one of our children, then this is
// because there is an anonymous container within this object that contains
// the beforeDescendant.
if (before_descendant_container->IsAnonymousBlock()) {
// Insert the child into the anonymous block box instead of here.
if (new_child->IsInline() ||
(new_child->IsFloatingOrOutOfFlowPositioned() &&
(StyleRef().IsDeprecatedFlexboxUsingFlexLayout() ||
(!IsFlexibleBoxIncludingNG() && !IsLayoutGridIncludingNG()))) ||
before_descendant->Parent()->SlowFirstChild() != before_descendant) {
before_descendant_container->AddChild(new_child, before_descendant);
} else {
AddChild(new_child, before_descendant->Parent());
}
return;
}
DCHECK(before_descendant_container->IsTable());
if (new_child->IsTablePart()) {
// Insert into the anonymous table.
before_descendant_container->AddChild(new_child, before_descendant);
return;
}
LayoutObject* before_child =
SplitAnonymousBoxesAroundChild(before_descendant);
DCHECK_EQ(before_child->Parent(), this);
if (before_child->Parent() != this) {
// We should never reach here. If we do, we need to use the
// safe fallback to use the topmost beforeChild container.
before_child = before_descendant_container;
}
AddChild(new_child, before_child);
}
void LayoutBlock::AddChild(LayoutObject* new_child,
LayoutObject* before_child) {
NOT_DESTROYED();
if (before_child && before_child->Parent() != this) {
AddChildBeforeDescendant(new_child, before_child);
return;
}
// Only LayoutBlockFlow should have inline children, and then we shouldn't be
// here.
DCHECK(!ChildrenInline());
if (new_child->IsInline() ||
(new_child->IsFloatingOrOutOfFlowPositioned() &&
(StyleRef().IsDeprecatedFlexboxUsingFlexLayout() ||
(!IsFlexibleBoxIncludingNG() && !IsLayoutGridIncludingNG())))) {
// If we're inserting an inline child but all of our children are blocks,
// then we have to make sure it is put into an anomyous block box. We try to
// use an existing anonymous box if possible, otherwise a new one is created
// and inserted into our list of children in the appropriate position.
LayoutObject* after_child =
before_child ? before_child->PreviousSibling() : LastChild();
if (after_child && after_child->IsAnonymousBlock()) {
after_child->AddChild(new_child);
return;
}
if (new_child->IsInline()) {
// No suitable existing anonymous box - create a new one.
LayoutBlock* new_box = CreateAnonymousBlock();
LayoutBox::AddChild(new_box, before_child);
new_box->AddChild(new_child);
return;
}
}
LayoutBox::AddChild(new_child, before_child);
}
void LayoutBlock::RemoveLeftoverAnonymousBlock(LayoutBlock* child) {
NOT_DESTROYED();
DCHECK(child->IsAnonymousBlock());
DCHECK(!child->ChildrenInline());
DCHECK_EQ(child->Parent(), this);
if (child->Continuation())
return;
// Promote all the leftover anonymous block's children (to become children of
// this block instead). We still want to keep the leftover block in the tree
// for a moment, for notification purposes done further below (flow threads
// and grids).
child->MoveAllChildrenTo(this, child->NextSibling());
// Remove all the information in the flow thread associated with the leftover
// anonymous block.
child->RemoveFromLayoutFlowThread();
// LayoutGrid keeps track of its children, we must notify it about changes in
// the tree.
if (child->Parent()->IsLayoutGrid())
To<LayoutGrid>(child->Parent())->DirtyGrid();
// Now remove the leftover anonymous block from the tree, and destroy it.
// We'll rip it out manually from the tree before destroying it, because we
// don't want to trigger any tree adjustments with regards to anonymous blocks
// (or any other kind of undesired chain-reaction).
Children()->RemoveChildNode(this, child, false);
child->Destroy();
}
void LayoutBlock::UpdateLayout() {
NOT_DESTROYED();
DCHECK(!GetScrollableArea() || GetScrollableArea()->GetScrollAnchor());
bool needs_scroll_anchoring =
IsScrollContainer() &&
GetScrollableArea()->ShouldPerformScrollAnchoring();
if (needs_scroll_anchoring)
GetScrollableArea()->GetScrollAnchor()->NotifyBeforeLayout();
// Table cells call UpdateBlockLayout directly, as does
// PaintLayerScrollableArea for nested scrollbar layouts. Most logic should be
// in UpdateBlockLayout instead of UpdateLayout.
UpdateBlockLayout(false);
// It's safe to check for control clip here, since controls can never be table
// cells. If we have a lightweight clip, there can never be any overflow from
// children.
if (HasControlClip() && HasLayoutOverflow())
ClearLayoutOverflow();
height_available_to_children_changed_ = false;
}
bool LayoutBlock::WidthAvailableToChildrenHasChanged() {
NOT_DESTROYED();
// TODO(robhogan): Does m_widthAvailableToChildrenChanged always get reset
// when it needs to?
bool width_available_to_children_has_changed =
width_available_to_children_changed_;
width_available_to_children_changed_ = false;
// If we use border-box sizing, have percentage padding, and our parent has
// changed width then the width available to our children has changed even
// though our own width has remained the same.
// TODO(mstensho): NeedsPreferredWidthsRecalculation() is used here to check
// if we have percentage padding, which is rather non-obvious. That method
// returns true in other cases as well.
width_available_to_children_has_changed |=
StyleRef().BoxSizing() == EBoxSizing::kBorderBox &&
NeedsPreferredWidthsRecalculation() &&
View()->GetLayoutState()->ContainingBlockLogicalWidthChanged();
return width_available_to_children_has_changed;
}
DISABLE_CFI_PERF
bool LayoutBlock::UpdateLogicalWidthAndColumnWidth() {
NOT_DESTROYED();
LayoutUnit old_width = LogicalWidth();
UpdateLogicalWidth();
return old_width != LogicalWidth() || WidthAvailableToChildrenHasChanged();
}
void LayoutBlock::UpdateBlockLayout(bool) {
NOT_DESTROYED();
NOTREACHED();
ClearNeedsLayout();
}
void LayoutBlock::AddVisualOverflowFromChildren() {
NOT_DESTROYED();
// It is an error to call this function on a LayoutBlock that it itself inside
// a display-locked subtree.
DCHECK(!DisplayLockUtilities::LockedAncestorPreventingPrePaint(*this));
if (ChildPrePaintBlockedByDisplayLock())
return;
DCHECK(!NeedsLayout());
if (ChildrenInline())
To<LayoutBlockFlow>(this)->AddVisualOverflowFromInlineChildren();
else
AddVisualOverflowFromBlockChildren();
}
void LayoutBlock::AddLayoutOverflowFromChildren() {
NOT_DESTROYED();
if (ChildLayoutBlockedByDisplayLock())
return;
if (ChildrenInline())
To<LayoutBlockFlow>(this)->AddLayoutOverflowFromInlineChildren();
else
AddLayoutOverflowFromBlockChildren();
}
void LayoutBlock::ComputeVisualOverflow(bool) {
NOT_DESTROYED();
DCHECK(!SelfNeedsLayout());
LayoutRect previous_visual_overflow_rect = VisualOverflowRect();
ClearVisualOverflow();
AddVisualOverflowFromChildren();
AddVisualEffectOverflow();
if (VisualOverflowRect() != previous_visual_overflow_rect) {
InvalidateIntersectionObserverCachedRects();
SetShouldCheckForPaintInvalidation();
GetFrameView()->SetIntersectionObservationState(LocalFrameView::kDesired);
}
}
DISABLE_CFI_PERF
void LayoutBlock::ComputeLayoutOverflow(LayoutUnit old_client_after_edge,
bool) {
NOT_DESTROYED();
ClearSelfNeedsLayoutOverflowRecalc();
ClearLayoutOverflow();
AddLayoutOverflowFromChildren();
AddLayoutOverflowFromPositionedObjects();
if (IsScrollContainer()) {
// When we have overflow clip, propagate the original spillout since it will
// include collapsed bottom margins and bottom padding. Set the axis we
// don't care about to be 1, since we want this overflow to always be
// considered reachable.
LayoutRect client_rect(NoOverflowRect());
LayoutRect rect_to_apply;
if (IsHorizontalWritingMode())
rect_to_apply = LayoutRect(
client_rect.X(), client_rect.Y(), LayoutUnit(1),
(old_client_after_edge - client_rect.Y()).ClampNegativeToZero());
else
rect_to_apply = LayoutRect(
client_rect.X(), client_rect.Y(),
(old_client_after_edge - client_rect.X()).ClampNegativeToZero(),
LayoutUnit(1));
AddLayoutOverflow(rect_to_apply);
SetLayoutClientAfterEdge(old_client_after_edge);
if (PaddingEnd() && !ChildrenInline()) {
EOverflow overflow = StyleRef().OverflowInlineDirection();
if (overflow == EOverflow::kAuto) {
UseCounter::Count(GetDocument(),
WebFeature::kInlineOverflowAutoWithInlineEndPadding);
} else if (overflow == EOverflow::kScroll) {
UseCounter::Count(
GetDocument(),
WebFeature::kInlineOverflowScrollWithInlineEndPadding);
}
}
}
}
void LayoutBlock::AddVisualOverflowFromBlockChildren() {
NOT_DESTROYED();
for (LayoutBox* child = FirstChildBox(); child;
child = child->NextSiblingBox()) {
if ((!IsLayoutNGContainingBlock(this) && child->IsFloating()) ||
child->IsOutOfFlowPositioned() || child->IsColumnSpanAll())
continue;
// If the child contains inline with outline and continuation, its
// visual overflow computed during its layout might be inaccurate because
// the layout of continuations might not be up-to-date at that time.
// Re-add overflow from inline children to ensure its overflow covers
// the outline which may enclose continuations.
auto* child_block_flow = DynamicTo<LayoutBlockFlow>(child);
if (child_block_flow &&
child_block_flow->ContainsInlineWithOutlineAndContinuation() &&
!child_block_flow->ChildPrePaintBlockedByDisplayLock()) {
child_block_flow->AddVisualOverflowFromInlineChildren();
}
AddVisualOverflowFromChild(*child);
}
}
void LayoutBlock::AddLayoutOverflowFromBlockChildren() {
NOT_DESTROYED();
for (LayoutBox* child = FirstChildBox(); child;
child = child->NextSiblingBox()) {
if ((!IsLayoutNGContainingBlock(this) && child->IsFloating()) ||
child->IsOutOfFlowPositioned() || child->IsColumnSpanAll())
continue;
// If the child contains inline with outline and continuation, its
// visual overflow computed during its layout might be inaccurate because
// the layout of continuations might not be up-to-date at that time.
// Re-add overflow from inline children to ensure its overflow covers
// the outline which may enclose continuations.
auto* child_block_flow = DynamicTo<LayoutBlockFlow>(child);
if (child_block_flow &&
child_block_flow->ContainsInlineWithOutlineAndContinuation() &&
!child_block_flow->ChildPrePaintBlockedByDisplayLock()) {
child_block_flow->AddLayoutOverflowFromInlineChildren();
}
AddLayoutOverflowFromChild(*child);
}
}
void LayoutBlock::AddLayoutOverflowFromPositionedObjects() {
NOT_DESTROYED();
if (ChildLayoutBlockedByDisplayLock())
return;
TrackedLayoutBoxLinkedHashSet* positioned_descendants = PositionedObjects();
if (!positioned_descendants)
return;
for (const auto& positioned_object : *positioned_descendants) {
// Fixed positioned elements whose containing block is the LayoutView
// don't contribute to layout overflow, since they don't scroll with the
// content.
if (!IsA<LayoutView>(this) ||
positioned_object->StyleRef().GetPosition() != EPosition::kFixed) {
AddLayoutOverflowFromChild(*positioned_object,
ToLayoutSize(positioned_object->Location()));
}
}
}
static inline bool ChangeInAvailableLogicalHeightAffectsChild(
LayoutBlock* parent,
LayoutBox& child) {
if (parent->StyleRef().BoxSizing() != EBoxSizing::kBorderBox)
return false;
return parent->StyleRef().IsHorizontalWritingMode() &&
!child.StyleRef().IsHorizontalWritingMode();
}
void LayoutBlock::UpdateBlockChildDirtyBitsBeforeLayout(bool relayout_children,
LayoutBox& child) {
NOT_DESTROYED();
if (child.IsOutOfFlowPositioned()) {
// It's rather useless to mark out-of-flow children at this point. We may
// not be their containing block (and if we are, it's just pure luck), so
// this would be the wrong place for it. Furthermore, it would cause trouble
// for out-of-flow descendants of column spanners, if the containing block
// is outside the spanner but inside the multicol container.
return;
}
// FIXME: Technically percentage height objects only need a relayout if
// their percentage isn't going to be turned into an auto value. Add a
// method to determine this, so that we can avoid the relayout.
bool has_relative_logical_height =
child.HasRelativeLogicalHeight() ||
(child.IsAnonymous() && HasRelativeLogicalHeight()) ||
child.StretchesToViewport();
if (relayout_children ||
(has_relative_logical_height && !IsA<LayoutView>(this)) ||
(height_available_to_children_changed_ &&
ChangeInAvailableLogicalHeightAffectsChild(this, child)) ||
(child.IsListMarker() && IsListItem() &&
To<LayoutBlockFlow>(this)->ContainsFloats())) {
if (child.IsLayoutNGObject())
child.SetSelfNeedsLayoutForAvailableSpace(true);
else
child.SetChildNeedsLayout(kMarkOnlyThis);
}
}
void LayoutBlock::SimplifiedNormalFlowLayout() {
NOT_DESTROYED();
if (ChildrenInline()) {
SECURITY_DCHECK(IsLayoutBlockFlow());
auto* block_flow = To<LayoutBlockFlow>(this);
block_flow->SimplifiedNormalFlowInlineLayout();
} else {
for (LayoutBox* box = FirstChildBox(); box; box = box->NextSiblingBox()) {
if (!box->IsOutOfFlowPositioned()) {
if (box->IsLayoutMultiColumnSpannerPlaceholder())
To<LayoutMultiColumnSpannerPlaceholder>(box)
->MarkForLayoutIfObjectInFlowThreadNeedsLayout();
box->LayoutIfNeeded();
}
}
}
}
bool LayoutBlock::SimplifiedLayout() {
NOT_DESTROYED();
// Check if we need to do a full layout.
if (NormalChildNeedsLayout() || SelfNeedsLayout())
return false;
// Check that we actually need to do a simplified layout.
if (!PosChildNeedsLayout() &&
!(NeedsSimplifiedNormalFlowLayout() || NeedsPositionedMovementLayout()))
return false;
{
// LayoutState needs this deliberate scope to pop before paint invalidation.
LayoutState state(*this);
if (NeedsPositionedMovementLayout() &&
!TryLayoutDoingPositionedMovementOnly())
return false;
// If this block is inside a multicol container, we may not be able to
// perform simplified layout.
if (LayoutFlowThread* flow_thread = FlowThreadContainingBlock()) {
if (!flow_thread->CanSkipLayout(*this))
return false;
}
// Additionally, if this block itself establishes a multicol container, we
// may not be able to perform simplified layout inside it. This is really
// only unsafe if there are spanners in there, but let's just bail.
if (const auto* block_flow = DynamicTo<LayoutBlockFlow>(this)) {
if (block_flow->MultiColumnFlowThread())
return false;
}
if (ChildLayoutBlockedByDisplayLock())
return false;
TextAutosizer::LayoutScope text_autosizer_layout_scope(this);
// Lay out positioned descendants or objects that just need to recompute
// overflow.
if (NeedsSimplifiedNormalFlowLayout())
SimplifiedNormalFlowLayout();
// Lay out our positioned objects if our positioned child bit is set.
// Also, if an absolute position element inside a relative positioned
// container moves, and the absolute element has a fixed position child
// neither the fixed element nor its container learn of the movement since
// posChildNeedsLayout() is only marked as far as the relative positioned
// container. So if we can have fixed pos objects in our positioned objects
// list check if any of them are statically positioned and thus need to move
// with their absolute ancestors.
bool can_contain_fixed_pos_objects = CanContainFixedPositionObjects();
if (PosChildNeedsLayout() || NeedsPositionedMovementLayout() ||
can_contain_fixed_pos_objects)
LayoutPositionedObjects(
false, NeedsPositionedMovementLayout()
? kForcedLayoutAfterContainingBlockMoved
: (!PosChildNeedsLayout() && can_contain_fixed_pos_objects
? kLayoutOnlyFixedPositionedObjects
: kDefaultLayout));
// Recompute our overflow information.
// FIXME: We could do better here by computing a temporary overflow object
// from layoutPositionedObjects and only updating our overflow if we either
// used to have overflow or if the new temporary object has overflow.
// For now just always recompute overflow. This is no worse performance-wise
// than the old code that called rightmostPosition and lowestPosition on
// every relayout so it's not a regression. computeOverflow expects the
// bottom edge before we clamp our height. Since this information isn't
// available during simplifiedLayout, we cache the value in m_overflow.
LayoutUnit old_client_after_edge = LayoutClientAfterEdge();
ComputeLayoutOverflow(old_client_after_edge, true);
}
UpdateAfterLayout();
ClearNeedsLayout();
return true;
}
void LayoutBlock::MarkFixedPositionObjectForLayoutIfNeeded(
LayoutObject* child,
SubtreeLayoutScope& layout_scope) {
NOT_DESTROYED();
if (child->StyleRef().GetPosition() != EPosition::kFixed)
return;
bool has_static_block_position =
child->StyleRef().HasStaticBlockPosition(IsHorizontalWritingMode());
bool has_static_inline_position =
child->StyleRef().HasStaticInlinePosition(IsHorizontalWritingMode());
if (!has_static_block_position && !has_static_inline_position)
return;
LayoutObject* o = child->Parent();
bool is_layout_view = IsA<LayoutView>(o);
while (!is_layout_view && o->StyleRef().GetPosition() != EPosition::kAbsolute)
o = o->Parent();
// The LayoutView is absolute-positioned, but does not move.
if (is_layout_view)
return;
// We must compute child's width and height, but not update them now.
// The child will update its width and height when it gets laid out, and needs
// to see them change there.
auto* box = To<LayoutBox>(child);
if (has_static_inline_position) {
LogicalExtentComputedValues computed_values;
box->ComputeLogicalWidth(computed_values);
LayoutUnit new_left = computed_values.position_;
if (new_left != box->LogicalLeft())
layout_scope.SetChildNeedsLayout(child);
}
if (has_static_block_position) {
LogicalExtentComputedValues computed_values;
box->ComputeLogicalHeight(computed_values);
LayoutUnit new_top = computed_values.position_;
if (new_top != box->LogicalTop())
layout_scope.SetChildNeedsLayout(child);
}
}
LayoutUnit LayoutBlock::MarginIntrinsicLogicalWidthForChild(
const LayoutBox& child) const {
NOT_DESTROYED();
// A margin has three types: fixed, percentage, and auto (variable).
// Auto and percentage margins become 0 when computing min/max width.
// Fixed margins can be added in as is.
const Length& margin_left = child.StyleRef().MarginStartUsing(StyleRef());
const Length& margin_right = child.StyleRef().MarginEndUsing(StyleRef());
LayoutUnit margin;
if (margin_left.IsFixed())
margin += margin_left.Value();
if (margin_right.IsFixed())
margin += margin_right.Value();
return margin;
}
static bool NeedsLayoutDueToStaticPosition(LayoutBox* child) {
// When a non-positioned block element moves, it may have positioned children
// that are implicitly positioned relative to the non-positioned block.
const ComputedStyle* style = child->Style();
bool is_horizontal = style->IsHorizontalWritingMode();
if (style->HasStaticBlockPosition(is_horizontal)) {
LayoutBox::LogicalExtentComputedValues computed_values;
LayoutUnit current_logical_top = child->LogicalTop();
LayoutUnit current_logical_height = child->LogicalHeight();
child->ComputeLogicalHeight(current_logical_height, current_logical_top,
computed_values);
if (computed_values.position_ != current_logical_top ||
computed_values.extent_ != current_logical_height)
return true;
}
if (style->HasStaticInlinePosition(is_horizontal)) {
LayoutBox::LogicalExtentComputedValues computed_values;
LayoutUnit current_logical_left = child->LogicalLeft();
LayoutUnit current_logical_width = child->LogicalWidth();
child->ComputeLogicalWidth(computed_values);
if (computed_values.position_ != current_logical_left ||
computed_values.extent_ != current_logical_width)
return true;
}
return false;
}
void LayoutBlock::LayoutPositionedObjects(bool relayout_children,
PositionedLayoutBehavior info) {
NOT_DESTROYED();
if (ChildLayoutBlockedByDisplayLock())
return;
TrackedLayoutBoxLinkedHashSet* positioned_descendants = PositionedObjects();
if (!positioned_descendants)
return;
for (const auto& positioned_object : *positioned_descendants) {
LayoutPositionedObject(positioned_object, relayout_children, info);
}
}
void LayoutBlock::LayoutPositionedObject(LayoutBox* positioned_object,
bool relayout_children,
PositionedLayoutBehavior info) {
NOT_DESTROYED();
positioned_object->SetShouldCheckForPaintInvalidation();
SubtreeLayoutScope layout_scope(*positioned_object);
// If positionedObject is fixed-positioned and moves with an absolute-
// positioned ancestor (other than the LayoutView, which cannot move),
// mark it for layout now.
MarkFixedPositionObjectForLayoutIfNeeded(positioned_object, layout_scope);
if (info == kLayoutOnlyFixedPositionedObjects) {
positioned_object->LayoutIfNeeded();
return;
}
if (!positioned_object->NormalChildNeedsLayout()) {
bool update_child_needs_layout =
relayout_children || height_available_to_children_changed_;
if (!update_child_needs_layout) {
if (!positioned_object->IsLayoutNGObject() ||
To<LayoutBlock>(positioned_object)
->IsLegacyInitiatedOutOfFlowLayout()) {
update_child_needs_layout |=
NeedsLayoutDueToStaticPosition(positioned_object);
}
}
if (update_child_needs_layout)
layout_scope.SetChildNeedsLayout(positioned_object);
}
LayoutUnit logical_top_estimate;
bool is_paginated = View()->GetLayoutState()->IsPaginated();
bool needs_block_direction_location_set_before_layout =
is_paginated &&
positioned_object->GetLegacyPaginationBreakability() != kForbidBreaks;
bool bogus_logical_top_estimate = false;
if (needs_block_direction_location_set_before_layout) {
// Out-of-flow objects are normally positioned after layout (while in-flow
// objects are positioned before layout). If the child object is paginated
// in the same context as we are, estimate its logical top now. We need to
// know this up-front, to correctly evaluate if we need to mark for
// relayout, and, if our estimate is correct, we'll even be able to insert
// correct pagination struts on the first attempt.
const ComputedStyle& style = positioned_object->StyleRef();
if (!style.LogicalBottom().IsAuto() && style.LogicalTop().IsAuto() &&
style.LogicalHeight().IsAuto()) {
// This child is bottom-aligned with auto block size. We cannot make a
// decent estimate before layout. Just estimate something as far above a
// fragmentainer break as possible. This is a way to try our best to avoid
// hitting fragmentainer breaks, as that could impact the block size of
// the child (increase it if contents need to be pushed to the next
// fragmentainer, or decrease it if a descendant margin collides into a
// fragmentainer boundary), and thus give us a bad block-start offset.
logical_top_estimate = -OffsetFromLogicalTopOfFirstPage();
bogus_logical_top_estimate = true;
} else {
LogicalExtentComputedValues computed_values;
positioned_object->ComputeLogicalHeight(
positioned_object->LogicalHeight(), positioned_object->LogicalTop(),
computed_values);
logical_top_estimate = computed_values.position_;
}
positioned_object->SetLogicalTop(logical_top_estimate);
}
if (!positioned_object->NeedsLayout()) {
MarkChildForPaginationRelayoutIfNeeded(*positioned_object, layout_scope);
// If we're not able to set a decent block start estimate, we need to force
// layout to figure it out.
if (bogus_logical_top_estimate)
layout_scope.SetChildNeedsLayout(positioned_object);
}
// FIXME: We should be able to do a r->setNeedsPositionedMovementLayout()
// here instead of a full layout. Need to investigate why it does not
// trigger the correct invalidations in that case. crbug.com/350756
if (info == kForcedLayoutAfterContainingBlockMoved) {
positioned_object->SetNeedsLayout(
layout_invalidation_reason::kAncestorMoved, kMarkOnlyThis);
}
if (positioned_object->NeedsLayout())
positioned_object->UpdateLayout();
LayoutObject* parent = positioned_object->Parent();
bool layout_changed = false;
if ((parent->IsLayoutNGFlexibleBox() &&
!positioned_object->IsLayoutNGObject() &&
LayoutFlexibleBox::SetStaticPositionForChildInFlexNGContainer(
*positioned_object, To<LayoutBlock>(parent))) ||
(parent->IsFlexibleBox() &&
To<LayoutFlexibleBox>(parent)->SetStaticPositionForPositionedLayout(
*positioned_object))) {
// The static position of an abspos child of a flexbox depends on its size
// (for example, they can be centered). So we may have to reposition the
// item after layout.
// TODO(cbiesinger): We could probably avoid a layout here and just
// reposition?
positioned_object->ForceLayout();
layout_changed = true;
}
// Lay out again if our estimate was wrong.
if (!layout_changed && needs_block_direction_location_set_before_layout &&
logical_top_estimate != LogicalTopForChild(*positioned_object)) {
positioned_object->ForceLayout();
}
if (is_paginated)
UpdateFragmentationInfoForChild(*positioned_object);
}
void LayoutBlock::MarkPositionedObjectsForLayout() {
NOT_DESTROYED();
if (TrackedLayoutBoxLinkedHashSet* positioned_descendants =
PositionedObjects()) {
for (const auto& descendant : *positioned_descendants)
descendant->SetChildNeedsLayout();