Skip to content

Commit 2cbf36c

Browse files
committed
Refactor caretWidth into a more reasonable place
https://bugs.webkit.org/show_bug.cgi?id=248899 rdar://102449973 Reviewed by Tim Horton. The `caretWidth` constant is just sitting at the top of `RenderObject.h` currently. This PR refactors it into a method in a more sensical place. * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/editing/Editor.cpp: (WebCore::collapseCaretWidth): * Source/WebCore/rendering/CaretRectComputation.cpp: (WebCore::caretWidth): (WebCore::computeCaretRectForEmptyElement): (WebCore::computeCaretRectForLinePosition): (WebCore::computeCaretRectForSVGInlineText): (WebCore::computeCaretRectForBox): * Source/WebCore/rendering/CaretRectComputation.h: * Source/WebCore/rendering/RenderBlockFlow.h: (WebCore::RenderBlockFlow::endPaddingWidthForCaret const): * Source/WebCore/rendering/RenderObject.h: Canonical link: https://commits.webkit.org/257539@main
1 parent 2a5bd0f commit 2cbf36c

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

Source/WebCore/Headers.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
19391939
plugins/PluginViewBase.h
19401940

19411941
rendering/BreakLines.h
1942+
rendering/CaretRectComputation.h
19421943
rendering/CSSFilter.h
19431944
rendering/ClipRect.h
19441945
rendering/EventRegion.h

Source/WebCore/WebCore.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,7 +5598,7 @@
55985598
E4C4C61A27452A7900A040E7 /* ChildChangeInvalidation.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C4C61827452A7900A040E7 /* ChildChangeInvalidation.h */; };
55995599
E4C91A0E1802343100A17F6D /* TextPaintStyle.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C91A0D1802343100A17F6D /* TextPaintStyle.h */; };
56005600
E4C91A16180999F100A17F6D /* RenderTextLineBoxes.h in Headers */ = {isa = PBXBuildFile; fileRef = E4C91A15180999F100A17F6D /* RenderTextLineBoxes.h */; settings = {ATTRIBUTES = (Private, ); }; };
5601-
E4D33F3B252AEECD00837D05 /* InlineRunAndOffset.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D33F39252AEECC00837D05 /* InlineRunAndOffset.h */; };
5601+
E4D33F3B252AEECD00837D05 /* InlineRunAndOffset.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D33F39252AEECC00837D05 /* InlineRunAndOffset.h */; settings = {ATTRIBUTES = (Private, ); }; };
56025602
E4D33F41252C4B8300837D05 /* InlineIteratorLineBox.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D33F3F252C4B8200837D05 /* InlineIteratorLineBox.h */; settings = {ATTRIBUTES = (Private, ); }; };
56035603
E4D33F44252C50E200837D05 /* InlineIteratorLineBoxLegacyPath.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D33F43252C50E200837D05 /* InlineIteratorLineBoxLegacyPath.h */; settings = {ATTRIBUTES = (Private, ); }; };
56045604
E4D33F46252C50FC00837D05 /* InlineIteratorLineBoxModernPath.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D33F45252C50FB00837D05 /* InlineIteratorLineBoxModernPath.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -5618,7 +5618,7 @@
56185618
E4E8B4F5216B956500B8834D /* FontCascadeDescription.h in Headers */ = {isa = PBXBuildFile; fileRef = E4E8B4F2216B8B6000B8834D /* FontCascadeDescription.h */; settings = {ATTRIBUTES = (Private, ); }; };
56195619
E4E94D6122FF158A00DD191F /* LegacyLineLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = E4A1AC7822FAFD500017B75B /* LegacyLineLayout.h */; settings = {ATTRIBUTES = (Private, ); }; };
56205620
E4ED3ECC2768A51D00F17AC8 /* HasSelectorFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = E4ED3ECA2768A51C00F17AC8 /* HasSelectorFilter.h */; };
5621-
E4F0BE3125712F6E009E7431 /* CaretRectComputation.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F0BE2E25710A75009E7431 /* CaretRectComputation.h */; };
5621+
E4F0BE3125712F6E009E7431 /* CaretRectComputation.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F0BE2E25710A75009E7431 /* CaretRectComputation.h */; settings = {ATTRIBUTES = (Private, ); }; };
56225622
E4F1573927B9261500B330D8 /* ContainerQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F1573227B6991F00B330D8 /* ContainerQuery.h */; settings = {ATTRIBUTES = (Private, ); }; };
56235623
E4F1574127B93B7700B330D8 /* ContainerQueryParser.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F1573D27B93B4C00B330D8 /* ContainerQueryParser.h */; };
56245624
E4F38D1B2626F13B007B1064 /* DefaultResourceLoadPriority.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F38D192626F13B007B1064 /* DefaultResourceLoadPriority.h */; };

Source/WebCore/editing/Editor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,9 +3430,9 @@ void Editor::removeTextPlaceholder(TextPlaceholderElement& placeholder)
34303430
static inline void collapseCaretWidth(IntRect& rect)
34313431
{
34323432
// FIXME: Width adjustment doesn't work for rotated text.
3433-
if (rect.width() == caretWidth)
3433+
if (rect.width() == caretWidth())
34343434
rect.setWidth(0);
3435-
else if (rect.height() == caretWidth)
3435+
else if (rect.height() == caretWidth())
34363436
rect.setHeight(0);
34373437
}
34383438

Source/WebCore/rendering/CaretRectComputation.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939

4040
namespace WebCore {
4141

42+
#if USE(APPLE_INTERNAL_SDK) && PLATFORM(MAC)
43+
#import <WebKitAdditions/CaretRectComputationAdditions.cpp>
44+
#else
45+
int caretWidth()
46+
{
47+
#if PLATFORM(IOS_FAMILY)
48+
return 2; // This value should be kept in sync with UIKit. See <rdar://problem/15580601>.
49+
#else
50+
return 1;
51+
#endif
52+
}
53+
#endif
54+
4255
static LayoutRect computeCaretRectForEmptyElement(const RenderBoxModelObject& renderer, LayoutUnit width, LayoutUnit textIndentOffset, CaretRectMode caretRectMode)
4356
{
4457
ASSERT(!renderer.firstChild() || renderer.firstChild()->isPseudoElement());
@@ -92,18 +105,18 @@ static LayoutRect computeCaretRectForEmptyElement(const RenderBoxModelObject& re
92105
x -= textIndentOffset / 2;
93106
break;
94107
case AlignRight:
95-
x = maxX - caretWidth;
108+
x = maxX - caretWidth();
96109
if (!currentStyle.isLeftToRightDirection())
97110
x -= textIndentOffset;
98111
break;
99112
}
100-
x = std::min(x, std::max<LayoutUnit>(maxX - caretWidth, 0));
113+
x = std::min(x, std::max<LayoutUnit>(maxX - caretWidth(), 0));
101114

102115
auto lineHeight = renderer.lineHeight(true, currentStyle.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
103116
auto height = std::min(lineHeight, LayoutUnit { currentStyle.metricsOfPrimaryFont().height() });
104117
auto y = renderer.paddingTop() + renderer.borderTop() + (lineHeight > height ? (lineHeight - height) / 2 : LayoutUnit { });
105118

106-
auto rect = LayoutRect(x, y, caretWidth, height);
119+
auto rect = LayoutRect(x, y, caretWidth(), height);
107120

108121
if (caretRectMode == CaretRectMode::ExpandToEndOfLine)
109122
rect.shiftMaxXEdgeTo(width);
@@ -121,9 +134,9 @@ static LayoutRect computeCaretRectForLinePosition(const InlineIterator::LineBoxI
121134

122135
// Distribute the caret's width to either side of the offset.
123136
float left = logicalLeftPosition;
124-
int caretWidthLeftOfOffset = caretWidth / 2;
137+
int caretWidthLeftOfOffset = caretWidth() / 2;
125138
left -= caretWidthLeftOfOffset;
126-
int caretWidthRightOfOffset = caretWidth - caretWidthLeftOfOffset;
139+
int caretWidthRightOfOffset = caretWidth() - caretWidthLeftOfOffset;
127140
left = roundf(left);
128141

129142
float lineLeft = lineSelectionRect.x();
@@ -154,13 +167,13 @@ static LayoutRect computeCaretRectForLinePosition(const InlineIterator::LineBoxI
154167

155168
if (rightAligned) {
156169
left = std::max(left, leftEdge);
157-
left = std::min(left, lineRight - caretWidth);
170+
left = std::min(left, lineRight - caretWidth());
158171
} else {
159172
left = std::min(left, rightEdge - caretWidthRightOfOffset);
160173
left = std::max(left, lineLeft);
161174
}
162175

163-
auto rect = IntRect(left, top, caretWidth, height);
176+
auto rect = IntRect(left, top, caretWidth(), height);
164177

165178
if (caretRectMode == CaretRectMode::ExpandToEndOfLine)
166179
rect.shiftMaxXEdgeTo(lineRight);
@@ -207,12 +220,12 @@ static LayoutRect computeCaretRectForSVGInlineText(const InlineBoxAndOffset& box
207220
if (caretOffset < textBox.start() + textBox.len()) {
208221
LayoutRect rect = textBox.localSelectionRect(caretOffset, caretOffset + 1);
209222
LayoutUnit x = textBox.isLeftToRightDirection() ? rect.x() : rect.maxX();
210-
return LayoutRect(x, rect.y(), caretWidth, rect.height());
223+
return LayoutRect(x, rect.y(), caretWidth(), rect.height());
211224
}
212225

213226
LayoutRect rect = textBox.localSelectionRect(caretOffset - 1, caretOffset);
214227
LayoutUnit x = textBox.isLeftToRightDirection() ? rect.maxX() : rect.x();
215-
return { x, rect.y(), caretWidth, rect.height() };
228+
return { x, rect.y(), caretWidth(), rect.height() };
216229
}
217230

218231
static LayoutRect computeCaretRectForBox(const RenderBox& renderer, const InlineBoxAndOffset& boxAndOffset, CaretRectMode caretRectMode)
@@ -222,11 +235,11 @@ static LayoutRect computeCaretRectForBox(const RenderBox& renderer, const Inline
222235
// They never refer to children.
223236
// FIXME: Paint the carets inside empty blocks differently than the carets before/after elements.
224237

225-
LayoutRect rect(renderer.location(), LayoutSize(caretWidth, renderer.height()));
238+
LayoutRect rect(renderer.location(), LayoutSize(caretWidth(), renderer.height()));
226239
bool ltr = boxAndOffset.box ? boxAndOffset.box->isLeftToRightDirection() : renderer.style().isLeftToRightDirection();
227240

228241
if ((!boxAndOffset.offset) ^ ltr)
229-
rect.move(LayoutSize(renderer.width() - caretWidth, 0_lu));
242+
rect.move(LayoutSize(renderer.width() - caretWidth(), 0_lu));
230243

231244
if (boxAndOffset.box) {
232245
auto lineBox = boxAndOffset.box->lineBox();

Source/WebCore/rendering/CaretRectComputation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ enum class CaretRectMode {
3535
ExpandToEndOfLine
3636
};
3737

38+
int caretWidth();
39+
3840
LayoutRect computeLocalCaretRect(const RenderObject&, const InlineBoxAndOffset&, CaretRectMode = CaretRectMode::Normal);
3941

4042
};

Source/WebCore/rendering/RenderBlockFlow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#pragma once
2424

25+
#include "CaretRectComputation.h"
2526
#include "FloatingObjects.h"
2627
#include "LegacyLineLayout.h"
2728
#include "LineWidth.h"
@@ -610,7 +611,7 @@ inline LayoutIntegration::LineLayout* RenderBlockFlow::modernLineLayout()
610611
inline LayoutUnit RenderBlockFlow::endPaddingWidthForCaret() const
611612
{
612613
if (element() && element()->isRootEditableElement() && hasNonVisibleOverflow() && style().isLeftToRightDirection() && !paddingEnd())
613-
return caretWidth;
614+
return caretWidth();
614615
return { };
615616
}
616617

Source/WebCore/rendering/RenderObject.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ struct InlineBoxAndOffset;
8484
struct PaintInfo;
8585
struct SimpleRange;
8686

87-
#if PLATFORM(IOS_FAMILY)
88-
const int caretWidth = 2; // This value should be kept in sync with UIKit. See <rdar://problem/15580601>.
89-
#else
90-
const int caretWidth = 1;
91-
#endif
92-
9387
struct ScrollRectToVisibleOptions;
9488

9589
namespace Layout {

0 commit comments

Comments
 (0)