Commit 0720948f authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Refactor ng_caret_rect.h/cc

This patch refactors functions in ng_caret_rect.h/cc so that:

1. Functions stop passing around the containing NG inline formatting context.

2. Part of ComputeNGLocalCaretRect() is wrapped into a new overload of
   ComputeNGCaretPosition() taking PositionWithAffinity parameter.

This is a preparation patch for implementing NG line-related functions
in crrev.com/c/1008631

Bug: 822575
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I9ee8d90b327fec9684c0bdbf52bee6dad6fafb7f
Reviewed-on: https://chromium-review.googlesource.com/1011217
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551109}
parent f1c92daa
...@@ -106,9 +106,8 @@ LocalCaretRect LocalCaretRectOfPositionTemplate( ...@@ -106,9 +106,8 @@ LocalCaretRect LocalCaretRectOfPositionTemplate(
ComputeInlineAdjustedPosition(position); ComputeInlineAdjustedPosition(position);
if (adjusted.IsNotNull()) { if (adjusted.IsNotNull()) {
if (const LayoutBlockFlow* context = if (NGInlineFormattingContextOf(adjusted.GetPosition()))
NGInlineFormattingContextOf(adjusted.GetPosition())) return ComputeNGLocalCaretRect(adjusted);
return ComputeNGLocalCaretRect(*context, adjusted);
// TODO(editing-dev): This DCHECK is for ensuring the correctness of // TODO(editing-dev): This DCHECK is for ensuring the correctness of
// breaking |ComputeInlineBoxPosition| into |ComputeInlineAdjustedPosition| // breaking |ComputeInlineBoxPosition| into |ComputeInlineAdjustedPosition|
...@@ -158,12 +157,11 @@ LocalCaretRect LocalSelectionRectOfPositionTemplate( ...@@ -158,12 +157,11 @@ LocalCaretRect LocalSelectionRectOfPositionTemplate(
if (adjusted.IsNull()) if (adjusted.IsNull())
return LocalCaretRect(); return LocalCaretRect();
if (const LayoutBlockFlow* context = if (NGInlineFormattingContextOf(adjusted.GetPosition())) {
NGInlineFormattingContextOf(adjusted.GetPosition())) {
// TODO(editing-dev): Use selection height instead of caret height, or // TODO(editing-dev): Use selection height instead of caret height, or
// decide if we need to keep the distinction between caret height and // decide if we need to keep the distinction between caret height and
// selection height in NG. // selection height in NG.
return ComputeNGLocalCaretRect(*context, adjusted); return ComputeNGLocalCaretRect(adjusted);
} }
// TODO(editing-dev): This DCHECK is for ensuring the correctness of // TODO(editing-dev): This DCHECK is for ensuring the correctness of
......
...@@ -18,10 +18,8 @@ const LayoutBlockFlow* NGInlineFormattingContextOf( ...@@ -18,10 +18,8 @@ const LayoutBlockFlow* NGInlineFormattingContextOf(
} }
LocalCaretRect ComputeNGLocalCaretRect( LocalCaretRect ComputeNGLocalCaretRect(
const LayoutBlockFlow& context,
const PositionInFlatTreeWithAffinity& position) { const PositionInFlatTreeWithAffinity& position) {
return ComputeNGLocalCaretRect(context, return ComputeNGLocalCaretRect(ToPositionInDOMTreeWithAffinity(position));
ToPositionInDOMTreeWithAffinity(position));
} }
} // namespace blink } // namespace blink
...@@ -18,8 +18,7 @@ struct LocalCaretRect; ...@@ -18,8 +18,7 @@ struct LocalCaretRect;
const LayoutBlockFlow* NGInlineFormattingContextOf(const PositionInFlatTree&); const LayoutBlockFlow* NGInlineFormattingContextOf(const PositionInFlatTree&);
LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow&, LocalCaretRect ComputeNGLocalCaretRect(const PositionInFlatTreeWithAffinity&);
const PositionInFlatTreeWithAffinity&);
} // namespace blink } // namespace blink
......
...@@ -185,7 +185,6 @@ CaretPositionResolution TryResolveCaretPositionWithFragment( ...@@ -185,7 +185,6 @@ CaretPositionResolution TryResolveCaretPositionWithFragment(
// Helpers for converting NGCaretPositions to caret rects. // Helpers for converting NGCaretPositions to caret rects.
NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide( NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide(
const LayoutBlockFlow& context,
const NGPaintFragment& fragment, const NGPaintFragment& fragment,
NGCaretPositionType position_type) { NGCaretPositionType position_type) {
const bool is_horizontal = fragment.Style().IsHorizontalWritingMode(); const bool is_horizontal = fragment.Style().IsHorizontalWritingMode();
...@@ -223,7 +222,6 @@ NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide( ...@@ -223,7 +222,6 @@ NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide(
} }
NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset( NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
const LayoutBlockFlow& context,
const NGPaintFragment& paint_fragment, const NGPaintFragment& paint_fragment,
unsigned offset) { unsigned offset) {
const NGPhysicalTextFragment& fragment = const NGPhysicalTextFragment& fragment =
...@@ -255,7 +253,8 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset( ...@@ -255,7 +253,8 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
paint_fragment.InlineOffsetToContainerBox(); paint_fragment.InlineOffsetToContainerBox();
NGPhysicalSize caret_size(caret_width, caret_height); NGPhysicalSize caret_size(caret_width, caret_height);
const NGPhysicalBoxFragment& context_fragment = *context.CurrentFragment(); const NGPaintFragment& context_fragment =
*NGPaintFragment::GetForInlineContainer(fragment.GetLayoutObject());
const NGPaintFragment* line_box = paint_fragment.ContainerLineBox(); const NGPaintFragment* line_box = paint_fragment.ContainerLineBox();
const NGPhysicalOffset line_box_offset = const NGPhysicalOffset line_box_offset =
line_box->InlineOffsetToContainerBox(); line_box->InlineOffsetToContainerBox();
...@@ -284,8 +283,7 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset( ...@@ -284,8 +283,7 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
return NGPhysicalOffsetRect(caret_location, caret_size); return NGPhysicalOffsetRect(caret_location, caret_size);
} }
LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context, LocalCaretRect ComputeLocalCaretRect(const NGCaretPosition& caret_position) {
const NGCaretPosition& caret_position) {
if (caret_position.IsNull()) if (caret_position.IsNull())
return LocalCaretRect(); return LocalCaretRect();
...@@ -294,7 +292,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context, ...@@ -294,7 +292,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context,
case NGCaretPositionType::kAfterBox: { case NGCaretPositionType::kAfterBox: {
DCHECK(caret_position.fragment->PhysicalFragment().IsBox()); DCHECK(caret_position.fragment->PhysicalFragment().IsBox());
const NGPhysicalOffsetRect fragment_local_rect = const NGPhysicalOffsetRect fragment_local_rect =
ComputeLocalCaretRectByBoxSide(context, *caret_position.fragment, ComputeLocalCaretRectByBoxSide(*caret_position.fragment,
caret_position.position_type); caret_position.position_type);
return {caret_position.fragment->GetLayoutObject(), return {caret_position.fragment->GetLayoutObject(),
fragment_local_rect.ToLayoutRect()}; fragment_local_rect.ToLayoutRect()};
...@@ -303,7 +301,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context, ...@@ -303,7 +301,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context,
DCHECK(caret_position.fragment->PhysicalFragment().IsText()); DCHECK(caret_position.fragment->PhysicalFragment().IsText());
DCHECK(caret_position.text_offset.has_value()); DCHECK(caret_position.text_offset.has_value());
const NGPhysicalOffsetRect caret_rect = ComputeLocalCaretRectAtTextOffset( const NGPhysicalOffsetRect caret_rect = ComputeLocalCaretRectAtTextOffset(
context, *caret_position.fragment, *caret_position.text_offset); *caret_position.fragment, *caret_position.text_offset);
return {caret_position.fragment->GetLayoutObject(), return {caret_position.fragment->GetLayoutObject(),
caret_rect.ToLayoutRect()}; caret_rect.ToLayoutRect()};
...@@ -361,24 +359,30 @@ NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow& context, ...@@ -361,24 +359,30 @@ NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow& context,
return candidate; return candidate;
} }
LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow& context, NGCaretPosition ComputeNGCaretPosition(const PositionWithAffinity& position) {
const PositionWithAffinity& position) {
AssertValidPositionForCaretRectComputation(position); AssertValidPositionForCaretRectComputation(position);
DCHECK_EQ(&context, NGInlineFormattingContextOf(position.GetPosition())); const LayoutBlockFlow* context =
const NGOffsetMapping* mapping = NGOffsetMapping::GetFor(&context); NGInlineFormattingContextOf(position.GetPosition());
if (!context)
return NGCaretPosition();
const NGOffsetMapping* mapping = NGOffsetMapping::GetFor(context);
DCHECK(mapping); DCHECK(mapping);
const Optional<unsigned> maybe_offset = const Optional<unsigned> maybe_offset =
mapping->GetTextContentOffset(position.GetPosition()); mapping->GetTextContentOffset(position.GetPosition());
if (!maybe_offset.has_value()) { if (!maybe_offset.has_value()) {
// TODO(xiaochengh): Investigate if we reach here. // TODO(xiaochengh): Investigate if we reach here.
NOTREACHED(); NOTREACHED();
return LocalCaretRect(); return NGCaretPosition();
} }
const unsigned offset = maybe_offset.value(); const unsigned offset = maybe_offset.value();
const TextAffinity affinity = position.Affinity(); const TextAffinity affinity = position.Affinity();
return ComputeLocalCaretRect( return ComputeNGCaretPosition(*context, offset, affinity);
context, ComputeNGCaretPosition(context, offset, affinity)); }
LocalCaretRect ComputeNGLocalCaretRect(const PositionWithAffinity& position) {
return ComputeLocalCaretRect(ComputeNGCaretPosition(position));
} }
} // namespace blink } // namespace blink
...@@ -18,11 +18,11 @@ class NGPaintFragment; ...@@ -18,11 +18,11 @@ class NGPaintFragment;
class LayoutBlockFlow; class LayoutBlockFlow;
struct LocalCaretRect; struct LocalCaretRect;
// Given an inline formatting context and a position in the context, returns the // Given a position with affinity, returns the caret rect if the position is
// caret rect if a caret should be placed at the position, with the given // laid out with LayoutNG, and a caret can be placed at the position with the
// affinity. The caret rect location is local to the given formatting context. // given affinity. The caret rect location is local to the containing inline
CORE_EXPORT LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow&, // formatting context.
const PositionWithAffinity&); CORE_EXPORT LocalCaretRect ComputeNGLocalCaretRect(const PositionWithAffinity&);
// An NGCaretPosition indicates a caret position relative to an inline // An NGCaretPosition indicates a caret position relative to an inline
// NGPaintFragment: // NGPaintFragment:
...@@ -53,6 +53,10 @@ CORE_EXPORT NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow&, ...@@ -53,6 +53,10 @@ CORE_EXPORT NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow&,
unsigned, unsigned,
TextAffinity); TextAffinity);
// Shorthand of the above when the input is a position instead of a
// (context, offset) pair.
NGCaretPosition ComputeNGCaretPosition(const PositionWithAffinity&);
} // namespace blink } // namespace blink
#endif // NGCaretRect_h #endif // NGCaretRect_h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment