Skip to content

Truly independent web browser

LibWeb: Find ranges from text directives

Sam Atkinsb4236b3a087f
diff --git a/Libraries/LibWeb/HTML/TextDirective.cpp b/Libraries/LibWeb/HTML/TextDirective.cpp
index b612c3888b2..f9dceace33f 100644
--- a/Libraries/LibWeb/HTML/TextDirective.cpp
+++ b/Libraries/LibWeb/HTML/TextDirective.cpp
@@ -5,7 +5,17 @@
*/
#include <AK/Assertions.h>
+#include <AK/Variant.h>
+#include <LibUnicode/CharacterTypes.h>
+#include <LibUnicode/Collator.h>
+#include <LibUnicode/Segmenter.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/Element.h>
+#include <LibWeb/DOM/Range.h>
+#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/TextDirective.h>
+#include <LibWeb/Layout/SearchableText.h>
+#include <LibWeb/Layout/Viewport.h>
namespace Web::HTML {
@@ -196,4 +206,629 @@ Vector<TextDirective> parse_the_fragment_directive(StringView fragment_directive
return output;
}
+namespace {
+
+class SearchPosition {
+public:
+ struct InBlock {
+ size_t block_index;
+ size_t offset;
+ };
+
+ static SearchPosition in_block(size_t block_index, size_t offset)
+ {
+ return InBlock { block_index, offset };
+ }
+
+ static SearchPosition in_block(InBlock position)
+ {
+ return position;
+ }
+
+ static SearchPosition end_of_document()
+ {
+ return EndOfDocument {};
+ }
+
+ bool is_end_of_document() const
+ {
+ return m_position.has<EndOfDocument>();
+ }
+
+ Optional<InBlock&> block_position()
+ {
+ if (!m_position.has<InBlock>())
+ return {};
+ return m_position.get<InBlock>();
+ }
+
+ Optional<InBlock const&> block_position() const
+ {
+ if (!m_position.has<InBlock>())
+ return {};
+ return m_position.get<InBlock>();
+ }
+
+ int operator<=>(SearchPosition const& other) const
+ {
+ if (is_end_of_document())
+ return other.is_end_of_document() ? 0 : 1;
+ if (other.is_end_of_document())
+ return -1;
+
+ auto const& this_position = m_position.get<InBlock>();
+ auto const& other_position = other.m_position.get<InBlock>();
+ if (this_position.block_index < other_position.block_index)
+ return -1;
+ if (this_position.block_index > other_position.block_index)
+ return 1;
+ if (this_position.offset < other_position.offset)
+ return -1;
+ if (this_position.offset > other_position.offset)
+ return 1;
+ return 0;
+ }
+
+private:
+ struct EndOfDocument { };
+
+ SearchPosition(InBlock position)
+ : m_position(position)
+ {
+ }
+
+ SearchPosition(EndOfDocument position)
+ : m_position(position)
+ {
+ }
+
+ Variant<InBlock, EndOfDocument> m_position;
+};
+
+struct SearchRange {
+ Layout::SearchableText const& searchable_text;
+ SearchPosition start;
+ SearchPosition end;
+
+ bool collapsed() const
+ {
+ return (start <=> end) >= 0;
+ }
+};
+
+struct TextMatch {
+ SearchPosition::InBlock start;
+ SearchPosition::InBlock end;
+};
+
+struct CachedSubstringSearcher {
+ size_t block_index;
+ Utf16String query;
+ NonnullOwnPtr<Unicode::Collator::SubstringSearcher> searcher;
+};
+
+struct CachedWordSegmenter {
+ size_t block_index;
+ Utf16String locale;
+ NonnullOwnPtr<Unicode::Segmenter> segmenter;
+};
+
+struct TextDirectiveSearchContext {
+ NonnullOwnPtr<Unicode::Collator> collator;
+ Vector<CachedSubstringSearcher> substring_searchers {};
+ Vector<CachedWordSegmenter> word_segmenters {};
+};
+
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#word-boundaries
+static bool is_at_a_word_boundary(Utf16View text, size_t position, Unicode::Segmenter& segmenter)
+{
+ // A number position is at a word boundary in a string text, given a locale locale, if, using
+ // locale, either a word boundary immediately precedes the positionth code unit, or text's
+ // length is more than 0 and position equals either 0 or text's length.
+ if (!text.is_empty() && (position == 0 || position == text.length_in_code_units()))
+ return true;
+ if (position > text.length_in_code_units())
+ return false;
+
+ return segmenter.is_boundary(position);
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#get-boundary-point-at-index
+static Optional<DOM::BoundaryPoint> get_boundary_point_at_index(size_t index, Layout::SearchableText::Block const& block, bool is_end)
+{
+ // 1. Let counted be 0.
+ size_t counted = 0;
+
+ // 2. For each curNode of nodes:
+ for (size_t segment_index = 0; segment_index < block.segments.size(); ++segment_index) {
+ auto const& segment = block.segments[segment_index];
+ auto node = segment.dom_node.ptr();
+ if (!node)
+ return {};
+
+ auto const next_segment_offset = segment_index + 1 < block.segments.size()
+ ? block.segments[segment_index + 1].start_offset
+ : block.text.length_in_code_units();
+ auto const segment_length = next_segment_offset - segment.start_offset;
+
+ // 1. Let nodeEnd be counted + curNode's length.
+ auto node_end = counted + segment_length;
+
+ // 2. If isEnd is true, add 1 to nodeEnd.
+ if (is_end)
+ ++node_end;
+
+ // 3. If nodeEnd is greater than index then:
+ if (node_end > index) {
+ // 1. Return the boundary point (curNode, index - counted).
+ // NB: TextNode::compute_text_for_rendering() preserves the DOM range which produced
+ // each rendered UTF-16 code unit. Use that mapping because text transformations can
+ // change the number of code units relative to CharacterData/data.
+ auto const mapping_index = is_end && index > 0 ? index - 1 : index;
+ if (mapping_index >= block.offset_mapping.size())
+ return {};
+ auto const& mapping = block.offset_mapping[mapping_index];
+ return DOM::BoundaryPoint {
+ *node,
+ static_cast<WebIDL::UnsignedLong>(is_end && index > 0 ? mapping.dom_end_offset : mapping.dom_start_offset),
+ };
+ }
+
+ // 4. Increment counted by curNode's length.
+ counted += segment_length;
+ }
+
+ // 3. Return null.
+ return {};
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document
+static Optional<TextMatch> find_a_range_from_a_node_list(Utf16View query_string, SearchRange const& search_range, size_t block_index, bool word_start_bounded, bool word_end_bounded, bool match_must_be_at_beginning, TextDirectiveSearchContext& search_context)
+{
+ auto const& block = search_range.searchable_text.blocks()[block_index];
+
+ // 1. Let searchBuffer be the concatenation of the CharacterData/data of each item in nodes.
+ auto search_buffer = block.text.utf16_view();
+
+ // 2. Let searchStart be 0.
+ size_t search_start = 0;
+
+ // 3. If the first item in nodes is searchRange's start node then set searchStart to
+ // searchRange's start offset.
+ if (auto start_position = search_range.start.block_position(); start_position.has_value() && block_index == start_position->block_index)
+ search_start = start_position->offset;
+
+ // 4. Let start and end be boundary points, initially null.
+ Optional<DOM::BoundaryPoint> start;
+ Optional<DOM::BoundaryPoint> end;
+
+ // 5. Let matchIndex be null.
+ Optional<Unicode::Collator::Match> match;
+
+ Unicode::Collator::SubstringSearcher* substring_searcher = nullptr;
+ for (auto& cached_searcher : search_context.substring_searchers) {
+ if (cached_searcher.block_index == block_index && cached_searcher.query.utf16_view() == query_string) {
+ substring_searcher = cached_searcher.searcher.ptr();
+ break;
+ }
+ }
+ if (!substring_searcher) {
+ search_context.substring_searchers.append({ block_index, Utf16String::from_utf16(query_string), search_context.collator->create_substring_searcher(search_buffer, query_string) });
+ substring_searcher = search_context.substring_searchers.last().searcher.ptr();
+ }
+
+ auto word_segmenter_for_locale = [&](Utf16String const& locale) -> Unicode::Segmenter& {
+ for (auto& cached_segmenter : search_context.word_segmenters) {
+ if (cached_segmenter.block_index == block_index && cached_segmenter.locale == locale)
+ return *cached_segmenter.segmenter;
+ }
+
+ auto segmenter = Unicode::Segmenter::create(locale, Unicode::SegmenterGranularity::Word);
+ segmenter->set_segmented_text(search_buffer);
+ search_context.word_segmenters.append({ block_index, locale, move(segmenter) });
+ return *search_context.word_segmenters.last().segmenter;
+ };
+
+ // 6. While matchIndex is null
+ while (!match.has_value()) {
+ // 1. Set matchIndex to the index of the first instance of queryString in searchBuffer, starting at
+ // searchStart. The string search must be performed using a base character comparison, or the primary level,
+ // as defined in UTS10.
+ match = substring_searcher->find_from(search_start);
+
+ // 2. If matchIndex is null, return null.
+ if (!match.has_value())
+ return {};
+
+ // 3. If matchMustBeAtBeginning is true and matchIndex is not 0, return null.
+ // AD-HOC: searchBuffer represents an entire rendered block rather than a freshly sliced node list, so
+ // searchStart is its logical index 0.
+ if (match_must_be_at_beginning && match->start != search_start)
+ return {};
+
+ // 4. Let endIx be matchIndex + queryString's length.
+ // AD-HOC: ICU returns the actual end of a primary-strength match, which may have a different UTF-16 length
+ // from queryString when the strings are canonically equivalent.
+ auto end_index = match->end;
+
+ // 5. Set start to the boundary point result of get boundary point at index matchIndex run over nodes with
+ // isEnd false.
+ start = get_boundary_point_at_index(match->start, block, false);
+
+ // 6. Set end to the boundary point result of get boundary point at index endIx run over nodes with isEnd true.
+ end = get_boundary_point_at_index(end_index, block, true);
+ if (!start.has_value() || !end.has_value())
+ return {};
+
+ auto locale_for_boundary_point = [](DOM::BoundaryPoint const& boundary_point) {
+ if (auto* element = boundary_point.node->parent_or_shadow_host_element()) {
+ if (auto language = element->lang(); language.has_value())
+ return language.release_value();
+ }
+ return Utf16String {};
+ };
+
+ // 7. If wordStartBounded is true and matchIndex is not at a word boundary in searchBuffer, given the language
+ // from start's node as the locale; or wordEndBounded is true and endIx is not at a word boundary in
+ // searchBuffer, given the language from end's node as the locale:
+ auto start_locale = locale_for_boundary_point(*start);
+ auto end_locale = locale_for_boundary_point(*end);
+ if ((word_start_bounded && !is_at_a_word_boundary(search_buffer, match->start, word_segmenter_for_locale(start_locale)))
+ || (word_end_bounded && !is_at_a_word_boundary(search_buffer, end_index, word_segmenter_for_locale(end_locale)))) {
+ // 1. Set searchStart to matchIndex + 1.
+ search_start = match->start + 1;
+
+ // 2. Set matchIndex to null.
+ match.clear();
+ }
+ }
+
+ // 7. Let endInset be 0.
+ size_t end_inset = 0;
+
+ // 8. If the last item in nodes is searchRange's end node then set endInset to (searchRange's end node's length -
+ // searchRange's end offset).
+ if (auto end_position = search_range.end.block_position(); end_position.has_value() && block_index == end_position->block_index) {
+ auto const search_buffer_length = search_buffer.length_in_code_units();
+ end_inset = end_position->offset < search_buffer_length
+ ? search_buffer_length - end_position->offset
+ : 0;
+ }
+
+ // 9. If matchIndex + queryString's length is greater than searchBuffer's length - endInset return null.
+ if (match->end > search_buffer.length_in_code_units() - end_inset)
+ return {};
+
+ // 10. Assert: start and end are non-null, valid boundary points in searchRange.
+ VERIFY(start.has_value());
+ VERIFY(end.has_value());
+
+ // 11. Return a range with start and end.
+ return TextMatch {
+ .start = { block_index, match->start },
+ .end = { block_index, match->end },
+ };
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#find-a-string-in-range
+static Optional<TextMatch> find_a_string_in_a_range(Utf16View query, SearchRange& search_range, bool word_start_bounded, bool word_end_bounded, bool match_must_be_at_beginning, TextDirectiveSearchContext& search_context)
+{
+ // 1. While searchRange is not collapsed:
+ while (!search_range.collapsed()) {
+ // 1. Let curNode be searchRange's start node.
+ // NB: Layout::SearchableText::collect() precomputes curNode and the following traversal steps. Each block in
+ // the resulting index represents one textNodeList and records its position in document order.
+
+ // 2. If curNode is part of a non-searchable subtree:
+ // 1. Set searchRange's start node to the next node, in shadow-including tree order, that isn't a
+ // shadow-including descendant of curNode.
+ // 2. Set searchRange's start offset to 0.
+ // 3. Continue.
+ // NB: Layout::SearchableText::collect() omits non-searchable subtrees while constructing the index.
+
+ // 3. If curNode is not a visible text node:
+ // 1. Set searchRange's start node to the next node, in shadow-including tree order, that is not a doctype.
+ // 2. Set searchRange's start offset to 0.
+ // 3. Continue.
+ // NB: Layout::SearchableText::collect() omits nodes that are not visible text nodes while constructing the
+ // index.
+
+ // 4. Let blockAncestor be the nearest block ancestor of curNode.
+ // NB: Layout::SearchableText::collect() uses the nearest block ancestor to divide the index into blocks.
+
+ // 5. Let textNodeList be a list of Text nodes, initially empty.
+ // NB: A SearchableText block's segments retain the Text nodes and DOM offsets corresponding to its rendered
+ // text.
+
+ // 6. While curNode is a shadow-including descendant of blockAncestor and the position of the boundary point
+ // (curNode, 0) is not after searchRange's end:
+ // 1. If curNode has block-level display then break.
+ // 2. If curNode is search invisible:
+ // 1. Set curNode to the next node, in shadow-including tree order, that isn't a shadow-including
+ // descendant of curNode.
+ // 2. Continue.
+ // 3. If curNode is a visible text node then append it to textNodeList.
+ // 4. Set curNode to the next node in shadow-including tree order.
+ // NB: Layout::SearchableText::collect() performs this loop and stores each completed textNodeList as a block.
+ auto const start_position = search_range.start.block_position();
+ VERIFY(start_position.has_value());
+ auto const block_index = start_position->block_index;
+
+ // 7. Run the find a range from a node list steps given query, searchRange, textNodeList, wordStartBounded,
+ // wordEndBounded and matchMustBeAtBeginning as input. If the resulting range is not null, then return it.
+ if (auto match = find_a_range_from_a_node_list(query, search_range, block_index, word_start_bounded, word_end_bounded, match_must_be_at_beginning, search_context); match.has_value())
+ return match;
+
+ // 8. If matchMustBeAtBeginning is true, return null.
+ if (match_must_be_at_beginning)
+ return {};
+
+ // 9. If curNode is null, then break.
+ auto const end_position = search_range.end.block_position();
+ if ((end_position.has_value() && block_index >= end_position->block_index)
+ || block_index + 1 >= search_range.searchable_text.blocks().size())
+ break;
+
+ // 10. Assert: curNode follows searchRange's start node.
+ // NB: SearchableText blocks are stored in document order, so the next block necessarily follows the current
+ // start block.
+
+ // 11. Set searchRange's start to the boundary point (curNode, 0).
+ search_range.start = SearchPosition::in_block(block_index + 1, 0);
+ }
+
+ // 2. Return null.
+ return {};
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#next-non-whitespace-position
+static void advance_a_range_start_to_the_next_non_whitespace_position(SearchRange& range)
+{
+ // 1. While range is not collapsed:
+ while (!range.collapsed()) {
+ // 1. Let node be range's start node.
+ // NB: The current SearchableText block represents node and its searchable rendered text.
+ auto start_position = range.start.block_position();
+ VERIFY(start_position.has_value());
+ auto const& block = range.searchable_text.blocks()[start_position->block_index];
+
+ // 2. Let offset be range's start offset.
+ auto text = block.text.utf16_view();
+
+ // 3. If node is part of a non-searchable subtree or if node is not a visible text node or if offset is equal
+ // to node's length then:
+ // NB: Layout::SearchableText::collect() omits non-searchable subtrees and nodes that are not visible text
+ // nodes, so only the offset condition remains to be checked here.
+ if (start_position->offset >= text.length_in_code_units()) {
+ // 1. Set range's start node to the next node, in shadow-including tree order.
+ auto next_position = start_position->block_index + 1 < range.searchable_text.blocks().size()
+ ? SearchPosition::in_block(start_position->block_index + 1, 0)
+ : SearchPosition::end_of_document();
+ if ((next_position <=> range.end) >= 0) {
+ range.start = range.end;
+ return;
+ }
+
+ // 2. Set range's start offset to 0.
+ range.start = move(next_position);
+
+ // 3. Continue.
+ continue;
+ }
+
+ // 4. If the Text/substring data of node at offset and count 6 is equal to the string "&nbsp;" then:
+ if (text.substring_view(start_position->offset).starts_with(u"&nbsp;"sv)) {
+ // 1. Add 6 to range's start offset.
+ start_position->offset += 6;
+ continue;
+ }
+
+ // 5. Otherwise, if the Text/substring data of node at offset and count 5 is equal to the string "&nbsp"
+ // then:
+ if (text.substring_view(start_position->offset).starts_with(u"&nbsp"sv)) {
+ // 1. Add 5 to range's start offset.
+ start_position->offset += 5;
+ continue;
+ }
+
+ // 6. Otherwise:
+ // 1. Let cp be the code point at the offset index in node's CharacterData/data.
+ auto code_point = text.code_point_at(start_position->offset);
+
+ // 2. If cp does not have the White_Space property set, return.
+ if (!Unicode::code_point_has_white_space_property(code_point))
+ return;
+
+ // 3. Add 1 to range's start offset.
+ // NB: SearchRange offsets are UTF-16 code-unit offsets, so advance by two code units for a supplementary code
+ // point.
+ start_position->offset += code_point > 0xffff ? 2 : 1;
+ }
+}
+
+// https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document
+Optional<GC::Ref<DOM::Range>> find_a_range_from_a_text_directive(TextDirective const& parsed_values, DOM::Document& document)
+{
+ document.update_layout(DOM::UpdateLayoutReason::DocumentFindMatchingText);
+ auto* viewport = document.layout_node();
+ if (!viewport)
+ return {};
+
+ auto const& searchable_text = viewport->searchable_text();
+ if (searchable_text.blocks().is_empty())
+ return {};
+
+ // 1. Let searchRange be a range with start (document, 0) and end (document, document's length).
+ SearchRange search_range {
+ searchable_text,
+ SearchPosition::in_block(0, 0),
+ SearchPosition::end_of_document(),
+ };
+
+ TextDirectiveSearchContext search_context {
+ .collator = Unicode::Collator::create(
+ {}, Unicode::Usage::Search, {}, Unicode::Sensitivity::Base,
+ Unicode::CaseFirst::False, false, false),
+ };
+
+ auto first_boundary_point_after = [&](SearchPosition::InBlock position) {
+ auto const block_length = searchable_text.blocks()[position.block_index].text.length_in_code_units();
+ if (position.offset < block_length)
+ return SearchPosition::in_block(position.block_index, position.offset + 1);
+ if (position.block_index + 1 < searchable_text.blocks().size())
+ return SearchPosition::in_block(position.block_index + 1, 0);
+ return search_range.end;
+ };
+
+ auto create_range = [&](TextMatch const& match) -> Optional<GC::Ref<DOM::Range>> {
+ auto start = get_boundary_point_at_index(match.start.offset, searchable_text.blocks()[match.start.block_index], false);
+ auto end = get_boundary_point_at_index(match.end.offset, searchable_text.blocks()[match.end.block_index], true);
+ if (!start.has_value() || !end.has_value() || &start->node->root() != &end->node->root())
+ return {};
+ return DOM::Range::create(start->node, start->offset, end->node, end->offset);
+ };
+
+ // 2. While searchRange is not collapsed:
+ while (!search_range.collapsed()) {
+ // 1. Let potentialMatch be null.
+ Optional<TextMatch> potential_match;
+
+ // 2. If parsedValues's prefix is not null:
+ if (parsed_values.prefix.has_value()) {
+ // 1. Let prefixMatch be the the result of running the find a string in range steps
+ // with query parsedValues's prefix, searchRange searchRange, wordStartBounded true,
+ // wordEndBounded false and matchMustBeAtBeginning false.
+ auto prefix_match = find_a_string_in_a_range(*parsed_values.prefix, search_range, true, false, false, search_context);
+
+ // 2. If prefixMatch is null, return null.
+ if (!prefix_match.has_value())
+ return {};
+
+ // 3. Set searchRange's start to the first boundary point after prefixMatch's start.
+ search_range.start = first_boundary_point_after(prefix_match->start);
+
+ // 4. Let matchRange be a range whose start is prefixMatch's end and end is
+ // searchRange's end.
+ SearchRange match_range { searchable_text, SearchPosition::in_block(prefix_match->end), search_range.end };
+
+ // 5. Advance matchRange's start to the next non-whitespace position.
+ advance_a_range_start_to_the_next_non_whitespace_position(match_range);
+
+ // 6. If matchRange is collapsed return null.
+ if (match_range.collapsed())
+ return {};
+
+ // 7. Assert: matchRange's start node is a Text node.
+ auto const match_range_start = match_range.start.block_position();
+ VERIFY(match_range_start.has_value());
+ VERIFY(match_range_start->block_index < searchable_text.blocks().size());
+
+ // 8. Let mustEndAtWordBoundary be true if parsedValues's end is non-null or
+ // parsedValues's suffix is null, false otherwise.
+ auto must_end_at_word_boundary = parsed_values.end.has_value() || !parsed_values.suffix.has_value();
+
+ // 9. Set potentialMatch to the result of running find a string in range with query
+ // start, matchRange, wordStartBounded false, wordEndBounded mustEndAtWordBoundary and
+ // matchMustBeAtBeginning true.
+ potential_match = find_a_string_in_a_range(parsed_values.start, match_range, false, must_end_at_word_boundary, true, search_context);
+
+ // 10. If potentialMatch is null, continue.
+ if (!potential_match.has_value())
+ continue;
+ }
+
+ // 3. Otherwise:
+ else {
+ // 1. Let mustEndAtWordBoundary be true if parsedValues's end is non-null or
+ // parsedValues's suffix is null, false otherwise.
+ auto must_end_at_word_boundary = parsed_values.end.has_value() || !parsed_values.suffix.has_value();
+
+ // 2. Set potentialMatch to the result of running find a string in range with query
+ // start, searchRange, wordStartBounded true, wordEndBounded mustEndAtWordBoundary and
+ // matchMustBeAtBeginning false.
+ potential_match = find_a_string_in_a_range(parsed_values.start, search_range, true, must_end_at_word_boundary, false, search_context);
+
+ // 3. If potentialMatch is null, return null.
+ if (!potential_match.has_value())
+ return {};
+
+ // 4. Set searchRange's start to the first boundary point after potentialMatch's
+ // start.
+ search_range.start = first_boundary_point_after(potential_match->start);
+ }
+
+ // 4. Let rangeEndSearchRange be a range whose start is potentialMatch's end and whose end
+ // is searchRange's end.
+ SearchRange range_end_search_range { searchable_text, SearchPosition::in_block(potential_match->end), search_range.end };
+
+ // If an exact match without suffix context ends at the end of searchRange, the following
+ // loop is already collapsed and the range is complete.
+ if (!parsed_values.end.has_value() && !parsed_values.suffix.has_value())
+ return create_range(*potential_match);
+
+ // 5. While rangeEndSearchRange is not collapsed:
+ while (!range_end_search_range.collapsed()) {
+ // 1. If parsedValues's end item is non-null, then:
+ if (parsed_values.end.has_value()) {
+ // 1. Let mustEndAtWordBoundary be true if parsedValues's suffix is null,
+ // false otherwise.
+ auto must_end_at_word_boundary = !parsed_values.suffix.has_value();
+
+ // 2. Let endMatch be the result of running find a string in range with query
+ // end, rangeEndSearchRange, wordStartBounded true, wordEndBounded
+ // mustEndAtWordBoundary and matchMustBeAtBeginning false.
+ auto end_match = find_a_string_in_a_range(*parsed_values.end, range_end_search_range, true, must_end_at_word_boundary, false, search_context);
+
+ // 3. If endMatch is null then return null.
+ if (!end_match.has_value())
+ return {};
+
+ // 4. Set potentialMatch's end to endMatch's end.
+ potential_match->end = end_match->end;
+ }
+
+ // 3. If parsedValues's suffix is null, return potentialMatch.
+ if (!parsed_values.suffix.has_value())
+ return create_range(*potential_match);
+
+ // 4. Let suffixRange be a range with start equal to potentialMatch's end and end
+ // equal to searchRange's end.
+ SearchRange suffix_range { searchable_text, SearchPosition::in_block(potential_match->end), search_range.end };
+
+ // 5. Advance suffixRange's start to the next non-whitespace position.
+ advance_a_range_start_to_the_next_non_whitespace_position(suffix_range);
+
+ // 6. Let suffixMatch be result of running find a string in range with query suffix,
+ // suffixRange, wordStartBounded false, wordEndBounded true and
+ // matchMustBeAtBeginning true.
+ auto suffix_match = find_a_string_in_a_range(*parsed_values.suffix, suffix_range, false, true, true, search_context);
+
+ // 7. If suffixMatch is non-null, return potentialMatch.
+ if (suffix_match.has_value())
+ return create_range(*potential_match);
+
+ // 8. If parsedValues's end item is null and suffixMatch is null, then break.
+ if (!parsed_values.end.has_value())
+ break;
+
+ // 9. Set rangeEndSearchRange's start to potentialMatch's end.
+ range_end_search_range.start = SearchPosition::in_block(potential_match->end);
+ }
+
+ // 6. If rangeEndSearchRange is collapsed then:
+ if (range_end_search_range.collapsed()) {
+ // 1. Assert: parsedValues's end item is non-null.
+ VERIFY(parsed_values.end.has_value());
+
+ // 2. Return null.
+ return {};
+ }
+ }
+
+ // 3. Return null.
+ return {};
+}
+
}
diff --git a/Libraries/LibWeb/HTML/TextDirective.h b/Libraries/LibWeb/HTML/TextDirective.h
index c6b9babeb18..66e982bf717 100644
--- a/Libraries/LibWeb/HTML/TextDirective.h
+++ b/Libraries/LibWeb/HTML/TextDirective.h
@@ -10,8 +10,10 @@
#include <AK/String.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
+#include <LibGC/Ptr.h>
#include <LibURL/URL.h>
#include <LibWeb/Export.h>
+#include <LibWeb/Forward.h>
namespace Web::HTML {
@@ -26,5 +28,6 @@ WEB_API Optional<String> remove_the_fragment_directive(URL::URL&);
WEB_API Optional<Utf16String> percent_decode_a_text_directive_term(Optional<StringView> term);
WEB_API Optional<TextDirective> parse_a_text_directive(StringView text_directive_value);
WEB_API Vector<TextDirective> parse_the_fragment_directive(StringView fragment_directive);
+WEB_API Optional<GC::Ref<DOM::Range>> find_a_range_from_a_text_directive(TextDirective const&, DOM::Document&);
}
diff --git a/Libraries/LibWeb/Layout/RenderedText.h b/Libraries/LibWeb/Layout/RenderedText.h
new file mode 100644
index 00000000000..d057201c682
--- /dev/null
+++ b/Libraries/LibWeb/Layout/RenderedText.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Utf16String.h>
+#include <AK/Vector.h>
+
+namespace Web::Layout {
+
+struct RenderedTextOffset {
+ size_t dom_start_offset { 0 };
+ size_t dom_end_offset { 0 };
+};
+
+struct RenderedTextForDOM {
+ Utf16String text;
+ Vector<RenderedTextOffset> offset_mapping;
+};
+
+}
diff --git a/Libraries/LibWeb/Layout/SearchableText.cpp b/Libraries/LibWeb/Layout/SearchableText.cpp
index f10eaa1dff0..72afdee2154 100644
--- a/Libraries/LibWeb/Layout/SearchableText.cpp
+++ b/Libraries/LibWeb/Layout/SearchableText.cpp
@@ -209,7 +209,7 @@ SearchableText SearchableText::collect(Viewport& viewport)
auto const* parent_inherited_text_values = parent->style_group<CSS::ComputedValues::InheritedTextValues>();
VERIFY(parent_inherited_text_values);
- auto const append_text = [&](Utf16View text_to_append, size_t dom_start_offset, TextNode const* layout_text_node = nullptr) {
+ auto const append_text = [&](Utf16View text_to_append, size_t dom_start_offset, TextNode const* layout_text_node = nullptr, Vector<RenderedTextOffset> const* offset_mapping = nullptr) {
auto const white_space_collapse = parent_inherited_text_values->white_space_collapse_value();
auto const should_collapse = first_is_one_of(
white_space_collapse,
@@ -238,11 +238,12 @@ SearchableText SearchableText::collect(Viewport& viewport)
};
if (layout_text_node) {
- // The Rust layout tree owns the rendered-to-DOM edit map. Until that mapping is
- // exposed through this consumer, this path uses the DOM range represented by the
- // text row and keeps rendered-text collection centralized in TextNode.
- for (size_t i = 0; i < text_to_append.length_in_code_units(); ++i)
- append_rendered_code_unit(text_to_append.code_unit_at(i), dom_start_offset + i, dom_start_offset + i + 1);
+ VERIFY(offset_mapping);
+ VERIFY(offset_mapping->size() == text_to_append.length_in_code_units());
+ for (size_t i = 0; i < text_to_append.length_in_code_units(); ++i) {
+ auto const& mapping = (*offset_mapping)[i];
+ append_rendered_code_unit(text_to_append.code_unit_at(i), mapping.dom_start_offset, mapping.dom_end_offset);
+ }
return;
}
@@ -252,10 +253,8 @@ SearchableText SearchableText::collect(Viewport& viewport)
if (auto rendered_text = rendered_text_nodes.get(&text); rendered_text.has_value()) {
for (auto const* text_node : *rendered_text) {
- append_text(
- text_node->rendered_text_for_dom(false).utf16_view(),
- 0,
- text_node);
+ auto rendered_text_for_dom = text_node->rendered_text_for_dom_with_offset_mapping();
+ append_text(rendered_text_for_dom.text.utf16_view(), 0, text_node, &rendered_text_for_dom.offset_mapping);
}
} else {
append_text(text.data().utf16_view(), 0);
diff --git a/Libraries/LibWeb/Layout/SearchableText.h b/Libraries/LibWeb/Layout/SearchableText.h
index 174f1ed2291..e8e80194a3d 100644
--- a/Libraries/LibWeb/Layout/SearchableText.h
+++ b/Libraries/LibWeb/Layout/SearchableText.h
@@ -12,6 +12,7 @@
#include <LibGC/Ptr.h>
#include <LibGC/Weak.h>
#include <LibWeb/Forward.h>
+#include <LibWeb/Layout/RenderedText.h>
namespace Web::Layout {
@@ -37,11 +38,6 @@ public:
bool is_inert { false };
};
- struct RenderedTextOffset {
- size_t dom_start_offset { 0 };
- size_t dom_end_offset { 0 };
- };
-
struct Block {
Utf16String text;
Vector<Segment> segments;
diff --git a/Libraries/LibWeb/Layout/TextNode.cpp b/Libraries/LibWeb/Layout/TextNode.cpp
index 98a6887b3ba..a14722272ec 100644
--- a/Libraries/LibWeb/Layout/TextNode.cpp
+++ b/Libraries/LibWeb/Layout/TextNode.cpp
@@ -44,6 +44,21 @@ Utf16String TextNode::rendered_text_for_dom(bool collapse_whitespace) const
return text;
}
+RenderedTextForDOM TextNode::rendered_text_for_dom_with_offset_mapping() const
+{
+ RenderedTextForDOM rendered_text;
+ RustFFI::layout_script_rendered_text_with_dom_offsets(document_host(), slot_id(this), &rendered_text,
+ [](void* context, RustFFI::FfiRenderedTextWithOffsetsView view) {
+ auto& output = *static_cast<RenderedTextForDOM*>(context);
+ output.text = Utf16String::from_utf16({ reinterpret_cast<char16_t const*>(view.text), view.length_in_code_units });
+ for (size_t i = 0; i < view.offset_mapping_length; ++i) {
+ auto const& mapping = view.offset_mapping[i];
+ output.offset_mapping.append({ mapping.dom_start_offset, mapping.dom_end_offset });
+ }
+ });
+ return rendered_text;
+}
+
RustFFI::FfiTextSourceRange TextNode::word_range_at(size_t dom_offset) const
{
return RustFFI::layout_text_word_range(document_host(), slot_id(this), dom_offset);
diff --git a/Libraries/LibWeb/Layout/TextNode.h b/Libraries/LibWeb/Layout/TextNode.h
index ce05fe9bbcf..9aeb55a6064 100644
--- a/Libraries/LibWeb/Layout/TextNode.h
+++ b/Libraries/LibWeb/Layout/TextNode.h
@@ -14,6 +14,7 @@
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Layout/Box.h>
+#include <LibWeb/Layout/RenderedText.h>
namespace Web::Layout {
@@ -36,6 +37,7 @@ public:
virtual Utf16String const& text() const { return dom_node().data(); }
Utf16String rendered_text_for_dom(bool collapse_whitespace) const;
+ RenderedTextForDOM rendered_text_for_dom_with_offset_mapping() const;
RustFFI::FfiTextSourceRange word_range_at(size_t dom_offset) const;
private:
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 858d54fcfc6..634e10b8804 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2768,6 +2768,8 @@ fn main() -> Result<(), Box<dyn Error>> {
"FfiNodeIdentity".to_string(),
"FfiPseudoElement".to_string(),
"FfiRenderedTextView".to_string(),
+ "FfiRenderedTextOffset".to_string(),
+ "FfiRenderedTextWithOffsetsView".to_string(),
"FfiReplacedContentFacts".to_string(),
"FfiStylePayloads".to_string(),
"FfiUtf16View".to_string(),
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index cc49863d646..98bc15fae16 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -432,6 +432,20 @@ pub struct FfiRenderedTextView {
pub length_in_code_units: usize,
}
+#[repr(C)]
+pub struct FfiRenderedTextOffset {
+ pub dom_start_offset: usize,
+ pub dom_end_offset: usize,
+}
+
+#[repr(C)]
+pub struct FfiRenderedTextWithOffsetsView {
+ pub text: *const u16,
+ pub length_in_code_units: usize,
+ pub offset_mapping: *const FfiRenderedTextOffset,
+ pub offset_mapping_length: usize,
+}
+
/// The text the rows of the text node whose primary row is `primary` render, with whitespace collapsed where their
/// style collapses it if `collapse_whitespace`, handed to `append`.
///
@@ -469,6 +483,39 @@ pub unsafe extern "C" fn layout_script_rendered_text(
unsafe { append(context, view) };
}
+/// The rendered text and its rendered-to-DOM UTF-16 offset mapping for a text row.
+///
+/// # Safety
+///
+/// `host` must be a live document host on its document's thread, `primary` must be a live text row, and `append` must
+/// copy both views synchronously.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn layout_script_rendered_text_with_dom_offsets(
+ host: &DocumentHost,
+ primary: NodeSlotId,
+ context: *mut c_void,
+ append: unsafe extern "C" fn(*mut c_void, FfiRenderedTextWithOffsetsView),
+) {
+ let (text, offsets) = read_arena(host, |arena| {
+ crate::layout::text_queries::rendered_text_with_dom_offsets(arena, primary)
+ });
+ let offset_mapping: Vec<_> = offsets
+ .into_iter()
+ .map(|(dom_start_offset, dom_end_offset)| FfiRenderedTextOffset {
+ dom_start_offset,
+ dom_end_offset,
+ })
+ .collect();
+ let view = FfiRenderedTextWithOffsetsView {
+ text: text.as_ptr(),
+ length_in_code_units: text.len(),
+ offset_mapping: offset_mapping.as_ptr(),
+ offset_mapping_length: offset_mapping.len(),
+ };
+ // SAFETY: Both vectors outlive the call, which copies the views.
+ unsafe { append(context, view) };
+}
+
/// The text the pseudo-element of kind `generated_for` on the element `style_node` resolved its generated content to
/// when its box was built, as a raw `AK::Utf16String` representation for which the caller takes one reference.
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/layout/text_queries.rs b/Libraries/LibWeb/Rust/src/layout/text_queries.rs
index 6a5026aba71..e8d08c48148 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_queries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_queries.rs
@@ -151,6 +151,38 @@ pub(crate) fn rendered_text(arena: &mut LayoutNodeArena, primary: NodeSlotId, co
text
}
+/// The rendered text of a text row, with each UTF-16 code unit mapped back to the DOM offsets that produced it.
+pub(crate) fn rendered_text_with_dom_offsets(
+ arena: &mut LayoutNodeArena,
+ primary: NodeSlotId,
+) -> (Vec<u16>, Vec<(usize, usize)>) {
+ ensure_text_fragments(arena, primary);
+ let mut text = MappedText::default();
+ for &node in arena.text_fragments(primary).as_slice() {
+ let content = arena.text_content(node).expect("fragment was refreshed");
+ text.append(
+ &content.text,
+ TextPosition { node, offset: 0 },
+ TextPosition {
+ node,
+ offset: content.text.len(),
+ },
+ );
+ }
+
+ let mut offsets = Vec::with_capacity(text.text.len());
+ for offset in 0..text.text.len() {
+ let start = text
+ .dom_position(arena, offset, Start)
+ .expect("rendered code unit has a start");
+ let end = text
+ .dom_position(arena, offset + 1, End)
+ .expect("rendered code unit has an end");
+ offsets.push((start.offset, end.offset));
+ }
+ (text.text, offsets)
+}
+
/// [`rendered_text`] as `rows` carry it, where they carry the rendered text of each of the text node's rows: a text row
/// carries what [`rendered_text`] answers unless it is among `awaiting_render`, the rows that wait for their text to be
/// rendered again, by slot index.
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-missing-suffix-at-document-end.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-missing-suffix-at-document-end.txt
new file mode 100644
index 00000000000..7f89008eef3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-missing-suffix-at-document-end.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass A missing suffix after a match in the final searchable text node does not crash
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-text-transform-offset-mapping.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-text-transform-offset-mapping.txt
new file mode 100644
index 00000000000..7360b7d626d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-text-transform-offset-mapping.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass A match expanded by text-transform maps to valid DOM boundary offsets
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-missing-suffix-at-document-end.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-missing-suffix-at-document-end.html
new file mode 100644
index 00000000000..b384cae226e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-missing-suffix-at-document-end.html
@@ -0,0 +1,17 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Text fragment with a missing suffix at the end of the document</title>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#find-a-range-from-a-text-directive">
+<script src="wpt-import/resources/testharness.js"></script>
+<script src="wpt-import/resources/testharnessreport.js"></script>
+
+<p>alpha beta gamma</p><p>hello</p>
+
+<script>
+promise_test(async () => {
+ await new Promise(resolve => addEventListener("load", resolve, { once: true }));
+
+ location.href = "#:~:text=hello,-world";
+ await new Promise(resolve => requestAnimationFrame(resolve));
+}, "A missing suffix after a match in the final searchable text node does not crash");
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-text-transform-offset-mapping.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-text-transform-offset-mapping.html
new file mode 100644
index 00000000000..7a89daf4a42
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-text-transform-offset-mapping.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Text fragment offset mapping through text-transform</title>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#get-boundary-point-at-index">
+<script src="wpt-import/resources/testharness.js"></script>
+<script src="wpt-import/resources/testharnessreport.js"></script>
+
+<style>
+ #target { margin-top: 2000px; }
+</style>
+
+<p id="target" style="text-transform: uppercase">straße</p>
+
+<script>
+promise_test(async () => {
+ await new Promise(resolve => addEventListener("load", resolve, { once: true }));
+
+ location.href = "#:~:text=STRASSE";
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ assert_greater_than(scrollY, 0, "the transformed match was scrolled into view");
+}, "A match expanded by text-transform maps to valid DOM boundary offsets");
+</script>