forked from movableink/webkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderObject.cpp
More file actions
3086 lines (2619 loc) · 120 KB
/
RenderObject.cpp
File metadata and controls
3086 lines (2619 loc) · 120 KB
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) 2000 Dirk Mueller (mueller@kde.org)
* (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2004-2024 Apple Inc. All rights reserved.
* Copyright (C) 2009 Google Inc. All rights reserved.
* Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
*
* 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 "config.h"
#include "RenderObject.h"
#include "AXObjectCache.h"
#include "BoundaryPointInlines.h"
#include "ContainerNodeInlines.h"
#include "DocumentInlines.h"
#include "EditingInlines.h"
#include "Editor.h"
#include "ElementAncestorIteratorInlines.h"
#include "FloatQuad.h"
#include "FrameSelection.h"
#include "GeometryUtilities.h"
#include "GraphicsContext.h"
#include "GraphicsLayer.h"
#include "HTMLBRElement.h"
#include "HTMLNames.h"
#include "HitTestResult.h"
#include "LayoutBox.h"
#include "LayoutIntegrationCoverage.h"
#include "LegacyRenderSVGModelObject.h"
#include "LegacyRenderSVGRoot.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "LogicalSelectionOffsetCaches.h"
#include "NodeInlines.h"
#include "Page.h"
#include "PseudoElement.h"
#include "ReferencedSVGResources.h"
#include "RenderChildIterator.h"
#include "RenderCounter.h"
#include "RenderElementInlines.h"
#include "RenderFragmentedFlow.h"
#include "RenderGrid.h"
#include "RenderInline.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#include "RenderLayerCompositor.h"
#include "RenderLayerScrollableArea.h"
#include "RenderLineBreak.h"
#include "RenderMultiColumnFlow.h"
#include "RenderMultiColumnSet.h"
#include "RenderMultiColumnSpannerPlaceholder.h"
#include "RenderObjectInlines.h"
#include "RenderReplica.h"
#include "RenderSVGBlock.h"
#include "RenderSVGInline.h"
#include "RenderSVGModelObject.h"
#include "RenderScrollbarPart.h"
#include "RenderTableRow.h"
#include "RenderTextControl.h"
#include "RenderTheme.h"
#include "RenderTreeBuilder.h"
#include "RenderView.h"
#include "RenderViewTransitionCapture.h"
#include "RenderWidget.h"
#include "RenderedPosition.h"
#include "SVGRenderSupport.h"
#include "Settings.h"
#include "StyleResolver.h"
#include "TransformState.h"
#include "ViewTransition.h"
#include <algorithm>
#include <stdio.h>
#include <wtf/HexNumber.h>
#include <wtf/RefCountedLeakCounter.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/TextStream.h>
#if PLATFORM(IOS_FAMILY)
#include "SelectionGeometry.h"
#endif
namespace WebCore {
using namespace HTMLNames;
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(RenderObject);
WTF_MAKE_TZONE_ALLOCATED_IMPL(RenderObject::RenderObjectRareData);
#if ASSERT_ENABLED
RenderObject::SetLayoutNeededForbiddenScope::SetLayoutNeededForbiddenScope(const RenderObject& renderObject, bool isForbidden)
: m_renderObject(renderObject)
, m_preexistingForbidden(m_renderObject->isSetNeedsLayoutForbidden())
{
m_renderObject->setNeedsLayoutIsForbidden(isForbidden);
}
RenderObject::SetLayoutNeededForbiddenScope::~SetLayoutNeededForbiddenScope()
{
m_renderObject->setNeedsLayoutIsForbidden(m_preexistingForbidden);
}
#endif
struct SameSizeAsRenderObject final : public CachedImageClient {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
WTF_STRUCT_OVERRIDE_DELETE_FOR_CHECKED_PTR(SameSizeAsRenderObject);
virtual ~SameSizeAsRenderObject() = default; // Allocate vtable pointer.
#if ASSERT_ENABLED
unsigned m_debugBitfields : 2;
#endif
unsigned m_stateBitfields;
WeakRef<Node, WeakPtrImplWithEventTargetData> node;
SingleThreadWeakPtr<RenderObject> pointers;
SingleThreadPackedWeakPtr<RenderObject> m_previous;
uint16_t m_typeFlags;
SingleThreadPackedWeakPtr<RenderObject> m_next;
uint8_t m_type;
uint8_t m_typeSpecificFlags;
CheckedPtr<Layout::Box> layoutBox;
};
#if CPU(ADDRESS64)
static_assert(sizeof(RenderObject) == sizeof(SameSizeAsRenderObject), "RenderObject should stay small");
#endif
DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, renderObjectCounter, ("RenderObject"));
void RenderObjectDeleter::operator() (RenderObject* renderer) const
{
renderer->destroy();
}
RenderObject::RenderObject(Type type, Node& node, OptionSet<TypeFlag> typeFlags, TypeSpecificFlags typeSpecificFlags)
: CachedImageClient()
#if ASSERT_ENABLED
, m_hasAXObject(false)
, m_setNeedsLayoutForbidden(false)
#endif
, m_node(node)
, m_typeFlags(node.isDocumentNode() ? (typeFlags | TypeFlag::IsAnonymous) : typeFlags)
, m_type(type)
, m_typeSpecificFlags(typeSpecificFlags)
{
ASSERT(!typeFlags.contains(TypeFlag::IsAnonymous));
if (CheckedPtr renderView = node.document().renderView())
renderView->didCreateRenderer();
#ifndef NDEBUG
renderObjectCounter.increment();
#endif
}
RenderObject::~RenderObject()
{
clearLayoutBox();
ASSERT(!m_hasAXObject);
#ifndef NDEBUG
renderObjectCounter.decrement();
#endif
ASSERT(!hasRareData());
}
CheckedRef<RenderView> RenderObject::checkedView() const
{
return view();
}
void RenderObject::setLayoutBox(Layout::Box& box)
{
m_layoutBox = &box;
m_layoutBox->setRendererForIntegration(this);
}
void RenderObject::clearLayoutBox()
{
if (!m_layoutBox)
return;
ASSERT(m_layoutBox->rendererForIntegration() == this);
m_layoutBox->setRendererForIntegration(nullptr);
m_layoutBox = nullptr;
}
RenderTheme& RenderObject::theme() const
{
return RenderTheme::singleton();
}
bool RenderObject::isDescendantOf(const RenderObject* ancestor) const
{
for (auto* renderer = this; renderer; renderer = renderer->m_parent.get()) {
if (renderer == ancestor)
return true;
}
return false;
}
RenderElement* RenderObject::firstNonAnonymousAncestor() const
{
auto* ancestor = parent();
while (ancestor && ancestor->isAnonymous())
ancestor = ancestor->parent();
return ancestor;
}
bool RenderObject::isLegend() const
{
return node() && node()->hasTagName(legendTag);
}
bool RenderObject::isFieldset() const
{
return node() && node()->hasTagName(fieldsetTag);
}
bool RenderObject::isHTMLMarquee() const
{
return node() && node()->renderer() == this && node()->hasTagName(marqueeTag);
}
void RenderObject::setFragmentedFlowStateIncludingDescendants(FragmentedFlowState state, SkipDescendentFragmentedFlow skipDescendentFragmentedFlow)
{
setFragmentedFlowState(state);
auto* renderElement = dynamicDowncast<RenderElement>(*this);
if (!renderElement)
return;
for (CheckedRef child : childrenOfType<RenderObject>(*renderElement)) {
// If the child is a fragmentation context it already updated the descendants flag accordingly.
if (child->isRenderFragmentedFlow() && skipDescendentFragmentedFlow == SkipDescendentFragmentedFlow::Yes)
continue;
if (child->isOutOfFlowPositioned()) {
// Fragmented status propagation stops at out-of-flow boundary.
auto isInsideMulticolumnFlow = [&] {
auto* containingBlock = child->containingBlock();
if (!containingBlock) {
ASSERT_NOT_REACHED();
return false;
}
return containingBlock->fragmentedFlowState() == FragmentedFlowState::InsideFlow;
};
if (!isInsideMulticolumnFlow())
continue;
}
ASSERT(skipDescendentFragmentedFlow == SkipDescendentFragmentedFlow::No || state != child->fragmentedFlowState());
child->setFragmentedFlowStateIncludingDescendants(state, skipDescendentFragmentedFlow);
}
}
RenderObject::FragmentedFlowState RenderObject::computedFragmentedFlowState(const RenderObject& renderer)
{
if (!renderer.parent())
return renderer.fragmentedFlowState();
if (is<RenderMultiColumnFlow>(renderer)) {
// Multicolumn flows do not inherit the flow state.
return FragmentedFlowState::InsideFlow;
}
auto inheritedFlowState = RenderObject::FragmentedFlowState::NotInsideFlow;
if (is<RenderText>(renderer))
inheritedFlowState = renderer.parent()->fragmentedFlowState();
else if (is<RenderSVGBlock>(renderer) || is<RenderSVGInline>(renderer) || is<LegacyRenderSVGModelObject>(renderer)) {
// containingBlock() skips svg boundary (SVG root is a RenderReplaced).
if (CheckedPtr svgRoot = SVGRenderSupport::findTreeRootObject(downcast<RenderElement>(renderer)))
inheritedFlowState = svgRoot->fragmentedFlowState();
} else if (CheckedPtr container = renderer.container())
inheritedFlowState = container->fragmentedFlowState();
else {
// Splitting lines or doing continuation, so just keep the current state.
inheritedFlowState = renderer.fragmentedFlowState();
}
return inheritedFlowState;
}
void RenderObject::initializeFragmentedFlowStateOnInsertion()
{
ASSERT(parent());
// A RenderFragmentedFlow is always considered to be inside itself, so it never has to change its state in response to parent changes.
if (isRenderFragmentedFlow())
return;
auto computedState = computedFragmentedFlowState(*this);
if (fragmentedFlowState() == computedState)
return;
setFragmentedFlowStateIncludingDescendants(computedState, SkipDescendentFragmentedFlow::No);
}
void RenderObject::resetFragmentedFlowStateOnRemoval()
{
ASSERT(!renderTreeBeingDestroyed());
if (fragmentedFlowState() == FragmentedFlowState::NotInsideFlow)
return;
if (auto* renderElement = dynamicDowncast<RenderElement>(*this)) {
renderElement->removeFromRenderFragmentedFlow();
return;
}
// A RenderFragmentedFlow is always considered to be inside itself, so it never has to change its state in response to parent changes.
if (isRenderFragmentedFlow())
return;
setFragmentedFlowStateIncludingDescendants(FragmentedFlowState::NotInsideFlow);
}
void RenderObject::setParent(RenderElement* parent)
{
m_parent = parent;
}
RenderObject* RenderObject::nextInPreOrder() const
{
if (RenderObject* o = firstChildSlow())
return o;
return nextInPreOrderAfterChildren();
}
RenderObject* RenderObject::nextInPreOrderAfterChildren() const
{
RenderObject* o;
if (!(o = nextSibling())) {
o = parent();
while (o && !o->nextSibling())
o = o->parent();
if (o)
o = o->nextSibling();
}
return o;
}
RenderObject* RenderObject::nextInPreOrder(const RenderObject* stayWithin) const
{
if (RenderObject* o = firstChildSlow())
return o;
return nextInPreOrderAfterChildren(stayWithin);
}
RenderObject* RenderObject::nextInPreOrderAfterChildren(const RenderObject* stayWithin) const
{
if (this == stayWithin)
return nullptr;
const RenderObject* current = this;
RenderObject* next;
while (!(next = current->nextSibling())) {
current = current->parent();
if (!current || current == stayWithin)
return nullptr;
}
return next;
}
RenderObject* RenderObject::previousInPreOrder() const
{
if (RenderObject* o = previousSibling()) {
while (RenderObject* last = o->lastChildSlow())
o = last;
return o;
}
return parent();
}
RenderObject* RenderObject::previousInPreOrder(const RenderObject* stayWithin) const
{
if (this == stayWithin)
return nullptr;
return previousInPreOrder();
}
RenderObject* RenderObject::childAt(unsigned index) const
{
RenderObject* child = firstChildSlow();
for (unsigned i = 0; child && i < index; i++)
child = child->nextSibling();
return child;
}
RenderObject* RenderObject::firstLeafChild() const
{
RenderObject* r = firstChildSlow();
while (r) {
RenderObject* n = nullptr;
n = r->firstChildSlow();
if (!n)
break;
r = n;
}
return r;
}
RenderObject* RenderObject::lastLeafChild() const
{
RenderObject* r = lastChildSlow();
while (r) {
RenderObject* n = nullptr;
n = r->lastChildSlow();
if (!n)
break;
r = n;
}
return r;
}
#if ENABLE(TEXT_AUTOSIZING)
// Non-recursive version of the DFS search.
RenderObject* RenderObject::traverseNext(const RenderObject* stayWithin, HeightTypeTraverseNextInclusionFunction inclusionFunction, int& currentDepth, int& newFixedDepth) const
{
BlockContentHeightType overflowType;
// Check for suitable children.
for (CheckedPtr child = firstChildSlow(); child; child = child->nextSibling()) {
overflowType = inclusionFunction(*child);
if (overflowType != FixedHeight) {
currentDepth++;
if (overflowType == OverflowHeight)
newFixedDepth = currentDepth;
ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
return child.get();
}
}
if (this == stayWithin)
return nullptr;
// Now we traverse other nodes if they exist, otherwise
// we go to the parent node and try doing the same.
const RenderObject* n = this;
while (n) {
while (n && !n->nextSibling() && (!stayWithin || n->parent() != stayWithin)) {
n = n->parent();
currentDepth--;
}
if (!n)
return nullptr;
for (CheckedPtr sibling = n->nextSibling(); sibling; sibling = sibling->nextSibling()) {
overflowType = inclusionFunction(*sibling);
if (overflowType != FixedHeight) {
if (overflowType == OverflowHeight)
newFixedDepth = currentDepth;
ASSERT(!stayWithin || !n->nextSibling() || n->nextSibling()->isDescendantOf(stayWithin));
return sibling.get();
}
}
if (!stayWithin || n->parent() != stayWithin) {
n = n->parent();
currentDepth--;
} else
return nullptr;
}
return nullptr;
}
#endif // ENABLE(TEXT_AUTOSIZING)
RenderLayer* RenderObject::enclosingLayer() const
{
for (auto& renderer : lineageOfType<RenderLayerModelObject>(*this)) {
if (renderer.hasLayer())
return renderer.layer();
}
return nullptr;
}
RenderBox& RenderObject::enclosingBox() const
{
return *lineageOfType<RenderBox>(const_cast<RenderObject&>(*this)).first();
}
RenderBoxModelObject& RenderObject::enclosingBoxModelObject() const
{
return *lineageOfType<RenderBoxModelObject>(const_cast<RenderObject&>(*this)).first();
}
RenderBox* RenderObject::enclosingScrollableContainer() const
{
// Walk up the container chain to find the scrollable container that contains
// this RenderObject. The important thing here is that `container()` respects
// the containing block chain for positioned elements. This is important because
// scrollable overflow does not establish a new containing block for children.
for (auto* candidate = container(); candidate; candidate = candidate->container()) {
// Currently the RenderView can look like it has scrollable overflow, but we never
// want to return this as our container. Instead we should use the root element.
if (candidate->isRenderView())
break;
if (candidate->hasPotentiallyScrollableOverflow())
return downcast<RenderBox>(candidate);
}
// If we reach the root, then the root element is the scrolling container.
return document().documentElement() ? document().documentElement()->renderBox() : nullptr;
}
static inline bool isLayoutBoundary(const RenderElement& renderer)
{
// FIXME: In future it may be possible to broaden these conditions in order to improve performance.
if (renderer.isRenderView())
return true;
auto& style = renderer.style();
if (CheckedPtr textControl = dynamicDowncast<RenderTextControl>(renderer)) {
if (!textControl->isFlexItem() && !textControl->isGridItem() && style.fieldSizing() != FieldSizing::Content) {
// Flexing type of layout systems may compute different size than what input's preferred width is which won't happen unless they run their layout as well.
return true;
}
}
if (renderer.shouldApplyLayoutContainment() && renderer.shouldApplySizeContainment())
return true;
if (renderer.isRenderOrLegacyRenderSVGRoot())
return true;
if (!renderer.hasNonVisibleOverflow()) {
// While createsNewFormattingContext (a few lines below) covers this case, overflow visible is a super common value so we should be able
// to bail out here fast.
return false;
}
if (style.width().isIntrinsicOrAuto() || style.height().isIntrinsicOrAuto() || style.height().isPercentOrCalculated())
return false;
if (renderer.document().settings().layerBasedSVGEngineEnabled() && renderer.isSVGLayerAwareRenderer())
return false;
// Table parts can't be relayout roots since the table is responsible for layouting all the parts.
if (renderer.isTablePart())
return false;
if (CheckedPtr renderBlock = dynamicDowncast<RenderBlock>(renderer); !renderBlock->createsNewFormattingContext())
return false;
return true;
}
void RenderObject::clearNeedsLayout(HadSkippedLayout hadSkippedLayout)
{
// FIXME: Consider not setting the "ever had layout" bit to true when "hadSkippedLayout"
setEverHadLayout();
setHadSkippedLayout(hadSkippedLayout == HadSkippedLayout::Yes);
if (hasLayer())
downcast<RenderLayerModelObject>(*this).layer()->setSelfAndChildrenNeedPositionUpdate();
m_stateBitfields.clearFlag(StateFlag::NeedsLayout);
setPosChildNeedsLayoutBit(false);
setNeedsSimplifiedNormalFlowLayoutBit(false);
setNormalChildNeedsLayoutBit(false);
setOutOfFlowChildNeedsStaticPositionLayoutBit(false);
setNeedsPositionedMovementLayoutBit(false);
#if ASSERT_ENABLED
auto checkIfOutOfFlowDescendantsNeedLayout = [&](auto& renderBlock) {
if (auto* outOfFlowDescendants = renderBlock.outOfFlowBoxes()) {
for (auto& renderer : *outOfFlowDescendants)
ASSERT(!renderer.needsLayout());
}
};
if (auto* renderBlock = dynamicDowncast<RenderBlock>(*this))
checkIfOutOfFlowDescendantsNeedLayout(*renderBlock);
#endif // ASSERT_ENABLED
}
void RenderObject::scheduleLayout(RenderElement* layoutRoot)
{
if (auto* renderView = dynamicDowncast<RenderView>(layoutRoot))
return renderView->protectedFrameView()->checkedLayoutContext()->scheduleLayout();
if (layoutRoot && layoutRoot->isRooted())
layoutRoot->view().protectedFrameView()->checkedLayoutContext()->scheduleSubtreeLayout(*layoutRoot);
}
RenderElement* RenderObject::markContainingBlocksForLayout(RenderElement* layoutRoot)
{
ASSERT(!isSetNeedsLayoutForbidden());
if (is<RenderView>(*this))
return downcast<RenderElement>(this);
CheckedPtr ancestor = container();
bool simplifiedNormalFlowLayout = needsSimplifiedNormalFlowLayout() && !selfNeedsLayout() && !normalChildNeedsLayout();
bool hasOutOfFlowPosition = isOutOfFlowPositioned();
while (ancestor) {
// FIXME: Remove this once we remove the special cases for counters, quotes and mathml calling setNeedsLayout during preferred width computation.
SetLayoutNeededForbiddenScope layoutForbiddenScope(*ancestor, isSetNeedsLayoutForbidden());
// Don't mark the outermost object of an unrooted subtree. That object will be
// marked when the subtree is added to the document.
CheckedPtr container = ancestor->container();
if (!container && !ancestor->isRenderView()) {
// Internal render tree shuffle.
return { };
}
if (simplifiedNormalFlowLayout && ancestor->overflowChangesMayAffectLayout())
simplifiedNormalFlowLayout = false;
if (hasOutOfFlowPosition) {
bool willSkipRelativelyPositionedInlines = !ancestor->isRenderBlock() || ancestor->isAnonymousBlock();
// Skip relatively positioned inlines and anonymous blocks to get to the enclosing RenderBlock.
while (ancestor && (!ancestor->isRenderBlock() || ancestor->isAnonymousBlock()))
ancestor = ancestor->container();
if (!ancestor || ancestor->posChildNeedsLayout())
return { };
if (willSkipRelativelyPositionedInlines)
container = ancestor->container();
ancestor->setPosChildNeedsLayoutBit(true);
simplifiedNormalFlowLayout = true;
} else if (simplifiedNormalFlowLayout) {
if (ancestor->needsSimplifiedNormalFlowLayout())
return { };
ancestor->setNeedsSimplifiedNormalFlowLayoutBit(true);
} else {
if (ancestor->normalChildNeedsLayout())
return { };
ancestor->setNormalChildNeedsLayoutBit(true);
}
ASSERT(!ancestor->isSetNeedsLayoutForbidden());
if (layoutRoot) {
// Having a valid layout root also mean we should not stop at layout boundaries.
if (ancestor == layoutRoot)
return layoutRoot;
} else if (isLayoutBoundary(*ancestor))
return ancestor.get();
if (auto* renderGrid = dynamicDowncast<RenderGrid>(container.get()); renderGrid && renderGrid->isExtrinsicallySized())
simplifiedNormalFlowLayout = true;
hasOutOfFlowPosition = ancestor->isOutOfFlowPositioned();
ancestor = WTFMove(container);
}
return { };
}
void RenderObject::setNeedsPreferredWidthsUpdate(MarkingBehavior markParents)
{
if (needsPreferredLogicalWidthsUpdate() && (!hasRareData() || !rareData().preferredLogicalWidthsNeedUpdateIsMarkOnlyThis)) {
// Both this and our ancestor chain are already marked dirty.
return;
}
m_stateBitfields.setFlag(StateFlag::PreferredLogicalWidthsNeedUpdate, true);
if (isOutOfFlowPositioned()) {
// A positioned object has no effect on the min/max width of its containing block ever. No need to mark ancestor chain.
return;
}
if (markParents == MarkOnlyThis) {
ensureRareData().preferredLogicalWidthsNeedUpdateIsMarkOnlyThis = true;
return;
}
invalidateContainerPreferredLogicalWidths();
if (hasRareData())
ensureRareData().preferredLogicalWidthsNeedUpdateIsMarkOnlyThis = false;
}
void RenderObject::invalidateContainerPreferredLogicalWidths()
{
// In order to avoid pathological behavior when inlines are deeply nested, we do include them
// in the chain that we mark dirty (even though they're kind of irrelevant).
CheckedPtr ancestor = isRenderTableCell() ? containingBlock() : container();
while (ancestor) {
if (ancestor->needsPreferredLogicalWidthsUpdate() && (!ancestor->hasRareData() || !ancestor->rareData().preferredLogicalWidthsNeedUpdateIsMarkOnlyThis))
break;
// Don't invalidate the outermost object of an unrooted subtree. That object will be
// invalidated when the subtree is added to the document.
CheckedPtr container = ancestor->isRenderTableCell() ? ancestor->containingBlock() : ancestor->container();
if (!container && !ancestor->isRenderView())
break;
ancestor->m_stateBitfields.setFlag(StateFlag::PreferredLogicalWidthsNeedUpdate, true);
if (ancestor->style().hasOutOfFlowPosition()) {
// A positioned object has no effect on the min/max width of its containing block ever.
// We can optimize this case and not go up any further.
break;
}
ancestor = WTFMove(container);
}
}
void RenderObject::setLayerNeedsFullRepaint()
{
ASSERT(hasLayer());
downcast<RenderLayerModelObject>(*this).checkedLayer()->setRepaintStatus(RepaintStatus::NeedsFullRepaint);
}
void RenderObject::setLayerNeedsFullRepaintForPositionedMovementLayout()
{
ASSERT(hasLayer());
downcast<RenderLayerModelObject>(*this).checkedLayer()->setRepaintStatus(RepaintStatus::NeedsFullRepaintForPositionedMovementLayout);
}
static inline RenderBlock* nearestNonAnonymousContainingBlockIncludingSelf(RenderElement* renderer)
{
while (renderer && (!is<RenderBlock>(*renderer) || renderer->isAnonymousBlock()))
renderer = renderer->containingBlock();
return downcast<RenderBlock>(renderer);
}
RenderBlock* RenderObject::containingBlockForPositionType(PositionType positionType, const RenderObject& renderer)
{
if (positionType == PositionType::Static || positionType == PositionType::Relative || positionType == PositionType::Sticky) {
auto containingBlockForObjectInFlow = [&] {
auto* ancestor = renderer.parent();
while (ancestor && ((ancestor->isInline() && !ancestor->isReplacedOrAtomicInline()) || !ancestor->isRenderBlock()))
ancestor = ancestor->parent();
return downcast<RenderBlock>(ancestor);
};
return containingBlockForObjectInFlow();
}
if (positionType == PositionType::Absolute) {
auto containingBlockForAbsolutePosition = [&] {
if (CheckedPtr renderInline = dynamicDowncast<RenderInline>(renderer); renderInline && renderInline->style().position() == PositionType::Relative) {
// A relatively positioned RenderInline forwards its absolute positioned descendants to
// its nearest non-anonymous containing block (to avoid having positioned objects list in RenderInlines).
return nearestNonAnonymousContainingBlockIncludingSelf(renderer.parent());
}
CheckedPtr ancestor = renderer.parent();
while (ancestor && !ancestor->canContainAbsolutelyPositionedObjects())
ancestor = ancestor->parent();
// Make sure we only return non-anonymous RenderBlock as containing block.
return nearestNonAnonymousContainingBlockIncludingSelf(ancestor.get());
};
return containingBlockForAbsolutePosition();
}
if (positionType == PositionType::Fixed) {
auto containingBlockForFixedPosition = [&] () -> RenderBlock* {
CheckedPtr ancestor = renderer.parent();
while (ancestor && !ancestor->canContainFixedPositionObjects()) {
if (isInTopLayerOrBackdrop(ancestor->style(), ancestor->element()))
return &renderer.view();
ancestor = ancestor->parent();
}
return nearestNonAnonymousContainingBlockIncludingSelf(ancestor.get());
};
return containingBlockForFixedPosition();
}
ASSERT_NOT_REACHED();
return nullptr;
}
RenderBlock* RenderObject::containingBlock() const
{
// FIXME: See https://bugs.webkit.org/show_bug.cgi?id=270977 for RenderLineBreak special treatment.
if (is<RenderText>(*this) || is<RenderLineBreak>(*this))
return containingBlockForPositionType(PositionType::Static, *this);
auto containingBlockForRenderer = [](const auto& renderer) -> RenderBlock* {
if (isInTopLayerOrBackdrop(renderer.style(), renderer.element()))
return &renderer.view();
return containingBlockForPositionType(renderer.style().position(), renderer);
};
if (!parent()) {
if (auto* part = dynamicDowncast<RenderScrollbarPart>(*this)) {
if (CheckedPtr scrollbarPart = part->rendererOwningScrollbar())
return containingBlockForRenderer(*scrollbarPart);
return nullptr;
}
}
return containingBlockForRenderer(downcast<RenderElement>(*this));
}
CheckedPtr<RenderBlock> RenderObject::checkedContainingBlock() const
{
return containingBlock();
}
void RenderObject::addPDFURLRect(const PaintInfo& paintInfo, const LayoutPoint& paintOffset) const
{
Vector<LayoutRect> focusRingRects;
addFocusRingRects(focusRingRects, paintOffset, paintInfo.paintContainer);
LayoutRect urlRect = unionRect(focusRingRects);
if (urlRect.isEmpty())
return;
RefPtr element = dynamicDowncast<Element>(node());
if (!element || !element->isLink())
return;
const AtomString& href = element->getAttribute(hrefAttr);
if (href.isNull())
return;
if (paintInfo.context().supportsInternalLinks()) {
String outAnchorName;
RefPtr linkTarget = element->findAnchorElementForLink(outAnchorName);
if (linkTarget) {
paintInfo.context().setDestinationForRect(outAnchorName, urlRect);
return;
}
}
paintInfo.context().setURLForRect(element->protectedDocument()->completeURL(href), urlRect);
}
#if PLATFORM(IOS_FAMILY)
// This function is similar in spirit to RenderText::absoluteRectsForRange, but returns rectangles
// which are annotated with additional state which helps iOS draw selections in its unique way.
// No annotations are added in this class.
// FIXME: Move to RenderText with absoluteRectsForRange()?
void RenderObject::collectSelectionGeometries(Vector<SelectionGeometry>& geometries, unsigned start, unsigned end)
{
Vector<FloatQuad> quads;
if (!firstChildSlow()) {
// FIXME: WebKit's position for an empty span after a BR is incorrect, so we can't trust
// quads for them. We don't need selection geometries for those anyway though, since they
// are just empty containers. See <https://bugs.webkit.org/show_bug.cgi?id=49358>.
CheckedPtr previous = previousSibling();
RefPtr node = this->node();
if (!previous || !previous->isBR() || !node || !node->isContainerNode() || !isInline()) {
// For inline elements we don't use absoluteQuads, since it takes into account continuations and leads to wrong results.
absoluteQuadsForSelection(quads);
}
} else {
unsigned offset = start;
for (CheckedPtr child = childAt(start); child && offset < end; child = child->nextSibling(), ++offset)
child->absoluteQuads(quads);
}
for (auto& quad : quads)
geometries.append(SelectionGeometry(quad, HTMLElement::selectionRenderingBehavior(protectedNode().get()), isHorizontalWritingMode(), checkedView()->pageNumberForBlockProgressionOffset(quad.enclosingBoundingBox().x())));
}
#endif
IntRect RenderObject::absoluteBoundingBoxRect(bool useTransforms, bool* wasFixed) const
{
if (useTransforms) {
Vector<FloatQuad> quads;
absoluteQuads(quads, wasFixed);
return enclosingIntRect(unitedBoundingBoxes(quads)).toRectWithExtentsClippedToNumericLimits();
}
FloatPoint absPos = localToAbsolute(FloatPoint(), { } /* ignore transforms */, wasFixed);
Vector<LayoutRect> rects;
boundingRects(rects, flooredLayoutPoint(absPos));
size_t n = rects.size();
if (!n)
return IntRect();
LayoutRect result = unionRect(rects);
return snappedIntRect(result).toRectWithExtentsClippedToNumericLimits();
}
void RenderObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
{
Vector<LayoutRect> rects;
// FIXME: addFocusRingRects() needs to be passed this transform-unaware
// localToAbsolute() offset here because RenderInline::addFocusRingRects()
// implicitly assumes that. This doesn't work correctly with transformed
// descendants.
FloatPoint absolutePoint = localToAbsolute();
addFocusRingRects(rects, flooredLayoutPoint(absolutePoint));
float deviceScaleFactor = document().deviceScaleFactor();
for (auto rect : rects) {
rect.moveBy(LayoutPoint(-absolutePoint));
quads.append(localToAbsoluteQuad(FloatQuad(snapRectToDevicePixels(rect, deviceScaleFactor))));
}
}
void RenderObject::addAbsoluteRectForLayer(LayoutRect& result)
{
if (hasLayer())
result.unite(absoluteBoundingBoxRectIgnoringTransforms());
auto* renderElement = dynamicDowncast<RenderElement>(*this);
if (!renderElement)
return;
for (CheckedRef child : childrenOfType<RenderObject>(*renderElement))
child->addAbsoluteRectForLayer(result);
}
// FIXME: change this to use the subtreePaint terminology
LayoutRect RenderObject::paintingRootRect(LayoutRect& topLevelRect)
{
LayoutRect result = absoluteBoundingBoxRectIgnoringTransforms();
topLevelRect = result;
if (auto* renderElement = dynamicDowncast<RenderElement>(*this)) {
for (CheckedRef child : childrenOfType<RenderObject>(*renderElement))
child->addAbsoluteRectForLayer(result);
}
return result;
}
static inline bool canRelyOnAncestorLayerFullRepaint(const RenderObject& rendererToRepaint, const RenderLayer& ancestorLayer)
{
auto* renderElement = dynamicDowncast<RenderElement>(rendererToRepaint);
if (!renderElement || !renderElement->hasSelfPaintingLayer())
return true;
return ancestorLayer.renderer().hasNonVisibleOverflow();
}
RenderObject::RepaintContainerStatus RenderObject::containerForRepaint() const
{
CheckedPtr<const RenderLayerModelObject> repaintContainer;
auto fullRepaintAlreadyScheduled = false;
if (view().usesCompositing()) {
if (CheckedPtr parentLayer = enclosingLayer()) {
auto compLayerStatus = parentLayer->enclosingCompositingLayerForRepaint();
if (compLayerStatus.layer) {
repaintContainer = &compLayerStatus.layer->renderer();
fullRepaintAlreadyScheduled = compLayerStatus.fullRepaintAlreadyScheduled && canRelyOnAncestorLayerFullRepaint(*this, *compLayerStatus.layer);
}
}
}
if (view().hasSoftwareFilters()) {
if (CheckedPtr parentLayer = enclosingLayer()) {
if (CheckedPtr enclosingFilterLayer = parentLayer->enclosingFilterLayer()) {
fullRepaintAlreadyScheduled = parentLayer->needsFullRepaint() && canRelyOnAncestorLayerFullRepaint(*this, *parentLayer);
return { fullRepaintAlreadyScheduled, &enclosingFilterLayer->renderer() };
}
}
}
// If we have a flow thread, then we need to do individual repaints within the RenderFragmentContainers instead.
// Return the flow thread as a repaint container in order to create a chokepoint that allows us to change
// repainting to do individual region repaints.
if (CheckedPtr parentRenderFragmentedFlow = enclosingFragmentedFlow()) {
// If we have already found a repaint container then we will repaint into that container only if it is part of the same
// flow thread. Otherwise we will need to catch the repaint call and send it to the flow thread.
CheckedPtr repaintContainerFragmentedFlow = repaintContainer ? repaintContainer->enclosingFragmentedFlow() : nullptr;
if (!repaintContainerFragmentedFlow || repaintContainerFragmentedFlow != parentRenderFragmentedFlow)
repaintContainer = WTFMove(parentRenderFragmentedFlow);
}
return { fullRepaintAlreadyScheduled, WTFMove(repaintContainer) };
}
void RenderObject::propagateRepaintToParentWithOutlineAutoIfNeeded(const RenderLayerModelObject& repaintContainer, const LayoutRect& repaintRect) const
{
if (!hasOutlineAutoAncestor())
return;
// FIXME: We should really propagate only when the child renderer sticks out.
bool repaintRectNeedsConverting = false;
// Issue repaint on the renderer with outline: auto.
for (CheckedPtr renderer = this; renderer; renderer = renderer->parent()) {
CheckedPtr originalRenderer = renderer;
if (CheckedPtr previousMultiColumnSet = dynamicDowncast<RenderMultiColumnSet>(renderer->previousSibling()); previousMultiColumnSet && !renderer->isRenderMultiColumnSet() && !renderer->isLegend()) {
CheckedPtr enclosingMultiColumnFlow = previousMultiColumnSet->multiColumnFlow();
CheckedPtr renderMultiColumnPlaceholder = enclosingMultiColumnFlow->findColumnSpannerPlaceholder(downcast<RenderBox>(*renderer));
ASSERT(renderMultiColumnPlaceholder);
renderer = WTFMove(renderMultiColumnPlaceholder);
}
bool rendererHasOutlineAutoAncestor = renderer->hasOutlineAutoAncestor() || originalRenderer->hasOutlineAutoAncestor();
ASSERT(rendererHasOutlineAutoAncestor
|| originalRenderer->outlineStyleForRepaint().hasAutoOutlineStyle()
|| (is<RenderBoxModelObject>(*renderer) && downcast<RenderBoxModelObject>(*renderer).isContinuation()));
if (originalRenderer == &repaintContainer && rendererHasOutlineAutoAncestor)
repaintRectNeedsConverting = true;
if (rendererHasOutlineAutoAncestor)
continue;
// Issue repaint on the correct repaint container.
LayoutRect adjustedRepaintRect = repaintRect;
adjustedRepaintRect.inflate(originalRenderer->outlineStyleForRepaint().outlineSize());
if (!repaintRectNeedsConverting)
repaintContainer.repaintRectangle(adjustedRepaintRect);
else if (CheckedPtr rendererWithOutline = dynamicDowncast<RenderLayerModelObject>(originalRenderer.get())) {
adjustedRepaintRect = LayoutRect(repaintContainer.localToContainerQuad(FloatRect(adjustedRepaintRect), rendererWithOutline.get()).boundingBox());
rendererWithOutline->repaintRectangle(adjustedRepaintRect);