Commit 8a7b38d2 authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Add IsAfterNonCollapsedCharacter(node, offset) API

This patch adds a new API to LayoutNG offset mapping, so that we can
easily check if a given DOM offset is right after a non-collapsed
character.

This patch is a preparation for adding an NG version of
LayoutText::ContainsCaretOffset().

Bug: 771398
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I93e8a89089661c6f17da6a7a847f75914cb36f75
Reviewed-on: https://chromium-review.googlesource.com/724230
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#509915}
parent f758d115
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
namespace blink { namespace blink {
// TODO(xiaochengh): Rename this test to NGOffsetMappingTest.
class NGInlineNodeOffsetMappingTest : public RenderingTest { class NGInlineNodeOffsetMappingTest : public RenderingTest {
protected: protected:
void SetUp() override { void SetUp() override {
...@@ -70,6 +72,10 @@ class NGInlineNodeOffsetMappingTest : public RenderingTest { ...@@ -70,6 +72,10 @@ class NGInlineNodeOffsetMappingTest : public RenderingTest {
return GetOffsetMapping().IsNonCollapsedCharacter(node, offset); return GetOffsetMapping().IsNonCollapsedCharacter(node, offset);
} }
bool IsAfterNonCollapsedCharacter(const Node& node, unsigned offset) const {
return GetOffsetMapping().IsAfterNonCollapsedCharacter(node, offset);
}
RefPtr<const ComputedStyle> style_; RefPtr<const ComputedStyle> style_;
LayoutNGBlockFlow* layout_block_flow_ = nullptr; LayoutNGBlockFlow* layout_block_flow_ = nullptr;
LayoutObject* layout_object_ = nullptr; LayoutObject* layout_object_ = nullptr;
...@@ -151,6 +157,12 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNode) { ...@@ -151,6 +157,12 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNode) {
EXPECT_TRUE(IsNonCollapsedCharacter(*foo_node, 1)); EXPECT_TRUE(IsNonCollapsedCharacter(*foo_node, 1));
EXPECT_TRUE(IsNonCollapsedCharacter(*foo_node, 2)); EXPECT_TRUE(IsNonCollapsedCharacter(*foo_node, 2));
EXPECT_FALSE(IsNonCollapsedCharacter(*foo_node, 3)); // false at node end EXPECT_FALSE(IsNonCollapsedCharacter(*foo_node, 3)); // false at node end
// false at node start
EXPECT_FALSE(IsAfterNonCollapsedCharacter(*foo_node, 0));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 1));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 2));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 3));
} }
TEST_F(NGInlineNodeOffsetMappingTest, TwoTextNodes) { TEST_F(NGInlineNodeOffsetMappingTest, TwoTextNodes) {
...@@ -198,6 +210,18 @@ TEST_F(NGInlineNodeOffsetMappingTest, TwoTextNodes) { ...@@ -198,6 +210,18 @@ TEST_F(NGInlineNodeOffsetMappingTest, TwoTextNodes) {
EXPECT_TRUE(IsNonCollapsedCharacter(*bar_node, 1)); EXPECT_TRUE(IsNonCollapsedCharacter(*bar_node, 1));
EXPECT_TRUE(IsNonCollapsedCharacter(*bar_node, 2)); EXPECT_TRUE(IsNonCollapsedCharacter(*bar_node, 2));
EXPECT_FALSE(IsNonCollapsedCharacter(*bar_node, 3)); // false at node end EXPECT_FALSE(IsNonCollapsedCharacter(*bar_node, 3)); // false at node end
// false at node start
EXPECT_FALSE(IsAfterNonCollapsedCharacter(*foo_node, 0));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 1));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 2));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*foo_node, 3));
// false at node start
EXPECT_FALSE(IsAfterNonCollapsedCharacter(*bar_node, 0));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*bar_node, 1));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*bar_node, 2));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*bar_node, 3));
} }
TEST_F(NGInlineNodeOffsetMappingTest, BRBetweenTextNodes) { TEST_F(NGInlineNodeOffsetMappingTest, BRBetweenTextNodes) {
...@@ -295,6 +319,16 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNodeWithCollapsedSpace) { ...@@ -295,6 +319,16 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNodeWithCollapsedSpace) {
EXPECT_TRUE(IsNonCollapsedCharacter(*node, 6)); EXPECT_TRUE(IsNonCollapsedCharacter(*node, 6));
EXPECT_TRUE(IsNonCollapsedCharacter(*node, 7)); EXPECT_TRUE(IsNonCollapsedCharacter(*node, 7));
EXPECT_FALSE(IsNonCollapsedCharacter(*node, 8)); EXPECT_FALSE(IsNonCollapsedCharacter(*node, 8));
EXPECT_FALSE(IsAfterNonCollapsedCharacter(*node, 0));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 1));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 2));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 3));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 4));
EXPECT_FALSE(IsAfterNonCollapsedCharacter(*node, 5));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 6));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 7));
EXPECT_TRUE(IsAfterNonCollapsedCharacter(*node, 8));
} }
TEST_F(NGInlineNodeOffsetMappingTest, FullyCollapsedWhiteSpaceNode) { TEST_F(NGInlineNodeOffsetMappingTest, FullyCollapsedWhiteSpaceNode) {
......
...@@ -170,4 +170,17 @@ bool NGOffsetMappingResult::IsNonCollapsedCharacter(const Node& node, ...@@ -170,4 +170,17 @@ bool NGOffsetMappingResult::IsNonCollapsedCharacter(const Node& node,
unit->GetType() != NGOffsetMappingUnitType::kCollapsed; unit->GetType() != NGOffsetMappingUnitType::kCollapsed;
} }
bool NGOffsetMappingResult::IsAfterNonCollapsedCharacter(
const Node& node,
unsigned offset) const {
if (!offset)
return false;
// In case we have one unit ending at |offset| and another starting at
// |offset|, we need to find the former. Hence, search with |offset - 1|.
const NGOffsetMappingUnit* unit =
GetMappingUnitForDOMOffset(node, offset - 1);
return unit && offset > unit->DOMStart() &&
unit->GetType() != NGOffsetMappingUnitType::kCollapsed;
}
} // namespace blink } // namespace blink
...@@ -122,8 +122,13 @@ class CORE_EXPORT NGOffsetMappingResult { ...@@ -122,8 +122,13 @@ class CORE_EXPORT NGOffsetMappingResult {
// Returns true if the character at the position is non-collapsed. If the // Returns true if the character at the position is non-collapsed. If the
// offset is at the end of the node, returns false. // offset is at the end of the node, returns false.
// TODO(xiaochengh): Rename to IsBeforeNonCollapsedCharacter().
bool IsNonCollapsedCharacter(const Node&, unsigned offset) const; bool IsNonCollapsedCharacter(const Node&, unsigned offset) const;
// Returns true if the offset is right after a non-collapsed character. If the
// offset is at the beginning of the node, returns false.
bool IsAfterNonCollapsedCharacter(const Node&, unsigned offset) const;
// TODO(xiaochengh): Add APIs for reverse mapping. // TODO(xiaochengh): Add APIs for reverse mapping.
private: private:
......
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