Skip to content

Commit

Permalink
Refactor Renderedposition::ComputeCompositedSelection using LocalCare…
Browse files Browse the repository at this point in the history
…tRect

This patch refactors the function replacing
LayoutObject::LocalCaretRect(InlineBox, int offset)
with LocalCaretRectOfPosition(PositionWithAffinity) for NG.

Since RenderedPosition stores a InlineBox and computes LayoutRect
in its instance functions, this patch makes the functions static
and receive LocalCaretRect.
As a result, this patch makes ComputeCompositedSelection independent
from RenderedPosition class.

This patch also remove GetSamplePointForVisibility test
because it is now local function only used by IsVisible
which is test inside ComputeCompositedSelection test;

Note:
CompositedSelectionBoundsTest also tests ComputeCompositedSelection

Bug: 789870
Change-Id: If955d56deac1477e65d71523fb628d75b421b2ab
Reviewed-on: https://chromium-review.googlesource.com/875842
Reviewed-by: Xiaocheng Hu <xiaochengh@chromium.org>
Commit-Queue: Yoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#531788}
  • Loading branch information
Yoichi Osato authored and Commit Bot committed Jan 25, 2018
1 parent 779e08c commit b8d8c34
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 84 deletions.
77 changes: 45 additions & 32 deletions third_party/WebKit/Source/core/editing/RenderedPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/editing/FrameSelection.h"
#include "core/editing/InlineBoxPosition.h"
#include "core/editing/InlineBoxTraversal.h"
#include "core/editing/LocalCaretRect.h"
#include "core/editing/TextAffinity.h"
#include "core/editing/VisiblePosition.h"
#include "core/editing/VisibleSelection.h"
Expand Down Expand Up @@ -261,13 +262,14 @@ IntRect RenderedPosition::AbsoluteRect(

// Convert a local point into the coordinate system of backing coordinates.
// Also returns the backing layer if needed.
FloatPoint RenderedPosition::LocalToInvalidationBackingPoint(
const LayoutPoint& local_point) const {
static FloatPoint LocalToInvalidationBackingPoint(
const LayoutPoint& local_point,
const LayoutObject& layout_object) {
const LayoutBoxModelObject& paint_invalidation_container =
layout_object_->ContainerForPaintInvalidation();
layout_object.ContainerForPaintInvalidation();
DCHECK(paint_invalidation_container.Layer());

FloatPoint container_point = layout_object_->LocalToAncestorPoint(
FloatPoint container_point = layout_object.LocalToAncestorPoint(
FloatPoint(local_point), &paint_invalidation_container,
kTraverseDocumentBoundaries);

Expand Down Expand Up @@ -302,10 +304,11 @@ static GraphicsLayer* GetGraphicsLayerBacking(
&paint_invalidation_container);
}

std::pair<LayoutPoint, LayoutPoint>
RenderedPosition::GetLocalSelectionEndpoints(bool selection_start) const {
const LayoutRect rect = layout_object_->LocalCaretRect(inline_box_, offset_);
if (layout_object_->Style()->IsHorizontalWritingMode()) {
std::pair<LayoutPoint, LayoutPoint> static GetLocalSelectionEndpoints(
bool selection_start,
const LocalCaretRect& local_caret_rect) {
const LayoutRect rect = local_caret_rect.rect;
if (local_caret_rect.layout_object->Style()->IsHorizontalWritingMode()) {
const LayoutPoint edge_top_in_layer = rect.MinXMinYCorner();
const LayoutPoint edge_bottom_in_layer = rect.MinXMaxYCorner();
return {edge_top_in_layer, edge_bottom_in_layer};
Expand All @@ -324,29 +327,35 @@ RenderedPosition::GetLocalSelectionEndpoints(bool selection_start) const {
return {edge_top_in_layer, edge_bottom_in_layer};
}

CompositedSelectionBound RenderedPosition::PositionInGraphicsLayerBacking(
bool selection_start) const {
if (IsNull())
// TODO(yoichio): Move the function body here to avoid forward declaration.
static bool IsVisible(bool, const LocalCaretRect&);
static CompositedSelectionBound PositionInGraphicsLayerBacking(
bool selection_start,
const PositionWithAffinity& position) {
const LocalCaretRect& local_caret_rect = LocalCaretRectOfPosition(position);
const LayoutObject* const layout_object = local_caret_rect.layout_object;
if (!layout_object)
return CompositedSelectionBound();

CompositedSelectionBound bound;
// Flipped blocks writing mode is not only vertical but also right to left.
if (!layout_object_->Style()->IsHorizontalWritingMode()) {
bound.is_text_direction_rtl = layout_object_->HasFlippedBlocksWritingMode();
if (!layout_object->Style()->IsHorizontalWritingMode()) {
bound.is_text_direction_rtl = layout_object->HasFlippedBlocksWritingMode();
}

LayoutPoint edge_top_in_layer, edge_bottom_in_layer;
std::tie(edge_top_in_layer, edge_bottom_in_layer) =
GetLocalSelectionEndpoints(selection_start);
bound.edge_top_in_layer = LocalToInvalidationBackingPoint(edge_top_in_layer);
GetLocalSelectionEndpoints(selection_start, local_caret_rect);
bound.edge_top_in_layer =
LocalToInvalidationBackingPoint(edge_top_in_layer, *layout_object);
bound.edge_bottom_in_layer =
LocalToInvalidationBackingPoint(edge_bottom_in_layer);
bound.layer = GetGraphicsLayerBacking(*layout_object_);
bound.hidden = !IsVisible(selection_start);
LocalToInvalidationBackingPoint(edge_bottom_in_layer, *layout_object);
bound.layer = GetGraphicsLayerBacking(*layout_object);
bound.hidden = !IsVisible(selection_start, local_caret_rect);
return bound;
}

LayoutPoint RenderedPosition::GetSamplePointForVisibility(
static LayoutPoint GetSamplePointForVisibility(
const LayoutPoint& edge_top_in_layer,
const LayoutPoint& edge_bottom_in_layer) {
FloatSize diff(edge_top_in_layer - edge_bottom_in_layer);
Expand All @@ -358,11 +367,14 @@ LayoutPoint RenderedPosition::GetSamplePointForVisibility(
return sample_point;
}

bool RenderedPosition::IsVisible(bool selection_start) const {
if (IsNull())
// Returns whether this position is not visible on the screen (because
// clipped out).
static bool IsVisible(bool selection_start,
const LocalCaretRect& local_caret_rect) {
if (!local_caret_rect.layout_object)
return false;

Node* node = layout_object_->GetNode();
Node* const node = local_caret_rect.layout_object->GetNode();
if (!node)
return true;
TextControlElement* text_control = EnclosingTextControl(node);
Expand All @@ -377,14 +389,15 @@ bool RenderedPosition::IsVisible(bool selection_start) const {

LayoutPoint edge_top_in_layer, edge_bottom_in_layer;
std::tie(edge_top_in_layer, edge_bottom_in_layer) =
GetLocalSelectionEndpoints(selection_start);
GetLocalSelectionEndpoints(selection_start, local_caret_rect);
LayoutPoint sample_point =
GetSamplePointForVisibility(edge_top_in_layer, edge_bottom_in_layer);

LayoutBox* text_control_object = ToLayoutBox(layout_object);
LayoutPoint position_in_input(layout_object_->LocalToAncestorPoint(
FloatPoint(sample_point), text_control_object,
kTraverseDocumentBoundaries));
LayoutPoint position_in_input(
local_caret_rect.layout_object->LocalToAncestorPoint(
FloatPoint(sample_point), text_control_object,
kTraverseDocumentBoundaries));
if (!text_control_object->BorderBoxRect().Contains(position_in_input))
return false;

Expand Down Expand Up @@ -415,15 +428,15 @@ CompositedSelection RenderedPosition::ComputeCompositedSelection(
return {};

CompositedSelection selection;
VisiblePosition visible_start(visible_selection.VisibleStart());
RenderedPosition rendered_start(visible_start);
selection.start = rendered_start.PositionInGraphicsLayerBacking(true);
PositionWithAffinity position_start(visible_selection.Start(),
visible_selection.Affinity());
selection.start = PositionInGraphicsLayerBacking(true, position_start);
if (!selection.start.layer)
return {};

VisiblePosition visible_end(visible_selection.VisibleEnd());
RenderedPosition rendered_end(visible_end);
selection.end = rendered_end.PositionInGraphicsLayerBacking(false);
PositionWithAffinity position_end(visible_selection.End(),
visible_selection.Affinity());
selection.end = PositionInGraphicsLayerBacking(false, position_end);
if (!selection.end.layer)
return {};

Expand Down
23 changes: 2 additions & 21 deletions third_party/WebKit/Source/core/editing/RenderedPosition.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
namespace blink {

class FrameSelection;
class LayoutPoint;
class LayoutUnit;
class LayoutObject;
struct CompositedSelectionBound;
struct CompositedSelection;

class CORE_EXPORT RenderedPosition {
Expand Down Expand Up @@ -88,13 +86,8 @@ class CORE_EXPORT RenderedPosition {

IntRect AbsoluteRect(LayoutUnit* extra_width_to_end_of_line = nullptr) const;

CompositedSelectionBound PositionInGraphicsLayerBacking(
bool selection_start) const;

// Returns whether this position is not visible on the screen (because
// clipped out).
bool IsVisible(bool selection_start) const;

// TODO(editing-dev): This function doesn't use RenderedPosition
// instance anymore. Consider moving.
static CompositedSelection ComputeCompositedSelection(const FrameSelection&);

private:
Expand All @@ -114,24 +107,12 @@ class CORE_EXPORT RenderedPosition {
bool AtRightBoundaryOfBidiRun(ShouldMatchBidiLevel,
unsigned char bidi_level_of_run) const;

std::pair<LayoutPoint, LayoutPoint> GetLocalSelectionEndpoints(
bool selection_star) const;

FloatPoint LocalToInvalidationBackingPoint(
const LayoutPoint& local_point) const;

static LayoutPoint GetSamplePointForVisibility(
const LayoutPoint& edge_top_in_layer,
const LayoutPoint& edge_bottom_in_layer);

const LayoutObject* layout_object_;
const InlineBox* inline_box_;
int offset_;

mutable Optional<const InlineBox*> prev_leaf_child_;
mutable Optional<const InlineBox*> next_leaf_child_;

FRIEND_TEST_ALL_PREFIXES(RenderedPositionTest, GetSamplePointForVisibility);
};

inline RenderedPosition::RenderedPosition()
Expand Down
62 changes: 31 additions & 31 deletions third_party/WebKit/Source/core/editing/RenderedPositionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@
#include "core/editing/RenderedPosition.h"

#include "build/build_config.h"
#include "core/css/CSSStyleDeclaration.h"
#include "core/editing/VisiblePosition.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/FrameSelection.h"
#include "core/editing/SelectionTemplate.h"
#include "core/editing/testing/EditingTestBase.h"
#include "core/frame/Settings.h"
#include "core/html/forms/HTMLInputElement.h"
#include "core/paint/compositing/CompositedSelection.h"

namespace blink {

class RenderedPositionTest : public EditingTestBase {};

#if defined(OS_ANDROID)
#define MAYBE_IsVisible DISABLED_IsVisible
#define MAYBE_ComputeCompositedSelection DISABLED_ComputeCompositedSelection
#else
#define MAYBE_IsVisible IsVisible
#define MAYBE_ComputeCompositedSelection ComputeCompositedSelection
#endif
TEST_F(RenderedPositionTest, MAYBE_IsVisible) {
SetBodyContent(
"<input id=target width=100 value='test test test test test tes tes test'"
" style='will-change: transform; font-size:40pt;'/>");

Element* target = GetDocument().getElementById("target");
Position visible_position(
target->GetLayoutObject()->SlowFirstChild()->SlowFirstChild()->GetNode(),
0);
RenderedPosition rendered_visible_position(
CreateVisiblePosition(visible_position));
EXPECT_TRUE(rendered_visible_position.IsVisible(true));

Position hidden_position(
target->GetLayoutObject()->SlowFirstChild()->SlowFirstChild()->GetNode(),
32);
RenderedPosition rendered_hidden_position(
CreateVisiblePosition(hidden_position));
EXPECT_FALSE(rendered_hidden_position.IsVisible(true));
}
TEST_F(RenderedPositionTest, MAYBE_ComputeCompositedSelection) {
// Enable compositing.
GetPage().GetSettings().SetAcceleratedCompositingEnabled(true);
GetDocument().View()->SetParentVisible(true);
GetDocument().View()->SetSelfVisible(true);
GetDocument().View()->UpdateAllLifecyclePhases();

TEST_F(RenderedPositionTest, GetSamplePointForVisibility) {
LayoutPoint top(-1, 10);
LayoutPoint bottom(20, 10);
EXPECT_EQ(LayoutPoint(19, 10),
RenderedPosition::GetSamplePointForVisibility(top, bottom));
SetBodyContent(
"<input id=target width=20 value='test test test test test tes tes test'"
"style='width: 100px; height: 20px;'>");
HTMLInputElement* target =
ToHTMLInputElement(GetDocument().getElementById("target"));
DCHECK(target);
target->focus();
Selection().SetSelection(
SelectionInDOMTree::Builder()
.SelectAllChildren(*target->InnerEditorElement())
.Build(),
SetSelectionOptions::Builder().SetShouldShowHandle(true).Build());
UpdateAllLifecyclePhases();

const CompositedSelection& composited_selection =
RenderedPosition::ComputeCompositedSelection(Selection());
EXPECT_FALSE(composited_selection.start.hidden);
EXPECT_TRUE(composited_selection.end.hidden);
}

#undef MAYBE_IsVisible
} // namespace blink

0 comments on commit b8d8c34

Please sign in to comment.