Index: third_party/WebKit/Source/core/layout/ng/inline/ng_offset_mapping_builder.cc |
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_offset_mapping_builder.cc b/third_party/WebKit/Source/core/layout/ng/inline/ng_offset_mapping_builder.cc |
index 46c551c224066683e16223b7fb2e9dc0cd94b9dd..7b6c4746b428b655bb101a10ca4ed6425d152f6b 100644 |
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_offset_mapping_builder.cc |
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_offset_mapping_builder.cc |
@@ -4,6 +4,9 @@ |
#include "core/layout/ng/inline/ng_offset_mapping_builder.h" |
+#include <algorithm> |
+#include "core/editing/VisibleUnits.h" |
+#include "core/layout/LayoutText.h" |
#include "core/layout/ng/inline/ng_offset_mapping_result.h" |
#include "platform/wtf/text/StringBuilder.h" |
@@ -45,6 +48,34 @@ std::pair<NGOffsetMappingUnitType, unsigned> GetMappingUnitTypeAndEnd( |
} // namespace |
+const NGOffsetMappingUnit* NGOffsetMappingResult::GetMappingUnitForDOMOffset( |
+ const Node& node, |
+ unsigned offset) { |
+ const LayoutObject* layout_object = AssociatedLayoutObjectOf(node, offset); |
+ if (!layout_object || !layout_object->IsText() || |
+ !ranges.Contains(ToLayoutText(layout_object))) |
+ return nullptr; |
+ |
+ unsigned range_start; |
+ unsigned range_end; |
+ std::tie(range_start, range_end) = ranges.at(ToLayoutText(layout_object)); |
+ |
+ if (range_start == range_end || units[range_start].dom_start > offset) |
+ return nullptr; |
+ |
+ // Find the last unit where unit.dom_start <= offset |
+ unsigned unit_index = |
+ std::upper_bound(units.begin() + range_start, units.begin() + range_end, |
+ offset, |
+ [](unsigned offset, const NGOffsetMappingUnit& unit) { |
+ return offset < unit.dom_start; |
+ }) - |
+ units.begin() - 1; |
+ if (units[unit_index].dom_end < offset) |
+ return nullptr; |
+ return &units[unit_index]; |
+} |
+ |
NGOffsetMappingBuilder::NGOffsetMappingBuilder() { |
mapping_.push_back(0); |
} |