Skip to content

Commit ac6b35c

Browse files
committed
[IFC] Replace InlineIterator traversal with modern C++ style iterators and ranges
https://bugs.webkit.org/show_bug.cgi?id=327538 rdar://190023418 Reviewed by Alan Baradlay. InlineIterator was designed to fit the linked lists of legacy inline tree which meant it simulated pointer traversal. With legacy tree gone we can modernize the interface. This patch makes the iterators standard bidirectional iterators and switches traversal in many places to range-for syntax. The ranges are standard views so std::ranges algorithms and std::views adaptors like std::views::reverse work with them. The iterators are still the nullable box type and keep their named traversal functions, which are now the same as ++ and --. An iterator converts to another kind of iterator positioned at the same box. Outside traversal the interface stays largely the same. Box handles are now copied when iterating, so InlineContent uses CanMakeSingleThreadWeakPtr to avoid atomic refcount churn. The thread-safe CanMakeWeakPtr use predated the single-thread variant. Copying box handles now destroys SingleThreadWeakPtrs in NODELETE functions. Mark SingleThreadWeakPtrImpl::deref() SUPPRESS_NODELETE, mirroring what 320003@main did for WeakPtr's DefaultWeakPtrImpl. * Source/WTF/wtf/WeakPtrImpl.h: (WTF::SingleThreadWeakPtrImpl::deref const): * Source/WebCore/layout/integration/inline/InlineIteratorBox.cpp: * Source/WebCore/layout/integration/inline/InlineIteratorBox.h: * Source/WebCore/layout/integration/inline/InlineIteratorBoxInlines.h: * Source/WebCore/layout/integration/inline/InlineIteratorInlineBox.cpp: * Source/WebCore/layout/integration/inline/InlineIteratorInlineBox.h: * Source/WebCore/layout/integration/inline/InlineIteratorLineBox.cpp: * Source/WebCore/layout/integration/inline/InlineIteratorLineBox.h: * Source/WebCore/layout/integration/inline/InlineIteratorLogicalOrderTraversal.h: * Source/WebCore/layout/integration/inline/InlineIteratorSVGTextBox.cpp: * Source/WebCore/layout/integration/inline/InlineIteratorSVGTextBox.h: * Source/WebCore/layout/integration/inline/InlineIteratorTextBox.cpp: * Source/WebCore/layout/integration/inline/InlineIteratorTextBox.h: * Source/WebCore/layout/integration/inline/LayoutIntegrationInlineContent.h: Callers updated. Canonical link: https://commits.webkit.org/324129@main
1 parent 9850e5b commit ac6b35c

40 files changed

Lines changed: 627 additions & 679 deletions

‎Source/WTF/wtf/WeakPtrImpl.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ class SingleThreadWeakPtrImpl final : public WeakPtrImplBaseSingleThread<SingleT
166166
: WeakPtrImplBaseSingleThread<SingleThreadWeakPtrImpl>(ptr)
167167
{
168168
}
169+
170+
// SingleThreadWeakPtrImpl is an implementation detail of SingleThreadWeakPtr.
171+
// Deleting this object does not logically result in destruction of a user object.
172+
SUPPRESS_NODELETE void LEGACY_NODELETE deref() const {
173+
WeakPtrImplBaseSingleThread<SingleThreadWeakPtrImpl>::deref();
174+
}
169175
};
170176

171177
} // namespace WTF

‎Source/WebCore/accessibility/AccessibilityRenderObject.cpp‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ AXTextRuns AccessibilityRenderObject::textRuns()
16201620
};
16211621

16221622
// Appends text to the current lineString, collapsing whitespace as necessary (similar to how TextIterator::handleTextRun() does).
1623-
auto appendToLineString = [&] (const InlineIterator::TextBoxIterator& textBox) {
1623+
auto appendToLineString = [&](const InlineIterator::TextBoxIterator& textBox) {
16241624
auto text = textBox->originalText();
16251625
if (text.isEmpty())
16261626
return;
@@ -1637,9 +1637,7 @@ AXTextRuns AccessibilityRenderObject::textRuns()
16371637

16381638
auto textRun = textBox->textRun(InlineIterator::TextRunMode::Editing);
16391639
auto lineBox = textBox->lineBox();
1640-
if (!lineBox)
1641-
return;
1642-
lineHeight = LineSelection::logicalRect(*lineBox).height();
1640+
lineHeight = LineSelection::logicalRect(lineBox).height();
16431641

16441642
// Compute distanceFromBoundsInDirection only for the first text box on each
16451643
// line. Multiple text boxes can share a line (e.g. due to inline formatting
@@ -1649,7 +1647,7 @@ AXTextRuns AccessibilityRenderObject::textRuns()
16491647
if (!didComputeDistanceFromBounds) {
16501648
didComputeDistanceFromBounds = true;
16511649
computeBoundsOffsetsIfNeeded();
1652-
distanceFromBoundsInDirection = isHorizontal ? textRun.xPos() + lineBox->contentLogicalLeft() + containingBlockOffset - elementRectOffset : -textRun.xPos() + containingBlockOffset - elementRectOffset;
1650+
distanceFromBoundsInDirection = isHorizontal ? textRun.xPos() + lineBox.contentLogicalLeft() + containingBlockOffset - elementRectOffset : -textRun.xPos() + containingBlockOffset - elementRectOffset;
16531651
}
16541652

16551653
// Populate GlyphBuffer with all of the glyphs for the text runs, enabling us to measure character widths.

‎Source/WebCore/editing/Editing.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ static void forEachRenderedBoxBetween(const RenderedPosition& first, const Rende
13411341
return;
13421342
}
13431343

1344-
if (first.lineBox().atEnd()) {
1344+
if (!first.lineBox()) {
13451345
ASSERT_NOT_REACHED();
13461346
return;
13471347
}
@@ -1425,12 +1425,12 @@ static std::optional<SimpleRange> makeVisuallyContiguousIfNeeded(const SimpleRan
14251425
auto [start, end] = positionsForRange(range);
14261426
auto firstLineDirection = TextDirection::LTR;
14271427
RenderedPosition renderedStart { start };
1428-
if (renderedStart.isNull() || renderedStart.lineBox().atEnd())
1428+
if (renderedStart.isNull() || !renderedStart.lineBox())
14291429
return std::nullopt;
14301430

14311431
auto lastLineDirection = TextDirection::LTR;
14321432
RenderedPosition renderedEnd { end };
1433-
if (renderedEnd.isNull() || renderedEnd.lineBox().atEnd())
1433+
if (renderedEnd.isNull() || !renderedEnd.lineBox())
14341434
return std::nullopt;
14351435

14361436
if (renderedStart.box() == renderedEnd.box())

‎Source/WebCore/editing/FrameSelection.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,14 +2037,14 @@ static bool inlineBackgroundCoversCaret(const RenderBoxModelObject& inlineBox, c
20372037
auto logicalCaretRect = isHorizontal ? caretRect : caretRect.transposedRect();
20382038
auto caretMiddleAlongBlockAxis = logicalCaretRect.y() + logicalCaretRect.height() / 2;
20392039

2040-
for (auto box = InlineIterator::lineLeftmostInlineBoxFor(inlineBox); box; box.traverseInlineBoxLineRightward()) {
2041-
auto lineRect = LayoutRect { LineSelection::logicalRect(*box->lineBox()) };
2040+
for (auto& box : InlineIterator::inlineBoxesFor(inlineBox)) {
2041+
auto lineRect = LayoutRect { LineSelection::logicalRect(box.lineBox()) };
20422042
lineRect.move(logicalOffset);
20432043
if (caretMiddleAlongBlockAxis < lineRect.y())
20442044
break;
20452045
if (caretMiddleAlongBlockAxis >= lineRect.maxY())
20462046
continue;
2047-
auto boxRect = LayoutRect { box->logicalRectIgnoringInlineDirection() };
2047+
auto boxRect = LayoutRect { box.logicalRectIgnoringInlineDirection() };
20482048
boxRect.move(logicalOffset);
20492049
if (logicalCaretRect.x() >= boxRect.x() && logicalCaretRect.maxX() <= boxRect.maxX())
20502050
return true;

‎Source/WebCore/editing/RenderedPosition.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ RenderedPosition RenderedPosition::leftBoundaryOfBidiRun(unsigned char bidiLevel
145145
do {
146146
auto prev = box->nextLineLeftwardOnLineIgnoringLineBreak();
147147
if (!prev || prev->bidiLevel() < bidiLevelOfRun)
148-
return RenderedPosition(&box->renderer(), box, box->leftmostCaretOffset());
148+
return RenderedPosition(protect(box->renderer()).ptr(), box, box->leftmostCaretOffset());
149149
box = prev;
150150
} while (box);
151151

@@ -162,7 +162,7 @@ RenderedPosition RenderedPosition::rightBoundaryOfBidiRun(unsigned char bidiLeve
162162
do {
163163
auto next = box->nextLineRightwardOnLineIgnoringLineBreak();
164164
if (!next || next->bidiLevel() < bidiLevelOfRun)
165-
return RenderedPosition(&box->renderer(), box, box->rightmostCaretOffset());
165+
return RenderedPosition(protect(box->renderer()).ptr(), box, box->rightmostCaretOffset());
166166
box = next;
167167
} while (box);
168168

‎Source/WebCore/editing/VisiblePosition.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ FloatRect VisiblePosition::absoluteSelectionBoundsForLine() const
692692
return { };
693693

694694
auto line = box->lineBox();
695-
return protect(line->formattingContextRoot())->localToAbsoluteQuad(FloatRect { LineSelection::physicalRect(*line) }).boundingBox();
695+
return protect(line.formattingContextRoot())->localToAbsoluteQuad(FloatRect { LineSelection::physicalRect(line) }).boundingBox();
696696
}
697697

698698
int VisiblePosition::lineDirectionPointForBlockDirectionNavigation() const

‎Source/WebCore/editing/VisibleUnits.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static const InlineIterator::LeafBoxIterator logicallyPreviousBox(const VisibleP
176176
if (auto previousBox = previousTextOrLineBreakBox(startBox, orderCache))
177177
return previousBox;
178178

179-
if (auto previousLineBox = startBox->lineBox()->previous()) {
179+
if (auto previousLineBox = startBox->lineBox().previous()) {
180180
// FIXME: Why isn't previousBoxInDifferentLine set here?
181181
if (auto previousBox = endTextOrLineBreakBox(previousLineBox, orderCache))
182182
return previousBox;
@@ -214,7 +214,7 @@ static const InlineIterator::LeafBoxIterator logicallyNextBox(const VisiblePosit
214214
if (auto nextBox = nextTextOrLineBreakBox(startBox, orderCache))
215215
return nextBox;
216216

217-
if (auto nextLineBox = startBox->lineBox()->next()) {
217+
if (auto nextLineBox = startBox->lineBox().next()) {
218218
// FIXME: Why isn't previousBoxInDifferentLine set here?
219219
if (auto nextBox = startTextOrLineBreakBox(nextLineBox, orderCache))
220220
return nextBox;
@@ -262,7 +262,7 @@ static UBreakIterator* wordBreakIteratorForMinOffsetBoundary(const VisiblePositi
262262
string.clear();
263263

264264
if (previousBox) {
265-
auto& previousTextBox = downcast<InlineIterator::TextBoxIterator>(previousBox);
265+
auto previousTextBox = InlineIterator::TextBoxIterator { previousBox };
266266
previousBoxLength = previousTextBox->length();
267267
append(string, previousTextBox->originalText());
268268
}
@@ -288,7 +288,7 @@ static UBreakIterator* wordBreakIteratorForMaxOffsetBoundary(const VisiblePositi
288288
append(string, textBox->originalText());
289289

290290
if (nextBox) {
291-
auto& nextTextBox = downcast<InlineIterator::TextBoxIterator>(nextBox);
291+
auto nextTextBox = InlineIterator::TextBoxIterator { nextBox };
292292
append(string, nextTextBox->originalText());
293293
}
294294

@@ -348,7 +348,7 @@ static VisiblePosition visualWordPosition(const VisiblePosition& visiblePosition
348348
continue;
349349
}
350350

351-
auto& textBox = downcast<InlineIterator::TextBoxIterator>(box);
351+
auto textBox = InlineIterator::TextBoxIterator { box };
352352
unsigned previousBoxLength = 0;
353353
bool previousBoxInDifferentLine = false;
354354
bool nextBoxInDifferentLine = false;
@@ -852,7 +852,7 @@ static VisiblePosition endPositionForLine(const VisiblePosition& c, LineEndpoint
852852
if (is<HTMLBRElement>(*endNode))
853853
pos = positionBeforeNode(*endNode);
854854
else if (RefPtr endTextNode = dynamicDowncast<Text>(*endNode); endTextNode && is<InlineIterator::TextBox>(*endBox)) {
855-
auto& endTextBox = downcast<InlineIterator::TextBox>(*endBox);
855+
auto endTextBox = downcast<InlineIterator::TextBox>(*endBox);
856856
int endOffset = endTextBox.start();
857857
if (!endTextBox.isLineBreak())
858858
endOffset += endTextBox.length();
@@ -977,7 +977,7 @@ VisiblePosition previousLinePosition(const VisiblePosition& visiblePosition, Lay
977977

978978
InlineIterator::LineBoxIterator lineBox;
979979
if (auto box = visiblePosition.inlineBoxAndOffset().box) {
980-
lineBox = box->lineBox()->previous();
980+
lineBox = box->lineBox().previous();
981981
// We want to skip zero height boxes.
982982
// This could happen in case of a line with trailing floats.
983983
// A line whose content is a block level box (block-in-inline) is not a line to move the caret
@@ -1037,7 +1037,7 @@ VisiblePosition nextLinePosition(const VisiblePosition& visiblePosition, LayoutU
10371037

10381038
InlineIterator::LineBoxIterator lineBox;
10391039
if (auto box = visiblePosition.inlineBoxAndOffset().box) {
1040-
lineBox = box->lineBox()->next();
1040+
lineBox = box->lineBox().next();
10411041
// We want to skip zero height boxes.
10421042
// This could happen in case of a line with trailing floats.
10431043
// A line whose content is a block level box (block-in-inline) is not a line to move the caret

‎Source/WebCore/layout/integration/inline/InlineIteratorBox.cpp‎

Lines changed: 19 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,8 @@
3838
namespace WebCore {
3939
namespace InlineIterator {
4040

41-
BoxIterator::BoxIterator(const Box& run)
42-
: m_box(run)
43-
{
44-
}
45-
46-
bool BoxIterator::operator==(const BoxIterator& other) const
47-
{
48-
if (atEnd() && other.atEnd())
49-
return true;
50-
51-
return m_box.m_inlineContent == other.m_box.m_inlineContent && m_box.m_boxIndex == other.m_box.m_boxIndex;
52-
}
53-
54-
bool BoxIterator::atEnd() const
55-
{
56-
return m_box.atEnd();
57-
}
58-
59-
BoxIterator& BoxIterator::traverseLineRightwardOnLine()
60-
{
61-
m_box.traverseNextBoxOnLine();
62-
return *this;
63-
}
64-
65-
BoxIterator& BoxIterator::traverseLineRightwardOnLineSkippingChildren()
66-
{
67-
m_box.traverseNextBoxOnLineSkippingChildren();
68-
return *this;
69-
}
70-
71-
BoxIterator& BoxIterator::traverseLineLeftwardOnLine()
72-
{
73-
m_box.traversePreviousBoxOnLine();
74-
return *this;
75-
}
41+
static_assert(std::ranges::bidirectional_range<LeafBoxRange> && std::ranges::common_range<LeafBoxRange> && std::ranges::view<LeafBoxRange> && std::ranges::borrowed_range<LeafBoxRange>);
42+
static_assert(std::ranges::bidirectional_range<BoxRange<BoxIterator>> && std::ranges::common_range<BoxRange<BoxIterator>> && std::ranges::view<BoxRange<BoxIterator>> && std::ranges::borrowed_range<BoxRange<BoxIterator>>);
7643

7744
bool Box::isSVGText() const
7845
{
@@ -81,46 +48,49 @@ bool Box::isSVGText() const
8148

8249
LeafBoxIterator Box::nextLineRightwardOnLine() const
8350
{
84-
return LeafBoxIterator(*this).traverseLineRightwardOnLine();
51+
return std::ranges::next(LeafBoxIterator { *this });
8552
}
8653

8754
LeafBoxIterator Box::nextLineLeftwardOnLine() const
8855
{
89-
return LeafBoxIterator(*this).traverseLineLeftwardOnLine();
56+
return std::ranges::prev(LeafBoxIterator { *this });
9057
}
9158

9259
LeafBoxIterator Box::nextLineRightwardOnLineIgnoringLineBreak() const
9360
{
94-
return LeafBoxIterator(*this).traverseLineRightwardOnLineIgnoringLineBreak();
61+
auto box = LeafBoxIterator { *this };
62+
box.traverseLineRightwardOnLineIgnoringLineBreak();
63+
return box;
9564
}
9665

9766
LeafBoxIterator Box::nextLineLeftwardOnLineIgnoringLineBreak() const
9867
{
99-
return LeafBoxIterator(*this).traverseLineLeftwardOnLineIgnoringLineBreak();
68+
auto box = LeafBoxIterator { *this };
69+
box.traverseLineLeftwardOnLineIgnoringLineBreak();
70+
return box;
10071
}
10172

10273
InlineBoxIterator Box::parentInlineBox() const
10374
{
10475
ASSERT(!atEnd());
10576

106-
auto candidate = *this;
107-
108-
if (isRootInlineBox()) {
109-
candidate.setAtEnd();
110-
return { candidate };
111-
}
77+
if (isRootInlineBox())
78+
return { };
11279

80+
auto candidate = *this;
11381
CheckedRef parentLayoutBox = displayBox().layoutBox().parent();
11482
do {
11583
candidate.traversePreviousBox();
11684
} while (!candidate.atEnd() && &candidate.displayBox().layoutBox() != parentLayoutBox.ptr());
11785

118-
ASSERT(candidate.atEnd() || candidate.displayBox().isInlineBox());
86+
if (candidate.atEnd())
87+
return { };
11988

89+
ASSERT(candidate.displayBox().isInlineBox());
12090
return { candidate };
12191
}
12292

123-
LineBoxIterator Box::lineBox() const
93+
LineBox Box::lineBox() const
12494
{
12595
return lineBoxFor(*m_inlineContent, lineIndex());
12696
}
@@ -144,39 +114,6 @@ RenderObject::HighlightState Box::selectionState() const
144114
return renderer().selectionState();
145115
}
146116

147-
LeafBoxIterator::LeafBoxIterator(const Box& run)
148-
: BoxIterator(run)
149-
{
150-
}
151-
152-
LeafBoxIterator& LeafBoxIterator::traverseLineRightwardOnLine()
153-
{
154-
m_box.traverseNextLeafOnLine();
155-
return *this;
156-
}
157-
158-
LeafBoxIterator& LeafBoxIterator::traverseLineLeftwardOnLine()
159-
{
160-
m_box.traversePreviousLeafOnLine();
161-
return *this;
162-
}
163-
164-
LeafBoxIterator& LeafBoxIterator::traverseLineRightwardOnLineIgnoringLineBreak()
165-
{
166-
do {
167-
traverseLineRightwardOnLine();
168-
} while (!atEnd() && m_box.isLineBreak());
169-
return *this;
170-
}
171-
172-
LeafBoxIterator& LeafBoxIterator::traverseLineLeftwardOnLineIgnoringLineBreak()
173-
{
174-
do {
175-
traverseLineLeftwardOnLine();
176-
} while (!atEnd() && m_box.isLineBreak());
177-
return *this;
178-
}
179-
180117
LeafBoxIterator boxFor(const RenderLineBreak& renderer)
181118
{
182119
if (CheckedPtr lineLayout = LayoutIntegration::LineLayout::containing(renderer))
@@ -191,9 +128,9 @@ LeafBoxIterator boxFor(const RenderBox& renderer)
191128
return { };
192129
}
193130

194-
LeafBoxIterator boxFor(const LayoutIntegration::InlineContent& content, size_t boxIndex)
131+
Box boxFor(const LayoutIntegration::InlineContent& content, size_t boxIndex)
195132
{
196-
return { Box { content, boxIndex } };
133+
return { content, boxIndex };
197134
}
198135

199136
}

0 commit comments

Comments
 (0)