1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

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. :^)
This commit is contained in:
Max Wipfli 2021-05-18 22:09:15 +02:00 committed by Andreas Kling
parent 7181cb3a9c
commit 9440a3c280
3 changed files with 32 additions and 45 deletions

View file

@ -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.