forked from qt/qtwebkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderBlock.cpp
More file actions
3851 lines (3244 loc) · 167 KB
/
Copy pathRenderBlock.cpp
File metadata and controls
3851 lines (3244 loc) · 167 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) 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 "config.h"
#include "RenderBlock.h"
#include "AXObjectCache.h"
#include "Document.h"
#include "Editor.h"
#include "Element.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HitTestLocation.h"
#include "HitTestResult.h"
#include "InlineElementBox.h"
#include "InlineIterator.h"
#include "InlineTextBox.h"
#include "LayoutRepainter.h"
#include "LogicalSelectionOffsetCaches.h"
#include "OverflowEvent.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderBlockFlow.h"
#include "RenderBoxRegionInfo.h"
#include "RenderButton.h"
#include "RenderCombineText.h"
#include "RenderDeprecatedFlexibleBox.h"
#include "RenderFlexibleBox.h"
#include "RenderInline.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderListMarker.h"
#include "RenderMenuList.h"
#include "RenderNamedFlowFragment.h"
#include "RenderNamedFlowThread.h"
#include "RenderRegion.h"
#include "RenderSVGResourceClipper.h"
#include "RenderTableCell.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "RenderTreePosition.h"
#include "RenderView.h"
#include "SVGTextRunRenderingContext.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "TextBreakIterator.h"
#include "TransformState.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/Optional.h>
#include <wtf/StackStats.h>
#include <wtf/TemporaryChange.h>
#if ENABLE(CSS_SHAPES)
#include "ShapeOutsideInfo.h"
#endif
using namespace WTF;
using namespace Unicode;
namespace WebCore {
using namespace HTMLNames;
struct SameSizeAsRenderBlock : public RenderBox {
};
COMPILE_ASSERT(sizeof(RenderBlock) == sizeof(SameSizeAsRenderBlock), RenderBlock_should_stay_small);
typedef HashMap<const RenderBlock*, std::unique_ptr<TrackedRendererListHashSet>> TrackedDescendantsMap;
typedef HashMap<const RenderBox*, std::unique_ptr<HashSet<const RenderBlock*>>> TrackedContainerMap;
static TrackedDescendantsMap* percentHeightDescendantsMap;
static TrackedContainerMap* percentHeightContainerMap;
static void insertIntoTrackedRendererMaps(const RenderBlock& container, RenderBox& descendant)
{
if (!percentHeightDescendantsMap) {
percentHeightDescendantsMap = new TrackedDescendantsMap;
percentHeightContainerMap = new TrackedContainerMap;
}
auto& descendantSet = percentHeightDescendantsMap->ensure(&container, [] {
return std::make_unique<TrackedRendererListHashSet>();
}).iterator->value;
bool added = descendantSet->add(&descendant).isNewEntry;
if (!added) {
ASSERT(percentHeightContainerMap->get(&descendant));
ASSERT(percentHeightContainerMap->get(&descendant)->contains(&container));
return;
}
auto& containerSet = percentHeightContainerMap->ensure(&descendant, [] {
return std::make_unique<HashSet<const RenderBlock*>>();
}).iterator->value;
ASSERT(!containerSet->contains(&container));
containerSet->add(&container);
}
static void removeFromTrackedRendererMaps(RenderBox& descendant)
{
if (!percentHeightDescendantsMap)
return;
std::unique_ptr<HashSet<const RenderBlock*>> containerSet = percentHeightContainerMap->take(&descendant);
if (!containerSet)
return;
for (auto* container : *containerSet) {
// FIXME: Disabling this assert temporarily until we fix the layout
// bugs associated with positioned objects not properly cleared from
// their ancestor chain before being moved. See webkit bug 93766.
// ASSERT(descendant->isDescendantOf(container));
auto descendantsMapIterator = percentHeightDescendantsMap->find(container);
ASSERT(descendantsMapIterator != percentHeightDescendantsMap->end());
if (descendantsMapIterator == percentHeightDescendantsMap->end())
continue;
auto& descendantSet = descendantsMapIterator->value;
ASSERT(descendantSet->contains(&descendant));
descendantSet->remove(&descendant);
if (descendantSet->isEmpty())
percentHeightDescendantsMap->remove(descendantsMapIterator);
}
}
class PositionedDescendantsMap {
public:
enum class MoveDescendantToEnd { No, Yes };
void addDescendant(const RenderBlock& containingBlock, RenderBox& positionedDescendant, MoveDescendantToEnd moveDescendantToEnd)
{
// Protect against double insert where a descendant would end up with multiple containing blocks.
auto* previousContainingBlock = m_containerMap.get(&positionedDescendant);
if (previousContainingBlock && previousContainingBlock != &containingBlock) {
if (auto* descendants = m_descendantsMap.get(previousContainingBlock))
descendants->remove(&positionedDescendant);
}
auto& descendants = m_descendantsMap.ensure(&containingBlock, [] {
return std::make_unique<TrackedRendererListHashSet>();
}).iterator->value;
bool isNewEntry = moveDescendantToEnd == MoveDescendantToEnd::Yes ? descendants->appendOrMoveToLast(&positionedDescendant).isNewEntry
: descendants->add(&positionedDescendant).isNewEntry;
if (!isNewEntry) {
ASSERT(m_containerMap.contains(&positionedDescendant));
return;
}
m_containerMap.set(&positionedDescendant, &containingBlock);
}
void removeDescendant(const RenderBox& positionedDescendant)
{
auto* containingBlock = m_containerMap.take(&positionedDescendant);
if (!containingBlock)
return;
auto descendantsIterator = m_descendantsMap.find(containingBlock);
ASSERT(descendantsIterator != m_descendantsMap.end());
if (descendantsIterator == m_descendantsMap.end())
return;
auto& descendants = descendantsIterator->value;
ASSERT(descendants->contains(const_cast<RenderBox*>(&positionedDescendant)));
descendants->remove(const_cast<RenderBox*>(&positionedDescendant));
if (descendants->isEmpty())
m_descendantsMap.remove(descendantsIterator);
}
void removeContainingBlock(const RenderBlock& containingBlock)
{
auto descendants = m_descendantsMap.take(&containingBlock);
if (!descendants)
return;
for (auto* renderer : *descendants)
m_containerMap.remove(renderer);
}
TrackedRendererListHashSet* positionedRenderers(const RenderBlock& containingBlock) const
{
return m_descendantsMap.get(&containingBlock);
}
private:
using DescendantsMap = HashMap<const RenderBlock*, std::unique_ptr<TrackedRendererListHashSet>>;
using ContainerMap = HashMap<const RenderBox*, const RenderBlock*>;
DescendantsMap m_descendantsMap;
ContainerMap m_containerMap;
};
static PositionedDescendantsMap& positionedDescendantsMap()
{
static NeverDestroyed<PositionedDescendantsMap> mapForPositionedDescendants;
return mapForPositionedDescendants;
}
typedef HashMap<RenderBlock*, std::unique_ptr<ListHashSet<RenderInline*>>> ContinuationOutlineTableMap;
struct UpdateScrollInfoAfterLayoutTransaction {
UpdateScrollInfoAfterLayoutTransaction(const RenderView& view)
: nestedCount(0)
, view(&view)
{
}
int nestedCount;
const RenderView* view;
HashSet<RenderBlock*> blocks;
};
typedef Vector<UpdateScrollInfoAfterLayoutTransaction> DelayedUpdateScrollInfoStack;
static std::unique_ptr<DelayedUpdateScrollInfoStack>& updateScrollInfoAfterLayoutTransactionStack()
{
static NeverDestroyed<std::unique_ptr<DelayedUpdateScrollInfoStack>> delayedUpdatedScrollInfoStack;
return delayedUpdatedScrollInfoStack;
}
// Allocated only when some of these fields have non-default values
struct RenderBlockRareData {
WTF_MAKE_NONCOPYABLE(RenderBlockRareData); WTF_MAKE_FAST_ALLOCATED;
public:
RenderBlockRareData()
: m_paginationStrut(0)
, m_pageLogicalOffset(0)
, m_flowThreadContainingBlock(Nullopt)
{
}
LayoutUnit m_paginationStrut;
LayoutUnit m_pageLogicalOffset;
Optional<RenderFlowThread*> m_flowThreadContainingBlock;
};
typedef HashMap<const RenderBlock*, std::unique_ptr<RenderBlockRareData>> RenderBlockRareDataMap;
static RenderBlockRareDataMap* gRareDataMap = 0;
// This class helps dispatching the 'overflow' event on layout change. overflow can be set on RenderBoxes, yet the existing code
// only works on RenderBlocks. If this change, this class should be shared with other RenderBoxes.
class OverflowEventDispatcher {
WTF_MAKE_NONCOPYABLE(OverflowEventDispatcher);
public:
OverflowEventDispatcher(const RenderBlock* block)
: m_block(block)
, m_hadHorizontalLayoutOverflow(false)
, m_hadVerticalLayoutOverflow(false)
{
m_shouldDispatchEvent = !m_block->isAnonymous() && m_block->hasOverflowClip() && m_block->document().hasListenerType(Document::OVERFLOWCHANGED_LISTENER);
if (m_shouldDispatchEvent) {
m_hadHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
m_hadVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
}
}
~OverflowEventDispatcher()
{
if (!m_shouldDispatchEvent)
return;
bool hasHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
bool hasVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
bool horizontalLayoutOverflowChanged = hasHorizontalLayoutOverflow != m_hadHorizontalLayoutOverflow;
bool verticalLayoutOverflowChanged = hasVerticalLayoutOverflow != m_hadVerticalLayoutOverflow;
if (!horizontalLayoutOverflowChanged && !verticalLayoutOverflowChanged)
return;
Ref<OverflowEvent> overflowEvent = OverflowEvent::create(horizontalLayoutOverflowChanged, hasHorizontalLayoutOverflow, verticalLayoutOverflowChanged, hasVerticalLayoutOverflow);
overflowEvent->setTarget(m_block->element());
m_block->document().enqueueOverflowEvent(WTFMove(overflowEvent));
}
private:
const RenderBlock* m_block;
bool m_shouldDispatchEvent;
bool m_hadHorizontalLayoutOverflow;
bool m_hadVerticalLayoutOverflow;
};
RenderBlock::RenderBlock(Element& element, Ref<RenderStyle>&& style, BaseTypeFlags baseTypeFlags)
: RenderBox(element, WTFMove(style), baseTypeFlags | RenderBlockFlag)
{
}
RenderBlock::RenderBlock(Document& document, Ref<RenderStyle>&& style, BaseTypeFlags baseTypeFlags)
: RenderBox(document, WTFMove(style), baseTypeFlags | RenderBlockFlag)
{
}
static void removeBlockFromPercentageDescendantAndContainerMaps(RenderBlock* block)
{
if (!percentHeightDescendantsMap)
return;
std::unique_ptr<TrackedRendererListHashSet> descendantSet = percentHeightDescendantsMap->take(block);
if (!descendantSet)
return;
for (auto* descendant : *descendantSet) {
auto it = percentHeightContainerMap->find(descendant);
ASSERT(it != percentHeightContainerMap->end());
if (it == percentHeightContainerMap->end())
continue;
auto* containerSet = it->value.get();
ASSERT(containerSet->contains(block));
containerSet->remove(block);
if (containerSet->isEmpty())
percentHeightContainerMap->remove(it);
}
}
RenderBlock::~RenderBlock()
{
removeFromUpdateScrollInfoAfterLayoutTransaction();
if (gRareDataMap)
gRareDataMap->remove(this);
removeBlockFromPercentageDescendantAndContainerMaps(this);
positionedDescendantsMap().removeContainingBlock(*this);
}
void RenderBlock::willBeDestroyed()
{
if (!documentBeingDestroyed()) {
if (parent())
parent()->dirtyLinesFromChangedChild(*this);
}
RenderBox::willBeDestroyed();
}
bool RenderBlock::hasRareData() const
{
return gRareDataMap ? gRareDataMap->contains(this) : false;
}
void RenderBlock::removePositionedObjectsIfNeeded(const RenderStyle& oldStyle, const RenderStyle& newStyle)
{
bool hadTransform = oldStyle.hasTransformRelatedProperty();
bool willHaveTransform = newStyle.hasTransformRelatedProperty();
if (oldStyle.position() == newStyle.position() && hadTransform == willHaveTransform)
return;
// We are no longer the containing block for fixed descendants.
if (hadTransform && !willHaveTransform) {
// Our positioned descendants will be inserted into a new containing block's positioned objects list during the next layout.
removePositionedObjects(nullptr, NewContainingBlock);
return;
}
// We are no longer the containing block for absolute positioned descendants.
if (newStyle.position() == StaticPosition && !willHaveTransform) {
// Our positioned descendants will be inserted into a new containing block's positioned objects list during the next layout.
removePositionedObjects(nullptr, NewContainingBlock);
return;
}
// We are a new containing block.
if (oldStyle.position() == StaticPosition && !hadTransform) {
// Remove our absolutely positioned descendants from their current containing block.
// They will be inserted into our positioned objects list during layout.
auto* containingBlock = parent();
while (containingBlock && !is<RenderView>(*containingBlock)
&& (containingBlock->style().position() == StaticPosition || (containingBlock->isInline() && !containingBlock->isReplaced()))) {
if (containingBlock->style().position() == RelativePosition && containingBlock->isInline() && !containingBlock->isReplaced()) {
containingBlock = containingBlock->containingBlock();
break;
}
containingBlock = containingBlock->parent();
}
if (containingBlock && is<RenderBlock>(*containingBlock))
downcast<RenderBlock>(*containingBlock).removePositionedObjects(this, NewContainingBlock);
}
}
void RenderBlock::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
setReplaced(newStyle.isDisplayInlineType());
if (oldStyle)
removePositionedObjectsIfNeeded(*oldStyle, newStyle);
RenderBox::styleWillChange(diff, newStyle);
}
static bool borderOrPaddingLogicalWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
{
if (newStyle->isHorizontalWritingMode())
return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
|| oldStyle->borderRightWidth() != newStyle->borderRightWidth()
|| oldStyle->paddingLeft() != newStyle->paddingLeft()
|| oldStyle->paddingRight() != newStyle->paddingRight();
return oldStyle->borderTopWidth() != newStyle->borderTopWidth()
|| oldStyle->borderBottomWidth() != newStyle->borderBottomWidth()
|| oldStyle->paddingTop() != newStyle->paddingTop()
|| oldStyle->paddingBottom() != newStyle->paddingBottom();
}
void RenderBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderStyle& newStyle = style();
bool hadTransform = hasTransform();
bool flowThreadContainingBlockInvalidated = false;
if (oldStyle && oldStyle->position() != newStyle.position()) {
invalidateFlowThreadContainingBlockIncludingDescendants();
flowThreadContainingBlockInvalidated = true;
}
RenderBox::styleDidChange(diff, oldStyle);
if (hadTransform != hasTransform() && !flowThreadContainingBlockInvalidated)
invalidateFlowThreadContainingBlockIncludingDescendants();
if (!isAnonymousBlock()) {
// Ensure that all of our continuation blocks pick up the new style.
for (RenderBlock* currCont = blockElementContinuation(); currCont; currCont = currCont->blockElementContinuation()) {
RenderBoxModelObject* nextCont = currCont->continuation();
currCont->setContinuation(0);
currCont->setStyle(newStyle);
currCont->setContinuation(nextCont);
}
}
propagateStyleToAnonymousChildren(PropagateToBlockChildrenOnly);
// It's possible for our border/padding to change, but for the overall logical width 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.
setHasBorderOrPaddingLogicalWidthChanged(oldStyle && diff == StyleDifferenceLayout && needsLayout() && borderOrPaddingLogicalWidthChanged(oldStyle, &newStyle));
}
RenderBlock* RenderBlock::continuationBefore(RenderObject* beforeChild)
{
if (beforeChild && beforeChild->parent() == this)
return this;
RenderBlock* nextToLast = this;
RenderBlock* last = this;
for (auto* current = downcast<RenderBlock>(continuation()); current; current = downcast<RenderBlock>(current->continuation())) {
if (beforeChild && beforeChild->parent() == current) {
if (current->firstChild() == beforeChild)
return last;
return current;
}
nextToLast = last;
last = current;
}
if (!beforeChild && !last->firstChild())
return nextToLast;
return last;
}
void RenderBlock::addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild)
{
RenderBlock* flow = continuationBefore(beforeChild);
ASSERT(!beforeChild || is<RenderBlock>(*beforeChild->parent()));
RenderBoxModelObject* beforeChildParent = nullptr;
if (beforeChild)
beforeChildParent = downcast<RenderBoxModelObject>(beforeChild->parent());
else {
RenderBoxModelObject* continuation = flow->continuation();
if (continuation)
beforeChildParent = continuation;
else
beforeChildParent = flow;
}
if (newChild->isFloatingOrOutOfFlowPositioned()) {
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
return;
}
bool childIsNormal = newChild->isInline() || !newChild->style().columnSpan();
bool bcpIsNormal = beforeChildParent->isInline() || !beforeChildParent->style().columnSpan();
bool flowIsNormal = flow->isInline() || !flow->style().columnSpan();
if (flow == beforeChildParent) {
flow->addChildIgnoringContinuation(newChild, beforeChild);
return;
}
// The goal here is to match up if we can, so that we can coalesce and create the
// minimal # of continuations needed for the inline.
if (childIsNormal == bcpIsNormal) {
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
return;
}
if (flowIsNormal == childIsNormal) {
flow->addChildIgnoringContinuation(newChild, 0); // Just treat like an append.
return;
}
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
}
RenderPtr<RenderBlock> RenderBlock::clone() const
{
RenderPtr<RenderBlock> cloneBlock;
if (isAnonymousBlock()) {
cloneBlock = RenderPtr<RenderBlock>(createAnonymousBlock());
cloneBlock->setChildrenInline(childrenInline());
} else {
RenderTreePosition insertionPosition(*parent());
cloneBlock = static_pointer_cast<RenderBlock>(element()->createElementRenderer(style(), insertionPosition));
cloneBlock->initializeStyle();
// This takes care of setting the right value of childrenInline in case
// generated content is added to cloneBlock and 'this' does not have
// generated content added yet.
cloneBlock->setChildrenInline(cloneBlock->firstChild() ? cloneBlock->firstChild()->isInline() : childrenInline());
}
cloneBlock->setFlowThreadState(flowThreadState());
return cloneBlock;
}
void RenderBlock::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
if (continuation() && !isAnonymousBlock())
addChildToContinuation(newChild, beforeChild);
else
addChildIgnoringContinuation(newChild, beforeChild);
}
void RenderBlock::addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild)
{
if (beforeChild && beforeChild->parent() != this) {
RenderElement* beforeChildContainer = beforeChild->parent();
while (beforeChildContainer->parent() != this)
beforeChildContainer = beforeChildContainer->parent();
ASSERT(beforeChildContainer);
if (beforeChildContainer->isAnonymous()) {
// If the requested beforeChild is not one of our children, then this is because
// there is an anonymous container within this object that contains the beforeChild.
RenderElement* beforeChildAnonymousContainer = beforeChildContainer;
if (beforeChildAnonymousContainer->isAnonymousBlock()
#if ENABLE(FULLSCREEN_API)
// Full screen renderers and full screen placeholders act as anonymous blocks, not tables:
|| beforeChildAnonymousContainer->isRenderFullScreen()
|| beforeChildAnonymousContainer->isRenderFullScreenPlaceholder()
#endif
) {
// Insert the child into the anonymous block box instead of here.
if (newChild->isInline() || beforeChild->parent()->firstChild() != beforeChild)
beforeChild->parent()->addChild(newChild, beforeChild);
else
addChild(newChild, beforeChild->parent());
return;
}
ASSERT(beforeChildAnonymousContainer->isTable());
if (newChild->isTablePart()) {
// Insert into the anonymous table.
beforeChildAnonymousContainer->addChild(newChild, beforeChild);
return;
}
beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
ASSERT(beforeChild->parent() == this);
if (beforeChild->parent() != this) {
// We should never reach here. If we do, we need to use the
// safe fallback to use the topmost beforeChild container.
beforeChild = beforeChildContainer;
}
}
}
bool madeBoxesNonInline = false;
// A block has to either have all of its children inline, or all of its children as blocks.
// So, if our children are currently inline and a block child has to be inserted, we move all our
// inline children into anonymous block boxes.
if (childrenInline() && !newChild->isInline() && !newChild->isFloatingOrOutOfFlowPositioned()) {
// This is a block with inline content. Wrap the inline content in anonymous blocks.
makeChildrenNonInline(beforeChild);
madeBoxesNonInline = true;
if (beforeChild && beforeChild->parent() != this) {
beforeChild = beforeChild->parent();
ASSERT(beforeChild->isAnonymousBlock());
ASSERT(beforeChild->parent() == this);
}
} else if (!childrenInline() && (newChild->isFloatingOrOutOfFlowPositioned() || newChild->isInline())) {
// 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.
RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : lastChild();
if (afterChild && afterChild->isAnonymousBlock()) {
downcast<RenderBlock>(*afterChild).addChild(newChild);
return;
}
if (newChild->isInline()) {
// No suitable existing anonymous box - create a new one.
RenderBlock* newBox = createAnonymousBlock();
RenderBox::addChild(newBox, beforeChild);
newBox->addChild(newChild);
return;
}
}
invalidateLineLayoutPath();
RenderBox::addChild(newChild, beforeChild);
if (madeBoxesNonInline && is<RenderBlock>(parent()) && isAnonymousBlock())
downcast<RenderBlock>(*parent()).removeLeftoverAnonymousBlock(this);
// this object may be dead here
}
static void getInlineRun(RenderObject* start, RenderObject* boundary,
RenderObject*& inlineRunStart,
RenderObject*& inlineRunEnd)
{
// Beginning at |start| we find the largest contiguous run of inlines that
// we can. We denote the run with start and end points, |inlineRunStart|
// and |inlineRunEnd|. Note that these two values may be the same if
// we encounter only one inline.
//
// We skip any non-inlines we encounter as long as we haven't found any
// inlines yet.
//
// |boundary| indicates a non-inclusive boundary point. Regardless of whether |boundary|
// is inline or not, we will not include it in a run with inlines before it. It's as though we encountered
// a non-inline.
// Start by skipping as many non-inlines as we can.
RenderObject * curr = start;
bool sawInline;
do {
while (curr && !(curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()))
curr = curr->nextSibling();
inlineRunStart = inlineRunEnd = curr;
if (!curr)
return; // No more inline children to be found.
sawInline = curr->isInline();
curr = curr->nextSibling();
while (curr && (curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()) && (curr != boundary)) {
inlineRunEnd = curr;
if (curr->isInline())
sawInline = true;
curr = curr->nextSibling();
}
} while (!sawInline);
}
void RenderBlock::deleteLines()
{
if (AXObjectCache* cache = document().existingAXObjectCache())
cache->recomputeIsIgnored(this);
}
void RenderBlock::makeChildrenNonInline(RenderObject* insertionPoint)
{
// makeChildrenNonInline takes a block whose children are *all* inline and it
// makes sure that inline children are coalesced under anonymous
// blocks. If |insertionPoint| is defined, then it represents the insertion point for
// the new block child that is causing us to have to wrap all the inlines. This
// means that we cannot coalesce inlines before |insertionPoint| with inlines following
// |insertionPoint|, because the new child is going to be inserted in between the inlines,
// splitting them.
ASSERT(isInlineBlockOrInlineTable() || !isInline());
ASSERT(!insertionPoint || insertionPoint->parent() == this);
setChildrenInline(false);
RenderObject* child = firstChild();
if (!child)
return;
deleteLines();
while (child) {
RenderObject* inlineRunStart;
RenderObject* inlineRunEnd;
getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
if (!inlineRunStart)
break;
child = inlineRunEnd->nextSibling();
RenderBlock* block = createAnonymousBlock();
insertChildInternal(block, inlineRunStart, NotifyChildren);
moveChildrenTo(block, inlineRunStart, child);
}
#ifndef NDEBUG
for (RenderObject* c = firstChild(); c; c = c->nextSibling())
ASSERT(!c->isInline());
#endif
repaint();
}
void RenderBlock::removeLeftoverAnonymousBlock(RenderBlock* child)
{
ASSERT(child->isAnonymousBlock());
ASSERT(!child->childrenInline());
if (child->continuation())
return;
RenderObject* firstAnChild = child->firstChild();
RenderObject* lastAnChild = child->lastChild();
if (firstAnChild) {
RenderObject* o = firstAnChild;
while (o) {
o->setParent(this);
o = o->nextSibling();
}
firstAnChild->setPreviousSibling(child->previousSibling());
lastAnChild->setNextSibling(child->nextSibling());
if (child->previousSibling())
child->previousSibling()->setNextSibling(firstAnChild);
if (child->nextSibling())
child->nextSibling()->setPreviousSibling(lastAnChild);
if (child == firstChild())
setFirstChild(firstAnChild);
if (child == lastChild())
setLastChild(lastAnChild);
} else {
if (child == firstChild())
setFirstChild(child->nextSibling());
if (child == lastChild())
setLastChild(child->previousSibling());
if (child->previousSibling())
child->previousSibling()->setNextSibling(child->nextSibling());
if (child->nextSibling())
child->nextSibling()->setPreviousSibling(child->previousSibling());
}
child->setFirstChild(0);
child->m_next = 0;
// Remove all the information in the flow thread associated with the leftover anonymous block.
child->removeFromRenderFlowThread();
child->setParent(0);
child->setPreviousSibling(0);
child->setNextSibling(0);
child->destroy();
}
static bool canDropAnonymousBlock(const RenderBlock& anonymousBlock)
{
if (anonymousBlock.beingDestroyed() || anonymousBlock.continuation())
return false;
if (anonymousBlock.isRubyRun() || anonymousBlock.isRubyBase())
return false;
return true;
}
static bool canMergeContiguousAnonymousBlocks(RenderObject& oldChild, RenderObject* previous, RenderObject* next)
{
if (oldChild.documentBeingDestroyed() || oldChild.isInline() || oldChild.virtualContinuation())
return false;
if (previous) {
if (!previous->isAnonymousBlock())
return false;
RenderBlock& previousAnonymousBlock = downcast<RenderBlock>(*previous);
if (!canDropAnonymousBlock(previousAnonymousBlock))
return false;
}
if (next) {
if (!next->isAnonymousBlock())
return false;
RenderBlock& nextAnonymousBlock = downcast<RenderBlock>(*next);
if (!canDropAnonymousBlock(nextAnonymousBlock))
return false;
}
return true;
}
void RenderBlock::dropAnonymousBoxChild(RenderBlock& parent, RenderBlock& child)
{
parent.setNeedsLayoutAndPrefWidthsRecalc();
parent.setChildrenInline(child.childrenInline());
if (auto* childFlowThread = child.flowThreadContainingBlock())
childFlowThread->removeFlowChildInfo(&child);
RenderObject* nextSibling = child.nextSibling();
parent.removeChildInternal(child, child.hasLayer() ? NotifyChildren : DontNotifyChildren);
child.moveAllChildrenTo(&parent, nextSibling, child.hasLayer());
// Delete the now-empty block's lines and nuke it.
child.deleteLines();
child.destroy();
}
void RenderBlock::removeChild(RenderObject& oldChild)
{
// No need to waste time in merging or removing empty anonymous blocks.
// We can just bail out if our document is getting destroyed.
if (documentBeingDestroyed()) {
RenderBox::removeChild(oldChild);
return;
}
// If this child is a block, and if our previous and next siblings are both anonymous blocks
// with inline content, then we can fold the inline content back together.
RenderObject* prev = oldChild.previousSibling();
RenderObject* next = oldChild.nextSibling();
bool canMergeAnonymousBlocks = canMergeContiguousAnonymousBlocks(oldChild, prev, next);
if (canMergeAnonymousBlocks && prev && next) {
prev->setNeedsLayoutAndPrefWidthsRecalc();
RenderBlock& nextBlock = downcast<RenderBlock>(*next);
RenderBlock& prevBlock = downcast<RenderBlock>(*prev);
if (prev->childrenInline() != next->childrenInline()) {
RenderBlock& inlineChildrenBlock = prev->childrenInline() ? prevBlock : nextBlock;
RenderBlock& blockChildrenBlock = prev->childrenInline() ? nextBlock : prevBlock;
// Place the inline children block inside of the block children block instead of deleting it.
// In order to reuse it, we have to reset it to just be a generic anonymous block. Make sure
// to clear out inherited column properties by just making a new style, and to also clear the
// column span flag if it is set.
ASSERT(!inlineChildrenBlock.continuation());
// Cache this value as it might get changed in setStyle() call.
bool inlineChildrenBlockHasLayer = inlineChildrenBlock.hasLayer();
inlineChildrenBlock.setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
removeChildInternal(inlineChildrenBlock, inlineChildrenBlockHasLayer ? NotifyChildren : DontNotifyChildren);
// Now just put the inlineChildrenBlock inside the blockChildrenBlock.
RenderObject* beforeChild = prev == &inlineChildrenBlock ? blockChildrenBlock.firstChild() : nullptr;
blockChildrenBlock.insertChildInternal(&inlineChildrenBlock, beforeChild,
(inlineChildrenBlockHasLayer || blockChildrenBlock.hasLayer()) ? NotifyChildren : DontNotifyChildren);
next->setNeedsLayoutAndPrefWidthsRecalc();
// inlineChildrenBlock got reparented to blockChildrenBlock, so it is no longer a child
// of "this". we null out prev or next so that is not used later in the function.
if (&inlineChildrenBlock == &prevBlock)
prev = nullptr;
else
next = nullptr;
} else {
// Take all the children out of the |next| block and put them in
// the |prev| block.
nextBlock.moveAllChildrenIncludingFloatsTo(prevBlock, nextBlock.hasLayer() || prevBlock.hasLayer());
// Delete the now-empty block's lines and nuke it.
nextBlock.deleteLines();
nextBlock.destroy();
next = nullptr;
}
}
invalidateLineLayoutPath();
RenderBox::removeChild(oldChild);
RenderObject* child = prev ? prev : next;
if (canMergeAnonymousBlocks && child && !child->previousSibling() && !child->nextSibling() && canDropAnonymousBlockChild()) {
// The removal has knocked us down to containing only a single anonymous
// box. We can pull the content right back up into our box.
dropAnonymousBoxChild(*this, downcast<RenderBlock>(*child));
} else if (((prev && prev->isAnonymousBlock()) || (next && next->isAnonymousBlock())) && canDropAnonymousBlockChild()) {
// It's possible that the removal has knocked us down to a single anonymous
// block with floating siblings.
RenderBlock& anonBlock = downcast<RenderBlock>((prev && prev->isAnonymousBlock()) ? *prev : *next);
if (canDropAnonymousBlock(anonBlock)) {
bool dropAnonymousBlock = true;
for (auto& sibling : childrenOfType<RenderObject>(*this)) {
if (&sibling == &anonBlock)
continue;
if (!sibling.isFloating()) {
dropAnonymousBlock = false;
break;
}
}
if (dropAnonymousBlock)
dropAnonymousBoxChild(*this, anonBlock);
}
}
if (!firstChild()) {
// If this was our last child be sure to clear out our line boxes.
if (childrenInline())
deleteLines();
// If we are an empty anonymous block in the continuation chain,
// we need to remove ourself and fix the continuation chain.
if (!beingDestroyed() && isAnonymousBlockContinuation() && !oldChild.isListMarker()) {
auto containingBlockIgnoringAnonymous = containingBlock();
while (containingBlockIgnoringAnonymous && containingBlockIgnoringAnonymous->isAnonymousBlock())
containingBlockIgnoringAnonymous = containingBlockIgnoringAnonymous->containingBlock();
for (RenderObject* current = this; current; current = current->previousInPreOrder(containingBlockIgnoringAnonymous)) {
if (current->virtualContinuation() != this)
continue;
// Found our previous continuation. We just need to point it to
// |this|'s next continuation.
RenderBoxModelObject* nextContinuation = continuation();
if (is<RenderInline>(*current))
downcast<RenderInline>(*current).setContinuation(nextContinuation);
else if (is<RenderBlock>(*current))
downcast<RenderBlock>(*current).setContinuation(nextContinuation);
else
ASSERT_NOT_REACHED();
break;
}
setContinuation(nullptr);
destroy();
}
}
}
bool RenderBlock::childrenPreventSelfCollapsing() const
{
// Whether or not we collapse is dependent on whether all our normal flow children
// are also self-collapsing.
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (child->isFloatingOrOutOfFlowPositioned())
continue;
if (!child->isSelfCollapsingBlock())
return true;
}
return false;
}
bool RenderBlock::isSelfCollapsingBlock() const
{
// We are not self-collapsing if we
// (a) have a non-zero height according to layout (an optimization to avoid wasting time)
// (b) are a table,
// (c) have border/padding,
// (d) have a min-height
// (e) have specified that one of our margins can't collapse using a CSS extension
if (logicalHeight() > 0
|| isTable() || borderAndPaddingLogicalHeight()
|| style().logicalMinHeight().isPositive()
|| style().marginBeforeCollapse() == MSEPARATE || style().marginAfterCollapse() == MSEPARATE)
return false;
Length logicalHeightLength = style().logicalHeight();
bool hasAutoHeight = logicalHeightLength.isAuto();
if (logicalHeightLength.isPercentOrCalculated() && !document().inQuirksMode()) {
hasAutoHeight = true;
for (RenderBlock* cb = containingBlock(); cb && !is<RenderView>(*cb); cb = cb->containingBlock()) {
if (cb->style().logicalHeight().isFixed() || cb->isTableCell())
hasAutoHeight = false;
}
}
// If the height is 0 or auto, then whether or not we are a self-collapsing block depends
// on whether we have content that is all self-collapsing or not.
if (hasAutoHeight || ((logicalHeightLength.isFixed() || logicalHeightLength.isPercentOrCalculated()) && logicalHeightLength.isZero()))
return !childrenPreventSelfCollapsing();
return false;
}
static inline UpdateScrollInfoAfterLayoutTransaction* currentUpdateScrollInfoAfterLayoutTransaction()
{
if (!updateScrollInfoAfterLayoutTransactionStack())
return nullptr;
return &updateScrollInfoAfterLayoutTransactionStack()->last();
}
void RenderBlock::beginUpdateScrollInfoAfterLayoutTransaction()