LibWeb: Delete entire grapheme clusters on backspace/delete key presses
This commit is contained in:
parent
4734214ac1
commit
fb6ca386cc
Notes:
sideshowbarker
2024-07-17 07:14:09 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/fb6ca386cc Pull-request: https://github.com/SerenityOS/serenity/pull/17679 Reviewed-by: https://github.com/linusg ✅
2 changed files with 9 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue