From d7df6de6a6e77769733e1e71a23dcdc343f08083 Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Tue, 18 May 2021 13:13:58 +0200 Subject: [PATCH] LibWeb: Do nothing when pressing modifier keys in HTML contenteditable Before this patch, pressing modifier keys such as Ctrl would insert whitespace into editable DOM nodes. This patch crudely fixes that behavior by checking if the codepoint associated with the event is non-zero. --- Userland/Libraries/LibWeb/Page/EventHandler.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 819b7fde83..4b4649278d 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2021, Max Wipfli * * SPDX-License-Identifier: BSD-2-Clause */ @@ -378,6 +379,12 @@ bool EventHandler::focus_previous_element() return false; } +constexpr bool should_ignore_keydown_event(u32 codepoint) +{ + // FIXME: There are probably also keys with non-zero codepoints that should be filtered out. + return codepoint == 0; +} + bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point) { if (key == KeyCode::Key_Tab) { @@ -396,10 +403,9 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin m_frame.set_cursor_position({ *range->start_container(), range->start_offset() }); if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) { - m_edit_event_handler->handle_delete(range); return true; - } else { + } 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); @@ -465,13 +471,15 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin m_frame.set_cursor_position(move(new_position)); return true; - } else { + } 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)); - + return true; + } else { + // NOTE: Because modifier keys should be ignored, we need to return true. return true; } }