LibWeb: Use code unit offsets in Document::find_matching_text()

We were passing in byte offsets instead of UTF-16 code unit offsets,
which could lead to crashes if the offsets found exceeded the number of
code units in text fragments on the page.

Fixes #4908.

Co-authored-by: Tim Ledbetter <tim.ledbetter@ladybird.org>
This commit is contained in:
Jelle Raaijmakers 2025-06-10 16:14:05 +02:00 committed by Jelle Raaijmakers
commit b42c2c5e8f
Notes: github-actions[bot] 2025-06-13 13:10:45 +00:00
5 changed files with 24 additions and 15 deletions

View file

@ -60,7 +60,7 @@ void Viewport::update_text_blocks()
if (layout_node.is_box() || layout_node.is_generated()) {
if (!builder.is_empty()) {
text_blocks.append({ builder.to_string_without_validation(), text_positions });
text_blocks.append({ MUST(AK::utf8_to_utf16(builder.string_view())), text_positions });
current_start_position = 0;
text_positions.clear_with_capacity();
builder.clear();
@ -80,7 +80,8 @@ void Viewport::update_text_blocks()
}
auto const& current_node_text = text_node->text_for_rendering();
current_start_position += current_node_text.bytes_as_string_view().length();
auto const current_node_text_utf16 = MUST(AK::utf8_to_utf16(current_node_text));
current_start_position += current_node_text_utf16.data.size();
builder.append(move(current_node_text));
}
}
@ -89,7 +90,7 @@ void Viewport::update_text_blocks()
});
if (!builder.is_empty())
text_blocks.append({ builder.to_string_without_validation(), text_positions });
text_blocks.append({ MUST(AK::utf8_to_utf16(builder.string_view())), text_positions });
m_text_blocks = move(text_blocks);
}