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

[LayoutNG] Convert GetMappingUnitsForDOMOffsetRange to take EphemeralRange

This patch changes the API to take Position parameters to better support
mapping non-text anchored positions to LayoutNG canonical text.

After this patch, NGOffsetMapping is fully converted to take Position parameters.

Bug: 699017
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: Iee9956dc59c1ab34fe03f9f0f11fe890ddf9e568
Reviewed-on: https://chromium-review.googlesource.com/744733
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#513010}
parent 1911f596
......@@ -6,6 +6,7 @@
#include <algorithm>
#include "core/dom/FirstLetterPseudoElement.h"
#include "core/editing/EphemeralRange.h"
#include "core/editing/iterators/TextIteratorTextState.h"
#include "core/layout/LayoutTextFragment.h"
#include "core/layout/line/InlineTextBox.h"
......@@ -91,9 +92,12 @@ void TextIteratorTextNodeHandler::HandleTextNodeWithLayoutNG() {
}
while (offset_ < end_offset_ && !text_state_.PositionNode()) {
const EphemeralRange range_to_emit(Position(text_node_, offset_),
Position(text_node_, end_offset_));
// We may go through multiple mappings, which happens when there is
// ::first-letter and blockifying style.
auto* mapping = NGOffsetMapping::GetFor(Position(text_node_, offset_));
auto* mapping = NGOffsetMapping::GetFor(range_to_emit.StartPosition());
if (!mapping) {
offset_ = end_offset_;
return;
......@@ -101,8 +105,7 @@ void TextIteratorTextNodeHandler::HandleTextNodeWithLayoutNG() {
const unsigned initial_offset = offset_;
for (const NGOffsetMappingUnit& unit :
mapping->GetMappingUnitsForDOMOffsetRange(*text_node_, offset_,
end_offset_)) {
mapping->GetMappingUnitsForDOMRange(range_to_emit)) {
const unsigned run_start = std::max(offset_, unit.DOMStart());
const unsigned run_end = std::min(end_offset_, unit.DOMEnd());
if (run_start >= run_end ||
......
......@@ -6,6 +6,7 @@
#include "core/dom/Node.h"
#include "core/dom/Text.h"
#include "core/editing/EphemeralRange.h"
#include "core/editing/Position.h"
#include "core/layout/ng/inline/ng_inline_node.h"
......@@ -176,10 +177,15 @@ const NGOffsetMappingUnit* NGOffsetMapping::GetMappingUnitForPosition(
return unit;
}
NGMappingUnitRange NGOffsetMapping::GetMappingUnitsForDOMOffsetRange(
const Node& node,
unsigned start_offset,
unsigned end_offset) const {
NGMappingUnitRange NGOffsetMapping::GetMappingUnitsForDOMRange(
const EphemeralRange& range) const {
DCHECK(NGOffsetMapping::AcceptsPosition(range.StartPosition()));
DCHECK(NGOffsetMapping::AcceptsPosition(range.EndPosition()));
DCHECK_EQ(range.StartPosition().AnchorNode(),
range.EndPosition().AnchorNode());
const Node& node = *range.StartPosition().AnchorNode();
const unsigned start_offset = ToNodeOffsetPair(range.StartPosition()).second;
const unsigned end_offset = ToNodeOffsetPair(range.EndPosition()).second;
unsigned range_start;
unsigned range_end;
std::tie(range_start, range_end) = ranges_.at(&node);
......
......@@ -106,9 +106,6 @@ class CORE_EXPORT NGOffsetMapping {
// ------ Mapping APIs from DOM to text content ------
// TODO(xiaochengh): Change the functions to take Positions instead of (node,
// offset) pairs.
// Note: any Position passed to the APIs must be in either of the two types:
// 1. Offset-in-anchor in text node
// 2. Before/After-anchor of BR or atomic inline
......@@ -128,10 +125,9 @@ class CORE_EXPORT NGOffsetMapping {
const NGOffsetMappingUnit* GetMappingUnitForPosition(const Position&) const;
// Returns all NGOffsetMappingUnits whose DOM ranges has non-empty (but
// possibly collapsed) intersections with the passed in DOM offset range.
NGMappingUnitRange GetMappingUnitsForDOMOffsetRange(const Node&,
unsigned,
unsigned) const;
// possibly collapsed) intersections with the passed in DOM range. This API
// only accepts ranges whose start and end have the same anchor node.
NGMappingUnitRange GetMappingUnitsForDOMRange(const EphemeralRange&) const;
// Returns the text content offset corresponding to the given position.
// Returns nullopt when the position is not laid out in this block.
......
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