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
Notes: sideshowbarker 2024-07-17 07:14:09 +09:00
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

View file

@ -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);