From d90be22a735ab31dbfe3a75cf93b03aa061f0134 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 12 Oct 2021 14:44:00 +0200 Subject: [PATCH] LibWeb: Use strong pointers and null checks in handle_keydown() --- .../Libraries/LibWeb/Page/EventHandler.cpp | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 6fc36d603c..64dd778838 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -401,6 +401,15 @@ constexpr bool should_ignore_keydown_event(u32 code_point) bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point) { + if (!m_frame.active_document()) + return false; + + NonnullRefPtr document = *m_frame.active_document(); + if (!document->layout_node()) + return false; + + NonnullRefPtr layout_root = *document->layout_node(); + if (key == KeyCode::Key_Tab) { if (modifiers & KeyModifier::Mod_Shift) return focus_previous_element(); @@ -408,13 +417,10 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin return focus_next_element(); } - if (!layout_root()) - return false; - - if (layout_root()->selection().is_valid()) { - auto range = layout_root()->selection().to_dom_range()->normalized(); + if (layout_root->selection().is_valid()) { + auto range = layout_root->selection().to_dom_range()->normalized(); if (range->start_container()->is_editable()) { - m_frame.active_document()->layout_node()->set_selection({}); + layout_root->set_selection({}); // FIXME: This doesn't work for some reason? m_frame.set_cursor_position({ *range->start_container(), range->start_offset() }); @@ -468,12 +474,14 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin } auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point); - if (m_frame.active_document()->focused_element()) - return m_frame.active_document()->focused_element()->dispatch_event(move(event)); - else if (m_frame.active_document()->body()) - return m_frame.active_document()->body()->dispatch_event(move(event)); - else - return m_frame.active_document()->root().dispatch_event(move(event)); + + if (RefPtr focused_element = document->focused_element()) + return focused_element->dispatch_event(move(event)); + + if (RefPtr body = m_frame.active_document()->body()) + return body->dispatch_event(move(event)); + + return document->root().dispatch_event(move(event)); } bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)