1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:57:45 +00:00

LibWeb: Delete entire grapheme clusters on backspace/delete key presses

This commit is contained in:
Timothy Flynn 2023-03-01 07:30:02 -05:00 committed by Linus Groh
parent 4734214ac1
commit fb6ca386cc
2 changed files with 9 additions and 8 deletions

View file

@ -6,6 +6,7 @@
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibUnicode/Segmentation.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Position.h>
#include <LibWeb/DOM/Range.h>
@ -16,20 +17,20 @@
namespace Web {
void EditEventHandler::handle_delete_character_after(const DOM::Position& cursor_position)
void EditEventHandler::handle_delete_character_after(DOM::Position const& cursor_position)
{
if (cursor_position.offset_is_at_end_of_node()) {
auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(cursor_position.node()));
auto& text = node.data();
auto next_grapheme_offset = Unicode::next_grapheme_segmentation_boundary(Utf8View { text }, cursor_position.offset());
if (!next_grapheme_offset.has_value()) {
// FIXME: Move to the next node and delete the first character there.
return;
}
auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(cursor_position.node()));
auto& text = node.data();
auto code_point_length = Utf8View(text).iterator_at_byte_offset(cursor_position.offset()).underlying_code_point_length_in_bytes();
StringBuilder builder;
builder.append(text.substring_view(0, cursor_position.offset()));
builder.append(text.substring_view(cursor_position.offset() + code_point_length));
builder.append(text.substring_view(*next_grapheme_offset));
node.set_data(builder.to_deprecated_string());
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still