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

[LayoutNG] Change offset mapping annotation type to general LayoutObject

This patch is a preparation for supporting IMG and other atomic inlines
in offset mapping, for which we need to use general annotation type in
offset mapping builder instead of LayoutText.

Bug: 776272
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I0c9a24dc4ff97f1c839cb36f25dbef38e30cd4a8
Reviewed-on: https://chromium-review.googlesource.com/728925Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#510272}
parent 69eba886
......@@ -21,19 +21,21 @@ NGOffsetMappingUnitType GetUnitLengthMappingType(unsigned value) {
return NGOffsetMappingUnitType::kExpanded;
}
// Returns the associated node of a possibly null LayoutText.
const Node* GetAssociatedNode(const LayoutText* layout_text) {
if (!layout_text)
// Returns the associated node of a possibly null LayoutObject.
const Node* GetAssociatedNode(const LayoutObject* layout_object) {
if (!layout_object)
return nullptr;
if (layout_text->IsTextFragment())
return ToLayoutTextFragment(layout_text)->AssociatedTextNode();
return layout_text->GetNode();
if (!layout_object->IsText() ||
!ToLayoutText(layout_object)->IsTextFragment())
return layout_object->GetNode();
const LayoutTextFragment* fragment = ToLayoutTextFragment(layout_object);
return fragment->AssociatedTextNode();
}
// Finds the offset mapping unit starting from index |start|.
std::pair<NGOffsetMappingUnitType, unsigned> GetMappingUnitTypeAndEnd(
const Vector<unsigned>& mapping,
const Vector<const LayoutText*>& annotation,
const Vector<const LayoutObject*>& annotation,
unsigned start) {
DCHECK_LT(start + 1, mapping.size());
NGOffsetMappingUnitType type =
......@@ -54,6 +56,14 @@ std::pair<NGOffsetMappingUnitType, unsigned> GetMappingUnitTypeAndEnd(
return std::make_pair(type, end);
}
// If |layout_object| is the remaining text of a text node, returns the start
// offset; In all other cases, returns 0.
unsigned GetRemainingTextOffset(const LayoutObject* layout_object) {
if (!layout_object || !layout_object->IsText())
return 0;
return ToLayoutText(layout_object)->TextStartOffset();
}
} // namespace
NGOffsetMappingBuilder::NGOffsetMappingBuilder() {
......@@ -93,13 +103,13 @@ void NGOffsetMappingBuilder::CollapseTrailingSpace(unsigned skip_length) {
}
}
void NGOffsetMappingBuilder::Annotate(const LayoutText* layout_object) {
void NGOffsetMappingBuilder::Annotate(const LayoutObject* layout_object) {
std::fill(annotation_.begin(), annotation_.end(), layout_object);
}
void NGOffsetMappingBuilder::AnnotateRange(unsigned start,
unsigned end,
const LayoutText* layout_object) {
const LayoutObject* layout_object) {
DCHECK_LE(start, end);
DCHECK_LE(end, annotation_.size());
std::fill(annotation_.begin() + start, annotation_.begin() + end,
......@@ -107,7 +117,7 @@ void NGOffsetMappingBuilder::AnnotateRange(unsigned start,
}
void NGOffsetMappingBuilder::AnnotateSuffix(unsigned length,
const LayoutText* layout_object) {
const LayoutObject* layout_object) {
DCHECK_LE(length, annotation_.size());
AnnotateRange(annotation_.size() - length, annotation_.size(), layout_object);
}
......@@ -152,8 +162,10 @@ NGOffsetMappingResult NGOffsetMappingBuilder::Build() const {
current_node = GetAssociatedNode(annotation_[start]);
inline_start = start;
unit_range_start = units.size();
remaining_text_offset =
annotation_[start] ? annotation_[start]->TextStartOffset() : 0;
// We get a non-zero |remaining_text_offset| only when |current_node| is a
// text node that has blockified ::first-letter style, and we are at the
// remaining text of |current_node|.
remaining_text_offset = GetRemainingTextOffset(annotation_[start]);
}
if (!current_node) {
......@@ -186,7 +198,7 @@ Vector<unsigned> NGOffsetMappingBuilder::DumpOffsetMappingForTesting() const {
return mapping_;
}
Vector<const LayoutText*> NGOffsetMappingBuilder::DumpAnnotationForTesting()
Vector<const LayoutObject*> NGOffsetMappingBuilder::DumpAnnotationForTesting()
const {
return annotation_;
}
......
......@@ -12,7 +12,7 @@
namespace blink {
class LayoutText;
class LayoutObject;
class NGOffsetMappingResult;
// This is the helper class for constructing the DOM-to-TextContent offset
......@@ -28,13 +28,13 @@ class CORE_EXPORT NGOffsetMappingBuilder {
// Associate the offset mapping with a simple annotation with the given node
// as its value.
void Annotate(const LayoutText*);
void Annotate(const LayoutObject*);
// Annotate the offset range with the given node.
void AnnotateRange(unsigned start, unsigned end, const LayoutText*);
void AnnotateRange(unsigned start, unsigned end, const LayoutObject*);
// Annotatie the offset range ending at domain end of the specified length.
void AnnotateSuffix(unsigned length, const LayoutText*);
void AnnotateSuffix(unsigned length, const LayoutObject*);
// Append an identity offset mapping of the specified length with null
// annotation to the builder.
......@@ -74,7 +74,7 @@ class CORE_EXPORT NGOffsetMappingBuilder {
// Exposed for testing only.
Vector<unsigned> DumpOffsetMappingForTesting() const;
Vector<const LayoutText*> DumpAnnotationForTesting() const;
Vector<const LayoutObject*> DumpAnnotationForTesting() const;
private:
// A mock implementation of the offset mapping builder that stores the mapping
......@@ -84,7 +84,7 @@ class CORE_EXPORT NGOffsetMappingBuilder {
// A mock implementation that stores the annotation value of all offsets in
// the plain way. It will be replaced by a real implementation for efficiency.
Vector<const LayoutText*> annotation_;
Vector<const LayoutObject*> annotation_;
// The destination string of the offset mapping.
String destination_string_;
......
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