1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003-2023 Apple Inc. All rights reserved.
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderInline.h"
#include "BorderPainter.h"
#include "Chrome.h"
#include "FloatQuad.h"
#include "FrameSelection.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "InlineIteratorBoxInlines.h"
#include "InlineIteratorInlineBox.h"
#include "InlineIteratorLineBox.h"
#include "LayoutIntegrationLineLayout.h"
#include "LegacyInlineTextBox.h"
#include "RenderBlock.h"
#include "RenderBoxInlines.h"
#include "RenderChildIterator.h"
#include "RenderElementInlines.h"
#include "RenderFragmentedFlow.h"
#include "RenderGeometryMap.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderLayoutState.h"
#include "RenderLineBreak.h"
#include "RenderListMarker.h"
#include "RenderTable.h"
#include "RenderTheme.h"
#include "RenderTreeBuilder.h"
#include "RenderView.h"
#include "Settings.h"
#include "StyleInheritedData.h"
#include "TransformState.h"
#include "VisiblePosition.h"
#include "WillChangeData.h"
#include <wtf/SetForScope.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(RenderInline);
RenderInline::RenderInline(Type type, Element& element, RenderStyle&& style)
: RenderBoxModelObject(type, element, WTFMove(style), TypeFlag::IsRenderInline, { })
{
setChildrenInline(true);
ASSERT(isRenderInline());
}
RenderInline::RenderInline(Type type, Document& document, RenderStyle&& style)
: RenderBoxModelObject(type, document, WTFMove(style), TypeFlag::IsRenderInline, { })
{
setChildrenInline(true);
ASSERT(isRenderInline());
}
RenderInline::~RenderInline() = default;
void RenderInline::willBeDestroyed()
{
#if ASSERT_ENABLED
// Make sure we do not retain "this" in the continuation outline table map of our containing blocks.
if (parent() && style().usedVisibility() == Visibility::Visible && hasOutline()) {
bool containingBlockPaintsContinuationOutline = continuation() || isContinuation();
if (containingBlockPaintsContinuationOutline) {
if (RenderBlock* cb = containingBlock()) {
if (RenderBlock* cbCb = cb->containingBlock())
ASSERT(!cbCb->paintsContinuationOutline(this));
}
}
}
#endif // ASSERT_ENABLED
if (!renderTreeBeingDestroyed()) {
if (auto* inlineBox = firstLegacyInlineBox()) {
// We can't wait for RenderBoxModelObject::destroy to clear the selection,
// because by then we will have nuked the line boxes.
if (isSelectionBorder())
frame().selection().setNeedsSelectionUpdate();
// If line boxes are contained inside a root, that means we're an inline.
// In that case, we need to remove all the line boxes so that the parent
// lines aren't pointing to deleted children. If the first line box does
// not have a parent that means they are either already disconnected or
// root lines that can just be destroyed without disconnecting.
if (inlineBox->parent()) {
for (auto* box = inlineBox; box; box = box->nextLineBox())
box->removeFromParent();
}
} else if (auto* parent = this->parent(); parent && parent->isSVGRenderer())
parent->dirtyLineFromChangedChild();
}
m_legacyLineBoxes.deleteLineBoxes();
RenderBoxModelObject::willBeDestroyed();
}
void RenderInline::updateFromStyle()
{
RenderBoxModelObject::updateFromStyle();
// FIXME: Support transforms and reflections on inline flows someday.
setHasTransformRelatedProperty(false);
setHasReflection(false);
}
static RenderElement* inFlowPositionedInlineAncestor(RenderElement* p)
{
while (p && p->isRenderInline()) {
if (p->isInFlowPositioned())
return p;
p = p->parent();
}
return nullptr;
}
static void updateStyleOfAnonymousBlockContinuations(const RenderBlock& block, const RenderStyle* newStyle, const RenderStyle* oldStyle)
{
// If any descendant blocks exist then they will be in the next anonymous block and its siblings.
for (RenderBox* box = block.nextSiblingBox(); box && box->isAnonymousBlock(); box = box->nextSiblingBox()) {
if (box->style().position() == newStyle->position())
continue;
CheckedPtr block = dynamicDowncast<RenderBlock>(*box);
if (!block)
continue;
if (!block->isContinuation())
continue;
// If we are no longer in-flow positioned but our descendant block(s) still have an in-flow positioned ancestor then
// their containing anonymous block should keep its in-flow positioning.
RenderInline* continuation = block->inlineContinuation();
if (oldStyle->hasInFlowPosition() && inFlowPositionedInlineAncestor(continuation))
continue;
auto blockStyle = RenderStyle::createAnonymousStyleWithDisplay(block->style(), DisplayType::Block);
blockStyle.setPosition(newStyle->position());
block->setStyle(WTFMove(blockStyle));
}
}
void RenderInline::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
RenderBoxModelObject::styleWillChange(diff, newStyle);
// RenderInlines forward their absolute positioned descendants to their (non-anonymous) containing block.
// Check if this non-anonymous containing block can hold the absolute positioned elements when the inline is no longer positioned.
if (canContainAbsolutelyPositionedObjects() && newStyle.position() == PositionType::Static) {
auto* container = RenderObject::containingBlockForPositionType(PositionType::Absolute, *this);
if (container && !container->canContainAbsolutelyPositionedObjects())
container->removePositionedObjects(nullptr, NewContainingBlock);
}
}
void RenderInline::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBoxModelObject::styleDidChange(diff, oldStyle);
// Ensure that all of the split inlines pick up the new style. We
// only do this if we're an inline, since we don't want to propagate
// a block's style to the other inlines.
// e.g., <font>foo <h4>goo</h4> moo</font>. The <font> inlines before
// and after the block share the same style, but the block doesn't
// need to pass its style on to anyone else.
auto& newStyle = style();
RenderInline* continuation = inlineContinuation();
if (continuation && !isContinuation()) {
for (RenderInline* currCont = continuation; currCont; currCont = currCont->inlineContinuation())
currCont->setStyle(RenderStyle::clone(newStyle));
// If an inline's in-flow positioning has changed and it is part of an active continuation as a descendant of an anonymous containing block,
// then any descendant blocks will need to change their in-flow positioning accordingly.
// Do this by updating the position of the descendant blocks' containing anonymous blocks - there may be more than one.
if (containingBlock()->isAnonymousBlock() && oldStyle && newStyle.position() != oldStyle->position() && (newStyle.hasInFlowPosition() || oldStyle->hasInFlowPosition()))
updateStyleOfAnonymousBlockContinuations(*containingBlock(), &newStyle, oldStyle);
}
propagateStyleToAnonymousChildren(StylePropagationType::AllChildren);
}
bool RenderInline::mayAffectLayout() const
{
auto* parentStyle = &parent()->style();
auto* parentRenderInline = dynamicDowncast<RenderInline>(*parent());
auto hasHardLineBreakChildOnly = firstChild() && firstChild() == lastChild() && firstChild()->isBR();
bool checkFonts = document().inNoQuirksMode();
auto mayAffectLayout = (parentRenderInline && parentRenderInline->mayAffectLayout())
|| (parentRenderInline && parentStyle->verticalAlign() != VerticalAlign::Baseline)
|| style().verticalAlign() != VerticalAlign::Baseline
|| style().textEmphasisMark() != TextEmphasisMark::None
|| (checkFonts && (!parentStyle->fontCascade().metricsOfPrimaryFont().hasIdenticalAscentDescentAndLineGap(style().fontCascade().metricsOfPrimaryFont())
|| parentStyle->lineHeight() != style().lineHeight()))
|| hasHardLineBreakChildOnly;
if (!mayAffectLayout && checkFonts) {
// Have to check the first line style as well.
parentStyle = &parent()->firstLineStyle();
auto& childStyle = firstLineStyle();
mayAffectLayout = !parentStyle->fontCascade().metricsOfPrimaryFont().hasIdenticalAscentDescentAndLineGap(childStyle.fontCascade().metricsOfPrimaryFont())
|| childStyle.verticalAlign() != VerticalAlign::Baseline
|| parentStyle->lineHeight() != childStyle.lineHeight();
}
return mayAffectLayout;
}
void RenderInline::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (auto* lineLayout = LayoutIntegration::LineLayout::containing(*this))
lineLayout->paint(paintInfo, paintOffset, this);
}
template<typename GeneratorContext>
void RenderInline::generateLineBoxRects(GeneratorContext& context) const
{
if (auto* lineLayout = LayoutIntegration::LineLayout::containing(*this)) {
auto inlineBoxRects = lineLayout->collectInlineBoxRects(*this);
if (inlineBoxRects.isEmpty()) {
context.addRect({ });
return;
}
for (auto inlineBoxRect : inlineBoxRects)
context.addRect(inlineBoxRect);
return;
}
if (auto* curr = firstLegacyInlineBox()) {
for (; curr; curr = curr->nextLineBox())
context.addRect(FloatRect(curr->topLeft(), curr->size()));
} else
context.addRect(FloatRect());
}
class AbsoluteRectsGeneratorContext {
public:
AbsoluteRectsGeneratorContext(Vector<LayoutRect>& rects, const LayoutPoint& accumulatedOffset)
: m_rects(rects)
, m_accumulatedOffset(accumulatedOffset) { }
void addRect(const FloatRect& rect)
{
LayoutRect adjustedRect = LayoutRect(rect);
adjustedRect.moveBy(m_accumulatedOffset);
m_rects.append(adjustedRect);
}
private:
Vector<LayoutRect>& m_rects;
const LayoutPoint& m_accumulatedOffset;
};
void RenderInline::boundingRects(Vector<LayoutRect>& rects, const LayoutPoint& accumulatedOffset) const
{
AbsoluteRectsGeneratorContext context(rects, accumulatedOffset);
generateLineBoxRects(context);
if (auto* continuation = this->continuation()) {
if (auto* box = dynamicDowncast<RenderBox>(*continuation)) {
continuation->boundingRects(rects, toLayoutPoint(accumulatedOffset - containingBlock()->location() + box->locationOffset()));
} else
continuation->boundingRects(rects, toLayoutPoint(accumulatedOffset - containingBlock()->location()));
}
}
namespace {
class AbsoluteQuadsGeneratorContext {
public:
AbsoluteQuadsGeneratorContext(const RenderInline* renderer, Vector<FloatQuad>& quads)
: m_quads(quads)
, m_geometryMap()
{
m_geometryMap.pushMappingsToAncestor(renderer, nullptr);
}
void addRect(const FloatRect& rect)
{
m_quads.append(m_geometryMap.absoluteRect(rect));
}
private:
Vector<FloatQuad>& m_quads;
RenderGeometryMap m_geometryMap;
};
} // unnamed namespace
void RenderInline::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
absoluteQuadsIgnoringContinuation({ }, quads, wasFixed);
if (continuation())
collectAbsoluteQuadsForContinuation(quads, wasFixed);
}
void RenderInline::absoluteQuadsIgnoringContinuation(const FloatRect&, Vector<FloatQuad>& quads, bool*) const
{
AbsoluteQuadsGeneratorContext context(this, quads);
generateLineBoxRects(context);
}
#if PLATFORM(IOS_FAMILY)
void RenderInline::absoluteQuadsForSelection(Vector<FloatQuad>& quads) const
{
AbsoluteQuadsGeneratorContext context(this, quads);
generateLineBoxRects(context);
}
#endif
LayoutUnit RenderInline::offsetLeft() const
{
return adjustedPositionRelativeToOffsetParent(firstInlineBoxTopLeft()).x();
}
LayoutUnit RenderInline::offsetTop() const
{
return adjustedPositionRelativeToOffsetParent(firstInlineBoxTopLeft()).y();
}
LayoutPoint RenderInline::firstInlineBoxTopLeft() const
{
if (auto* lineLayout = LayoutIntegration::LineLayout::containing(*this))
return lineLayout->firstInlineBoxRect(*this).location();
if (auto* inlineBox = firstLegacyInlineBox())
return flooredLayoutPoint(inlineBox->locationIncludingFlipping());
return { };
}
static LayoutUnit computeMargin(const RenderInline* renderer, const Length& margin)
{
if (margin.isAuto())
return 0;
if (margin.isFixed())
return LayoutUnit(margin.value());
if (margin.isPercentOrCalculated())
return minimumValueForLength(margin, std::max<LayoutUnit>(0, renderer->containingBlock()->contentBoxLogicalWidth()));
return 0;
}
LayoutUnit RenderInline::marginLeft() const
{
return computeMargin(this, style().marginLeft());
}
LayoutUnit RenderInline::marginRight() const
{
return computeMargin(this, style().marginRight());
}
LayoutUnit RenderInline::marginTop() const
{
return computeMargin(this, style().marginTop());
}
LayoutUnit RenderInline::marginBottom() const
{
return computeMargin(this, style().marginBottom());
}
LayoutUnit RenderInline::marginStart(const WritingMode writingMode) const
{
return computeMargin(this, style().marginStart(writingMode));
}
LayoutUnit RenderInline::marginEnd(const WritingMode writingMode) const
{
return computeMargin(this, style().marginEnd(writingMode));
}
LayoutUnit RenderInline::marginBefore(const WritingMode writingMode) const
{
return computeMargin(this, style().marginBefore(writingMode));
}
LayoutUnit RenderInline::marginAfter(const WritingMode writingMode) const
{
return computeMargin(this, style().marginAfter(writingMode));
}
ASCIILiteral RenderInline::renderName() const
{
if (isRelativelyPositioned())
return "RenderInline (relative positioned)"_s;
if (isStickilyPositioned())
return "RenderInline (sticky positioned)"_s;
// FIXME: Temporary hack while the new generated content system is being implemented.
if (isPseudoElement())
return "RenderInline (generated)"_s;
if (isAnonymous())
return "RenderInline (generated)"_s;
return "RenderInline"_s;
}
bool RenderInline::nodeAtPoint(const HitTestRequest& request, HitTestResult& result,
const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
ASSERT(layer());
if (auto* lineLayout = LayoutIntegration::LineLayout::containing(*this))
return lineLayout->hitTest(request, result, locationInContainer, accumulatedOffset, hitTestAction, this);
return false;
}
VisiblePosition RenderInline::positionForPoint(const LayoutPoint& point, HitTestSource source, const RenderFragmentContainer* fragment)
{
auto& containingBlock = *this->containingBlock();
if (auto* continuation = this->continuation()) {
// Translate the coords from the pre-anonymous block to the post-anonymous block.
LayoutPoint parentBlockPoint = containingBlock.location() + point;
while (continuation) {
RenderBlock* currentBlock = continuation->isInline() ? continuation->containingBlock() : downcast<RenderBlock>(continuation);
if (continuation->isInline() || continuation->firstChild())
return continuation->positionForPoint(parentBlockPoint - currentBlock->locationOffset(), source, fragment);
continuation = continuation->inlineContinuation();
}
return RenderBoxModelObject::positionForPoint(point, source, fragment);
}
return containingBlock.positionForPoint(point, source, fragment);
}
class LinesBoundingBoxGeneratorContext {
public:
LinesBoundingBoxGeneratorContext(FloatRect& rect) : m_rect(rect) { }
void addRect(const FloatRect& rect)
{
m_rect.uniteIfNonZero(rect);
}
private:
FloatRect& m_rect;
};
LayoutUnit RenderInline::innerPaddingBoxWidth() const
{
auto firstInlineBoxPaddingBoxLeft = LayoutUnit { };
auto lastInlineBoxPaddingBoxRight = LayoutUnit { };
if (LayoutIntegration::LineLayout::containing(*this)) {
if (auto inlineBox = InlineIterator::lineLeftmostInlineBoxFor(*this)) {
if (writingMode().isBidiLTR()) {
firstInlineBoxPaddingBoxLeft = inlineBox->logicalLeftIgnoringInlineDirection() + borderStart();
for (; inlineBox->nextInlineBoxLineRightward(); inlineBox.traverseInlineBoxLineRightward()) { }
ASSERT(inlineBox);
lastInlineBoxPaddingBoxRight = inlineBox->logicalRightIgnoringInlineDirection() - borderEnd();
} else {
lastInlineBoxPaddingBoxRight = inlineBox->logicalRightIgnoringInlineDirection() - borderStart();
for (; inlineBox->nextInlineBoxLineRightward(); inlineBox.traverseInlineBoxLineRightward()) { }
ASSERT(inlineBox);
firstInlineBoxPaddingBoxLeft = inlineBox->logicalLeftIgnoringInlineDirection() + borderEnd();
}
return std::max(0_lu, lastInlineBoxPaddingBoxRight - firstInlineBoxPaddingBoxLeft);
}
return { };
}
auto* firstInlineBox = firstLegacyInlineBox();
auto* lastInlineBox = lastLegacyInlineBox();
if (!firstInlineBox || !lastInlineBox)
return { };
if (writingMode().isBidiLTR()) {
firstInlineBoxPaddingBoxLeft = firstInlineBox->logicalLeft();
lastInlineBoxPaddingBoxRight = lastInlineBox->logicalRight();
} else {
lastInlineBoxPaddingBoxRight = firstInlineBox->logicalRight();
firstInlineBoxPaddingBoxLeft = lastInlineBox->logicalLeft();
}
return std::max(0_lu, lastInlineBoxPaddingBoxRight - firstInlineBoxPaddingBoxLeft);
}
LayoutUnit RenderInline::innerPaddingBoxHeight() const
{
auto innerPaddingBoxLogicalHeight = LayoutUnit { isHorizontalWritingMode() ? linesBoundingBox().height() : linesBoundingBox().width() };
innerPaddingBoxLogicalHeight -= (borderBefore() + borderAfter());
return innerPaddingBoxLogicalHeight;
}
IntRect RenderInline::linesBoundingBox() const
{
if (auto* layout = LayoutIntegration::LineLayout::containing(*this)) {
if (!layoutBox() || !layout->contains(*this)) {
// Repaint may be issued on subtrees during content mutation with newly inserted renderers
// (or we just forgot to initiate layout before querying geometry on stale content after moving inline boxes between blocks).
ASSERT(needsLayout());
return { };
}
if (isRenderSVGInline()) {
// FIXME: Always build the bounding box like this. LineLayouyt::enclosingBorderBoxRectFor does not include
// any post-layout box adjustments.
FloatRect result;
for (auto box = InlineIterator::lineLeftmostInlineBoxFor(*this); box; box.traverseInlineBoxLineRightward()) {
auto rect = box->visualRectIgnoringBlockDirection();
result.unite(rect);
}
return enclosingIntRect(result);
}
return enclosingIntRect(layout->enclosingBorderBoxRectFor(*this));
}
// See <rdar://problem/5289721>, for an unknown reason the linked list here is sometimes inconsistent, first is non-zero and last is zero. We have been
// unable to reproduce this at all (and consequently unable to figure ot why this is happening). The assert will hopefully catch the problem in debug
// builds and help us someday figure out why. We also put in a redundant check of lastLineBox() to avoid the crash for now.
ASSERT(!firstLegacyInlineBox() == !lastLegacyInlineBox()); // Either both are null or both exist.
IntRect result;
if (firstLegacyInlineBox() && lastLegacyInlineBox()) {
// Return the width of the minimal left side and the maximal right side.
float logicalLeftSide = 0;
float logicalRightSide = 0;
for (auto* curr = firstLegacyInlineBox(); curr; curr = curr->nextLineBox()) {
if (curr == firstLegacyInlineBox() || curr->logicalLeft() < logicalLeftSide)
logicalLeftSide = curr->logicalLeft();
if (curr == firstLegacyInlineBox() || curr->logicalRight() > logicalRightSide)
logicalRightSide = curr->logicalRight();
}
bool isHorizontal = writingMode().isHorizontal();
float x = isHorizontal ? logicalLeftSide : firstLegacyInlineBox()->x();
float y = isHorizontal ? firstLegacyInlineBox()->y() : logicalLeftSide;
float width = isHorizontal ? logicalRightSide - logicalLeftSide : lastLegacyInlineBox()->logicalBottom() - x;
float height = isHorizontal ? lastLegacyInlineBox()->logicalBottom() - y : logicalRightSide - logicalLeftSide;
result = enclosingIntRect(FloatRect(x, y, width, height));
}
return result;
}
LayoutRect RenderInline::linesVisualOverflowBoundingBox() const
{
if (auto* layout = LayoutIntegration::LineLayout::containing(*this)) {
if (!layoutBox()) {
// Repaint may be issued on subtrees during content mutation with newly inserted renderers.
ASSERT(needsLayout());
return { };
}
return layout->inkOverflowBoundingBoxRectFor(*this);
}
if (!firstLegacyInlineBox() || !lastLegacyInlineBox())
return { };
// Return the width of the minimal left side and the maximal right side.
LayoutUnit logicalLeftSide = LayoutUnit::max();
LayoutUnit logicalRightSide = LayoutUnit::min();
for (auto* curr = firstLegacyInlineBox(); curr; curr = curr->nextLineBox()) {
logicalLeftSide = std::min(logicalLeftSide, curr->logicalLeftVisualOverflow());
logicalRightSide = std::max(logicalRightSide, curr->logicalRightVisualOverflow());
}
const LegacyRootInlineBox& firstRootBox = firstLegacyInlineBox()->root();
const LegacyRootInlineBox& lastRootBox = lastLegacyInlineBox()->root();
LayoutUnit logicalTop = firstLegacyInlineBox()->logicalTopVisualOverflow(firstRootBox.lineTop());
LayoutUnit logicalWidth = logicalRightSide - logicalLeftSide;
LayoutUnit logicalHeight = lastLegacyInlineBox()->logicalBottomVisualOverflow(lastRootBox.lineBottom()) - logicalTop;
LayoutRect rect(logicalLeftSide, logicalTop, logicalWidth, logicalHeight);
if (!writingMode().isHorizontal())
rect = rect.transposedRect();
return rect;
}
LayoutRect RenderInline::clippedOverflowRect(const RenderLayerModelObject* repaintContainer, VisibleRectContext context) const
{
// Only first-letter renderers are allowed in here during layout. They mutate the tree triggering repaints.
#ifndef NDEBUG
auto insideSelfPaintingInlineBox = [&] {
if (hasSelfPaintingLayer())
return true;
auto* containingBlock = this->containingBlock();
for (auto* ancestor = this->parent(); ancestor && ancestor != containingBlock; ancestor = ancestor->parent()) {
if (ancestor->hasSelfPaintingLayer())
return true;
}
return false;
};
ASSERT_UNUSED(insideSelfPaintingInlineBox, !view().frameView().layoutContext().isPaintOffsetCacheEnabled() || style().pseudoElementType() == PseudoId::FirstLetter || insideSelfPaintingInlineBox());
#endif
auto knownEmpty = [&] {
if (firstLegacyInlineBox())
return false;
if (continuation())
return false;
if (LayoutIntegration::LineLayout::containing(*this))
return false;
return true;
};
if (knownEmpty())
return LayoutRect();
auto repaintRect = linesVisualOverflowBoundingBox();
bool hitRepaintContainer = false;
// We need to add in the in-flow position offsets of any inlines (including us) up to our
// containing block.
RenderBlock* containingBlock = this->containingBlock();
for (const RenderElement* inlineFlow = this; inlineFlow; inlineFlow = inlineFlow->parent()) {
auto* renderInline = dynamicDowncast<RenderInline>(*inlineFlow);
if (!renderInline || inlineFlow == containingBlock)
break;
if (inlineFlow == repaintContainer) {
hitRepaintContainer = true;
break;
}
if (inlineFlow->style().hasInFlowPosition() && inlineFlow->hasLayer())
repaintRect.move(renderInline->layer()->offsetForInFlowPosition());
}
LayoutUnit outlineSize { style().outlineSize() };
repaintRect.inflate(outlineSize);
if (hitRepaintContainer || !containingBlock)
return repaintRect;
auto rects = RepaintRects { repaintRect };
if (containingBlock->hasNonVisibleOverflow())
containingBlock->applyCachedClipAndScrollPosition(rects, repaintContainer, context);
rects = containingBlock->computeRects(rects, repaintContainer, context);
repaintRect = rects.clippedOverflowRect;
if (outlineSize) {
for (auto& child : childrenOfType<RenderElement>(*this))
repaintRect.unite(child.rectWithOutlineForRepaint(repaintContainer, outlineSize));
if (RenderBoxModelObject* continuation = this->continuation()) {
if (!continuation->isInline() && continuation->parent())
repaintRect.unite(continuation->rectWithOutlineForRepaint(repaintContainer, outlineSize));
}
}
return repaintRect;
}
auto RenderInline::rectsForRepaintingAfterLayout(const RenderLayerModelObject* repaintContainer, RepaintOutlineBounds) const -> RepaintRects
{
// RepaintOutlineBounds is unused for inlines.
return { clippedOverflowRect(repaintContainer, visibleRectContextForRepaint()) };
}
LayoutRect RenderInline::rectWithOutlineForRepaint(const RenderLayerModelObject* repaintContainer, LayoutUnit outlineWidth) const
{
LayoutRect r(RenderBoxModelObject::rectWithOutlineForRepaint(repaintContainer, outlineWidth));
for (auto& child : childrenOfType<RenderElement>(*this))
r.unite(child.rectWithOutlineForRepaint(repaintContainer, outlineWidth));
return r;
}
auto RenderInline::computeVisibleRectsUsingPaintOffset(const RepaintRects& rects) const -> RepaintRects
{
auto adjustedRects = rects;
auto* layoutState = view().frameView().layoutContext().layoutState();
if (style().hasInFlowPosition() && layer())
adjustedRects.move(layer()->offsetForInFlowPosition());
adjustedRects.move(layoutState->paintOffset());
if (layoutState->isClipped())
adjustedRects.clippedOverflowRect.intersect(layoutState->clipRect());
return adjustedRects;
}
auto RenderInline::computeVisibleRectsInContainer(const RepaintRects& rects, const RenderLayerModelObject* container, VisibleRectContext context) const -> std::optional<RepaintRects>
{
// Repaint offset cache is only valid for root-relative repainting
if (view().frameView().layoutContext().isPaintOffsetCacheEnabled() && !container && !context.options.contains(VisibleRectContextOption::UseEdgeInclusiveIntersection))
return computeVisibleRectsUsingPaintOffset(rects);
if (container == this)
return rects;
bool containerSkipped;
RenderElement* localContainer = this->container(container, containerSkipped);
if (!localContainer)
return rects;
auto adjustedRects = rects;
if (style().hasInFlowPosition() && layer()) {
// Apply the in-flow position offset when invalidating a rectangle. The layer
// is translated, but the render box isn't, so we need to do this to get the
// right dirty rect. Since this is called from RenderObject::setStyle, the relative or sticky position
// flag on the RenderObject has been cleared, so use the one on the style().
auto offsetForInFlowPosition = layer()->offsetForInFlowPosition();
adjustedRects.move(offsetForInFlowPosition);
}
if (localContainer->hasNonVisibleOverflow()) {
// FIXME: Respect the value of context.options.
SetForScope change(context.options, context.options | VisibleRectContextOption::ApplyCompositedContainerScrolls);
bool isEmpty = !downcast<RenderLayerModelObject>(*localContainer).applyCachedClipAndScrollPosition(adjustedRects, container, context);
if (isEmpty) {
if (context.options.contains(VisibleRectContextOption::UseEdgeInclusiveIntersection))
return std::nullopt;
return adjustedRects;
}
}
if (containerSkipped) {
// If the repaintContainer is below o, then we need to map the rect into repaintContainer's coordinates.
auto containerOffset = container->offsetFromAncestorContainer(*localContainer);
adjustedRects.move(-containerOffset);
return adjustedRects;
}
return localContainer->computeVisibleRectsInContainer(adjustedRects, container, context);
}
LayoutSize RenderInline::offsetFromContainer(RenderElement& container, const LayoutPoint&, bool* offsetDependsOnPoint) const
{
ASSERT(&container == this->container());
LayoutSize offset;
if (isInFlowPositioned())
offset += offsetForInFlowPosition();
if (auto* box = dynamicDowncast<RenderBox>(container))
offset -= toLayoutSize(box->scrollPosition());
if (offsetDependsOnPoint)
*offsetDependsOnPoint = (is<RenderBox>(container) && container.writingMode().isBlockFlipped()) || is<RenderFragmentedFlow>(container);
return offset;
}
void RenderInline::mapLocalToContainer(const RenderLayerModelObject* ancestorContainer, TransformState& transformState, OptionSet<MapCoordinatesMode> mode, bool* wasFixed) const
{
if (ancestorContainer == this)
return;
if (view().frameView().layoutContext().isPaintOffsetCacheEnabled() && !ancestorContainer) {
auto* layoutState = view().frameView().layoutContext().layoutState();
LayoutSize offset = layoutState->paintOffset();
if (style().hasInFlowPosition() && layer())
offset += layer()->offsetForInFlowPosition();
transformState.move(offset);
return;
}
bool containerSkipped;
RenderElement* container = this->container(ancestorContainer, containerSkipped);
if (!container)
return;
if (mode.contains(ApplyContainerFlip)) {
if (CheckedPtr box = dynamicDowncast<RenderBox>(*container)) {
if (container->writingMode().isBlockFlipped()) {
LayoutPoint centerPoint(transformState.mappedPoint());
transformState.move(box->flipForWritingMode(centerPoint) - centerPoint);
}
mode.remove(ApplyContainerFlip);
}
}
LayoutSize containerOffset = offsetFromContainer(*container, LayoutPoint(transformState.mappedPoint()));
pushOntoTransformState(transformState, mode, ancestorContainer, container, containerOffset, containerSkipped);
if (containerSkipped)
return;
container->mapLocalToContainer(ancestorContainer, transformState, mode, wasFixed);
}
const RenderObject* RenderInline::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
ASSERT(ancestorToStopAt != this);
bool ancestorSkipped;
RenderElement* container = this->container(ancestorToStopAt, ancestorSkipped);
if (!container)
return nullptr;
pushOntoGeometryMap(geometryMap, ancestorToStopAt, container, ancestorSkipped);
return ancestorSkipped ? ancestorToStopAt : container;
}
void RenderInline::updateHitTestResult(HitTestResult& result, const LayoutPoint& point) const
{
if (result.innerNode())
return;
LayoutPoint localPoint(point);
if (RefPtr node = nodeForHitTest()) {
if (isContinuation()) {
// We're in the continuation of a split inline. Adjust our local point to be in the coordinate space
// of the principal renderer's containing block. This will end up being the innerNonSharedNode.
auto* firstBlock = node->renderer()->containingBlock();
localPoint.moveBy(containingBlock()->location() - firstBlock->locationOffset());
}
result.setInnerNode(node.get());
if (!result.innerNonSharedNode())
result.setInnerNonSharedNode(node.get());
result.setLocalPoint(localPoint);
}
}
void RenderInline::deleteLegacyLineBoxes()
{
m_legacyLineBoxes.deleteLineBoxes();
}
std::unique_ptr<LegacyInlineFlowBox> RenderInline::createInlineFlowBox()
{
return makeUnique<LegacyInlineFlowBox>(*this);
}
LegacyInlineFlowBox* RenderInline::createAndAppendInlineFlowBox()
{
auto newFlowBox = createInlineFlowBox();
auto flowBox = newFlowBox.get();
m_legacyLineBoxes.appendLineBox(WTFMove(newFlowBox));
return flowBox;
}
LayoutUnit RenderInline::lineHeight(bool firstLine, LineDirectionMode /*direction*/, LinePositionMode /*linePositionMode*/) const
{
auto& lineStyle = firstLine ? firstLineStyle() : style();
return LayoutUnit::fromFloatCeil(lineStyle.computedLineHeight());
}
LayoutUnit RenderInline::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
{
auto& style = firstLine ? firstLineStyle() : this->style();
auto& fontMetrics = style.metricsOfPrimaryFont();
return LayoutUnit { fontMetrics.ascent(baselineType) + (lineHeight(firstLine, direction, linePositionMode) - fontMetrics.height()) / 2 };
}
LayoutSize RenderInline::offsetForInFlowPositionedInline(const RenderBox* child) const
{
// FIXME: This function isn't right with mixed writing modes.
ASSERT(isInFlowPositioned());
if (!isInFlowPositioned())
return { };
// When we have an enclosing relpositioned inline, we need to add in the offset of the first line
// box from the rest of the content, but only in the cases where we know we're positioned
// relative to the inline itself.
auto inlinePosition = layer()->staticInlinePosition();
auto blockPosition = layer()->staticBlockPosition();
if (auto* inlineBox = firstLegacyInlineBox()) {
inlinePosition = LayoutUnit::fromFloatRound(inlineBox->logicalLeft());
blockPosition = inlineBox->logicalTop();
} else if (LayoutIntegration::LineLayout::containing(*this)) {
if (!layoutBox()) {
// Repaint may be issued on subtrees during content mutation with newly inserted renderers.
ASSERT(needsLayout());
return { };
}
if (auto inlineBox = InlineIterator::lineLeftmostInlineBoxFor(*this)) {
inlinePosition = LayoutUnit::fromFloatRound(inlineBox->logicalLeftIgnoringInlineDirection());
blockPosition = inlineBox->logicalTop();
} else if (auto* blockContainer = containingBlock()) {
// This must be a block with no in-flow content e.g. <div><span><abs pos box></span></div> where we don't construct any display box at all.
auto contentBoxLocation = blockContainer->contentBoxLocation();
inlinePosition = contentBoxLocation.x();
blockPosition = contentBoxLocation.y();
}
}
// Per http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-width an absolute positioned box with a static position
// should locate itself as though it is a normal flow box in relation to its containing block.
LayoutSize logicalOffset;
if (!child->style().hasStaticInlinePosition(writingMode().isHorizontal()))
logicalOffset.setWidth(inlinePosition);
if (!child->style().hasStaticBlockPosition(writingMode().isHorizontal()))
logicalOffset.setHeight(blockPosition);
return writingMode().isHorizontal() ? logicalOffset : logicalOffset.transposedSize();
}
void RenderInline::imageChanged(WrappedImagePtr, const IntRect*)
{
if (!parent())
return;
// FIXME: We can do better.
repaint();
}
namespace {
class AbsoluteRectsIgnoringEmptyGeneratorContext : public AbsoluteRectsGeneratorContext {
public:
AbsoluteRectsIgnoringEmptyGeneratorContext(Vector<LayoutRect>& rects, const LayoutPoint& accumulatedOffset)
: AbsoluteRectsGeneratorContext(rects, accumulatedOffset) { }
void addRect(const FloatRect& rect)
{
if (!rect.isEmpty())
AbsoluteRectsGeneratorContext::addRect(rect);
}
};
} // unnamed namespace
void RenderInline::addFocusRingRects(Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer) const
{
AbsoluteRectsIgnoringEmptyGeneratorContext context(rects, additionalOffset);
generateLineBoxRects(context);
for (auto& child : childrenOfType<RenderElement>(*this)) {
if (is<RenderListMarker>(child))
continue;
FloatPoint pos(additionalOffset);
// FIXME: This doesn't work correctly with transforms.
if (child.hasLayer())
pos = child.localToContainerPoint(FloatPoint(), paintContainer);
else if (auto* box = dynamicDowncast<RenderBox>(child))
pos.move(box->locationOffset());
child.addFocusRingRects(rects, flooredIntPoint(pos), paintContainer);
}
if (RenderBoxModelObject* continuation = this->continuation()) {
if (continuation->isInline())
continuation->addFocusRingRects(rects, flooredLayoutPoint(LayoutPoint(additionalOffset + continuation->containingBlock()->location() - containingBlock()->location())), paintContainer);
else
continuation->addFocusRingRects(rects, flooredLayoutPoint(LayoutPoint(additionalOffset + downcast<RenderBox>(*continuation).location() - containingBlock()->location())), paintContainer);
}
}
void RenderInline::paintOutline(PaintInfo& paintInfo, const LayoutPoint& paintOffset) const
{
if (!hasOutline())
return;
auto& styleToUse = style();
// Only paint the focus ring by hand if the theme isn't able to draw it.
if (styleToUse.outlineStyleIsAuto() == OutlineIsAuto::On && !theme().supportsFocusRing(*this, styleToUse)) {
Vector<LayoutRect> focusRingRects;
addFocusRingRects(focusRingRects, paintOffset, paintInfo.paintContainer);
paintFocusRing(paintInfo, styleToUse, focusRingRects);
}
if (hasOutlineAnnotation() && styleToUse.outlineStyleIsAuto() == OutlineIsAuto::Off && !theme().supportsFocusRing(*this, styleToUse))
addPDFURLRect(paintInfo, paintOffset);
GraphicsContext& graphicsContext = paintInfo.context();
if (graphicsContext.paintingDisabled())
return;
if (styleToUse.outlineStyleIsAuto() == OutlineIsAuto::On || !styleToUse.hasOutline())
return;
if (!containingBlock()) {
ASSERT_NOT_REACHED();
return;
}
auto isHorizontalWritingMode = this->isHorizontalWritingMode();
auto& containingBlock = *this->containingBlock();
auto isFlipped = containingBlock.writingMode().isBlockFlipped();
Vector<LayoutRect> rects;
for (auto box = InlineIterator::lineLeftmostInlineBoxFor(*this); box; box.traverseInlineBoxLineRightward()) {
auto lineBox = box->lineBox();
auto logicalTop = std::max(lineBox->contentLogicalTop(), box->logicalTop());
auto logicalBottom = std::min(lineBox->contentLogicalBottom(), box->logicalBottom());
auto enclosingVisualRect = FloatRect { box->logicalLeftIgnoringInlineDirection(), logicalTop, box->logicalWidth(), logicalBottom - logicalTop };
if (!isHorizontalWritingMode)
enclosingVisualRect = enclosingVisualRect.transposedRect();
if (isFlipped)
containingBlock.flipForWritingMode(enclosingVisualRect);
rects.append(LayoutRect { enclosingVisualRect });
}
BorderPainter { *this, paintInfo }.paintOutline(paintOffset, rects);
}
bool isEmptyInline(const RenderInline& renderer)
{
for (auto& current : childrenOfType<RenderObject>(renderer)) {
if (current.isFloatingOrOutOfFlowPositioned())
continue;
if (auto* text = dynamicDowncast<RenderText>(current)) {
if (!text->containsOnlyCollapsibleWhitespace())
return false;
continue;
}
auto* renderInline = dynamicDowncast<RenderInline>(current);
if (!renderInline || !isEmptyInline(*renderInline))
return false;
}
return true;
}
inline bool RenderInline::willChangeCreatesStackingContext() const
{
return style().willChange() && style().willChange()->canCreateStackingContext();
}
bool RenderInline::requiresLayer() const
{
return isInFlowPositioned() || createsGroup() || hasClipPath() || shouldApplyPaintContainment() || willChangeCreatesStackingContext() || hasRunningAcceleratedAnimations() || requiresRenderingConsolidationForViewTransition();
}
} // namespace WebCore
|