From 939131176003b2c221cdf9546f0513b9c671e590 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Feb 2022 19:27:10 +0100 Subject: [PATCH] LibWeb: Make HTMLInputElement move cursor into text node when focused This mechanism feels rather awkward, but it's better than nothing. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 6 ++++++ Userland/Libraries/LibWeb/DOM/Element.h | 3 +++ .../Libraries/LibWeb/HTML/HTMLInputElement.cpp | 15 +++++++++++++++ Userland/Libraries/LibWeb/HTML/HTMLInputElement.h | 5 +++++ 4 files changed, 29 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 0cbdc93ca7..f6d6fae338 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -894,8 +894,14 @@ void Document::set_focused_element(Element* element) if (m_focused_element == element) return; + if (m_focused_element) + m_focused_element->did_lose_focus(); + m_focused_element = element; + if (m_focused_element) + m_focused_element->did_receive_focus(); + if (m_layout_root) m_layout_root->set_needs_display(); } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 6c982b0b7e..439fe6e19e 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -132,6 +132,9 @@ public: virtual RefPtr create_layout_node(NonnullRefPtr); + virtual void did_receive_focus() { } + virtual void did_lose_focus() { } + protected: virtual void children_changed() override; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index b5b2d6b33b..77005f55b2 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -112,6 +112,21 @@ void HTMLInputElement::create_shadow_tree_if_needed() set_shadow_root(move(shadow_root)); } +void HTMLInputElement::did_receive_focus() +{ + auto* browsing_context = document().browsing_context(); + if (!browsing_context) + return; + if (!m_text_node) + return; + browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 }); +} + +bool HTMLInputElement::is_focusable() const +{ + return m_text_node; +} + void HTMLInputElement::inserted() { HTMLElement::inserted(); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 2ba1ce2009..edd1d2c2d9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -36,6 +36,8 @@ public: void did_click_button(Badge); + virtual bool is_focusable() const override; + private: // ^DOM::Node virtual void inserted() override; @@ -44,6 +46,9 @@ private: // ^HTML::FormAssociatedElement virtual HTMLElement& form_associated_element_to_html_element() override { return *this; } + // ^DOM::EventTarget + virtual void did_receive_focus() override; + void create_shadow_tree_if_needed(); RefPtr m_text_node;