diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-03-01 07:30:02 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-01 14:24:01 +0000 |
commit | fb6ca386cc0bd65712b976f7fc004555ba72fb30 (patch) | |
tree | 64e8df78ca1eea17a32b4b39a4c354d1b5236645 /Userland/Libraries/LibWeb | |
parent | 4734214ac15d29090797e145ac111a49786e632a (diff) | |
download | serenity-fb6ca386cc0bd65712b976f7fc004555ba72fb30.zip |
LibWeb: Delete entire grapheme clusters on backspace/delete key presses
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/Page/EditEventHandler.cpp | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Page/EditEventHandler.h | 2 |
2 files changed, 9 insertions, 8 deletions
diff --git a/Userland/Libraries/LibWeb/Page/EditEventHandler.cpp b/Userland/Libraries/LibWeb/Page/EditEventHandler.cpp index 95895d8069..657b46db2b 100644 --- a/Userland/Libraries/LibWeb/Page/EditEventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EditEventHandler.cpp @@ -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 diff --git a/Userland/Libraries/LibWeb/Page/EditEventHandler.h b/Userland/Libraries/LibWeb/Page/EditEventHandler.h index 4091c3c096..20e2a889b9 100644 --- a/Userland/Libraries/LibWeb/Page/EditEventHandler.h +++ b/Userland/Libraries/LibWeb/Page/EditEventHandler.h @@ -20,7 +20,7 @@ public: virtual ~EditEventHandler() = default; - virtual void handle_delete_character_after(const DOM::Position&); + virtual void handle_delete_character_after(DOM::Position const&); virtual void handle_delete(DOM::Range&); virtual void handle_insert(DOM::Position, u32 code_point); |