diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-05-18 22:09:15 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-21 21:57:03 +0200 |
commit | 9440a3c280fbdfb32b6311a3a880384dca207579 (patch) | |
tree | 179bf4f70027294238d9c1a090909fc041f499ec /Userland/Libraries/LibWeb/Page/EventHandler.cpp | |
parent | 7181cb3a9ce11736170ac6f7863413ad19e17fbd (diff) | |
download | serenity-9440a3c280fbdfb32b6311a3a880384dca207579.zip |
LibWeb: Improve Unicode compatibility of HTML contenteditable
This patch updates the Page::keydown_event event handler to implement
crude Unicode support. It implements new method in EditEventHandler to
more easily handle deleting a single character after the cursor.
Furthermore, it makes use of the previously implemented methods to
increment and decrement the cursor position, which take into account
that Unicode codepoint may be multiple bytes wide.
This means it is now possible to mostly edit Unicode in editable DOM
nodes without any crashes. :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/Page/EventHandler.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/Page/EventHandler.cpp | 50 |
1 files changed, 8 insertions, 42 deletions
diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 4b4649278d..c5c491dc43 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -408,11 +408,7 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin } else if (!should_ignore_keydown_event(code_point)) { m_edit_event_handler->handle_delete(range); m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); - - auto new_position = m_frame.cursor_position(); - new_position.set_offset(new_position.offset() + 1); - m_frame.set_cursor_position(move(new_position)); - + m_frame.increment_cursor_position_offset(); return true; } } @@ -420,63 +416,33 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) { if (key == KeyCode::Key_Backspace) { - auto position = m_frame.cursor_position(); - - if (position.offset() == 0) { + if (!m_frame.decrement_cursor_position_offset()) { // FIXME: Move to the previous node and delete the last character there. return true; } - auto new_position = m_frame.cursor_position(); - new_position.set_offset(position.offset() - 1); - m_frame.set_cursor_position(move(new_position)); - - m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset() - 1, *position.node(), position.offset())); - + m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position()); return true; } else if (key == KeyCode::Key_Delete) { - auto position = m_frame.cursor_position(); - - if (position.offset() >= downcast<DOM::Text>(position.node())->data().length()) { + if (m_frame.cursor_position().offset_is_at_end_of_node()) { // FIXME: Move to the next node and delete the first character there. return true; } - - m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset(), *position.node(), position.offset() + 1)); - + m_edit_event_handler->handle_delete_character_after(m_frame.cursor_position()); return true; } else if (key == KeyCode::Key_Right) { - auto position = m_frame.cursor_position(); - - if (position.offset() >= downcast<DOM::Text>(position.node())->data().length()) { + if (!m_frame.increment_cursor_position_offset()) { // FIXME: Move to the next node. - return true; } - - auto new_position = m_frame.cursor_position(); - new_position.set_offset(position.offset() + 1); - m_frame.set_cursor_position(move(new_position)); - return true; } else if (key == KeyCode::Key_Left) { - auto position = m_frame.cursor_position(); - - if (position.offset() == 0) { + if (!m_frame.decrement_cursor_position_offset()) { // FIXME: Move to the previous node. - return true; } - - auto new_position = m_frame.cursor_position(); - new_position.set_offset(new_position.offset() - 1); - m_frame.set_cursor_position(move(new_position)); - return true; } else if (!should_ignore_keydown_event(code_point)) { m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); - - auto new_position = m_frame.cursor_position(); - new_position.set_offset(new_position.offset() + 1); - m_frame.set_cursor_position(move(new_position)); + m_frame.increment_cursor_position_offset(); return true; } else { // NOTE: Because modifier keys should be ignored, we need to return true. |