Commit 50f5a150 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Introduce SelectionTemplate::IsBaseFirst()

This patch Introduces |SelectionTemplate::IsBaseFirst()| and utilize it for
|Start()| and |End()| as preparation of make |SelectionForUndoStep::From()|
faster.

Bug: 751945
Change-Id: I560c23bc6c4d72de3be3985fd6584805ac9e7da7
Reviewed-on: https://chromium-review.googlesource.com/598664Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#492001}
parent 036052f7
......@@ -14,6 +14,7 @@ SelectionTemplate<Strategy>::SelectionTemplate(const SelectionTemplate& other)
: base_(other.base_),
extent_(other.extent_),
affinity_(other.affinity_),
direction_(other.direction_),
is_directional_(other.is_directional_)
#if DCHECK_IS_ON()
,
......@@ -136,17 +137,21 @@ void SelectionTemplate<Strategy>::ShowTreeForThis() const {
template <typename Strategy>
const PositionTemplate<Strategy>&
SelectionTemplate<Strategy>::ComputeEndPosition() const {
if (base_ == extent_)
return base_;
return base_ < extent_ ? extent_ : base_;
return IsBaseFirst() ? extent_ : base_;
}
template <typename Strategy>
const PositionTemplate<Strategy>&
SelectionTemplate<Strategy>::ComputeStartPosition() const {
if (base_ == extent_)
return base_;
return base_ < extent_ ? base_ : extent_;
return IsBaseFirst() ? base_ : extent_;
}
template <typename Strategy>
bool SelectionTemplate<Strategy>::IsBaseFirst() const {
DCHECK(AssertValid());
if (direction_ == Direction::kNotComputed)
direction_ = base_ <= extent_ ? Direction::kForward : Direction::kBackward;
return direction_ == Direction::kForward;
}
template <typename Strategy>
......@@ -201,6 +206,8 @@ template <typename Strategy>
SelectionTemplate<Strategy> SelectionTemplate<Strategy>::Builder::Build()
const {
DCHECK(selection_.AssertValid());
selection_.direction_ =
selection_.IsNone() ? Direction::kForward : Direction::kNotComputed;
return selection_;
}
......
......@@ -82,6 +82,7 @@ class CORE_EXPORT SelectionTemplate final {
const PositionTemplate<Strategy>& Base() const;
const PositionTemplate<Strategy>& Extent() const;
TextAffinity Affinity() const { return affinity_; }
bool IsBaseFirst() const;
bool IsCaret() const;
bool IsDirectional() const { return is_directional_; }
bool IsNone() const { return base_.IsNull(); }
......@@ -108,11 +109,18 @@ class CORE_EXPORT SelectionTemplate final {
private:
friend class SelectionEditor;
enum class Direction {
kNotComputed,
kForward, // base <= extent
kBackward, // base > extent
};
Document* GetDocument() const;
PositionTemplate<Strategy> base_;
PositionTemplate<Strategy> extent_;
TextAffinity affinity_ = TextAffinity::kDownstream;
mutable Direction direction_ = Direction::kForward;
bool is_directional_ = false;
#if DCHECK_IS_ON()
uint64_t dom_tree_version_;
......
......@@ -15,12 +15,32 @@ TEST_F(SelectionTest, defaultConstructor) {
SelectionInDOMTree selection;
EXPECT_EQ(TextAffinity::kDownstream, selection.Affinity());
EXPECT_TRUE(selection.IsBaseFirst());
EXPECT_FALSE(selection.IsDirectional());
EXPECT_TRUE(selection.IsNone());
EXPECT_EQ(Position(), selection.Base());
EXPECT_EQ(Position(), selection.Extent());
}
TEST_F(SelectionTest, IsBaseFirst) {
SetBodyContent("<div id='sample'>abcdef</div>");
Element* sample = GetDocument().getElementById("sample");
Position base(Position(sample->firstChild(), 4));
Position extent(Position(sample->firstChild(), 2));
SelectionInDOMTree::Builder builder;
builder.Collapse(base);
builder.Extend(extent);
const SelectionInDOMTree& selection = builder.Build();
EXPECT_EQ(TextAffinity::kDownstream, selection.Affinity());
EXPECT_FALSE(selection.IsBaseFirst());
EXPECT_FALSE(selection.IsDirectional());
EXPECT_FALSE(selection.IsNone());
EXPECT_EQ(base, selection.Base());
EXPECT_EQ(extent, selection.Extent());
}
TEST_F(SelectionTest, caret) {
SetBodyContent("<div id='sample'>abcdef</div>");
......@@ -31,6 +51,7 @@ TEST_F(SelectionTest, caret) {
const SelectionInDOMTree& selection = builder.Build();
EXPECT_EQ(TextAffinity::kDownstream, selection.Affinity());
EXPECT_TRUE(selection.IsBaseFirst());
EXPECT_FALSE(selection.IsDirectional());
EXPECT_FALSE(selection.IsNone());
EXPECT_EQ(position, selection.Base());
......@@ -49,6 +70,7 @@ TEST_F(SelectionTest, range) {
const SelectionInDOMTree& selection = builder.Build();
EXPECT_EQ(TextAffinity::kDownstream, selection.Affinity());
EXPECT_TRUE(selection.IsBaseFirst());
EXPECT_FALSE(selection.IsDirectional());
EXPECT_FALSE(selection.IsNone());
EXPECT_EQ(base, selection.Base());
......
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