Commit 14695450 authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Reference the canonical text in offset mapping

This patch adds a reference to the canonical text in offset mapping, so
that we don't need to make a detour to NGInlineNode when working on both
offset mapping and canonical text.

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

Bug: 771398
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I8cf035c44ab90452819d92b91e7853b848781a41
Reviewed-on: https://chromium-review.googlesource.com/724286Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#509930}
parent d5b5df6a
......@@ -405,6 +405,7 @@ const NGOffsetMappingResult& NGInlineNode::ComputeOffsetMappingIfNeeded() {
// TODO(xiaochengh): This doesn't compute offset mapping correctly when
// text-transform CSS property changes text length.
NGOffsetMappingBuilder& mapping_builder = builder.GetOffsetMappingBuilder();
mapping_builder.SetDestinationString(Text());
MutableData()->offset_mapping_ =
WTF::MakeUnique<NGOffsetMappingResult>(mapping_builder.Build());
}
......
......@@ -126,6 +126,8 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNode) {
const Node* foo_node = layout_object_->GetNode();
const NGOffsetMappingResult& result = GetOffsetMapping();
EXPECT_EQ("foo", result.GetText());
ASSERT_EQ(1u, result.GetUnits().size());
TEST_UNIT(result.GetUnits()[0], NGOffsetMappingUnitType::kIdentity, foo_node,
0u, 3u, 0u, 3u);
......@@ -173,6 +175,8 @@ TEST_F(NGInlineNodeOffsetMappingTest, TwoTextNodes) {
const Node* bar_node = bar->GetNode();
const NGOffsetMappingResult& result = GetOffsetMapping();
EXPECT_EQ("foobar", result.GetText());
ASSERT_EQ(2u, result.GetUnits().size());
TEST_UNIT(result.GetUnits()[0], NGOffsetMappingUnitType::kIdentity, foo_node,
0u, 3u, 0u, 3u);
......@@ -234,6 +238,8 @@ TEST_F(NGInlineNodeOffsetMappingTest, BRBetweenTextNodes) {
const Node* bar_node = bar->GetNode();
const NGOffsetMappingResult& result = GetOffsetMapping();
EXPECT_EQ("foo\nbar", result.GetText());
ASSERT_EQ(3u, result.GetUnits().size());
TEST_UNIT(result.GetUnits()[0], NGOffsetMappingUnitType::kIdentity, foo_node,
0u, 3u, 0u, 3u);
......@@ -271,6 +277,8 @@ TEST_F(NGInlineNodeOffsetMappingTest, OneTextNodeWithCollapsedSpace) {
const Node* node = layout_object_->GetNode();
const NGOffsetMappingResult& result = GetOffsetMapping();
EXPECT_EQ("foo bar", result.GetText());
ASSERT_EQ(3u, result.GetUnits().size());
TEST_UNIT(result.GetUnits()[0], NGOffsetMappingUnitType::kIdentity, node, 0u,
4u, 0u, 4u);
......@@ -346,6 +354,8 @@ TEST_F(NGInlineNodeOffsetMappingTest, FullyCollapsedWhiteSpaceNode) {
const Node* space_node = space->GetNode();
const NGOffsetMappingResult& result = GetOffsetMapping();
EXPECT_EQ("foo bar", result.GetText());
ASSERT_EQ(3u, result.GetUnits().size());
TEST_UNIT(result.GetUnits()[0], NGOffsetMappingUnitType::kIdentity, foo_node,
0u, 4u, 0u, 4u);
......
......@@ -128,6 +128,13 @@ void NGOffsetMappingBuilder::Composite(const NGOffsetMappingBuilder& other) {
mapping_[i] = other.mapping_[mapping_[i]];
}
void NGOffsetMappingBuilder::SetDestinationString(String string) {
// TODO(xiaochengh): Add the check below when we stop writing back to
// LayoutText for inline painting.
// DCHECK_EQ(mapping_.back(), string.length());
destination_string_ = string;
}
NGOffsetMappingResult NGOffsetMappingBuilder::Build() const {
NGOffsetMappingResult::UnitVector units;
NGOffsetMappingResult::RangeMap ranges;
......@@ -171,7 +178,8 @@ NGOffsetMappingResult NGOffsetMappingBuilder::Build() const {
if (current_node) {
ranges.insert(current_node, std::make_pair(unit_range_start, units.size()));
}
return NGOffsetMappingResult(std::move(units), std::move(ranges));
return NGOffsetMappingResult(std::move(units), std::move(ranges),
destination_string_);
}
Vector<unsigned> NGOffsetMappingBuilder::DumpOffsetMappingForTesting() const {
......
......@@ -8,6 +8,7 @@
#include "core/CoreExport.h"
#include "platform/wtf/Allocator.h"
#include "platform/wtf/Vector.h"
#include "platform/wtf/text/WTFString.h"
namespace blink {
......@@ -65,6 +66,9 @@ class CORE_EXPORT NGOffsetMappingBuilder {
// Composite the offset mapping held by another builder to this builder.
void Composite(const NGOffsetMappingBuilder&);
// Set the destination string of the offset mapping.
void SetDestinationString(String);
// Finalize and return the offset mapping.
NGOffsetMappingResult Build() const;
......@@ -82,6 +86,9 @@ class CORE_EXPORT NGOffsetMappingBuilder {
// the plain way. It will be replaced by a real implementation for efficiency.
Vector<const LayoutText*> annotation_;
// The destination string of the offset mapping.
String destination_string_;
DISALLOW_COPY_AND_ASSIGN(NGOffsetMappingBuilder);
};
......
......@@ -51,12 +51,14 @@ const NGOffsetMappingResult* GetNGOffsetMappingFor(const Node& node,
}
NGOffsetMappingResult::NGOffsetMappingResult(NGOffsetMappingResult&& other)
: NGOffsetMappingResult(std::move(other.units_), std::move(other.ranges_)) {
}
: NGOffsetMappingResult(std::move(other.units_),
std::move(other.ranges_),
other.text_) {}
NGOffsetMappingResult::NGOffsetMappingResult(UnitVector&& units,
RangeMap&& ranges)
: units_(units), ranges_(ranges) {}
RangeMap&& ranges,
String text)
: units_(units), ranges_(ranges), text_(text) {}
NGOffsetMappingResult::~NGOffsetMappingResult() = default;
......
......@@ -10,6 +10,7 @@
#include "platform/wtf/Allocator.h"
#include "platform/wtf/HashMap.h"
#include "platform/wtf/Vector.h"
#include "platform/wtf/text/WTFString.h"
namespace blink {
......@@ -90,11 +91,12 @@ class CORE_EXPORT NGOffsetMappingResult {
HashMap<Persistent<const Node>, std::pair<unsigned, unsigned>>;
NGOffsetMappingResult(NGOffsetMappingResult&&);
NGOffsetMappingResult(UnitVector&&, RangeMap&&);
NGOffsetMappingResult(UnitVector&&, RangeMap&&, String);
~NGOffsetMappingResult();
const UnitVector& GetUnits() const { return units_; }
const RangeMap& GetRanges() const { return ranges_; }
const String& GetText() const { return text_; }
// Returns the NGOffsetMappingUnit that contains the given offset in the DOM
// node. If there are multiple qualifying units, returns the last one.
......@@ -134,6 +136,7 @@ class CORE_EXPORT NGOffsetMappingResult {
private:
UnitVector units_;
RangeMap ranges_;
String text_;
DISALLOW_COPY_AND_ASSIGN(NGOffsetMappingResult);
};
......
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