RenderFlowThread's containing block cache should be invalidated before calling styleD...
[WebKit-https.git] / Source / WebCore / rendering / RenderBlock.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2007 David Smith (catfish.man@gmail.com)
5  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "RenderBlock.h"
26
27 #include "AXObjectCache.h"
28 #include "Document.h"
29 #include "Editor.h"
30 #include "Element.h"
31 #include "FloatQuad.h"
32 #include "Frame.h"
33 #include "FrameSelection.h"
34 #include "FrameView.h"
35 #include "GraphicsContext.h"
36 #include "HTMLNames.h"
37 #include "HitTestLocation.h"
38 #include "HitTestResult.h"
39 #include "InlineElementBox.h"
40 #include "InlineIterator.h"
41 #include "InlineTextBox.h"
42 #include "LayoutRepainter.h"
43 #include "LogicalSelectionOffsetCaches.h"
44 #include "OverflowEvent.h"
45 #include "Page.h"
46 #include "PaintInfo.h"
47 #include "RenderBlockFlow.h"
48 #include "RenderBoxRegionInfo.h"
49 #include "RenderButton.h"
50 #include "RenderChildIterator.h"
51 #include "RenderCombineText.h"
52 #include "RenderDeprecatedFlexibleBox.h"
53 #include "RenderFlexibleBox.h"
54 #include "RenderInline.h"
55 #include "RenderIterator.h"
56 #include "RenderLayer.h"
57 #include "RenderListMarker.h"
58 #include "RenderMenuList.h"
59 #include "RenderNamedFlowFragment.h"
60 #include "RenderNamedFlowThread.h"
61 #include "RenderRegion.h"
62 #include "RenderSVGResourceClipper.h"
63 #include "RenderTableCell.h"
64 #include "RenderTextFragment.h"
65 #include "RenderTheme.h"
66 #include "RenderTreePosition.h"
67 #include "RenderView.h"
68 #include "Settings.h"
69 #include "ShadowRoot.h"
70 #include "ShapeOutsideInfo.h"
71 #include <wtf/text/TextBreakIterator.h>
72 #include "TransformState.h"
73
74 #include <wtf/NeverDestroyed.h>
75 #include <wtf/Optional.h>
76 #include <wtf/StackStats.h>
77 #include <wtf/TemporaryChange.h>
78
79 using namespace WTF;
80 using namespace Unicode;
81
82 namespace WebCore {
83
84 using namespace HTMLNames;
85
86 struct SameSizeAsRenderBlock : public RenderBox {
87 };
88
89 COMPILE_ASSERT(sizeof(RenderBlock) == sizeof(SameSizeAsRenderBlock), RenderBlock_should_stay_small);
90
91 typedef HashMap<const RenderBlock*, std::unique_ptr<TrackedRendererListHashSet>> TrackedDescendantsMap;
92 typedef HashMap<const RenderBox*, std::unique_ptr<HashSet<const RenderBlock*>>> TrackedContainerMap;
93
94 static TrackedDescendantsMap* percentHeightDescendantsMap;
95 static TrackedContainerMap* percentHeightContainerMap;
96
97 static void insertIntoTrackedRendererMaps(const RenderBlock& container, RenderBox& descendant)
98 {
99     if (!percentHeightDescendantsMap) {
100         percentHeightDescendantsMap = new TrackedDescendantsMap;
101         percentHeightContainerMap = new TrackedContainerMap;
102     }
103     
104     auto& descendantSet = percentHeightDescendantsMap->ensure(&container, [] {
105         return std::make_unique<TrackedRendererListHashSet>();
106     }).iterator->value;
107
108     bool added = descendantSet->add(&descendant).isNewEntry;
109     if (!added) {
110         ASSERT(percentHeightContainerMap->get(&descendant));
111         ASSERT(percentHeightContainerMap->get(&descendant)->contains(&container));
112         return;
113     }
114     
115     auto& containerSet = percentHeightContainerMap->ensure(&descendant, [] {
116         return std::make_unique<HashSet<const RenderBlock*>>();
117     }).iterator->value;
118
119     ASSERT(!containerSet->contains(&container));
120     containerSet->add(&container);
121 }
122
123 static void removeFromTrackedRendererMaps(RenderBox& descendant)
124 {
125     if (!percentHeightDescendantsMap)
126         return;
127     
128     std::unique_ptr<HashSet<const RenderBlock*>> containerSet = percentHeightContainerMap->take(&descendant);
129     if (!containerSet)
130         return;
131     
132     for (auto* container : *containerSet) {
133         // FIXME: Disabling this assert temporarily until we fix the layout
134         // bugs associated with positioned objects not properly cleared from
135         // their ancestor chain before being moved. See webkit bug 93766.
136         // ASSERT(descendant->isDescendantOf(container));
137         auto descendantsMapIterator = percentHeightDescendantsMap->find(container);
138         ASSERT(descendantsMapIterator != percentHeightDescendantsMap->end());
139         if (descendantsMapIterator == percentHeightDescendantsMap->end())
140             continue;
141         auto& descendantSet = descendantsMapIterator->value;
142         ASSERT(descendantSet->contains(&descendant));
143         descendantSet->remove(&descendant);
144         if (descendantSet->isEmpty())
145             percentHeightDescendantsMap->remove(descendantsMapIterator);
146     }
147 }
148
149 class PositionedDescendantsMap {
150 public:
151     enum class MoveDescendantToEnd { No, Yes };
152     void addDescendant(const RenderBlock& containingBlock, RenderBox& positionedDescendant, MoveDescendantToEnd moveDescendantToEnd)
153     {
154         // Protect against double insert where a descendant would end up with multiple containing blocks.
155         auto* previousContainingBlock = m_containerMap.get(&positionedDescendant);
156         if (previousContainingBlock && previousContainingBlock != &containingBlock) {
157             if (auto* descendants = m_descendantsMap.get(previousContainingBlock))
158                 descendants->remove(&positionedDescendant);
159         }
160
161         auto& descendants = m_descendantsMap.ensure(&containingBlock, [] {
162             return std::make_unique<TrackedRendererListHashSet>();
163         }).iterator->value;
164
165         bool isNewEntry = moveDescendantToEnd == MoveDescendantToEnd::Yes ? descendants->appendOrMoveToLast(&positionedDescendant).isNewEntry
166             : descendants->add(&positionedDescendant).isNewEntry;
167         if (!isNewEntry) {
168             ASSERT(m_containerMap.contains(&positionedDescendant));
169             return;
170         }
171         m_containerMap.set(&positionedDescendant, &containingBlock);
172     }
173
174     void removeDescendant(const RenderBox& positionedDescendant)
175     {
176         auto* containingBlock = m_containerMap.take(&positionedDescendant);
177         if (!containingBlock)
178             return;
179
180         auto descendantsIterator = m_descendantsMap.find(containingBlock);
181         ASSERT(descendantsIterator != m_descendantsMap.end());
182         if (descendantsIterator == m_descendantsMap.end())
183             return;
184
185         auto& descendants = descendantsIterator->value;
186         ASSERT(descendants->contains(const_cast<RenderBox*>(&positionedDescendant)));
187
188         descendants->remove(const_cast<RenderBox*>(&positionedDescendant));
189         if (descendants->isEmpty())
190             m_descendantsMap.remove(descendantsIterator);
191     }
192     
193     void removeContainingBlock(const RenderBlock& containingBlock)
194     {
195         auto descendants = m_descendantsMap.take(&containingBlock);
196         if (!descendants)
197             return;
198
199         for (auto* renderer : *descendants)
200             m_containerMap.remove(renderer);
201     }
202     
203     TrackedRendererListHashSet* positionedRenderers(const RenderBlock& containingBlock) const
204     {
205         return m_descendantsMap.get(&containingBlock);
206     }
207
208 private:
209     using DescendantsMap = HashMap<const RenderBlock*, std::unique_ptr<TrackedRendererListHashSet>>;
210     using ContainerMap = HashMap<const RenderBox*, const RenderBlock*>;
211     
212     DescendantsMap m_descendantsMap;
213     ContainerMap m_containerMap;
214 };
215
216 static PositionedDescendantsMap& positionedDescendantsMap()
217 {
218     static NeverDestroyed<PositionedDescendantsMap> mapForPositionedDescendants;
219     return mapForPositionedDescendants;
220 }
221
222 typedef HashMap<RenderBlock*, std::unique_ptr<ListHashSet<RenderInline*>>> ContinuationOutlineTableMap;
223
224 struct UpdateScrollInfoAfterLayoutTransaction {
225     UpdateScrollInfoAfterLayoutTransaction(const RenderView& view)
226         : nestedCount(0)
227         , view(&view)
228     {
229     }
230
231     int nestedCount;
232     const RenderView* view;
233     HashSet<RenderBlock*> blocks;
234 };
235
236 typedef Vector<UpdateScrollInfoAfterLayoutTransaction> DelayedUpdateScrollInfoStack;
237 static std::unique_ptr<DelayedUpdateScrollInfoStack>& updateScrollInfoAfterLayoutTransactionStack()
238 {
239     static NeverDestroyed<std::unique_ptr<DelayedUpdateScrollInfoStack>> delayedUpdatedScrollInfoStack;
240     return delayedUpdatedScrollInfoStack;
241 }
242
243 // Allocated only when some of these fields have non-default values
244
245 struct RenderBlockRareData {
246     WTF_MAKE_NONCOPYABLE(RenderBlockRareData); WTF_MAKE_FAST_ALLOCATED;
247 public:
248     RenderBlockRareData()
249         : m_paginationStrut(0)
250         , m_pageLogicalOffset(0)
251         , m_flowThreadContainingBlock(Nullopt)
252     {
253     }
254
255     LayoutUnit m_paginationStrut;
256     LayoutUnit m_pageLogicalOffset;
257
258     Optional<RenderFlowThread*> m_flowThreadContainingBlock;
259 };
260
261 typedef HashMap<const RenderBlock*, std::unique_ptr<RenderBlockRareData>> RenderBlockRareDataMap;
262 static RenderBlockRareDataMap* gRareDataMap = 0;
263
264 // This class helps dispatching the 'overflow' event on layout change. overflow can be set on RenderBoxes, yet the existing code
265 // only works on RenderBlocks. If this change, this class should be shared with other RenderBoxes.
266 class OverflowEventDispatcher {
267     WTF_MAKE_NONCOPYABLE(OverflowEventDispatcher);
268 public:
269     OverflowEventDispatcher(const RenderBlock* block)
270         : m_block(block)
271         , m_hadHorizontalLayoutOverflow(false)
272         , m_hadVerticalLayoutOverflow(false)
273     {
274         m_shouldDispatchEvent = !m_block->isAnonymous() && m_block->hasOverflowClip() && m_block->document().hasListenerType(Document::OVERFLOWCHANGED_LISTENER);
275         if (m_shouldDispatchEvent) {
276             m_hadHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
277             m_hadVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
278         }
279     }
280
281     ~OverflowEventDispatcher()
282     {
283         if (!m_shouldDispatchEvent)
284             return;
285
286         bool hasHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
287         bool hasVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
288
289         bool horizontalLayoutOverflowChanged = hasHorizontalLayoutOverflow != m_hadHorizontalLayoutOverflow;
290         bool verticalLayoutOverflowChanged = hasVerticalLayoutOverflow != m_hadVerticalLayoutOverflow;
291         if (!horizontalLayoutOverflowChanged && !verticalLayoutOverflowChanged)
292             return;
293
294         Ref<OverflowEvent> overflowEvent = OverflowEvent::create(horizontalLayoutOverflowChanged, hasHorizontalLayoutOverflow, verticalLayoutOverflowChanged, hasVerticalLayoutOverflow);
295         overflowEvent->setTarget(m_block->element());
296         m_block->document().enqueueOverflowEvent(WTFMove(overflowEvent));
297     }
298
299 private:
300     const RenderBlock* m_block;
301     bool m_shouldDispatchEvent;
302     bool m_hadHorizontalLayoutOverflow;
303     bool m_hadVerticalLayoutOverflow;
304 };
305
306 RenderBlock::RenderBlock(Element& element, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
307     : RenderBox(element, WTFMove(style), baseTypeFlags | RenderBlockFlag)
308 {
309 }
310
311 RenderBlock::RenderBlock(Document& document, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
312     : RenderBox(document, WTFMove(style), baseTypeFlags | RenderBlockFlag)
313 {
314 }
315
316 static void removeBlockFromPercentageDescendantAndContainerMaps(RenderBlock* block)
317 {
318     if (!percentHeightDescendantsMap)
319         return;
320     std::unique_ptr<TrackedRendererListHashSet> descendantSet = percentHeightDescendantsMap->take(block);
321     if (!descendantSet)
322         return;
323     
324     for (auto* descendant : *descendantSet) {
325         auto it = percentHeightContainerMap->find(descendant);
326         ASSERT(it != percentHeightContainerMap->end());
327         if (it == percentHeightContainerMap->end())
328             continue;
329         auto* containerSet = it->value.get();
330         ASSERT(containerSet->contains(block));
331         containerSet->remove(block);
332         if (containerSet->isEmpty())
333             percentHeightContainerMap->remove(it);
334     }
335 }
336
337 RenderBlock::~RenderBlock()
338 {
339     removeFromUpdateScrollInfoAfterLayoutTransaction();
340
341     if (gRareDataMap)
342         gRareDataMap->remove(this);
343     removeBlockFromPercentageDescendantAndContainerMaps(this);
344     positionedDescendantsMap().removeContainingBlock(*this);
345 }
346
347 void RenderBlock::willBeDestroyed()
348 {
349     if (!documentBeingDestroyed()) {
350         if (parent())
351             parent()->dirtyLinesFromChangedChild(*this);
352     }
353
354     RenderBox::willBeDestroyed();
355 }
356
357 bool RenderBlock::hasRareData() const
358 {
359     return gRareDataMap ? gRareDataMap->contains(this) : false;
360 }
361
362 void RenderBlock::removePositionedObjectsIfNeeded(const RenderStyle& oldStyle, const RenderStyle& newStyle)
363 {
364     bool hadTransform = oldStyle.hasTransformRelatedProperty();
365     bool willHaveTransform = newStyle.hasTransformRelatedProperty();
366     if (oldStyle.position() == newStyle.position() && hadTransform == willHaveTransform)
367         return;
368
369     // We are no longer the containing block for fixed descendants.
370     if (hadTransform && !willHaveTransform) {
371         // Our positioned descendants will be inserted into a new containing block's positioned objects list during the next layout.
372         removePositionedObjects(nullptr, NewContainingBlock);
373         return;
374     }
375
376     // We are no longer the containing block for absolute positioned descendants.
377     if (newStyle.position() == StaticPosition && !willHaveTransform) {
378         // Our positioned descendants will be inserted into a new containing block's positioned objects list during the next layout.
379         removePositionedObjects(nullptr, NewContainingBlock);
380         return;
381     }
382
383     // We are a new containing block.
384     if (oldStyle.position() == StaticPosition && !hadTransform) {
385         // Remove our absolutely positioned descendants from their current containing block.
386         // They will be inserted into our positioned objects list during layout.
387         auto* containingBlock = parent();
388         while (containingBlock && !is<RenderView>(*containingBlock)
389             && (containingBlock->style().position() == StaticPosition || (containingBlock->isInline() && !containingBlock->isReplaced()))) {
390             if (containingBlock->style().position() == RelativePosition && containingBlock->isInline() && !containingBlock->isReplaced()) {
391                 containingBlock = containingBlock->containingBlock();
392                 break;
393             }
394             containingBlock = containingBlock->parent();
395         }
396         if (containingBlock && is<RenderBlock>(*containingBlock))
397             downcast<RenderBlock>(*containingBlock).removePositionedObjects(this, NewContainingBlock);
398     }
399 }
400
401 void RenderBlock::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
402 {
403     const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
404     setReplaced(newStyle.isDisplayInlineType());
405     if (oldStyle)
406         removePositionedObjectsIfNeeded(*oldStyle, newStyle);
407     RenderBox::styleWillChange(diff, newStyle);
408 }
409
410 static bool borderOrPaddingLogicalWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
411 {
412     if (newStyle->isHorizontalWritingMode())
413         return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
414             || oldStyle->borderRightWidth() != newStyle->borderRightWidth()
415             || oldStyle->paddingLeft() != newStyle->paddingLeft()
416             || oldStyle->paddingRight() != newStyle->paddingRight();
417
418     return oldStyle->borderTopWidth() != newStyle->borderTopWidth()
419         || oldStyle->borderBottomWidth() != newStyle->borderBottomWidth()
420         || oldStyle->paddingTop() != newStyle->paddingTop()
421         || oldStyle->paddingBottom() != newStyle->paddingBottom();
422 }
423
424 void RenderBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
425 {
426     bool hadTransform = hasTransform();
427     RenderBox::styleDidChange(diff, oldStyle);
428
429     if (hadTransform != hasTransform())
430         resetFlowThreadContainingBlockAndChildInfoIncludingDescendants();
431
432     auto& newStyle = style();
433     if (!isAnonymousBlock()) {
434         // Ensure that all of our continuation blocks pick up the new style.
435         for (RenderBlock* currCont = blockElementContinuation(); currCont; currCont = currCont->blockElementContinuation()) {
436             RenderBoxModelObject* nextCont = currCont->continuation();
437             currCont->setContinuation(0);
438             currCont->setStyle(RenderStyle::clone(newStyle));
439             currCont->setContinuation(nextCont);
440         }
441     }
442
443     propagateStyleToAnonymousChildren(PropagateToBlockChildrenOnly);
444
445     // It's possible for our border/padding to change, but for the overall logical width of the block to
446     // end up being the same. We keep track of this change so in layoutBlock, we can know to set relayoutChildren=true.
447     setShouldForceRelayoutChildren(oldStyle && diff == StyleDifferenceLayout && needsLayout() && borderOrPaddingLogicalWidthChanged(oldStyle, &newStyle));
448 }
449
450 RenderBlock* RenderBlock::continuationBefore(RenderObject* beforeChild)
451 {
452     if (beforeChild && beforeChild->parent() == this)
453         return this;
454
455     RenderBlock* nextToLast = this;
456     RenderBlock* last = this;
457     for (auto* current = downcast<RenderBlock>(continuation()); current; current = downcast<RenderBlock>(current->continuation())) {
458         if (beforeChild && beforeChild->parent() == current) {
459             if (current->firstChild() == beforeChild)
460                 return last;
461             return current;
462         }
463
464         nextToLast = last;
465         last = current;
466     }
467
468     if (!beforeChild && !last->firstChild())
469         return nextToLast;
470     return last;
471 }
472
473 void RenderBlock::addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild)
474 {
475     RenderBlock* flow = continuationBefore(beforeChild);
476     ASSERT(!beforeChild || is<RenderBlock>(*beforeChild->parent()));
477     RenderBoxModelObject* beforeChildParent = nullptr;
478     if (beforeChild)
479         beforeChildParent = downcast<RenderBoxModelObject>(beforeChild->parent());
480     else {
481         RenderBoxModelObject* continuation = flow->continuation();
482         if (continuation)
483             beforeChildParent = continuation;
484         else
485             beforeChildParent = flow;
486     }
487
488     if (newChild->isFloatingOrOutOfFlowPositioned()) {
489         beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
490         return;
491     }
492
493     bool childIsNormal = newChild->isInline() || !newChild->style().columnSpan();
494     bool bcpIsNormal = beforeChildParent->isInline() || !beforeChildParent->style().columnSpan();
495     bool flowIsNormal = flow->isInline() || !flow->style().columnSpan();
496
497     if (flow == beforeChildParent) {
498         flow->addChildIgnoringContinuation(newChild, beforeChild);
499         return;
500     }
501     
502     // The goal here is to match up if we can, so that we can coalesce and create the
503     // minimal # of continuations needed for the inline.
504     if (childIsNormal == bcpIsNormal) {
505         beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
506         return;
507     }
508     if (flowIsNormal == childIsNormal) {
509         flow->addChildIgnoringContinuation(newChild, 0); // Just treat like an append.
510         return;
511     }
512     beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
513 }
514
515 RenderPtr<RenderBlock> RenderBlock::clone() const
516 {
517     RenderPtr<RenderBlock> cloneBlock;
518     if (isAnonymousBlock()) {
519         cloneBlock = RenderPtr<RenderBlock>(createAnonymousBlock());
520         cloneBlock->setChildrenInline(childrenInline());
521     } else {
522         RenderTreePosition insertionPosition(*parent());
523         cloneBlock = static_pointer_cast<RenderBlock>(element()->createElementRenderer(RenderStyle::clone(style()), insertionPosition));
524         cloneBlock->initializeStyle();
525
526         // This takes care of setting the right value of childrenInline in case
527         // generated content is added to cloneBlock and 'this' does not have
528         // generated content added yet.
529         cloneBlock->setChildrenInline(cloneBlock->firstChild() ? cloneBlock->firstChild()->isInline() : childrenInline());
530     }
531     cloneBlock->setFlowThreadState(flowThreadState());
532     return cloneBlock;
533 }
534
535 void RenderBlock::addChild(RenderObject* newChild, RenderObject* beforeChild)
536 {
537     if (continuation() && !isAnonymousBlock())
538         addChildToContinuation(newChild, beforeChild);
539     else
540         addChildIgnoringContinuation(newChild, beforeChild);
541 }
542
543 void RenderBlock::addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild)
544 {
545     if (beforeChild && beforeChild->parent() != this) {
546         RenderElement* beforeChildContainer = beforeChild->parent();
547         while (beforeChildContainer->parent() != this)
548             beforeChildContainer = beforeChildContainer->parent();
549         ASSERT(beforeChildContainer);
550
551         if (beforeChildContainer->isAnonymous()) {
552             // If the requested beforeChild is not one of our children, then this is because
553             // there is an anonymous container within this object that contains the beforeChild.
554             RenderElement* beforeChildAnonymousContainer = beforeChildContainer;
555             if (beforeChildAnonymousContainer->isAnonymousBlock()
556 #if ENABLE(FULLSCREEN_API)
557                 // Full screen renderers and full screen placeholders act as anonymous blocks, not tables:
558                 || beforeChildAnonymousContainer->isRenderFullScreen()
559                 || beforeChildAnonymousContainer->isRenderFullScreenPlaceholder()
560 #endif
561                 ) {
562                 // Insert the child into the anonymous block box instead of here.
563                 if (newChild->isInline() || beforeChild->parent()->firstChild() != beforeChild)
564                     beforeChild->parent()->addChild(newChild, beforeChild);
565                 else
566                     addChild(newChild, beforeChild->parent());
567                 return;
568             }
569
570             ASSERT(beforeChildAnonymousContainer->isTable());
571             if (newChild->isTablePart()) {
572                 // Insert into the anonymous table.
573                 beforeChildAnonymousContainer->addChild(newChild, beforeChild);
574                 return;
575             }
576
577             beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
578
579             ASSERT(beforeChild->parent() == this);
580             if (beforeChild->parent() != this) {
581                 // We should never reach here. If we do, we need to use the
582                 // safe fallback to use the topmost beforeChild container.
583                 beforeChild = beforeChildContainer;
584             }
585         }
586     }
587
588     bool madeBoxesNonInline = false;
589
590     // A block has to either have all of its children inline, or all of its children as blocks.
591     // So, if our children are currently inline and a block child has to be inserted, we move all our
592     // inline children into anonymous block boxes.
593     if (childrenInline() && !newChild->isInline() && !newChild->isFloatingOrOutOfFlowPositioned()) {
594         // This is a block with inline content. Wrap the inline content in anonymous blocks.
595         makeChildrenNonInline(beforeChild);
596         madeBoxesNonInline = true;
597
598         if (beforeChild && beforeChild->parent() != this) {
599             beforeChild = beforeChild->parent();
600             ASSERT(beforeChild->isAnonymousBlock());
601             ASSERT(beforeChild->parent() == this);
602         }
603     } else if (!childrenInline() && (newChild->isFloatingOrOutOfFlowPositioned() || newChild->isInline())) {
604         // If we're inserting an inline child but all of our children are blocks, then we have to make sure
605         // it is put into an anomyous block box. We try to use an existing anonymous box if possible, otherwise
606         // a new one is created and inserted into our list of children in the appropriate position.
607         RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : lastChild();
608
609         if (afterChild && afterChild->isAnonymousBlock()) {
610             downcast<RenderBlock>(*afterChild).addChild(newChild);
611             return;
612         }
613
614         if (newChild->isInline()) {
615             // No suitable existing anonymous box - create a new one.
616             RenderBlock* newBox = createAnonymousBlock();
617             RenderBox::addChild(newBox, beforeChild);
618             newBox->addChild(newChild);
619             return;
620         }
621     }
622
623     invalidateLineLayoutPath();
624
625     RenderBox::addChild(newChild, beforeChild);
626  
627     if (madeBoxesNonInline && is<RenderBlock>(parent()) && isAnonymousBlock())
628         downcast<RenderBlock>(*parent()).removeLeftoverAnonymousBlock(this);
629     // this object may be dead here
630 }
631
632 static void getInlineRun(RenderObject* start, RenderObject* boundary,
633                          RenderObject*& inlineRunStart,
634                          RenderObject*& inlineRunEnd)
635 {
636     // Beginning at |start| we find the largest contiguous run of inlines that
637     // we can.  We denote the run with start and end points, |inlineRunStart|
638     // and |inlineRunEnd|.  Note that these two values may be the same if
639     // we encounter only one inline.
640     //
641     // We skip any non-inlines we encounter as long as we haven't found any
642     // inlines yet.
643     //
644     // |boundary| indicates a non-inclusive boundary point.  Regardless of whether |boundary|
645     // is inline or not, we will not include it in a run with inlines before it.  It's as though we encountered
646     // a non-inline.
647     
648     // Start by skipping as many non-inlines as we can.
649     RenderObject * curr = start;
650     bool sawInline;
651     do {
652         while (curr && !(curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()))
653             curr = curr->nextSibling();
654         
655         inlineRunStart = inlineRunEnd = curr;
656         
657         if (!curr)
658             return; // No more inline children to be found.
659         
660         sawInline = curr->isInline();
661         
662         curr = curr->nextSibling();
663         while (curr && (curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()) && (curr != boundary)) {
664             inlineRunEnd = curr;
665             if (curr->isInline())
666                 sawInline = true;
667             curr = curr->nextSibling();
668         }
669     } while (!sawInline);
670 }
671
672 void RenderBlock::deleteLines()
673 {
674     if (AXObjectCache* cache = document().existingAXObjectCache())
675         cache->recomputeIsIgnored(this);
676 }
677
678 void RenderBlock::makeChildrenNonInline(RenderObject* insertionPoint)
679 {    
680     // makeChildrenNonInline takes a block whose children are *all* inline and it
681     // makes sure that inline children are coalesced under anonymous
682     // blocks.  If |insertionPoint| is defined, then it represents the insertion point for
683     // the new block child that is causing us to have to wrap all the inlines.  This
684     // means that we cannot coalesce inlines before |insertionPoint| with inlines following
685     // |insertionPoint|, because the new child is going to be inserted in between the inlines,
686     // splitting them.
687     ASSERT(isInlineBlockOrInlineTable() || !isInline());
688     ASSERT(!insertionPoint || insertionPoint->parent() == this);
689
690     setChildrenInline(false);
691
692     RenderObject* child = firstChild();
693     if (!child)
694         return;
695
696     deleteLines();
697
698     while (child) {
699         RenderObject* inlineRunStart;
700         RenderObject* inlineRunEnd;
701         getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
702
703         if (!inlineRunStart)
704             break;
705
706         child = inlineRunEnd->nextSibling();
707
708         RenderBlock* block = createAnonymousBlock();
709         insertChildInternal(block, inlineRunStart, NotifyChildren);
710         moveChildrenTo(block, inlineRunStart, child);
711     }
712
713 #ifndef NDEBUG
714     for (RenderObject* c = firstChild(); c; c = c->nextSibling())
715         ASSERT(!c->isInline());
716 #endif
717
718     repaint();
719 }
720
721 void RenderBlock::removeLeftoverAnonymousBlock(RenderBlock* child)
722 {
723     ASSERT(child->isAnonymousBlock());
724     ASSERT(!child->childrenInline());
725     
726     if (child->continuation())
727         return;
728     
729     RenderObject* firstAnChild = child->firstChild();
730     RenderObject* lastAnChild = child->lastChild();
731     if (firstAnChild) {
732         RenderObject* o = firstAnChild;
733         while (o) {
734             o->setParent(this);
735             o = o->nextSibling();
736         }
737         firstAnChild->setPreviousSibling(child->previousSibling());
738         lastAnChild->setNextSibling(child->nextSibling());
739         if (child->previousSibling())
740             child->previousSibling()->setNextSibling(firstAnChild);
741         if (child->nextSibling())
742             child->nextSibling()->setPreviousSibling(lastAnChild);
743             
744         if (child == firstChild())
745             setFirstChild(firstAnChild);
746         if (child == lastChild())
747             setLastChild(lastAnChild);
748     } else {
749         if (child == firstChild())
750             setFirstChild(child->nextSibling());
751         if (child == lastChild())
752             setLastChild(child->previousSibling());
753
754         if (child->previousSibling())
755             child->previousSibling()->setNextSibling(child->nextSibling());
756         if (child->nextSibling())
757             child->nextSibling()->setPreviousSibling(child->previousSibling());
758     }
759
760     child->setFirstChild(0);
761     child->m_next = 0;
762
763     // Remove all the information in the flow thread associated with the leftover anonymous block.
764     child->resetFlowThreadStateOnRemoval();
765
766     child->setParent(0);
767     child->setPreviousSibling(0);
768     child->setNextSibling(0);
769
770     child->destroy();
771 }
772
773 static bool canDropAnonymousBlock(const RenderBlock& anonymousBlock)
774 {
775     if (anonymousBlock.beingDestroyed() || anonymousBlock.continuation())
776         return false;
777     if (anonymousBlock.isRubyRun() || anonymousBlock.isRubyBase())
778         return false;
779     return true;
780 }
781
782 static bool canMergeContiguousAnonymousBlocks(RenderObject& oldChild, RenderObject* previous, RenderObject* next)
783 {
784     if (oldChild.documentBeingDestroyed() || oldChild.isInline() || oldChild.virtualContinuation())
785         return false;
786
787     if (previous) {
788         if (!previous->isAnonymousBlock())
789             return false;
790         RenderBlock& previousAnonymousBlock = downcast<RenderBlock>(*previous);
791         if (!canDropAnonymousBlock(previousAnonymousBlock))
792             return false;
793     }
794     if (next) {
795         if (!next->isAnonymousBlock())
796             return false;
797         RenderBlock& nextAnonymousBlock = downcast<RenderBlock>(*next);
798         if (!canDropAnonymousBlock(nextAnonymousBlock))
799             return false;
800     }
801     return true;
802 }
803
804 void RenderBlock::dropAnonymousBoxChild(RenderBlock& parent, RenderBlock& child)
805 {
806     parent.setNeedsLayoutAndPrefWidthsRecalc();
807     parent.setChildrenInline(child.childrenInline());
808     RenderObject* nextSibling = child.nextSibling();
809     parent.removeChildInternal(child, child.hasLayer() ? NotifyChildren : DontNotifyChildren);
810     child.moveAllChildrenTo(&parent, nextSibling, child.hasLayer());
811     // Delete the now-empty block's lines and nuke it.
812     child.deleteLines();
813     child.destroy();
814 }
815
816 void RenderBlock::removeChild(RenderObject& oldChild)
817 {
818     // No need to waste time in merging or removing empty anonymous blocks.
819     // We can just bail out if our document is getting destroyed.
820     if (documentBeingDestroyed()) {
821         RenderBox::removeChild(oldChild);
822         return;
823     }
824
825     // If this child is a block, and if our previous and next siblings are both anonymous blocks
826     // with inline content, then we can fold the inline content back together.
827     RenderObject* prev = oldChild.previousSibling();
828     RenderObject* next = oldChild.nextSibling();
829     bool canMergeAnonymousBlocks = canMergeContiguousAnonymousBlocks(oldChild, prev, next);
830     if (canMergeAnonymousBlocks && prev && next) {
831         prev->setNeedsLayoutAndPrefWidthsRecalc();
832         RenderBlock& nextBlock = downcast<RenderBlock>(*next);
833         RenderBlock& prevBlock = downcast<RenderBlock>(*prev);
834        
835         if (prev->childrenInline() != next->childrenInline()) {
836             RenderBlock& inlineChildrenBlock = prev->childrenInline() ? prevBlock : nextBlock;
837             RenderBlock& blockChildrenBlock = prev->childrenInline() ? nextBlock : prevBlock;
838             
839             // Place the inline children block inside of the block children block instead of deleting it.
840             // In order to reuse it, we have to reset it to just be a generic anonymous block.  Make sure
841             // to clear out inherited column properties by just making a new style, and to also clear the
842             // column span flag if it is set.
843             ASSERT(!inlineChildrenBlock.continuation());
844             // Cache this value as it might get changed in setStyle() call.
845             bool inlineChildrenBlockHasLayer = inlineChildrenBlock.hasLayer();
846             inlineChildrenBlock.setStyle(RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK));
847             removeChildInternal(inlineChildrenBlock, inlineChildrenBlockHasLayer ? NotifyChildren : DontNotifyChildren);
848             
849             // Now just put the inlineChildrenBlock inside the blockChildrenBlock.
850             RenderObject* beforeChild = prev == &inlineChildrenBlock ? blockChildrenBlock.firstChild() : nullptr;
851             blockChildrenBlock.insertChildInternal(&inlineChildrenBlock, beforeChild,
852                 (inlineChildrenBlockHasLayer || blockChildrenBlock.hasLayer()) ? NotifyChildren : DontNotifyChildren);
853             next->setNeedsLayoutAndPrefWidthsRecalc();
854             
855             // inlineChildrenBlock got reparented to blockChildrenBlock, so it is no longer a child
856             // of "this". we null out prev or next so that is not used later in the function.
857             if (&inlineChildrenBlock == &prevBlock)
858                 prev = nullptr;
859             else
860                 next = nullptr;
861         } else {
862             // Take all the children out of the |next| block and put them in
863             // the |prev| block.
864             nextBlock.moveAllChildrenIncludingFloatsTo(prevBlock, nextBlock.hasLayer() || prevBlock.hasLayer());
865             
866             // Delete the now-empty block's lines and nuke it.
867             nextBlock.deleteLines();
868             nextBlock.destroy();
869             next = nullptr;
870         }
871     }
872
873     invalidateLineLayoutPath();
874
875     RenderBox::removeChild(oldChild);
876
877     RenderObject* child = prev ? prev : next;
878     if (canMergeAnonymousBlocks && child && !child->previousSibling() && !child->nextSibling() && canDropAnonymousBlockChild()) {
879         // The removal has knocked us down to containing only a single anonymous
880         // box. We can pull the content right back up into our box.
881         dropAnonymousBoxChild(*this, downcast<RenderBlock>(*child));
882     } else if (((prev && prev->isAnonymousBlock()) || (next && next->isAnonymousBlock())) && canDropAnonymousBlockChild()) {
883         // It's possible that the removal has knocked us down to a single anonymous
884         // block with floating siblings.
885         RenderBlock& anonBlock = downcast<RenderBlock>((prev && prev->isAnonymousBlock()) ? *prev : *next);
886         if (canDropAnonymousBlock(anonBlock)) {
887             bool dropAnonymousBlock = true;
888             for (auto& sibling : childrenOfType<RenderObject>(*this)) {
889                 if (&sibling == &anonBlock)
890                     continue;
891                 if (!sibling.isFloating()) {
892                     dropAnonymousBlock = false;
893                     break;
894                 }
895             }
896             if (dropAnonymousBlock)
897                 dropAnonymousBoxChild(*this, anonBlock);
898         }
899     }
900
901     if (!firstChild()) {
902         // If this was our last child be sure to clear out our line boxes.
903         if (childrenInline())
904             deleteLines();
905
906         // If we are an empty anonymous block in the continuation chain,
907         // we need to remove ourself and fix the continuation chain.
908         if (!beingDestroyed() && isAnonymousBlockContinuation() && !oldChild.isListMarker()) {
909             auto containingBlockIgnoringAnonymous = containingBlock();
910             while (containingBlockIgnoringAnonymous && containingBlockIgnoringAnonymous->isAnonymousBlock())
911                 containingBlockIgnoringAnonymous = containingBlockIgnoringAnonymous->containingBlock();
912             for (RenderObject* current = this; current; current = current->previousInPreOrder(containingBlockIgnoringAnonymous)) {
913                 if (current->virtualContinuation() != this)
914                     continue;
915
916                 // Found our previous continuation. We just need to point it to
917                 // |this|'s next continuation.
918                 RenderBoxModelObject* nextContinuation = continuation();
919                 if (is<RenderInline>(*current))
920                     downcast<RenderInline>(*current).setContinuation(nextContinuation);
921                 else if (is<RenderBlock>(*current))
922                     downcast<RenderBlock>(*current).setContinuation(nextContinuation);
923                 else
924                     ASSERT_NOT_REACHED();
925
926                 break;
927             }
928             setContinuation(nullptr);
929             destroy();
930         }
931     }
932 }
933
934 bool RenderBlock::childrenPreventSelfCollapsing() const
935 {
936     // Whether or not we collapse is dependent on whether all our normal flow children
937     // are also self-collapsing.
938     for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
939         if (child->isFloatingOrOutOfFlowPositioned())
940             continue;
941         if (!child->isSelfCollapsingBlock())
942             return true;
943     }
944     return false;
945 }
946
947 bool RenderBlock::isSelfCollapsingBlock() const
948 {
949     // We are not self-collapsing if we
950     // (a) have a non-zero height according to layout (an optimization to avoid wasting time)
951     // (b) are a table,
952     // (c) have border/padding,
953     // (d) have a min-height
954     // (e) have specified that one of our margins can't collapse using a CSS extension
955     if (logicalHeight() > 0
956         || isTable() || borderAndPaddingLogicalHeight()
957         || style().logicalMinHeight().isPositive()
958         || style().marginBeforeCollapse() == MSEPARATE || style().marginAfterCollapse() == MSEPARATE)
959         return false;
960
961     Length logicalHeightLength = style().logicalHeight();
962     bool hasAutoHeight = logicalHeightLength.isAuto();
963     if (logicalHeightLength.isPercentOrCalculated() && !document().inQuirksMode()) {
964         hasAutoHeight = true;
965         for (RenderBlock* cb = containingBlock(); cb && !is<RenderView>(*cb); cb = cb->containingBlock()) {
966             if (cb->style().logicalHeight().isFixed() || cb->isTableCell())
967                 hasAutoHeight = false;
968         }
969     }
970
971     // If the height is 0 or auto, then whether or not we are a self-collapsing block depends
972     // on whether we have content that is all self-collapsing or not.
973     if (hasAutoHeight || ((logicalHeightLength.isFixed() || logicalHeightLength.isPercentOrCalculated()) && logicalHeightLength.isZero()))
974         return !childrenPreventSelfCollapsing();
975
976     return false;
977 }
978
979 static inline UpdateScrollInfoAfterLayoutTransaction* currentUpdateScrollInfoAfterLayoutTransaction()
980 {
981     if (!updateScrollInfoAfterLayoutTransactionStack())
982         return nullptr;
983     return &updateScrollInfoAfterLayoutTransactionStack()->last();
984 }
985
986 void RenderBlock::beginUpdateScrollInfoAfterLayoutTransaction()
987 {
988     if (!updateScrollInfoAfterLayoutTransactionStack())
989         updateScrollInfoAfterLayoutTransactionStack() = std::make_unique<DelayedUpdateScrollInfoStack>();
990     if (updateScrollInfoAfterLayoutTransactionStack()->isEmpty() || currentUpdateScrollInfoAfterLayoutTransaction()->view != &view())
991         updateScrollInfoAfterLayoutTransactionStack()->append(UpdateScrollInfoAfterLayoutTransaction(view()));
992     ++currentUpdateScrollInfoAfterLayoutTransaction()->nestedCount;
993 }
994
995 void RenderBlock::endAndCommitUpdateScrollInfoAfterLayoutTransaction()
996 {
997     UpdateScrollInfoAfterLayoutTransaction* transaction = currentUpdateScrollInfoAfterLayoutTransaction();
998     ASSERT(transaction);
999     ASSERT(transaction->view == &view());
1000     if (--transaction->nestedCount)
1001         return;
1002
1003     // Calling RenderLayer::updateScrollInfoAfterLayout() may cause its associated block to layout again and
1004     // updates its scroll info (i.e. call RenderBlock::updateScrollInfoAfterLayout()). We remove |transaction|
1005     // from the transaction stack to ensure that all subsequent calls to RenderBlock::updateScrollInfoAfterLayout()
1006     // are dispatched immediately. That is, to ensure that such subsequent calls aren't added to |transaction|
1007     // while we are processing it.
1008     Vector<RenderBlock*> blocksToUpdate;
1009     copyToVector(transaction->blocks, blocksToUpdate);
1010     updateScrollInfoAfterLayoutTransactionStack()->removeLast();
1011     if (updateScrollInfoAfterLayoutTransactionStack()->isEmpty())
1012         updateScrollInfoAfterLayoutTransactionStack() = nullptr;
1013
1014     for (auto* block : blocksToUpdate) {
1015         ASSERT(block->hasOverflowClip());
1016         block->layer()->updateScrollInfoAfterLayout();
1017         block->clearLayoutOverflow();
1018     }
1019 }
1020
1021 void RenderBlock::removeFromUpdateScrollInfoAfterLayoutTransaction()
1022 {
1023     if (UNLIKELY(updateScrollInfoAfterLayoutTransactionStack().get() != 0)) {
1024         UpdateScrollInfoAfterLayoutTransaction* transaction = currentUpdateScrollInfoAfterLayoutTransaction();
1025         ASSERT(transaction);
1026         if (transaction->view == &view())
1027             transaction->blocks.remove(this);
1028     }
1029 }
1030
1031 void RenderBlock::updateScrollInfoAfterLayout()
1032 {
1033     if (!hasOverflowClip())
1034         return;
1035     
1036     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=97937
1037     // Workaround for now. We cannot delay the scroll info for overflow
1038     // for items with opposite writing directions, as the contents needs
1039     // to overflow in that direction
1040     if (!style().isFlippedBlocksWritingMode()) {
1041         UpdateScrollInfoAfterLayoutTransaction* transaction = currentUpdateScrollInfoAfterLayoutTransaction();
1042         if (transaction && transaction->view == &view()) {
1043             transaction->blocks.add(this);
1044             return;
1045         }
1046     }
1047     if (layer())
1048         layer()->updateScrollInfoAfterLayout();
1049 }
1050
1051 void RenderBlock::layout()
1052 {
1053     StackStats::LayoutCheckPoint layoutCheckPoint;
1054     OverflowEventDispatcher dispatcher(this);
1055
1056     // Update our first letter info now.
1057     updateFirstLetter();
1058
1059     // Table cells call layoutBlock directly, so don't add any logic here.  Put code into
1060     // layoutBlock().
1061     layoutBlock(false);
1062     
1063     // It's safe to check for control clip here, since controls can never be table cells.
1064     // If we have a lightweight clip, there can never be any overflow from children.
1065     UpdateScrollInfoAfterLayoutTransaction* transaction = currentUpdateScrollInfoAfterLayoutTransaction();
1066     bool isDelayingUpdateScrollInfoAfterLayoutInView = transaction && transaction->view == &view();
1067     if (hasControlClip() && m_overflow && !isDelayingUpdateScrollInfoAfterLayoutInView)
1068         clearLayoutOverflow();
1069
1070     invalidateBackgroundObscurationStatus();
1071 }
1072
1073 static RenderBlockRareData* getBlockRareData(const RenderBlock* block)
1074 {
1075     return gRareDataMap ? gRareDataMap->get(block) : nullptr;
1076 }
1077
1078 static RenderBlockRareData& ensureBlockRareData(const RenderBlock* block)
1079 {
1080     if (!gRareDataMap)
1081         gRareDataMap = new RenderBlockRareDataMap;
1082     
1083     auto& rareData = gRareDataMap->add(block, nullptr).iterator->value;
1084     if (!rareData)
1085         rareData = std::make_unique<RenderBlockRareData>();
1086     return *rareData.get();
1087 }
1088
1089 void RenderBlock::preparePaginationBeforeBlockLayout(bool& relayoutChildren)
1090 {
1091     // Regions changing widths can force us to relayout our children.
1092     RenderFlowThread* flowThread = flowThreadContainingBlock();
1093     if (flowThread)
1094         flowThread->logicalWidthChangedInRegionsForBlock(this, relayoutChildren);
1095 }
1096
1097 bool RenderBlock::recomputeLogicalWidth()
1098 {
1099     LayoutUnit oldWidth = logicalWidth();
1100     
1101     updateLogicalWidth();
1102     
1103     bool hasBorderOrPaddingLogicalWidthChanged = this->hasBorderOrPaddingLogicalWidthChanged();
1104     setShouldForceRelayoutChildren(false);
1105
1106     return oldWidth != logicalWidth() || hasBorderOrPaddingLogicalWidthChanged;
1107 }
1108
1109 void RenderBlock::layoutBlock(bool, LayoutUnit)
1110 {
1111     ASSERT_NOT_REACHED();
1112     clearNeedsLayout();
1113 }
1114
1115 void RenderBlock::addOverflowFromChildren()
1116 {
1117     if (childrenInline())
1118         addOverflowFromInlineChildren();
1119     else
1120         addOverflowFromBlockChildren();
1121     
1122     // If this block is flowed inside a flow thread, make sure its overflow is propagated to the containing regions.
1123     if (m_overflow) {
1124         if (RenderFlowThread* containingFlowThread = flowThreadContainingBlock())
1125             containingFlowThread->addRegionsVisualOverflow(this, m_overflow->visualOverflowRect());
1126     }
1127 }
1128
1129 // Overflow is always relative to the border-box of the element in question.
1130 // Therefore, if the element has a vertical scrollbar placed on the left, an overflow rect at x=2px would conceptually intersect the scrollbar.
1131 void RenderBlock::computeOverflow(LayoutUnit oldClientAfterEdge, bool)
1132 {
1133     clearOverflow();
1134     addOverflowFromChildren();
1135
1136     addOverflowFromPositionedObjects();
1137
1138     if (hasOverflowClip()) {
1139         // When we have overflow clip, propagate the original spillout since it will include collapsed bottom margins
1140         // and bottom padding.  Set the axis we don't care about to be 1, since we want this overflow to always
1141         // be considered reachable.
1142         LayoutRect clientRect(flippedClientBoxRect());
1143         LayoutRect rectToApply;
1144         if (isHorizontalWritingMode())
1145             rectToApply = LayoutRect(clientRect.x(), clientRect.y(), 1, std::max<LayoutUnit>(0, oldClientAfterEdge - clientRect.y()));
1146         else
1147             rectToApply = LayoutRect(clientRect.x(), clientRect.y(), std::max<LayoutUnit>(0, oldClientAfterEdge - clientRect.x()), 1);
1148         addLayoutOverflow(rectToApply);
1149         if (hasRenderOverflow())
1150             m_overflow->setLayoutClientAfterEdge(oldClientAfterEdge);
1151     }
1152         
1153     // Add visual overflow from box-shadow, border-image-outset and outline.
1154     addVisualEffectOverflow();
1155
1156     // Add visual overflow from theme.
1157     addVisualOverflowFromTheme();
1158 }
1159
1160 void RenderBlock::clearLayoutOverflow()
1161 {
1162     if (!m_overflow)
1163         return;
1164     
1165     if (visualOverflowRect() == borderBoxRect()) {
1166         // FIXME: Implement complete solution for regions overflow.
1167         clearOverflow();
1168         return;
1169     }
1170     
1171     m_overflow->setLayoutOverflow(borderBoxRect());
1172 }
1173
1174 void RenderBlock::addOverflowFromBlockChildren()
1175 {
1176     for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
1177         if (!child->isFloatingOrOutOfFlowPositioned())
1178             addOverflowFromChild(child);
1179     }
1180 }
1181
1182 void RenderBlock::addOverflowFromPositionedObjects()
1183 {
1184     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
1185     if (!positionedDescendants)
1186         return;
1187
1188     for (auto it = positionedDescendants->begin(), end = positionedDescendants->end(); it != end; ++it) {
1189         RenderBox* positionedObject = *it;
1190         
1191         // Fixed positioned elements don't contribute to layout overflow, since they don't scroll with the content.
1192         if (positionedObject->style().position() != FixedPosition)
1193             addOverflowFromChild(positionedObject, { positionedObject->x(), positionedObject->y() });
1194     }
1195 }
1196
1197 void RenderBlock::addVisualOverflowFromTheme()
1198 {
1199     if (!style().hasAppearance())
1200         return;
1201
1202     FloatRect inflatedRect = borderBoxRect();
1203     theme().adjustRepaintRect(*this, inflatedRect);
1204     addVisualOverflow(snappedIntRect(LayoutRect(inflatedRect)));
1205
1206     if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1207         flowThread->addRegionsVisualOverflowFromTheme(this);
1208 }
1209
1210 LayoutUnit RenderBlock::computeStartPositionDeltaForChildAvoidingFloats(const RenderBox& child, LayoutUnit childMarginStart, RenderRegion* region)
1211 {
1212     LayoutUnit startPosition = startOffsetForContent(region);
1213
1214     // Add in our start margin.
1215     LayoutUnit oldPosition = startPosition + childMarginStart;
1216     LayoutUnit newPosition = oldPosition;
1217
1218     LayoutUnit blockOffset = logicalTopForChild(child);
1219     if (region)
1220         blockOffset = std::max(blockOffset, blockOffset + (region->logicalTopForFlowThreadContent() - offsetFromLogicalTopOfFirstPage()));
1221
1222     LayoutUnit startOff = startOffsetForLineInRegion(blockOffset, DoNotIndentText, region, logicalHeightForChild(child));
1223
1224     if (style().textAlign() != WEBKIT_CENTER && !child.style().marginStartUsing(&style()).isAuto()) {
1225         if (childMarginStart < 0)
1226             startOff += childMarginStart;
1227         newPosition = std::max(newPosition, startOff); // Let the float sit in the child's margin if it can fit.
1228     } else if (startOff != startPosition)
1229         newPosition = startOff + childMarginStart;
1230
1231     return newPosition - oldPosition;
1232 }
1233
1234 void RenderBlock::setLogicalLeftForChild(RenderBox& child, LayoutUnit logicalLeft, ApplyLayoutDeltaMode applyDelta)
1235 {
1236     if (isHorizontalWritingMode()) {
1237         if (applyDelta == ApplyLayoutDelta)
1238             view().addLayoutDelta(LayoutSize(child.x() - logicalLeft, 0));
1239         child.setX(logicalLeft);
1240     } else {
1241         if (applyDelta == ApplyLayoutDelta)
1242             view().addLayoutDelta(LayoutSize(0, child.y() - logicalLeft));
1243         child.setY(logicalLeft);
1244     }
1245 }
1246
1247 void RenderBlock::setLogicalTopForChild(RenderBox& child, LayoutUnit logicalTop, ApplyLayoutDeltaMode applyDelta)
1248 {
1249     if (isHorizontalWritingMode()) {
1250         if (applyDelta == ApplyLayoutDelta)
1251             view().addLayoutDelta(LayoutSize(0, child.y() - logicalTop));
1252         child.setY(logicalTop);
1253     } else {
1254         if (applyDelta == ApplyLayoutDelta)
1255             view().addLayoutDelta(LayoutSize(child.x() - logicalTop, 0));
1256         child.setX(logicalTop);
1257     }
1258 }
1259
1260 void RenderBlock::updateBlockChildDirtyBitsBeforeLayout(bool relayoutChildren, RenderBox& child)
1261 {
1262     // FIXME: Technically percentage height objects only need a relayout if their percentage isn't going to be turned into
1263     // an auto value. Add a method to determine this, so that we can avoid the relayout.
1264     if (relayoutChildren || (child.hasRelativeLogicalHeight() && !isRenderView()))
1265         child.setChildNeedsLayout(MarkOnlyThis);
1266
1267     // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1268     if (relayoutChildren && child.needsPreferredWidthsRecalculation())
1269         child.setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
1270 }
1271
1272 void RenderBlock::dirtyForLayoutFromPercentageHeightDescendants()
1273 {
1274     if (!percentHeightDescendantsMap)
1275         return;
1276
1277     TrackedRendererListHashSet* descendants = percentHeightDescendantsMap->get(this);
1278     if (!descendants)
1279         return;
1280
1281     for (auto it = descendants->begin(), end = descendants->end(); it != end; ++it) {
1282         RenderBox* box = *it;
1283         while (box != this) {
1284             if (box->normalChildNeedsLayout())
1285                 break;
1286             box->setChildNeedsLayout(MarkOnlyThis);
1287             
1288             // If the width of an image is affected by the height of a child (e.g., an image with an aspect ratio),
1289             // then we have to dirty preferred widths, since even enclosing blocks can become dirty as a result.
1290             // (A horizontal flexbox that contains an inline image wrapped in an anonymous block for example.)
1291             if (box->hasAspectRatio()) 
1292                 box->setPreferredLogicalWidthsDirty(true);
1293             
1294             box = box->containingBlock();
1295             ASSERT(box);
1296             if (!box)
1297                 break;
1298         }
1299     }
1300 }
1301
1302 void RenderBlock::simplifiedNormalFlowLayout()
1303 {
1304     if (childrenInline()) {
1305         ListHashSet<RootInlineBox*> lineBoxes;
1306         for (InlineWalker walker(*this); !walker.atEnd(); walker.advance()) {
1307             RenderObject& renderer = *walker.current();
1308             if (!renderer.isOutOfFlowPositioned() && (renderer.isReplaced() || renderer.isFloating())) {
1309                 RenderBox& box = downcast<RenderBox>(renderer);
1310                 box.layoutIfNeeded();
1311                 if (box.inlineBoxWrapper())
1312                     lineBoxes.add(&box.inlineBoxWrapper()->root());
1313             } else if (is<RenderText>(renderer) || (is<RenderInline>(renderer) && !walker.atEndOfInline()))
1314                 renderer.clearNeedsLayout();
1315         }
1316
1317         // FIXME: Glyph overflow will get lost in this case, but not really a big deal.
1318         // FIXME: Find a way to invalidate the knownToHaveNoOverflow flag on the InlineBoxes.
1319         GlyphOverflowAndFallbackFontsMap textBoxDataMap;                  
1320         for (auto it = lineBoxes.begin(), end = lineBoxes.end(); it != end; ++it) {
1321             RootInlineBox* box = *it;
1322             box->computeOverflow(box->lineTop(), box->lineBottom(), textBoxDataMap);
1323         }
1324     } else {
1325         for (auto* box = firstChildBox(); box; box = box->nextSiblingBox()) {
1326             if (!box->isOutOfFlowPositioned())
1327                 box->layoutIfNeeded();
1328         }
1329     }
1330 }
1331
1332 bool RenderBlock::canPerformSimplifiedLayout() const
1333 {
1334     return (posChildNeedsLayout() || needsSimplifiedNormalFlowLayout()) && !normalChildNeedsLayout() && !selfNeedsLayout();
1335 }
1336
1337 bool RenderBlock::simplifiedLayout()
1338 {
1339     if (!canPerformSimplifiedLayout())
1340         return false;
1341
1342     LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasTransform() || hasReflection() || style().isFlippedBlocksWritingMode());
1343     if (needsPositionedMovementLayout() && !tryLayoutDoingPositionedMovementOnly()) {
1344         statePusher.pop();
1345         return false;
1346     }
1347
1348     // Lay out positioned descendants or objects that just need to recompute overflow.
1349     if (needsSimplifiedNormalFlowLayout())
1350         simplifiedNormalFlowLayout();
1351
1352     // Make sure a forced break is applied after the content if we are a flow thread in a simplified layout.
1353     // This ensures the size information is correctly computed for the last auto-height region receiving content.
1354     if (is<RenderFlowThread>(*this))
1355         downcast<RenderFlowThread>(*this).applyBreakAfterContent(clientLogicalBottom());
1356
1357     // Lay out our positioned objects if our positioned child bit is set.
1358     // Also, if an absolute position element inside a relative positioned container moves, and the absolute element has a fixed position
1359     // child, neither the fixed element nor its container learn of the movement since posChildNeedsLayout() is only marked as far as the 
1360     // relative positioned container. So if we can have fixed pos objects in our positioned objects list check if any of them
1361     // are statically positioned and thus need to move with their absolute ancestors.
1362     bool canContainFixedPosObjects = canContainFixedPositionObjects();
1363     if (posChildNeedsLayout() || canContainFixedPosObjects)
1364         layoutPositionedObjects(false, !posChildNeedsLayout() && canContainFixedPosObjects);
1365
1366     // Recompute our overflow information.
1367     // FIXME: We could do better here by computing a temporary overflow object from layoutPositionedObjects and only
1368     // updating our overflow if we either used to have overflow or if the new temporary object has overflow.
1369     // For now just always recompute overflow.  This is no worse performance-wise than the old code that called rightmostPosition and
1370     // lowestPosition on every relayout so it's not a regression.
1371     // computeOverflow expects the bottom edge before we clamp our height. Since this information isn't available during
1372     // simplifiedLayout, we cache the value in m_overflow.
1373     LayoutUnit oldClientAfterEdge = hasRenderOverflow() ? m_overflow->layoutClientAfterEdge() : clientLogicalBottom();
1374     computeOverflow(oldClientAfterEdge, true);
1375
1376     statePusher.pop();
1377     
1378     updateLayerTransform();
1379
1380     updateScrollInfoAfterLayout();
1381
1382     clearNeedsLayout();
1383     return true;
1384 }
1385
1386 void RenderBlock::markFixedPositionObjectForLayoutIfNeeded(RenderBox& positionedChild)
1387 {
1388     if (positionedChild.style().position() != FixedPosition)
1389         return;
1390
1391     bool hasStaticBlockPosition = positionedChild.style().hasStaticBlockPosition(isHorizontalWritingMode());
1392     bool hasStaticInlinePosition = positionedChild.style().hasStaticInlinePosition(isHorizontalWritingMode());
1393     if (!hasStaticBlockPosition && !hasStaticInlinePosition)
1394         return;
1395
1396     auto* parent = positionedChild.parent();
1397     while (parent && !is<RenderView>(*parent) && parent->style().position() != AbsolutePosition)
1398         parent = parent->parent();
1399     if (!parent || parent->style().position() != AbsolutePosition)
1400         return;
1401
1402     if (hasStaticInlinePosition) {
1403         LogicalExtentComputedValues computedValues;
1404         positionedChild.computeLogicalWidthInRegion(computedValues);
1405         LayoutUnit newLeft = computedValues.m_position;
1406         if (newLeft != positionedChild.logicalLeft())
1407             positionedChild.setChildNeedsLayout(MarkOnlyThis);
1408     } else if (hasStaticBlockPosition) {
1409         LayoutUnit oldTop = positionedChild.logicalTop();
1410         positionedChild.updateLogicalHeight();
1411         if (positionedChild.logicalTop() != oldTop)
1412             positionedChild.setChildNeedsLayout(MarkOnlyThis);
1413     }
1414 }
1415
1416 LayoutUnit RenderBlock::marginIntrinsicLogicalWidthForChild(RenderBox& child) const
1417 {
1418     // A margin has three types: fixed, percentage, and auto (variable).
1419     // Auto and percentage margins become 0 when computing min/max width.
1420     // Fixed margins can be added in as is.
1421     Length marginLeft = child.style().marginStartUsing(&style());
1422     Length marginRight = child.style().marginEndUsing(&style());
1423     LayoutUnit margin = 0;
1424     if (marginLeft.isFixed())
1425         margin += marginLeft.value();
1426     if (marginRight.isFixed())
1427         margin += marginRight.value();
1428     return margin;
1429 }
1430
1431 void RenderBlock::layoutPositionedObject(RenderBox& r, bool relayoutChildren, bool fixedPositionObjectsOnly)
1432 {
1433     estimateRegionRangeForBoxChild(r);
1434
1435     // A fixed position element with an absolute positioned ancestor has no way of knowing if the latter has changed position. So
1436     // if this is a fixed position element, mark it for layout if it has an abspos ancestor and needs to move with that ancestor, i.e. 
1437     // it has static position.
1438     markFixedPositionObjectForLayoutIfNeeded(r);
1439     if (fixedPositionObjectsOnly) {
1440         r.layoutIfNeeded();
1441         return;
1442     }
1443
1444     // When a non-positioned block element moves, it may have positioned children that are implicitly positioned relative to the
1445     // non-positioned block.  Rather than trying to detect all of these movement cases, we just always lay out positioned
1446     // objects that are positioned implicitly like this.  Such objects are rare, and so in typical DHTML menu usage (where everything is
1447     // positioned explicitly) this should not incur a performance penalty.
1448     if (relayoutChildren || (r.style().hasStaticBlockPosition(isHorizontalWritingMode()) && r.parent() != this))
1449         r.setChildNeedsLayout(MarkOnlyThis);
1450         
1451     // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1452     if (relayoutChildren && r.needsPreferredWidthsRecalculation())
1453         r.setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
1454     
1455     r.markForPaginationRelayoutIfNeeded();
1456     
1457     // We don't have to do a full layout.  We just have to update our position. Try that first. If we have shrink-to-fit width
1458     // and we hit the available width constraint, the layoutIfNeeded() will catch it and do a full layout.
1459     if (r.needsPositionedMovementLayoutOnly() && r.tryLayoutDoingPositionedMovementOnly())
1460         r.clearNeedsLayout();
1461         
1462     // If we are paginated or in a line grid, compute a vertical position for our object now.
1463     // If it's wrong we'll lay out again.
1464     LayoutUnit oldLogicalTop = 0;
1465     bool needsBlockDirectionLocationSetBeforeLayout = r.needsLayout() && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
1466     if (needsBlockDirectionLocationSetBeforeLayout) {
1467         if (isHorizontalWritingMode() == r.isHorizontalWritingMode())
1468             r.updateLogicalHeight();
1469         else
1470             r.updateLogicalWidth();
1471         oldLogicalTop = logicalTopForChild(r);
1472     }
1473
1474     r.layoutIfNeeded();
1475
1476     // Lay out again if our estimate was wrong.
1477     if (needsBlockDirectionLocationSetBeforeLayout && logicalTopForChild(r) != oldLogicalTop) {
1478         r.setChildNeedsLayout(MarkOnlyThis);
1479         r.layoutIfNeeded();
1480     }
1481
1482     if (updateRegionRangeForBoxChild(r)) {
1483         r.setNeedsLayout(MarkOnlyThis);
1484         r.layoutIfNeeded();
1485     }
1486 }
1487
1488 void RenderBlock::layoutPositionedObjects(bool relayoutChildren, bool fixedPositionObjectsOnly)
1489 {
1490     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
1491     if (!positionedDescendants)
1492         return;
1493     
1494     // Do not cache positionedDescendants->end() in a local variable, since |positionedDescendants| can be mutated
1495     // as it is walked. We always need to fetch the new end() value dynamically.
1496     for (auto it = positionedDescendants->begin(); it != positionedDescendants->end(); ++it)
1497         layoutPositionedObject(**it, relayoutChildren, fixedPositionObjectsOnly);
1498 }
1499
1500 void RenderBlock::markPositionedObjectsForLayout()
1501 {
1502     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
1503     if (!positionedDescendants)
1504         return;
1505
1506     for (auto it = positionedDescendants->begin(), end = positionedDescendants->end(); it != end; ++it) {
1507         RenderBox* r = *it;
1508         r->setChildNeedsLayout();
1509     }
1510 }
1511
1512 void RenderBlock::markForPaginationRelayoutIfNeeded()
1513 {
1514     if (needsLayout() || !view().layoutState()->isPaginated())
1515         return;
1516
1517     if (view().layoutState()->pageLogicalHeightChanged() || (view().layoutState()->pageLogicalHeight() && view().layoutState()->pageLogicalOffset(this, logicalTop()) != pageLogicalOffset()))
1518         setChildNeedsLayout(MarkOnlyThis);
1519 }
1520
1521 void RenderBlock::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1522 {
1523     RenderNamedFlowFragment* namedFlowFragment = currentRenderNamedFlowFragment();
1524     // Check our region range to make sure we need to be painting in this region.
1525     if (namedFlowFragment && !namedFlowFragment->flowThread()->objectShouldFragmentInFlowRegion(this, namedFlowFragment))
1526         return;
1527
1528     LayoutPoint adjustedPaintOffset = paintOffset + location();
1529     PaintPhase phase = paintInfo.phase;
1530
1531     // Check if we need to do anything at all.
1532     // FIXME: Could eliminate the isDocumentElementRenderer() check if we fix background painting so that the RenderView
1533     // paints the root's background.
1534     if (!isDocumentElementRenderer()) {
1535         LayoutRect overflowBox = overflowRectForPaintRejection(namedFlowFragment);
1536         flipForWritingMode(overflowBox);
1537         overflowBox.moveBy(adjustedPaintOffset);
1538         if (!overflowBox.intersects(paintInfo.rect)
1539 #if PLATFORM(IOS)
1540             // FIXME: This may be applicable to non-iOS ports.
1541             && (!hasLayer() || !layer()->isComposited())
1542 #endif
1543         )
1544             return;
1545     }
1546
1547     bool pushedClip = pushContentsClip(paintInfo, adjustedPaintOffset);
1548     paintObject(paintInfo, adjustedPaintOffset);
1549     if (pushedClip)
1550         popContentsClip(paintInfo, phase, adjustedPaintOffset);
1551
1552     // Our scrollbar widgets paint exactly when we tell them to, so that they work properly with
1553     // z-index. We paint after we painted the background/border, so that the scrollbars will
1554     // sit above the background/border.
1555     if ((phase == PaintPhaseBlockBackground || phase == PaintPhaseChildBlockBackground) && hasOverflowClip() && layer()
1556         && style().visibility() == VISIBLE && paintInfo.shouldPaintWithinRoot(*this) && !paintInfo.paintRootBackgroundOnly())
1557         layer()->paintOverflowControls(paintInfo.context(), roundedIntPoint(adjustedPaintOffset), snappedIntRect(paintInfo.rect));
1558 }
1559
1560 void RenderBlock::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1561 {
1562     // Avoid painting descendants of the root element when stylesheets haven't loaded.  This eliminates FOUC.
1563     // It's ok not to draw, because later on, when all the stylesheets do load, styleResolverChanged() on the Document
1564     // will do a full repaint.
1565     if (document().didLayoutWithPendingStylesheets() && !isRenderView())
1566         return;
1567
1568     if (childrenInline())
1569         paintInlineChildren(paintInfo, paintOffset);
1570     else {
1571         PaintPhase newPhase = (paintInfo.phase == PaintPhaseChildOutlines) ? PaintPhaseOutline : paintInfo.phase;
1572         newPhase = (newPhase == PaintPhaseChildBlockBackgrounds) ? PaintPhaseChildBlockBackground : newPhase;
1573
1574         // We don't paint our own background, but we do let the kids paint their backgrounds.
1575         PaintInfo paintInfoForChild(paintInfo);
1576         paintInfoForChild.phase = newPhase;
1577         paintInfoForChild.updateSubtreePaintRootForChildren(this);
1578
1579         // FIXME: Paint-time pagination is obsolete and is now only used by embedded WebViews inside AppKit
1580         // NSViews. Do not add any more code for this.
1581         bool usePrintRect = !view().printRect().isEmpty();
1582         paintChildren(paintInfo, paintOffset, paintInfoForChild, usePrintRect);
1583     }
1584 }
1585
1586 void RenderBlock::paintChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset, PaintInfo& paintInfoForChild, bool usePrintRect)
1587 {
1588     for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) {
1589         if (!paintChild(*child, paintInfo, paintOffset, paintInfoForChild, usePrintRect))
1590             return;
1591     }
1592 }
1593
1594 bool RenderBlock::paintChild(RenderBox& child, PaintInfo& paintInfo, const LayoutPoint& paintOffset, PaintInfo& paintInfoForChild, bool usePrintRect, PaintBlockType paintType)
1595 {
1596     // Check for page-break-before: always, and if it's set, break and bail.
1597     bool checkBeforeAlways = !childrenInline() && (usePrintRect && alwaysPageBreak(child.style().breakBefore()));
1598     LayoutUnit absoluteChildY = paintOffset.y() + child.y();
1599     if (checkBeforeAlways
1600         && absoluteChildY > paintInfo.rect.y()
1601         && absoluteChildY < paintInfo.rect.maxY()) {
1602         view().setBestTruncatedAt(absoluteChildY, this, true);
1603         return false;
1604     }
1605
1606     if (!child.isFloating() && child.isReplaced() && usePrintRect && child.height() <= view().printRect().height()) {
1607         // Paginate block-level replaced elements.
1608         if (absoluteChildY + child.height() > view().printRect().maxY()) {
1609             if (absoluteChildY < view().truncatedAt())
1610                 view().setBestTruncatedAt(absoluteChildY, &child);
1611             // If we were able to truncate, don't paint.
1612             if (absoluteChildY >= view().truncatedAt())
1613                 return false;
1614         }
1615     }
1616
1617     LayoutPoint childPoint = flipForWritingModeForChild(&child, paintOffset);
1618     if (!child.hasSelfPaintingLayer() && !child.isFloating()) {
1619         if (paintType == PaintAsInlineBlock)
1620             child.paintAsInlineBlock(paintInfoForChild, childPoint);
1621         else
1622             child.paint(paintInfoForChild, childPoint);
1623     }
1624
1625     // Check for page-break-after: always, and if it's set, break and bail.
1626     bool checkAfterAlways = !childrenInline() && (usePrintRect && alwaysPageBreak(child.style().breakAfter()));
1627     if (checkAfterAlways
1628         && (absoluteChildY + child.height()) > paintInfo.rect.y()
1629         && (absoluteChildY + child.height()) < paintInfo.rect.maxY()) {
1630         view().setBestTruncatedAt(absoluteChildY + child.height() + std::max<LayoutUnit>(0, child.collapsedMarginAfter()), this, true);
1631         return false;
1632     }
1633
1634     return true;
1635 }
1636
1637 void RenderBlock::paintCaret(PaintInfo& paintInfo, const LayoutPoint& paintOffset, CaretType type)
1638 {
1639     // Paint the caret if the FrameSelection says so or if caret browsing is enabled
1640     bool caretBrowsing = frame().settings().caretBrowsingEnabled();
1641     RenderBlock* caretPainter;
1642     bool isContentEditable;
1643     if (type == CursorCaret) {
1644         caretPainter = frame().selection().caretRendererWithoutUpdatingLayout();
1645         isContentEditable = frame().selection().selection().hasEditableStyle();
1646     } else {
1647         caretPainter = frame().page()->dragCaretController().caretRenderer();
1648         isContentEditable = frame().page()->dragCaretController().isContentEditable();
1649     }
1650
1651     if (caretPainter == this && (isContentEditable || caretBrowsing)) {
1652         if (type == CursorCaret)
1653             frame().selection().paintCaret(paintInfo.context(), paintOffset, paintInfo.rect);
1654         else
1655             frame().page()->dragCaretController().paintDragCaret(&frame(), paintInfo.context(), paintOffset, paintInfo.rect);
1656     }
1657 }
1658
1659 void RenderBlock::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1660 {
1661     PaintPhase paintPhase = paintInfo.phase;
1662
1663     // 1. paint background, borders etc
1664     if ((paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) && style().visibility() == VISIBLE) {
1665         if (hasVisibleBoxDecorations()) {
1666             bool didClipToRegion = false;
1667             
1668             RenderNamedFlowFragment* namedFlowFragment = currentRenderNamedFlowFragment();
1669             if (namedFlowFragment && is<RenderNamedFlowThread>(paintInfo.paintContainer)) {
1670                 // If this box goes beyond the current region, then make sure not to overflow the region.
1671                 // This (overflowing region X altough also fragmented to region X+1) could happen when one of this box's children
1672                 // overflows region X and is an unsplittable element (like an image).
1673                 // The same applies for a box overflowing the top of region X when that box is also fragmented in region X-1.
1674
1675                 paintInfo.context().save();
1676                 didClipToRegion = true;
1677
1678                 paintInfo.context().clip(downcast<RenderNamedFlowThread>(*paintInfo.paintContainer).decorationsClipRectForBoxInNamedFlowFragment(*this, *namedFlowFragment));
1679             }
1680
1681             paintBoxDecorations(paintInfo, paintOffset);
1682             
1683             if (didClipToRegion)
1684                 paintInfo.context().restore();
1685         }
1686     }
1687
1688     if (paintPhase == PaintPhaseMask && style().visibility() == VISIBLE) {
1689         paintMask(paintInfo, paintOffset);
1690         return;
1691     }
1692
1693     if (paintPhase == PaintPhaseClippingMask && style().visibility() == VISIBLE) {
1694         paintClippingMask(paintInfo, paintOffset);
1695         return;
1696     }
1697
1698     // If just painting the root background, then return.
1699     if (paintInfo.paintRootBackgroundOnly())
1700         return;
1701
1702     // Adjust our painting position if we're inside a scrolled layer (e.g., an overflow:auto div).
1703     LayoutPoint scrolledOffset = paintOffset;
1704     scrolledOffset.moveBy(-scrollPosition());
1705
1706     // Column rules need to account for scrolling and clipping.
1707     // FIXME: Clipping of column rules does not work. We will need a separate paint phase for column rules I suspect in order to get
1708     // clipping correct (since it has to paint as background but is still considered "contents").
1709     if ((paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) && style().visibility() == VISIBLE)
1710         paintColumnRules(paintInfo, scrolledOffset);
1711
1712     // Done with backgrounds, borders and column rules.
1713     if (paintPhase == PaintPhaseBlockBackground)
1714         return;
1715     
1716     // 2. paint contents
1717     if (paintPhase != PaintPhaseSelfOutline)
1718         paintContents(paintInfo, scrolledOffset);
1719
1720     // 3. paint selection
1721     // FIXME: Make this work with multi column layouts.  For now don't fill gaps.
1722     bool isPrinting = document().printing();
1723     if (!isPrinting)
1724         paintSelection(paintInfo, scrolledOffset); // Fill in gaps in selection on lines and between blocks.
1725
1726     // 4. paint floats.
1727     if (paintPhase == PaintPhaseFloat || paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip)
1728         paintFloats(paintInfo, scrolledOffset, paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip);
1729
1730     // 5. paint outline.
1731     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline) && hasOutline() && style().visibility() == VISIBLE)
1732         paintOutline(paintInfo, LayoutRect(paintOffset, size()));
1733
1734     // 6. paint continuation outlines.
1735     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseChildOutlines)) {
1736         RenderInline* inlineCont = inlineElementContinuation();
1737         if (inlineCont && inlineCont->hasOutline() && inlineCont->style().visibility() == VISIBLE) {
1738             RenderInline* inlineRenderer = downcast<RenderInline>(inlineCont->element()->renderer());
1739             RenderBlock* containingBlock = this->containingBlock();
1740
1741             bool inlineEnclosedInSelfPaintingLayer = false;
1742             for (RenderBoxModelObject* box = inlineRenderer; box != containingBlock; box = &box->parent()->enclosingBoxModelObject()) {
1743                 if (box->hasSelfPaintingLayer()) {
1744                     inlineEnclosedInSelfPaintingLayer = true;
1745                     break;
1746                 }
1747             }
1748
1749             // Do not add continuations for outline painting by our containing block if we are a relative positioned
1750             // anonymous block (i.e. have our own layer), paint them straightaway instead. This is because a block depends on renderers in its continuation table being
1751             // in the same layer. 
1752             if (!inlineEnclosedInSelfPaintingLayer && !hasLayer())
1753                 containingBlock->addContinuationWithOutline(inlineRenderer);
1754             else if (!inlineRenderer->firstLineBox() || (!inlineEnclosedInSelfPaintingLayer && hasLayer()))
1755                 inlineRenderer->paintOutline(paintInfo, paintOffset - locationOffset() + inlineRenderer->containingBlock()->location());
1756         }
1757         paintContinuationOutlines(paintInfo, paintOffset);
1758     }
1759
1760     // 7. paint caret.
1761     // If the caret's node's render object's containing block is this block, and the paint action is PaintPhaseForeground,
1762     // then paint the caret.
1763     if (paintPhase == PaintPhaseForeground) {        
1764         paintCaret(paintInfo, paintOffset, CursorCaret);
1765         paintCaret(paintInfo, paintOffset, DragCaret);
1766     }
1767 }
1768
1769 RenderInline* RenderBlock::inlineElementContinuation() const
1770
1771     RenderBoxModelObject* continuation = this->continuation();
1772     return is<RenderInline>(continuation) ? downcast<RenderInline>(continuation) : nullptr;
1773 }
1774
1775 RenderBlock* RenderBlock::blockElementContinuation() const
1776 {
1777     RenderBoxModelObject* currentContinuation = continuation();
1778     if (!currentContinuation || currentContinuation->isInline())
1779         return nullptr;
1780     RenderBlock& nextContinuation = downcast<RenderBlock>(*currentContinuation);
1781     if (nextContinuation.isAnonymousBlock())
1782         return nextContinuation.blockElementContinuation();
1783     return &nextContinuation;
1784 }
1785     
1786 static ContinuationOutlineTableMap* continuationOutlineTable()
1787 {
1788     static NeverDestroyed<ContinuationOutlineTableMap> table;
1789     return &table.get();
1790 }
1791
1792 void RenderBlock::addContinuationWithOutline(RenderInline* flow)
1793 {
1794     // We can't make this work if the inline is in a layer.  We'll just rely on the broken
1795     // way of painting.
1796     ASSERT(!flow->layer() && !flow->isInlineElementContinuation());
1797     
1798     ContinuationOutlineTableMap* table = continuationOutlineTable();
1799     ListHashSet<RenderInline*>* continuations = table->get(this);
1800     if (!continuations) {
1801         continuations = new ListHashSet<RenderInline*>;
1802         table->set(this, std::unique_ptr<ListHashSet<RenderInline*>>(continuations));
1803     }
1804     
1805     continuations->add(flow);
1806 }
1807
1808 bool RenderBlock::paintsContinuationOutline(RenderInline* flow)
1809 {
1810     ContinuationOutlineTableMap* table = continuationOutlineTable();
1811     if (table->isEmpty())
1812         return false;
1813         
1814     ListHashSet<RenderInline*>* continuations = table->get(this);
1815     if (!continuations)
1816         return false;
1817
1818     return continuations->contains(flow);
1819 }
1820
1821 void RenderBlock::paintContinuationOutlines(PaintInfo& info, const LayoutPoint& paintOffset)
1822 {
1823     ContinuationOutlineTableMap* table = continuationOutlineTable();
1824     if (table->isEmpty())
1825         return;
1826         
1827     std::unique_ptr<ListHashSet<RenderInline*>> continuations = table->take(this);
1828     if (!continuations)
1829         return;
1830
1831     LayoutPoint accumulatedPaintOffset = paintOffset;
1832     // Paint each continuation outline.
1833     ListHashSet<RenderInline*>::iterator end = continuations->end();
1834     for (ListHashSet<RenderInline*>::iterator it = continuations->begin(); it != end; ++it) {
1835         // Need to add in the coordinates of the intervening blocks.
1836         RenderInline* flow = *it;
1837         RenderBlock* block = flow->containingBlock();
1838         for ( ; block && block != this; block = block->containingBlock())
1839             accumulatedPaintOffset.moveBy(block->location());
1840         ASSERT(block);   
1841         flow->paintOutline(info, accumulatedPaintOffset);
1842     }
1843 }
1844
1845 bool RenderBlock::shouldPaintSelectionGaps() const
1846 {
1847     if (frame().settings().selectionPaintingWithoutSelectionGapsEnabled())
1848         return false;
1849
1850     return selectionState() != SelectionNone && style().visibility() == VISIBLE && isSelectionRoot();
1851 }
1852
1853 bool RenderBlock::isSelectionRoot() const
1854 {
1855     if (isPseudoElement())
1856         return false;
1857     ASSERT(element() || isAnonymous());
1858         
1859     // FIXME: Eventually tables should have to learn how to fill gaps between cells, at least in simple non-spanning cases.
1860     if (isTable())
1861         return false;
1862         
1863     if (isBody() || isDocumentElementRenderer() || hasOverflowClip()
1864         || isPositioned() || isFloating()
1865         || isTableCell() || isInlineBlockOrInlineTable()
1866         || hasTransform() || hasReflection() || hasMask() || isWritingModeRoot()
1867         || isRenderFlowThread() || style().columnSpan() == ColumnSpanAll)
1868         return true;
1869     
1870     if (view().selectionUnsplitStart()) {
1871         Node* startElement = view().selectionUnsplitStart()->node();
1872         if (startElement && startElement->rootEditableElement() == element())
1873             return true;
1874     }
1875     
1876     return false;
1877 }
1878
1879 GapRects RenderBlock::selectionGapRectsForRepaint(const RenderLayerModelObject* repaintContainer)
1880 {
1881     ASSERT(!needsLayout());
1882
1883     if (!shouldPaintSelectionGaps())
1884         return GapRects();
1885
1886     FloatPoint containerPoint = localToContainerPoint(FloatPoint(), repaintContainer, UseTransforms);
1887     LayoutPoint offsetFromRepaintContainer(containerPoint - toFloatSize(scrollPosition()));
1888
1889     LogicalSelectionOffsetCaches cache(*this);
1890     LayoutUnit lastTop = 0;
1891     LayoutUnit lastLeft = logicalLeftSelectionOffset(*this, lastTop, cache);
1892     LayoutUnit lastRight = logicalRightSelectionOffset(*this, lastTop, cache);
1893     
1894     return selectionGaps(*this, offsetFromRepaintContainer, IntSize(), lastTop, lastLeft, lastRight, cache);
1895 }
1896
1897 void RenderBlock::paintSelection(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1898 {
1899 #if ENABLE(TEXT_SELECTION)
1900     if (shouldPaintSelectionGaps() && paintInfo.phase == PaintPhaseForeground) {
1901         LogicalSelectionOffsetCaches cache(*this);
1902         LayoutUnit lastTop = 0;
1903         LayoutUnit lastLeft = logicalLeftSelectionOffset(*this, lastTop, cache);
1904         LayoutUnit lastRight = logicalRightSelectionOffset(*this, lastTop, cache);
1905         GraphicsContextStateSaver stateSaver(paintInfo.context());
1906
1907         LayoutRect gapRectsBounds = selectionGaps(*this, paintOffset, LayoutSize(), lastTop, lastLeft, lastRight, cache, &paintInfo);
1908         if (!gapRectsBounds.isEmpty()) {
1909             if (RenderLayer* layer = enclosingLayer()) {
1910                 gapRectsBounds.moveBy(-paintOffset);
1911                 if (!hasLayer()) {
1912                     LayoutRect localBounds(gapRectsBounds);
1913                     flipForWritingMode(localBounds);
1914                     gapRectsBounds = localToContainerQuad(FloatRect(localBounds), &layer->renderer()).enclosingBoundingBox();
1915                     if (layer->renderer().isBox())
1916                         gapRectsBounds.moveBy(layer->renderBox()->scrollPosition());
1917                 }
1918                 layer->addBlockSelectionGapsBounds(gapRectsBounds);
1919             }
1920         }
1921     }
1922 #else
1923     UNUSED_PARAM(paintInfo);
1924     UNUSED_PARAM(paintOffset);
1925 #endif
1926 }
1927
1928 static void clipOutPositionedObjects(const PaintInfo* paintInfo, const LayoutPoint& offset, TrackedRendererListHashSet* positionedObjects)
1929 {
1930     if (!positionedObjects)
1931         return;
1932     
1933     TrackedRendererListHashSet::const_iterator end = positionedObjects->end();
1934     for (TrackedRendererListHashSet::const_iterator it = positionedObjects->begin(); it != end; ++it) {
1935         RenderBox* r = *it;
1936         paintInfo->context().clipOut(IntRect(offset.x() + r->x(), offset.y() + r->y(), r->width(), r->height()));
1937     }
1938 }
1939
1940 LayoutUnit blockDirectionOffset(RenderBlock& rootBlock, const LayoutSize& offsetFromRootBlock)
1941 {
1942     return rootBlock.isHorizontalWritingMode() ? offsetFromRootBlock.height() : offsetFromRootBlock.width();
1943 }
1944
1945 LayoutUnit inlineDirectionOffset(RenderBlock& rootBlock, const LayoutSize& offsetFromRootBlock)
1946 {
1947     return rootBlock.isHorizontalWritingMode() ? offsetFromRootBlock.width() : offsetFromRootBlock.height();
1948 }
1949
1950 LayoutRect RenderBlock::logicalRectToPhysicalRect(const LayoutPoint& rootBlockPhysicalPosition, const LayoutRect& logicalRect)
1951 {
1952     LayoutRect result;
1953     if (isHorizontalWritingMode())
1954         result = logicalRect;
1955     else
1956         result = LayoutRect(logicalRect.y(), logicalRect.x(), logicalRect.height(), logicalRect.width());
1957     flipForWritingMode(result);
1958     result.moveBy(rootBlockPhysicalPosition);
1959     return result;
1960 }
1961
1962 GapRects RenderBlock::selectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
1963     LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
1964 {
1965     // IMPORTANT: Callers of this method that intend for painting to happen need to do a save/restore.
1966     // Clip out floating and positioned objects when painting selection gaps.
1967     if (paintInfo) {
1968         // Note that we don't clip out overflow for positioned objects.  We just stick to the border box.
1969         LayoutRect flippedBlockRect(offsetFromRootBlock.width(), offsetFromRootBlock.height(), width(), height());
1970         rootBlock.flipForWritingMode(flippedBlockRect);
1971         flippedBlockRect.moveBy(rootBlockPhysicalPosition);
1972         clipOutPositionedObjects(paintInfo, flippedBlockRect.location(), positionedObjects());
1973         if (isBody() || isDocumentElementRenderer()) { // The <body> must make sure to examine its containingBlock's positioned objects.
1974             for (RenderBlock* cb = containingBlock(); cb && !is<RenderView>(*cb); cb = cb->containingBlock())
1975                 clipOutPositionedObjects(paintInfo, LayoutPoint(cb->x(), cb->y()), cb->positionedObjects()); // FIXME: Not right for flipped writing modes.
1976         }
1977         clipOutFloatingObjects(rootBlock, paintInfo, rootBlockPhysicalPosition, offsetFromRootBlock);
1978     }
1979
1980     // FIXME: overflow: auto/scroll regions need more math here, since painting in the border box is different from painting in the padding box (one is scrolled, the other is
1981     // fixed).
1982     GapRects result;
1983     if (!isRenderBlockFlow()) // FIXME: Make multi-column selection gap filling work someday.
1984         return result;
1985
1986     if (hasTransform() || style().columnSpan() == ColumnSpanAll || isInFlowRenderFlowThread()) {
1987         // FIXME: We should learn how to gap fill multiple columns and transforms eventually.
1988         lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
1989         lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
1990         lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
1991         return result;
1992     }
1993     
1994     RenderNamedFlowFragment* namedFlowFragment = currentRenderNamedFlowFragment();
1995     if (paintInfo && namedFlowFragment && is<RenderFlowThread>(*paintInfo->paintContainer)) {
1996         // Make sure the current object is actually flowed into the region being painted.
1997         if (!downcast<RenderFlowThread>(*paintInfo->paintContainer).objectShouldFragmentInFlowRegion(this, namedFlowFragment))
1998             return result;
1999     }
2000
2001     if (childrenInline())
2002         result = inlineSelectionGaps(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, cache, paintInfo);
2003     else
2004         result = blockSelectionGaps(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, cache, paintInfo);
2005
2006     // Fill the vertical gap all the way to the bottom of our block if the selection extends past our block.
2007     if (&rootBlock == this && (selectionState() != SelectionBoth && selectionState() != SelectionEnd) && !isRubyBase() && !isRubyText()) {
2008         result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
2009             lastLogicalTop, lastLogicalLeft, lastLogicalRight, logicalHeight(), cache, paintInfo));
2010     }
2011
2012     return result;
2013 }
2014
2015 GapRects RenderBlock::inlineSelectionGaps(RenderBlock&, const LayoutPoint&, const LayoutSize&, LayoutUnit&, LayoutUnit&, LayoutUnit&, const LogicalSelectionOffsetCaches&, const PaintInfo*)
2016 {
2017     ASSERT_NOT_REACHED();
2018     return GapRects();
2019 }
2020
2021 GapRects RenderBlock::blockSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2022     LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2023 {
2024     GapRects result;
2025
2026     // Jump right to the first block child that contains some selected objects.
2027     RenderBox* curr;
2028     for (curr = firstChildBox(); curr && curr->selectionState() == SelectionNone; curr = curr->nextSiblingBox()) { }
2029     
2030     if (!curr)
2031         return result;
2032
2033     LogicalSelectionOffsetCaches childCache(*this, cache);
2034
2035     for (bool sawSelectionEnd = false; curr && !sawSelectionEnd; curr = curr->nextSiblingBox()) {
2036         SelectionState childState = curr->selectionState();
2037         if (childState == SelectionBoth || childState == SelectionEnd)
2038             sawSelectionEnd = true;
2039
2040         if (curr->isFloatingOrOutOfFlowPositioned())
2041             continue; // We must be a normal flow object in order to even be considered.
2042
2043         if (curr->isInFlowPositioned() && curr->hasLayer()) {
2044             // If the relposition offset is anything other than 0, then treat this just like an absolute positioned element.
2045             // Just disregard it completely.
2046             LayoutSize relOffset = curr->layer()->offsetForInFlowPosition();
2047             if (relOffset.width() || relOffset.height())
2048                 continue;
2049         }
2050
2051         bool paintsOwnSelection = curr->shouldPaintSelectionGaps() || curr->isTable(); // FIXME: Eventually we won't special-case table like this.
2052         bool fillBlockGaps = (paintsOwnSelection || (curr->canBeSelectionLeaf() && childState != SelectionNone)) && !isRubyBase() && !isRubyText();
2053         if (fillBlockGaps) {
2054             // We need to fill the vertical gap above this object.
2055             if (childState == SelectionEnd || childState == SelectionInside) {
2056                 // Fill the gap above the object.
2057                 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
2058                     lastLogicalTop, lastLogicalLeft, lastLogicalRight, curr->logicalTop(), cache, paintInfo));
2059             }
2060
2061             // Only fill side gaps for objects that paint their own selection if we know for sure the selection is going to extend all the way *past*
2062             // our object.  We know this if the selection did not end inside our object.
2063             if (paintsOwnSelection && (childState == SelectionStart || sawSelectionEnd))
2064                 childState = SelectionNone;
2065
2066             // Fill side gaps on this object based off its state.
2067             bool leftGap, rightGap;
2068             getSelectionGapInfo(childState, leftGap, rightGap);
2069
2070             if (leftGap)
2071                 result.uniteLeft(logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, this, curr->logicalLeft(), curr->logicalTop(), curr->logicalHeight(), cache, paintInfo));
2072             if (rightGap)
2073                 result.uniteRight(logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, this, curr->logicalRight(), curr->logicalTop(), curr->logicalHeight(), cache, paintInfo));
2074
2075             // Update lastLogicalTop to be just underneath the object.  lastLogicalLeft and lastLogicalRight extend as far as
2076             // they can without bumping into floating or positioned objects.  Ideally they will go right up
2077             // to the border of the root selection block.
2078             lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + curr->logicalBottom();
2079             lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, curr->logicalBottom(), cache);
2080             lastLogicalRight = logicalRightSelectionOffset(rootBlock, curr->logicalBottom(), cache);
2081         } else if (childState != SelectionNone && is<RenderBlock>(*curr)) {
2082             // We must be a block that has some selected object inside it, so recur.
2083             result.unite(downcast<RenderBlock>(*curr).selectionGaps(rootBlock, rootBlockPhysicalPosition, LayoutSize(offsetFromRootBlock.width() + curr->x(), offsetFromRootBlock.height() + curr->y()),
2084                 lastLogicalTop, lastLogicalLeft, lastLogicalRight, childCache, paintInfo));
2085         }
2086     }
2087     return result;
2088 }
2089
2090 LayoutRect RenderBlock::blockSelectionGap(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2091     LayoutUnit lastLogicalTop, LayoutUnit lastLogicalLeft, LayoutUnit lastLogicalRight, LayoutUnit logicalBottom, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2092 {
2093     LayoutUnit logicalTop = lastLogicalTop;
2094     LayoutUnit logicalHeight = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalBottom - logicalTop;
2095     if (logicalHeight <= 0)
2096         return LayoutRect();
2097
2098     // Get the selection offsets for the bottom of the gap
2099     LayoutUnit logicalLeft = std::max(lastLogicalLeft, logicalLeftSelectionOffset(rootBlock, logicalBottom, cache));
2100     LayoutUnit logicalRight = std::min(lastLogicalRight, logicalRightSelectionOffset(rootBlock, logicalBottom, cache));
2101     LayoutUnit logicalWidth = logicalRight - logicalLeft;
2102     if (logicalWidth <= 0)
2103         return LayoutRect();
2104
2105     LayoutRect gapRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(logicalLeft, logicalTop, logicalWidth, logicalHeight));
2106     if (paintInfo)
2107         paintInfo->context().fillRect(snapRectToDevicePixels(gapRect, document().deviceScaleFactor()), selectionBackgroundColor());
2108     return gapRect;
2109 }
2110
2111 LayoutRect RenderBlock::logicalLeftSelectionGap(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2112     RenderBoxModelObject* selObj, LayoutUnit logicalLeft, LayoutUnit logicalTop, LayoutUnit logicalHeight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2113 {
2114     LayoutUnit rootBlockLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalTop;
2115     LayoutUnit rootBlockLogicalLeft = std::max(logicalLeftSelectionOffset(rootBlock, logicalTop, cache), logicalLeftSelectionOffset(rootBlock, logicalTop + logicalHeight, cache));
2116     LayoutUnit rootBlockLogicalRight = std::min(inlineDirectionOffset(rootBlock, offsetFromRootBlock) + logicalLeft,
2117         std::min(logicalRightSelectionOffset(rootBlock, logicalTop, cache), logicalRightSelectionOffset(rootBlock, logicalTop + logicalHeight, cache)));
2118     LayoutUnit rootBlockLogicalWidth = rootBlockLogicalRight - rootBlockLogicalLeft;
2119     if (rootBlockLogicalWidth <= 0)
2120         return LayoutRect();
2121
2122     LayoutRect gapRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(rootBlockLogicalLeft, rootBlockLogicalTop, rootBlockLogicalWidth, logicalHeight));
2123     if (paintInfo)
2124         paintInfo->context().fillRect(snapRectToDevicePixels(gapRect, document().deviceScaleFactor()), selObj->selectionBackgroundColor());
2125     return gapRect;
2126 }
2127
2128 LayoutRect RenderBlock::logicalRightSelectionGap(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2129     RenderBoxModelObject* selObj, LayoutUnit logicalRight, LayoutUnit logicalTop, LayoutUnit logicalHeight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2130 {
2131     LayoutUnit rootBlockLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalTop;
2132     LayoutUnit rootBlockLogicalLeft = std::max(inlineDirectionOffset(rootBlock, offsetFromRootBlock) + logicalRight,
2133         std::max(logicalLeftSelectionOffset(rootBlock, logicalTop, cache), logicalLeftSelectionOffset(rootBlock, logicalTop + logicalHeight, cache)));
2134     LayoutUnit rootBlockLogicalRight = std::min(logicalRightSelectionOffset(rootBlock, logicalTop, cache), logicalRightSelectionOffset(rootBlock, logicalTop + logicalHeight, cache));
2135     LayoutUnit rootBlockLogicalWidth = rootBlockLogicalRight - rootBlockLogicalLeft;
2136     if (rootBlockLogicalWidth <= 0)
2137         return LayoutRect();
2138
2139     LayoutRect gapRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(rootBlockLogicalLeft, rootBlockLogicalTop, rootBlockLogicalWidth, logicalHeight));
2140     if (paintInfo)
2141         paintInfo->context().fillRect(snapRectToDevicePixels(gapRect, document().deviceScaleFactor()), selObj->selectionBackgroundColor());
2142     return gapRect;
2143 }
2144
2145 void RenderBlock::getSelectionGapInfo(SelectionState state, bool& leftGap, bool& rightGap)
2146 {
2147     bool ltr = style().isLeftToRightDirection();
2148     leftGap = (state == RenderObject::SelectionInside) ||
2149               (state == RenderObject::SelectionEnd && ltr) ||
2150               (state == RenderObject::SelectionStart && !ltr);
2151     rightGap = (state == RenderObject::SelectionInside) ||
2152                (state == RenderObject::SelectionStart && ltr) ||
2153                (state == RenderObject::SelectionEnd && !ltr);
2154 }
2155
2156 LayoutUnit RenderBlock::logicalLeftSelectionOffset(RenderBlock& rootBlock, LayoutUnit position, const LogicalSelectionOffsetCaches& cache)
2157 {
2158     LayoutUnit logicalLeft = logicalLeftOffsetForLine(position, DoNotIndentText);
2159     if (logicalLeft == logicalLeftOffsetForContent()) {
2160         if (&rootBlock != this) // The border can potentially be further extended by our containingBlock().
2161             return cache.containingBlockInfo(*this).logicalLeftSelectionOffset(rootBlock, position + logicalTop());
2162         return logicalLeft;
2163     }
2164
2165     RenderBlock* cb = this;
2166     const LogicalSelectionOffsetCaches* currentCache = &cache;
2167     while (cb != &rootBlock) {
2168         logicalLeft += cb->logicalLeft();
2169
2170         ASSERT(currentCache);
2171         auto info = currentCache->containingBlockInfo(*cb);
2172         cb = info.block();
2173         if (!cb)
2174             break;
2175         currentCache = info.cache();
2176     }
2177     return logicalLeft;
2178 }
2179
2180 LayoutUnit RenderBlock::logicalRightSelectionOffset(RenderBlock& rootBlock, LayoutUnit position, const LogicalSelectionOffsetCaches& cache)
2181 {
2182     LayoutUnit logicalRight = logicalRightOffsetForLine(position, DoNotIndentText);
2183     if (logicalRight == logicalRightOffsetForContent()) {
2184         if (&rootBlock != this) // The border can potentially be further extended by our containingBlock().
2185             return cache.containingBlockInfo(*this).logicalRightSelectionOffset(rootBlock, position + logicalTop());
2186         return logicalRight;
2187     }
2188
2189     RenderBlock* cb = this;
2190     const LogicalSelectionOffsetCaches* currentCache = &cache;
2191     while (cb != &rootBlock) {
2192         logicalRight += cb->logicalLeft();
2193
2194         ASSERT(currentCache);
2195         auto info = currentCache->containingBlockInfo(*cb);
2196         cb = info.block();
2197         if (!cb)
2198             break;
2199         currentCache = info.cache();
2200     }
2201     return logicalRight;
2202 }
2203
2204 RenderBlock* RenderBlock::blockBeforeWithinSelectionRoot(LayoutSize& offset) const
2205 {
2206     if (isSelectionRoot())
2207         return nullptr;
2208
2209     const RenderElement* object = this;
2210     RenderObject* sibling;
2211     do {
2212         sibling = object->previousSibling();
2213         while (sibling && (!is<RenderBlock>(*sibling) || downcast<RenderBlock>(*sibling).isSelectionRoot()))
2214             sibling = sibling->previousSibling();
2215
2216         offset -= LayoutSize(downcast<RenderBlock>(*object).logicalLeft(), downcast<RenderBlock>(*object).logicalTop());
2217         object = object->parent();
2218     } while (!sibling && is<RenderBlock>(object) && !downcast<RenderBlock>(*object).isSelectionRoot());
2219
2220     if (!sibling)
2221         return nullptr;
2222
2223     RenderBlock* beforeBlock = downcast<RenderBlock>(sibling);
2224
2225     offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop());
2226
2227     RenderObject* child = beforeBlock->lastChild();
2228     while (is<RenderBlock>(child)) {
2229         beforeBlock = downcast<RenderBlock>(child);
2230         offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop());
2231         child = beforeBlock->lastChild();
2232     }
2233     return beforeBlock;
2234 }
2235
2236 TrackedRendererListHashSet* RenderBlock::positionedObjects() const
2237 {
2238     return positionedDescendantsMap().positionedRenderers(*this);
2239 }
2240
2241 void RenderBlock::insertPositionedObject(RenderBox& positioned)
2242 {
2243     ASSERT(!isAnonymousBlock());
2244     if (positioned.isRenderFlowThread())
2245         return;
2246     // FIXME: Find out if we can do this as part of positioned.setChildNeedsLayout(MarkOnlyThis)
2247     if (positioned.needsLayout()) {
2248         // We should turn this bit on only while in layout.
2249         ASSERT(posChildNeedsLayout() || view().frameView().isInLayout());
2250         setPosChildNeedsLayoutBit(true);
2251     }
2252     positionedDescendantsMap().addDescendant(*this, positioned, isRenderView() ? PositionedDescendantsMap::MoveDescendantToEnd::Yes
2253         : PositionedDescendantsMap::MoveDescendantToEnd::No);
2254 }
2255
2256 void RenderBlock::removePositionedObject(const RenderBox& rendererToRemove)
2257 {
2258     positionedDescendantsMap().removeDescendant(rendererToRemove);
2259 }
2260
2261 void RenderBlock::removePositionedObjects(const RenderBlock* newContainingBlockCandidate, ContainingBlockState containingBlockState)
2262 {
2263     auto* positionedDescendants = positionedObjects();
2264     if (!positionedDescendants)
2265         return;
2266     
2267     Vector<RenderBox*, 16> renderersToRemove;
2268     for (auto* renderer : *positionedDescendants) {
2269         if (newContainingBlockCandidate && !renderer->isDescendantOf(newContainingBlockCandidate))
2270             continue;
2271         renderersToRemove.append(renderer);
2272         if (containingBlockState == NewContainingBlock)
2273             renderer->setChildNeedsLayout(MarkOnlyThis);
2274         // It is the parent block's job to add positioned children to positioned objects list of its containing block.
2275         // Dirty the parent to ensure this happens.
2276         auto* parent = renderer->parent();
2277         while (parent && !parent->isRenderBlock())
2278             parent = parent->parent();
2279         if (parent)
2280             parent->setChildNeedsLayout();
2281     }
2282     for (auto* renderer : renderersToRemove)
2283         removePositionedObject(*renderer);
2284 }
2285
2286 void RenderBlock::addPercentHeightDescendant(RenderBox& descendant)
2287 {
2288     insertIntoTrackedRendererMaps(*this, descendant);
2289 }
2290
2291 void RenderBlock::removePercentHeightDescendant(RenderBox& descendant)
2292 {
2293     removeFromTrackedRendererMaps(descendant);
2294 }
2295
2296 TrackedRendererListHashSet* RenderBlock::percentHeightDescendants() const
2297 {
2298     return percentHeightDescendantsMap ? percentHeightDescendantsMap->get(this) : nullptr;
2299 }
2300
2301 bool RenderBlock::hasPercentHeightContainerMap()
2302 {
2303     return percentHeightContainerMap;
2304 }
2305
2306 bool RenderBlock::hasPercentHeightDescendant(RenderBox& descendant)
2307 {
2308     // We don't null check percentHeightContainerMap since the caller
2309     // already ensures this and we need to call this function on every
2310     // descendant in clearPercentHeightDescendantsFrom().
2311     ASSERT(percentHeightContainerMap);
2312     return percentHeightContainerMap->contains(&descendant);
2313 }
2314
2315 void RenderBlock::removePercentHeightDescendantIfNeeded(RenderBox& descendant)
2316 {
2317     // We query the map directly, rather than looking at style's
2318     // logicalHeight()/logicalMinHeight()/logicalMaxHeight() since those
2319     // can change with writing mode/directional changes.
2320     if (!hasPercentHeightContainerMap())
2321         return;
2322
2323     if (!hasPercentHeightDescendant(descendant))
2324         return;
2325
2326     removePercentHeightDescendant(descendant);
2327 }
2328
2329 void RenderBlock::clearPercentHeightDescendantsFrom(RenderBox& parent)
2330 {
2331     ASSERT(percentHeightContainerMap);
2332     for (RenderObject* child = parent.firstChild(); child; child = child->nextInPreOrder(&parent)) {
2333         if (!is<RenderBox>(*child))
2334             continue;
2335  
2336         auto& box = downcast<RenderBox>(*child);
2337         if (!hasPercentHeightDescendant(box))
2338             continue;
2339
2340         removePercentHeightDescendant(box);
2341     }
2342 }
2343
2344 LayoutUnit RenderBlock::textIndentOffset() const
2345 {
2346     LayoutUnit cw = 0;
2347     if (style().textIndent().isPercentOrCalculated())
2348         cw = containingBlock()->availableLogicalWidth();
2349     return minimumValueForLength(style().textIndent(), cw);
2350 }
2351
2352 LayoutUnit RenderBlock::logicalLeftOffsetForContent(RenderRegion* region) const
2353 {
2354     LayoutUnit logicalLeftOffset = style().isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop();
2355     if (shouldPlaceBlockDirectionScrollbarOnLeft())
2356         logicalLeftOffset += verticalScrollbarWidth();
2357     if (!region)
2358         return logicalLeftOffset;
2359     LayoutRect boxRect = borderBoxRectInRegion(region);
2360     return logicalLeftOffset + (isHorizontalWritingMode() ? boxRect.x() : boxRect.y());
2361 }
2362
2363 LayoutUnit RenderBlock::logicalRightOffsetForContent(RenderRegion* region) const
2364 {
2365     LayoutUnit logicalRightOffset = style().isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop();
2366     if (shouldPlaceBlockDirectionScrollbarOnLeft())
2367         logicalRightOffset += verticalScrollbarWidth();
2368     logicalRightOffset += availableLogicalWidth();
2369     if (!region)
2370         return logicalRightOffset;
2371     LayoutRect boxRect = borderBoxRectInRegion(region);
2372     return logicalRightOffset - (logicalWidth() - (isHorizontalWritingMode() ? boxRect.maxX() : boxRect.maxY()));
2373 }
2374
2375 LayoutUnit RenderBlock::adjustLogicalLeftOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
2376 {
2377     LayoutUnit left = offsetFromFloats;
2378
2379     if (applyTextIndent && style().isLeftToRightDirection())
2380         left += textIndentOffset();
2381
2382     if (style().lineAlign() == LineAlignNone)
2383         return left;
2384     
2385     // Push in our left offset so that it is aligned with the character grid.
2386     LayoutState* layoutState = view().layoutState();
2387     if (!layoutState)
2388         return left;
2389
2390     RenderBlock* lineGrid = layoutState->lineGrid();
2391     if (!lineGrid || lineGrid->style().writingMode() != style().writingMode())
2392         return left;
2393
2394     // FIXME: Should letter-spacing apply? This is complicated since it doesn't apply at the edge?
2395     float maxCharWidth = lineGrid->style().fontCascade().primaryFont().maxCharWidth();
2396     if (!maxCharWidth)
2397         return left;
2398
2399     LayoutUnit lineGridOffset = lineGrid->isHorizontalWritingMode() ? layoutState->lineGridOffset().width(): layoutState->lineGridOffset().height();
2400     LayoutUnit layoutOffset = lineGrid->isHorizontalWritingMode() ? layoutState->layoutOffset().width() : layoutState->layoutOffset().height();
2401     
2402     // Push in to the nearest character width (truncated so that we pixel snap left).
2403     // FIXME: Should be patched when subpixel layout lands, since this calculation doesn't have to pixel snap
2404     // any more (https://bugs.webkit.org/show_bug.cgi?id=79946).
2405     // FIXME: This is wrong for RTL (https://bugs.webkit.org/show_bug.cgi?id=79945).
2406     // FIXME: This doesn't work with columns or regions (https://bugs.webkit.org/show_bug.cgi?id=79942).
2407     // FIXME: This doesn't work when the inline position of the object isn't set ahead of time.
2408     // FIXME: Dynamic changes to the font or to the inline position need to result in a deep relayout.
2409     // (https://bugs.webkit.org/show_bug.cgi?id=79944)
2410     float remainder = fmodf(maxCharWidth - fmodf(left + layoutOffset - lineGridOffset, maxCharWidth), maxCharWidth);
2411     left += remainder;
2412     return left;
2413 }
2414
2415 LayoutUnit RenderBlock::adjustLogicalRightOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
2416 {
2417     LayoutUnit right = offsetFromFloats;
2418     
2419     if (applyTextIndent && !style().isLeftToRightDirection())
2420         right -= textIndentOffset();
2421     
2422     if (style().lineAlign() == LineAlignNone)
2423         return right;
2424     
2425     // Push in our right offset so that it is aligned with the character grid.
2426     LayoutState* layoutState = view().layoutState();
2427     if (!layoutState)
2428         return right;
2429
2430     RenderBlock* lineGrid = layoutState->lineGrid();
2431     if (!lineGrid || lineGrid->style().writingMode() != style().writingMode())
2432         return right;
2433
2434     // FIXME: Should letter-spacing apply? This is complicated since it doesn't apply at the edge?
2435     float maxCharWidth = lineGrid->style().fontCascade().primaryFont().maxCharWidth();
2436     if (!maxCharWidth)
2437         return right;
2438
2439     LayoutUnit lineGridOffset = lineGrid->isHorizontalWritingMode() ? layoutState->lineGridOffset().width(): layoutState->lineGridOffset().height();
2440     LayoutUnit layoutOffset = lineGrid->isHorizontalWritingMode() ? layoutState->layoutOffset().width() : layoutState->layoutOffset().height();
2441     
2442     // Push in to the nearest character width (truncated so that we pixel snap right).
2443     // FIXME: Should be patched when subpixel layout lands, since this calculation doesn't have to pixel snap
2444     // any more (https://bugs.webkit.org/show_bug.cgi?id=79946).
2445     // FIXME: This is wrong for RTL (https://bugs.webkit.org/show_bug.cgi?id=79945).
2446     // FIXME: This doesn't work with columns or regions (https://bugs.webkit.org/show_bug.cgi?id=79942).
2447     // FIXME: This doesn't work when the inline position of the object isn't set ahead of time.
2448     // FIXME: Dynamic changes to the font or to the inline position need to result in a deep relayout.
2449     // (https://bugs.webkit.org/show_bug.cgi?id=79944)
2450     float remainder = fmodf(fmodf(right + layoutOffset - lineGridOffset, maxCharWidth), maxCharWidth);
2451     right -= ceilf(remainder);
2452     return right;
2453 }
2454
2455 bool RenderBlock::avoidsFloats() const
2456 {
2457     // Floats can't intrude into our box if we have a non-auto column count or width.
2458     return RenderBox::avoidsFloats() || style().hasFlowFrom();
2459 }
2460
2461 bool RenderBlock::isPointInOverflowControl(HitTestResult& result, const LayoutPoint& locationInContainer, const LayoutPoint& accumulatedOffset)
2462 {
2463     if (!scrollsOverflow())
2464         return false;
2465
2466     return layer()->hitTestOverflowControls(result, roundedIntPoint(locationInContainer - toLayoutSize(accumulatedOffset)));
2467 }
2468
2469 Node* RenderBlock::nodeForHitTest() const
2470 {
2471     // If we are in the margins of block elements that are part of a
2472     // continuation we're actually still inside the enclosing element
2473     // that was split. Use the appropriate inner node.
2474     if (isRenderView())
2475         return &document();
2476     return isAnonymousBlockContinuation() ? continuation()->element() : element();
2477 }
2478
2479 bool RenderBlock::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2480 {
2481     LayoutPoint adjustedLocation(accumulatedOffset + location());
2482     LayoutSize localOffset = toLayoutSize(adjustedLocation);
2483
2484     RenderFlowThread* flowThread = flowThreadContainingBlock();
2485     RenderNamedFlowFragment* namedFlowFragment = flowThread ? downcast<RenderNamedFlowFragment>(flowThread->currentRegion()) : nullptr;
2486     // If we are now searching inside a region, make sure this element
2487     // is being fragmented into this region.
2488     if (namedFlowFragment && !flowThread->objectShouldFragmentInFlowRegion(this, namedFlowFragment))
2489         return false;
2490
2491     if (!isRenderView()) {
2492         // Check if we need to do anything at all.
2493         LayoutRect overflowBox = visualOverflowRect();
2494         flipForWritingMode(overflowBox);
2495         overflowBox.moveBy(adjustedLocation);
2496         if (!locationInContainer.intersects(overflowBox))
2497             return false;
2498     }
2499
2500     if ((hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) && isPointInOverflowControl(result, locationInContainer.point(), adjustedLocation)) {
2501         updateHitTestResult(result, locationInContainer.point() - localOffset);
2502         // FIXME: isPointInOverflowControl() doesn't handle rect-based tests yet.
2503         if (!result.addNodeToRectBasedTestResult(nodeForHitTest(), request, locationInContainer))
2504            return true;
2505     }
2506
2507     if (style().clipPath()) {
2508         switch (style().clipPath()->type()) {
2509         case ClipPathOperation::Shape: {
2510             auto& clipPath = downcast<ShapeClipPathOperation>(*style().clipPath());
2511
2512             LayoutRect referenceBoxRect;
2513             switch (clipPath.referenceBox()) {
2514             case CSSBoxType::MarginBox:
2515                 referenceBoxRect = marginBoxRect();
2516                 break;
2517             case CSSBoxType::BorderBox:
2518                 referenceBoxRect = borderBoxRect();
2519                 break;
2520             case CSSBoxType::PaddingBox:
2521                 referenceBoxRect = paddingBoxRect();
2522                 break;
2523             case CSSBoxType::ContentBox:
2524                 referenceBoxRect = contentBoxRect();
2525                 break;
2526             case CSSBoxType::BoxMissing:
2527             case CSSBoxType::Fill:
2528             case CSSBoxType::Stroke:
2529             case CSSBoxType::ViewBox:
2530                 referenceBoxRect = borderBoxRect();
2531             }
2532             if (!clipPath.pathForReferenceRect(referenceBoxRect).contains(locationInContainer.point() - localOffset, clipPath.windRule()))
2533                 return false;
2534             break;
2535         }
2536         case ClipPathOperation::Reference: {
2537             const auto& referenceClipPathOperation = downcast<ReferenceClipPathOperation>(*style().clipPath());
2538             auto* element = document().getElementById(referenceClipPathOperation.fragment());
2539             if (!element || !element->renderer())
2540                 break;
2541             if (!is<SVGClipPathElement>(*element))
2542                 break;
2543             auto& clipper = downcast<RenderSVGResourceClipper>(*element->renderer());
2544             if (!clipper.hitTestClipContent(FloatRect(borderBoxRect()), FloatPoint(locationInContainer.point() - localOffset)))
2545                 return false;
2546             break;
2547         }
2548         case ClipPathOperation::Box:
2549             break;
2550         }
2551     }
2552
2553     // If we have clipping, then we can't have any spillout.
2554     bool useOverflowClip = hasOverflowClip() && !hasSelfPaintingLayer();
2555     bool useClip = (hasControlClip() || useOverflowClip);
2556     bool checkChildren = !useClip || (hasControlClip() ? locationInContainer.intersects(controlClipRect(adjustedLocation)) : locationInContainer.intersects(overflowClipRect(adjustedLocation, namedFlowFragment, IncludeOverlayScrollbarSize)));
2557     if (checkChildren) {
2558         // Hit test descendants first.
2559         LayoutSize scrolledOffset(localOffset - toLayoutSize(scrollPosition()));
2560
2561         if (hitTestAction == HitTestFloat && hitTestFloats(request, result, locationInContainer, toLayoutPoint(scrolledOffset)))
2562             return true;
2563         if (hitTestContents(request, result, locationInContainer, toLayoutPoint(scrolledOffset), hitTestAction)) {
2564             updateHitTestResult(result, flipForWritingMode(locationInContainer.point() - localOffset));
2565             return true;
2566         }
2567     }
2568
2569     // Check if the point is outside radii.
2570     if (!isRenderView() && style().hasBorderRadius()) {
2571         LayoutRect borderRect = borderBoxRect();
2572         borderRect.moveBy(adjustedLocation);
2573         RoundedRect border = style().getRoundedBorderFor(borderRect);
2574         if (!locationInContainer.intersects(border))
2575             return false;
2576     }
2577
2578     // Now hit test our background
2579     if (hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) {
2580         LayoutRect boundsRect(adjustedLocation, size());
2581         if (visibleToHitTesting() && locationInContainer.intersects(boundsRect)) {
2582             updateHitTestResult(result, flipForWritingMode(locationInContainer.point() - localOffset));
2583             if (!result.addNodeToRectBasedTestResult(nodeForHitTest(), request, locationInContainer, boundsRect))
2584                 return true;
2585         }
2586     }
2587
2588     return false;
2589 }
2590
2591 bool RenderBlock::hitTestContents(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2592 {
2593     if (childrenInline() && !isTable())
2594         return hitTestInlineChildren(request, result, locationInContainer, accumulatedOffset, hitTestAction);
2595
2596     // Hit test our children.
2597     HitTestAction childHitTest = hitTestAction;
2598     if (hitTestAction == HitTestChildBlockBackgrounds)
2599         childHitTest = HitTestChildBlockBackground;
2600     for (auto* child = lastChildBox(); child; child = child->previousSiblingBox()) {
2601         LayoutPoint childPoint = flipForWritingModeForChild(child, accumulatedOffset);
2602         if (!child->hasSelfPaintingLayer() && !child->isFloating() && child->nodeAtPoint(request, result, locationInContainer, childPoint, childHitTest))
2603             return true;
2604     }
2605
2606     return false;
2607 }
2608
2609 static inline bool isEditingBoundary(RenderElement* ancestor, RenderObject& child)
2610 {
2611     ASSERT(!ancestor || ancestor->nonPseudoElement());
2612     ASSERT(child.nonPseudoNode());
2613     return !ancestor || !ancestor->parent() || (ancestor->hasLayer() && ancestor->parent()->isRenderView())
2614         || ancestor->nonPseudoElement()->hasEditableStyle() == child.nonPseudoNode()->hasEditableStyle();
2615 }
2616
2617 // FIXME: This function should go on RenderObject as an instance method. Then
2618 // all cases in which positionForPoint recurs could call this instead to
2619 // prevent crossing editable boundaries. This would require many tests.
2620 VisiblePosition positionForPointRespectingEditingBoundaries(RenderBlock& parent, RenderBox& child, const LayoutPoint& pointInParentCoordinates)
2621 {
2622     LayoutPoint childLocation = child.location();
2623     if (child.isInFlowPositioned())
2624         childLocation += child.offsetForInFlowPosition();
2625
2626     // FIXME: This is wrong if the child's writing-mode is different from the parent's.
2627     LayoutPoint pointInChildCoordinates(toLayoutPoint(pointInParentCoordinates - childLocation));
2628
2629     // If this is an anonymous renderer, we just recur normally
2630     Element* childElement= child.nonPseudoElement();
2631     if (!childElement)
2632         return child.positionForPoint(pointInChildCoordinates, nullptr);
2633
2634     // Otherwise, first make sure that the editability of the parent and child agree.
2635     // If they don't agree, then we return a visible position just before or after the child
2636     RenderElement* ancestor = &parent;
2637     while (ancestor && !ancestor->nonPseudoElement())
2638         ancestor = ancestor->parent();
2639
2640     // If we can't find an ancestor to check editability on, or editability is unchanged, we recur like normal
2641     if (isEditingBoundary(ancestor, child))
2642         return child.positionForPoint(pointInChildCoordinates, nullptr);
2643
2644     // Otherwise return before or after the child, depending on if the click was to the logical left or logical right of the child
2645     LayoutUnit childMiddle = parent.logicalWidthForChild(child) / 2;
2646     LayoutUnit logicalLeft = parent.isHorizontalWritingMode() ? pointInChildCoordinates.x() : pointInChildCoordinates.y();
2647     if (logicalLeft < childMiddle)
2648         return ancestor->createVisiblePosition(childElement->computeNodeIndex(), DOWNSTREAM);
2649     return ancestor->createVisiblePosition(childElement->computeNodeIndex() + 1, UPSTREAM);
2650 }
2651
2652 VisiblePosition RenderBlock::positionForPointWithInlineChildren(const LayoutPoint&, const RenderRegion*)
2653 {
2654     ASSERT_NOT_REACHED();
2655     return VisiblePosition();
2656 }
2657
2658 static inline bool isChildHitTestCandidate(const RenderBox& box)
2659 {
2660     return box.height() && box.style().visibility() == VISIBLE && !box.isFloatingOrOutOfFlowPositioned() && !box.isInFlowRenderFlowThread();
2661 }
2662
2663 // Valid candidates in a FlowThread must be rendered by the region.
2664 static inline bool isChildHitTestCandidate(const RenderBox& box, const RenderRegion* region, const LayoutPoint& point)
2665 {
2666     if (!isChildHitTestCandidate(box))
2667         return false;
2668     if (!region)
2669         return true;
2670     const RenderBlock& block = is<RenderBlock>(box) ? downcast<RenderBlock>(box) : *box.containingBlock();
2671     return block.regionAtBlockOffset(point.y()) == region;
2672 }
2673
2674 VisiblePosition RenderBlock::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
2675 {
2676     if (isTable())
2677         return RenderBox::positionForPoint(point, region);
2678
2679     if (isReplaced()) {
2680         // FIXME: This seems wrong when the object's writing-mode doesn't match the line's writing-mode.
2681         LayoutUnit pointLogicalLeft = isHorizontalWritingMode() ? point.x() : point.y();
2682         LayoutUnit pointLogicalTop = isHorizontalWritingMode() ? point.y() : point.x();
2683
2684         if (pointLogicalTop < 0 || (pointLogicalTop < logicalHeight() && pointLogicalLeft < 0))
2685             return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
2686         if (pointLogicalTop >= logicalHeight() || (pointLogicalTop >= 0 && pointLogicalLeft >= logicalWidth()))
2687             return createVisiblePosition(caretMaxOffset(), DOWNSTREAM);
2688     } 
2689
2690     LayoutPoint pointInContents = point;
2691     offsetForContents(pointInContents);
2692     LayoutPoint pointInLogicalContents(pointInContents);
2693     if (!isHorizontalWritingMode())
2694         pointInLogicalContents = pointInLogicalContents.transposedPoint();
2695
2696     if (childrenInline())
2697         return positionForPointWithInlineChildren(pointInLogicalContents, region);
2698
2699     RenderBox* lastCandidateBox = lastChildBox();
2700
2701     if (!region)
2702         region = regionAtBlockOffset(pointInLogicalContents.y());
2703
2704     while (lastCandidateBox && !isChildHitTestCandidate(*lastCandidateBox, region, pointInLogicalContents))
2705         lastCandidateBox = lastCandidateBox->previousSiblingBox();
2706
2707     bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
2708     if (lastCandidateBox) {
2709         if (pointInLogicalContents.y() > logicalTopForChild(*lastCandidateBox)
2710             || (!blocksAreFlipped && pointInLogicalContents.y() == logicalTopForChild(*lastCandidateBox)))
2711             return positionForPointRespectingEditingBoundaries(*this, *lastCandidateBox, pointInContents);
2712
2713         for (auto* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
2714             if (!isChildHitTestCandidate(*childBox, region, pointInLogicalContents))
2715                 continue;
2716             LayoutUnit childLogicalBottom = logicalTopForChild(*childBox) + logicalHeightForChild(*childBox);
2717             // We hit child if our click is above the bottom of its padding box (like IE6/7 and FF3).
2718             if (isChildHitTestCandidate(*childBox, region, pointInLogicalContents) && (pointInLogicalContents.y() < childLogicalBottom
2719                 || (blocksAreFlipped && pointInLogicalContents.y() == childLogicalBottom)))
2720                 return positionForPointRespectingEditingBoundaries(*this, *childBox, pointInContents);
2721         }
2722     }
2723
2724     // We only get here if there are no hit test candidate children below the click.
2725     return RenderBox::positionForPoint(point, region);
2726 }
2727
2728 void RenderBlock::offsetForContents(LayoutPoint& offset) const
2729 {
2730     offset = flipForWritingMode(offset);
2731     offset += toLayoutSize(scrollPosition());
2732     offset = flipForWritingMode(offset);
2733 }
2734
2735 void RenderBlock::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
2736 {
2737     ASSERT(!childrenInline());
2738     
2739     computeBlockPreferredLogicalWidths(minLogicalWidth, maxLogicalWidth);
2740
2741     maxLogicalWidth = std::max(minLogicalWidth, maxLogicalWidth);
2742
2743     int scrollbarWidth = intrinsicScrollbarLogicalWidth();
2744     maxLogicalWidth += scrollbarWidth;
2745     minLogicalWidth += scrollbarWidth;
2746 }
2747
2748 void RenderBlock::computePreferredLogicalWidths()
2749 {
2750     ASSERT(preferredLogicalWidthsDirty());
2751
2752     // FIXME: Do not even try to reshuffle first letter renderers when we are not in layout
2753     // until after webkit.org/b/163848 is fixed.
2754     updateFirstLetter(view().frameView().isInRenderTreeLayout() ? RenderTreeMutationIsAllowed::Yes : RenderTreeMutationIsAllowed::No);
2755
2756     m_minPreferredLogicalWidth = 0;
2757     m_maxPreferredLogicalWidth = 0;
2758
2759     const RenderStyle& styleToUse = style();
2760     if (!isTableCell() && styleToUse.logicalWidth().isFixed() && styleToUse.logicalWidth().value() >= 0
2761         && !(isDeprecatedFlexItem() && !styleToUse.logicalWidth().intValue()))
2762         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalWidth().value());
2763     else
2764         computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
2765     
2766     if (styleToUse.logicalMinWidth().isFixed() && styleToUse.logicalMinWidth().value() > 0) {
2767         m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
2768         m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
2769     }
2770     
2771     if (styleToUse.logicalMaxWidth().isFixed()) {
2772         m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
2773         m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
2774     }
2775     
2776     LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
2777     m_minPreferredLogicalWidth += borderAndPadding;
2778     m_maxPreferredLogicalWidth += borderAndPadding;
2779
2780     setPreferredLogicalWidthsDirty(false);
2781 }
2782
2783 void RenderBlock::computeBlockPreferredLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
2784 {
2785     const RenderStyle& styleToUse = style();
2786     bool nowrap = styleToUse.whiteSpace() == NOWRAP;
2787
2788     RenderObject* child = firstChild();
2789     RenderBlock* containingBlock = this->containingBlock();
2790     LayoutUnit floatLeftWidth = 0, floatRightWidth = 0;
2791     while (child) {
2792         // Positioned children don't affect the min/max width
2793         if (child->isOutOfFlowPositioned()) {
2794             child = child->nextSibling();
2795             continue;
2796         }
2797
2798         const RenderStyle& childStyle = child->style();
2799         if (child->isFloating() || (is<RenderBox>(*child) && downcast<RenderBox>(*child).avoidsFloats())) {
2800             LayoutUnit floatTotalWidth = floatLeftWidth + floatRightWidth;
2801             if (childStyle.clear() & CLEFT) {
2802                 maxLogicalWidth = std::max(floatTotalWidth, maxLogicalWidth);
2803                 floatLeftWidth = 0;
2804             }
2805             if (childStyle.clear() & CRIGHT) {
2806                 maxLogicalWidth = std::max(floatTotalWidth, maxLogicalWidth);
2807                 floatRightWidth = 0;
2808             }
2809         }
2810
2811         // A margin basically has three types: fixed, percentage, and auto (variable).
2812         // Auto and percentage margins simply become 0 when computing min/max width.
2813         // Fixed margins can be added in as is.
2814         Length startMarginLength = childStyle.marginStartUsing(&styleToUse);
2815         Length endMarginLength = childStyle.marginEndUsing(&styleToUse);
2816         LayoutUnit margin = 0;
2817         LayoutUnit marginStart = 0;
2818         LayoutUnit marginEnd = 0;
2819         if (startMarginLength.isFixed())
2820             marginStart += startMarginLength.value();
2821         if (endMarginLength.isFixed())
2822             marginEnd += endMarginLength.value();
2823         margin = marginStart + marginEnd;
2824
2825         LayoutUnit childMinPreferredLogicalWidth, childMaxPreferredLogicalWidth;
2826         if (is<RenderBox>(*child) && child->isHorizontalWritingMode() != isHorizontalWritingMode()) {
2827             auto& childBox = downcast<RenderBox>(*child);
2828             LogicalExtentComputedValues computedValues;
2829             childBox.computeLogicalHeight(childBox.borderAndPaddingLogicalHeight(), 0, computedValues);
2830             childMinPreferredLogicalWidth = childMaxPreferredLogicalWidth = computedValues.m_extent;
2831         } else {
2832             childMinPreferredLogicalWidth = child->minPreferredLogicalWidth();
2833             childMaxPreferredLogicalWidth = child->maxPreferredLogicalWidth();
2834
2835             if (is<RenderBlock>(*child)) {
2836                 const Length& computedInlineSize = child->style().logicalWidth();
2837                 if (computedInlineSize.isMaxContent())
2838                     childMinPreferredLogicalWidth = childMaxPreferredLogicalWidth;
2839                 else if (computedInlineSize.isMinContent())
2840                     childMaxPreferredLogicalWidth = childMinPreferredLogicalWidth;
2841             }
2842         }
2843
2844         LayoutUnit w = childMinPreferredLogicalWidth + margin;
2845         minLogicalWidth = std::max(w, minLogicalWidth);
2846         
2847         // IE ignores tables for calculation of nowrap. Makes some sense.
2848         if (nowrap && !child->isTable())
2849             maxLogicalWidth = std::max(w, maxLogicalWidth);
2850
2851         w = childMaxPreferredLogicalWidth + margin;
2852
2853         if (!child->isFloating()) {
2854             if (is<RenderBox>(*child) && downcast<RenderBox>(*child).avoidsFloats()) {
2855                 // Determine a left and right max value based off whether or not the floats can fit in the
2856                 // margins of the object.  For negative margins, we will attempt to overlap the float if the negative margin
2857                 // is smaller than the float width.
2858                 bool ltr = containingBlock ? containingBlock->style().isLeftToRightDirection() : styleToUse.isLeftToRightDirection();
2859                 LayoutUnit marginLogicalLeft = ltr ? marginStart : marginEnd;
2860                 LayoutUnit marginLogicalRight = ltr ? marginEnd : marginStart;
2861                 LayoutUnit maxLeft = marginLogicalLeft > 0 ? std::max(floatLeftWidth, marginLogicalLeft) : floatLeftWidth + marginLogicalLeft;
2862                 LayoutUnit maxRight = marginLogicalRight > 0 ? std::max(floatRightWidth, marginLogicalRight) : floatRightWidth + marginLogicalRight;
2863                 w = childMaxPreferredLogicalWidth + maxLeft + maxRight;
2864                 w = std::max(w, floatLeftWidth + floatRightWidth);
2865             }
2866             else
2867                 maxLogicalWidth = std::max(floatLeftWidth + floatRightWidth, maxLogicalWidth);
2868             floatLeftWidth = floatRightWidth = 0;
2869         }
2870         
2871         if (child->isFloating()) {
2872             if (childStyle.floating() == LeftFloat)
2873                 floatLeftWidth += w;
2874             else
2875                 floatRightWidth += w;
2876         } else
2877             maxLogicalWidth = std::max(w, maxLogicalWidth);
2878         
2879         child = child->nextSibling();
2880     }
2881
2882     // Always make sure these values are non-negative.
2883     minLogicalWidth = std::max<LayoutUnit>(0, minLogicalWidth);
2884     maxLogicalWidth = std::max<LayoutUnit>(0, maxLogicalWidth);
2885
2886     maxLogicalWidth = std::max(floatLeftWidth + floatRightWidth, maxLogicalWidth);
2887 }
2888
2889 bool RenderBlock::hasLineIfEmpty() const
2890 {
2891     if (!element())
2892         return false;
2893     
2894     if (element()->isRootEditableElement())
2895         return true;
2896     
2897     return false;
2898 }
2899
2900 LayoutUnit RenderBlock::lineHeight(bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
2901 {
2902     // Anonymous inline blocks don't include margins or any real line height.
2903     if (isAnonymousInlineBlock() && linePositionMode == PositionOnContainingLine)
2904         return direction == HorizontalLine ? height() : width();
2905     
2906     // Inline blocks are replaced elements. Otherwise, just pass off to
2907     // the base class.  If we're being queried as though we're the root line
2908     // box, then the fact that we're an inline-block is irrelevant, and we behave
2909     // just like a block.
2910     if (isReplaced() && linePositionMode == PositionOnContainingLine)
2911         return RenderBox::lineHeight(firstLine, direction, linePositionMode);
2912
2913     if (firstLine && view().usesFirstLineRules()) {
2914         auto& s = firstLine ? firstLineStyle() : style();
2915         if (&s != &style())
2916             return s.computedLineHeight();
2917     }
2918     
2919     return style().computedLineHeight();
2920 }
2921
2922 int RenderBlock::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
2923 {
2924     // Inline blocks are replaced elements. Otherwise, just pass off to
2925     // the base class.  If we're being queried as though we're the root line
2926     // box, then the fact that we're an inline-block is irrelevant, and we behave
2927     // just like a block.
2928     if (isReplaced() && linePositionMode == PositionOnContainingLine) {
2929         if (isAnonymousInlineBlock())
2930             return direction == HorizontalLine ? height() : width();
2931         
2932         // For "leaf" theme objects, let the theme decide what the baseline position is.
2933         // FIXME: Might be better to have a custom CSS property instead, so that if the theme
2934         // is turned off, checkboxes/radios will still have decent baselines.
2935         // FIXME: Need to patch form controls to deal with vertical lines.
2936         if (style().hasAppearance() && !theme().isControlContainer(style().appearance()))
2937             return theme().baselinePosition(*this);
2938             
2939         // CSS2.1 states that the baseline of an inline block is the baseline of the last line box in
2940         // the normal flow.  We make an exception for marquees, since their baselines are meaningless
2941         // (the content inside them moves).  This matches WinIE as well, which just bottom-aligns them.
2942         // We also give up on finding a baseline if we have a vertical scrollbar, or if we are scrolled
2943         // vertically (e.g., an overflow:hidden block that has had scrollTop moved).
2944         bool ignoreBaseline = (layer() && (layer()->marquee() || (direction == HorizontalLine ? (layer()->verticalScrollbar() || layer()->scrollOffset().y() != 0)
2945             : (layer()->horizontalScrollbar() || layer()->scrollOffset().x() != 0)))) || (isWritingModeRoot() && !isRubyRun());
2946         
2947         Optional<int> baselinePos = ignoreBaseline ? Optional<int>() : inlineBlockBaseline(direction);
2948         
2949         if (isDeprecatedFlexibleBox()) {
2950             // Historically, we did this check for all baselines. But we can't
2951             // remove this code from deprecated flexbox, because it effectively
2952             // breaks -webkit-line-clamp, which is used in the wild -- we would
2953             // calculate the baseline as if -webkit-line-clamp wasn't used.
2954             // For simplicity, we use this for all uses of deprecated flexbox.
2955             LayoutUnit bottomOfContent = direction == HorizontalLine ? borderTop() + paddingTop() + contentHeight() : borderRight() + paddingRight() + contentWidth();
2956             if (baselinePos && baselinePos.value() > bottomOfContent)
2957                 baselinePos = Optional<int>();
2958         }
2959         if (baselinePos)
2960             return direction == HorizontalLine ? marginTop() + baselinePos.value() : marginRight() + baselinePos.value();
2961
2962         return RenderBox::baselinePosition(baselineType, firstLine, direction, linePositionMode);
2963     }
2964
2965     const RenderStyle& style = firstLine ? firstLineStyle() : this->style();
2966     const FontMetrics& fontMetrics = style.fontMetrics();
2967     return fontMetrics.ascent(baselineType) + (lineHeight(firstLine, direction, linePositionMode) - fontMetrics.height()) / 2;
2968 }
2969
2970 LayoutUnit RenderBlock::minLineHeightForReplacedRenderer(bool isFirstLine, LayoutUnit replacedHeight) const
2971 {
2972     if (!document().inNoQuirksMode() && replacedHeight)
2973         return replacedHeight;
2974
2975     const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
2976     if (!(style.lineBoxContain() & LineBoxContainBlock))
2977         return 0;
2978
2979     return std::max<LayoutUnit>(replacedHeight, lineHeight(isFirstLine, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
2980 }
2981
2982 Optional<int> RenderBlock::firstLineBaseline() const
2983 {
2984     if (isWritingModeRoot() && !isRubyRun())
2985         return Optional<int>();
2986
2987     for (RenderBox* curr = firstChildBox(); curr; curr = curr->nextSiblingBox()) {
2988         if (!curr->isFloatingOrOutOfFlowPositioned()) {
2989             if (Optional<int> result = curr->firstLineBaseline())
2990                 return Optional<int>(curr->logicalTop() + result.value()); // Translate to our coordinate space.
2991         }
2992     }
2993
2994     return Optional<int>();
2995 }
2996
2997 Optional<int> RenderBlock::inlineBlockBaseline(LineDirectionMode lineDirection) const
2998 {
2999     if (isWritingModeRoot() && !isRubyRun())
3000         return Optional<int>();
3001
3002     bool haveNormalFlowChild = false;
3003     for (auto* box = lastChildBox(); box; box = box->previousSiblingBox()) {
3004         if (box->isFloatingOrOutOfFlowPositioned())
3005             continue;
3006         haveNormalFlowChild = true;
3007         if (Optional<int> result = box->inlineBlockBaseline(lineDirection))
3008             return Optional<int>(box->logicalTop() + result.value()); // Translate to our coordinate space.
3009     }
3010
3011     if (!haveNormalFlowChild && hasLineIfEmpty()) {
3012         auto& fontMetrics = firstLineStyle().fontMetrics();
3013         return Optional<int>(fontMetrics.ascent()
3014             + (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
3015             + (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight()));
3016     }
3017
3018     return Optional<int>();
3019 }
3020
3021 static inline bool isRenderBlockFlowOrRenderButton(RenderElement& renderElement)
3022 {
3023     // We include isRenderButton in this check because buttons are implemented
3024     // using flex box but should still support first-line|first-letter.
3025     // The flex box and specs require that flex box and grid do not support
3026     // first-line|first-letter, though.
3027     // FIXME: Remove when buttons are implemented with align-items instead of
3028     // flex box.
3029     return renderElement.isRenderBlockFlow() || renderElement.isRenderButton();
3030 }
3031
3032 RenderBlock* RenderBlock::firstLineBlock() const
3033 {
3034     RenderBlock* firstLineBlock = const_cast<RenderBlock*>(this);
3035     bool hasPseudo = false;
3036     while (true) {
3037         hasPseudo = firstLineBlock->style().hasPseudoStyle(FIRST_LINE);
3038         if (hasPseudo)
3039             break;
3040         RenderElement* parentBlock = firstLineBlock->parent();
3041         if (firstLineBlock->isReplaced() || firstLineBlock->isFloating()
3042             || !parentBlock || parentBlock->firstChild() != firstLineBlock || !isRenderBlockFlowOrRenderButton(*parentBlock))
3043             break;
3044         firstLineBlock = downcast<RenderBlock>(parentBlock);
3045     } 
3046     
3047     if (!hasPseudo)
3048         return nullptr;
3049     
3050     return firstLineBlock;
3051 }
3052
3053 static const RenderStyle& styleForFirstLetter(RenderElement* firstLetterBlock, RenderObject* firstLetterContainer)
3054 {
3055     auto* pseudoStyle = firstLetterBlock->getMutableCachedPseudoStyle(FIRST_LETTER, &firstLetterContainer->firstLineStyle());
3056     
3057     // If we have an initial letter drop that is >= 1, then we need to force floating to be on.
3058     if (pseudoStyle->initialLetterDrop() >= 1 && !pseudoStyle->isFloating())
3059         pseudoStyle->setFloating(pseudoStyle->isLeftToRightDirection() ? LeftFloat : RightFloat);
3060
3061     // We have to compute the correct font-size for the first-letter if it has an initial letter height set.
3062     RenderObject* paragraph = firstLetterContainer->isRenderBlockFlow() ? firstLetterContainer : firstLetterContainer->containingBlock();
3063     if (pseudoStyle->initialLetterHeight() >= 1 && pseudoStyle->fontMetrics().hasCapHeight() && paragraph->style().fontMetrics().hasCapHeight()) {
3064         // FIXME: For ideographic baselines, we want to go from line edge to line edge. This is equivalent to (N-1)*line-height + the font height.
3065         // We don't yet support ideographic baselines.
3066         // For an N-line first-letter and for alphabetic baselines, the cap-height of the first letter needs to equal (N-1)*line-height of paragraph lines + cap-height of the paragraph
3067         // Mathematically we can't rely on font-size, since font().height() doesn't necessarily match. For reliability, the best approach is simply to
3068         // compare the final measured cap-heights of the two fonts in order to get to the closest possible value.
3069         pseudoStyle->setLineBoxContain(LineBoxContainInitialLetter);
3070         int lineHeight = paragraph->style().computedLineHeight();
3071         
3072         // Set the font to be one line too big and then ratchet back to get to a precise fit. We can't just set the desired font size based off font height metrics
3073         // because many fonts bake ascent into the font metrics. Therefore we have to look at actual measured cap height values in order to know when we have a good fit.
3074         auto newFontDescription = pseudoStyle->fontDescription();
3075         float capRatio = pseudoStyle->fontMetrics().floatCapHeight() / pseudoStyle->fontSize();
3076         float startingFontSize = ((pseudoStyle->initialLetterHeight() - 1) * lineHeight + paragraph->style().fontMetrics().capHeight()) / capRatio;
3077         newFontDescription.setSpecifiedSize(startingFontSize);
3078         newFontDescription.setComputedSize(startingFontSize);
3079         pseudoStyle->setFontDescription(newFontDescription);
3080         pseudoStyle->fontCascade().update(pseudoStyle->fontCascade().fontSelector());
3081         
3082         int desiredCapHeight = (pseudoStyle->initialLetterHeight() - 1) * lineHeight + paragraph->style().fontMetrics().capHeight();
3083         int actualCapHeight = pseudoStyle->fontMetrics().capHeight();
3084         while (actualCapHeight > desiredCapHeight) {
3085             auto newFontDescription = pseudoStyle->fontDescription();
3086             newFontDescription.setSpecifiedSize(newFontDescription.specifiedSize() - 1);
3087             newFontDescription.setComputedSize(newFontDescription.computedSize() -1);
3088             pseudoStyle->setFontDescription(newFontDescription);
3089             pseudoStyle->fontCascade().update(pseudoStyle->fontCascade().fontSelector());
3090             actualCapHeight = pseudoStyle->fontMetrics().capHeight();
3091         }
3092     }
3093     
3094     // Force inline display (except for floating first-letters).
3095     pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
3096     // CSS2 says first-letter can't be positioned.
3097     pseudoStyle->setPosition(StaticPosition);
3098     return *pseudoStyle;
3099 }
3100
3101 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
3102 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps), "close" (Pe),
3103 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that precedes or follows the first letter should be included"
3104 static inline bool isPunctuationForFirstLetter(UChar c)
3105 {
3106     return U_GET_GC_MASK(c) & (U_GC_PS_MASK | U_GC_PE_MASK | U_GC_PI_MASK | U_GC_PF_MASK | U_GC_PO_MASK);
3107 }
3108
3109 static inline bool shouldSkipForFirstLetter(UChar c)
3110 {
3111     return isSpaceOrNewline(c) || c == noBreakSpace || isPunctuationForFirstLetter(c);
3112 }
3113
3114 static inline RenderBlock* findFirstLetterBlock(RenderBlock* start)
3115 {
3116     RenderBlock* firstLetterBlock = start;
3117     while (true) {
3118         bool canHaveFirstLetterRenderer = firstLetterBlock->style().hasPseudoStyle(FIRST_LETTER)
3119             && firstLetterBlock->canHaveGeneratedChildren()
3120             && isRenderBlockFlowOrRenderButton(*firstLetterBlock);
3121         if (canHaveFirstLetterRenderer)
3122             return firstLetterBlock;
3123
3124         RenderElement* parentBlock = firstLetterBlock->parent();
3125         if (firstLetterBlock->isReplaced() || !parentBlock || parentBlock->firstChild() != firstLetterBlock
3126             || !isRenderBlockFlowOrRenderButton(*parentBlock))
3127             return nullptr;
3128         firstLetterBlock = downcast<RenderBlock>(parentBlock);
3129     } 
3130
3131     return nullptr;
3132 }
3133
3134 void RenderBlock::updateFirstLetterStyle(RenderElement* firstLetterBlock, RenderObject* currentChild)
3135 {
3136     RenderElement* firstLetter = currentChild->parent();
3137     RenderElement* firstLetterContainer = firstLetter->parent();
3138     auto& pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetterContainer);
3139     ASSERT(firstLetter->isFloating() || firstLetter->isInline());
3140
3141     if (Style::determineChange(firstLetter->style(), pseudoStyle) == Style::Detach) {
3142         // The first-letter renderer needs to be replaced. Create a new renderer of the right type.
3143         RenderBoxModelObject* newFirstLetter;
3144         if (pseudoStyle.display() == INLINE)
3145             newFirstLetter = new RenderInline(document(), RenderStyle::clone(pseudoStyle));
3146         else
3147             newFirstLetter = new RenderBlockFlow(document(), RenderStyle::clone(pseudoStyle));
3148         newFirstLetter->initializeStyle();
3149
3150         // Move the first letter into the new renderer.
3151         LayoutStateDisabler layoutStateDisabler(view());
3152         while (RenderObject* child = firstLetter->firstChild()) {
3153             if (is<RenderText>(*child))
3154                 downcast<RenderText>(*child).removeAndDestroyTextBoxes();
3155             firstLetter->removeChild(*child);
3156             newFirstLetter->addChild(child, nullptr);
3157         }
3158
3159         RenderObject* nextSibling = firstLetter->nextSibling();
3160         if (RenderTextFragment* remainingText = downcast<RenderBoxModelObject>(*firstLetter).firstLetterRemainingText()) {
3161             ASSERT(remainingText->isAnonymous() || remainingText->textNode()->renderer() == remainingText);
3162             // Replace the old renderer with the new one.
3163             remainingText->setFirstLetter(*newFirstLetter);
3164             newFirstLetter->setFirstLetterRemainingText(remainingText);
3165         }
3166         // To prevent removal of single anonymous block in RenderBlock::removeChild and causing
3167         // |nextSibling| to go stale, we remove the old first letter using removeChildNode first.
3168         firstLetterContainer->removeChildInternal(*firstLetter, NotifyChildren);
3169         firstLetter->destroy();
3170         firstLetter = newFirstLetter;
3171         firstLetterContainer->addChild(firstLetter, nextSibling);
3172     } else
3173         firstLetter->setStyle(RenderStyle::clone(pseudoStyle));
3174 }
3175
3176 void RenderBlock::createFirstLetterRenderer(RenderElement* firstLetterBlock, RenderText* currentTextChild)
3177 {
3178     RenderElement* firstLetterContainer = currentTextChild->parent();
3179     auto& pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetterContainer);
3180     RenderBoxModelObject* firstLetter = nullptr;
3181     if (pseudoStyle.display() == INLINE)
3182         firstLetter = new RenderInline(document(), RenderStyle::clone(pseudoStyle));
3183     else
3184         firstLetter = new RenderBlockFlow(document(), RenderStyle::clone(pseudoStyle));
3185     firstLetter->initializeStyle();
3186     firstLetterContainer->addChild(firstLetter, currentTextChild);
3187
3188     // The original string is going to be either a generated content string or a DOM node's
3189     // string.  We want the original string before it got transformed in case first-letter has
3190     // no text-transform or a different text-transform applied to it.
3191     String oldText = currentTextChild->originalText();
3192     ASSERT(!oldText.isNull());
3193
3194     if (!oldText.isEmpty()) {
3195         unsigned length = 0;
3196
3197         // Account for leading spaces and punctuation.
3198         while (length < oldText.length() && shouldSkipForFirstLetter(oldText[length]))
3199             length++;
3200
3201         // Account for first grapheme cluster.
3202         length += numCharactersInGraphemeClusters(StringView(oldText).substring(length), 1);
3203         
3204         // Keep looking for whitespace and allowed punctuation, but avoid
3205         // accumulating just whitespace into the :first-letter.
3206         for (unsigned scanLength = length; scanLength < oldText.length(); ++scanLength) {
3207             UChar c = oldText[scanLength];
3208             
3209             if (!shouldSkipForFirstLetter(c))
3210                 break;
3211
3212             if (isPunctuationForFirstLetter(c))
3213                 length = scanLength + 1;
3214          }
3215          
3216         // Construct a text fragment for the text after the first letter.
3217         // This text fragment might be empty.
3218         RenderTextFragment* remainingText;
3219         if (currentTextChild->textNode())
3220             remainingText = new RenderTextFragment(*currentTextChild->textNode(), oldText, length, oldText.length() - length);
3221         else
3222             remainingText = new RenderTextFragment(document(), oldText, length, oldText.length() - length);
3223
3224         if (remainingText->textNode())
3225             remainingText->textNode()->setRenderer(remainingText);
3226
3227         firstLetterContainer->addChild(remainingText, currentTextChild);
3228         firstLetterContainer->removeChild(*currentTextChild);
3229         remainingText->setFirstLetter(*firstLetter);
3230         firstLetter->setFirstLetterRemainingText(remainingText);
3231         
3232         // construct text fragment for the first letter
3233         RenderTextFragment* letter;
3234         if (remainingText->textNode())
3235             letter = new RenderTextFragment(*remainingText->textNode(), oldText, 0, length);
3236         else
3237             letter = new RenderTextFragment(document(), oldText, 0, length);
3238
3239         firstLetter->addChild(letter);
3240
3241         currentTextChild->destroy();
3242     }
3243 }
3244     
3245 void RenderBlock::getFirstLetter(RenderObject*& firstLetter, RenderElement*& firstLetterContainer, RenderObject* skipObject)
3246 {
3247     firstLetter = nullptr;
3248     firstLetterContainer = nullptr;
3249
3250     if (!view().usesFirstLetterRules())
3251         return;
3252
3253     // Don't recur
3254     if (style().styleType() == FIRST_LETTER)
3255         return;
3256     
3257     // FIXME: We need to destroy the first-letter object if it is no longer the first child. Need to find
3258     // an efficient way to check for that situation though before implementing anything.
3259     firstLetterContainer = findFirstLetterBlock(this);
3260     if (!firstLetterContainer)
3261         return;
3262     
3263     // Drill into inlines looking for our first text descendant.
3264     firstLetter = firstLetterContainer->firstChild();
3265     while (firstLetter) {
3266         if (is<RenderText>(*firstLetter)) {
3267             if (firstLetter == skipObject) {
3268                 firstLetter = firstLetter->nextSibling();
3269                 continue;
3270             }
3271             
3272             break;
3273         }
3274
3275         RenderElement& current = downcast<RenderElement>(*firstLetter);
3276         if (is<RenderListMarker