diff --git a/Userland/Libraries/LibWeb/DOM/Text.cpp b/Userland/Libraries/LibWeb/DOM/Text.cpp index 72fd150e88..c5a53367c4 100644 --- a/Userland/Libraries/LibWeb/DOM/Text.cpp +++ b/Userland/Libraries/LibWeb/DOM/Text.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -33,7 +32,7 @@ void Text::initialize(JS::Realm& realm) void Text::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); - visitor.visit(m_owner_input_element.ptr()); + visitor.visit(dynamic_cast(m_owner.ptr())); } // https://dom.spec.whatwg.org/#dom-text-text @@ -44,11 +43,6 @@ WebIDL::ExceptionOr> Text::construct_impl(JS::Realm& real return realm.heap().allocate(realm, window.associated_document(), data); } -void Text::set_owner_input_element(Badge, HTML::HTMLInputElement& input_element) -{ - m_owner_input_element = &input_element; -} - // https://dom.spec.whatwg.org/#dom-text-splittext // https://dom.spec.whatwg.org/#concept-text-split WebIDL::ExceptionOr> Text::split_text(size_t offset) diff --git a/Userland/Libraries/LibWeb/DOM/Text.h b/Userland/Libraries/LibWeb/DOM/Text.h index dd941ca37b..c803c5fc1c 100644 --- a/Userland/Libraries/LibWeb/DOM/Text.h +++ b/Userland/Libraries/LibWeb/DOM/Text.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,6 +13,12 @@ namespace Web::DOM { +class EditableTextNodeOwner { +public: + virtual ~EditableTextNodeOwner() = default; + virtual void did_edit_text_node(Badge) = 0; +}; + class Text : public CharacterData { WEB_PLATFORM_OBJECT(Text, CharacterData); @@ -26,8 +33,9 @@ public: void set_always_editable(bool b) { m_always_editable = b; } - void set_owner_input_element(Badge, HTML::HTMLInputElement&); - HTML::HTMLInputElement* owner_input_element() { return m_owner_input_element.ptr(); } + template T> + void set_editable_text_node_owner(Badge, EditableTextNodeOwner& owner_element) { m_owner = &owner_element; } + EditableTextNodeOwner* editable_text_node_owner() { return m_owner.ptr(); } WebIDL::ExceptionOr> split_text(size_t offset); @@ -42,7 +50,7 @@ protected: virtual void visit_edges(Cell::Visitor&) override; private: - JS::GCPtr m_owner_input_element; + JS::GCPtr m_owner; bool m_always_editable { false }; bool m_is_password_input { false }; diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index e30fc4371b..8acd202c68 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -483,8 +483,8 @@ void BrowsingContext::did_edit(Badge) if (m_cursor_position.node() && is(*m_cursor_position.node())) { auto& text_node = static_cast(*m_cursor_position.node()); - if (auto* input_element = text_node.owner_input_element()) - input_element->did_edit_text_node({}); + if (auto* text_node_owner = text_node.editable_text_node_owner()) + text_node_owner->did_edit_text_node({}); } } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 9515fed11e..e02c0a9890 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -504,7 +503,7 @@ void HTMLInputElement::create_shadow_tree_if_needed() m_placeholder_text_node = heap().allocate(realm(), document(), MUST(String::from_deprecated_string(initial_value))); m_placeholder_text_node->set_data(deprecated_attribute(HTML::AttributeNames::placeholder)); - m_placeholder_text_node->set_owner_input_element({}, *this); + m_placeholder_text_node->set_editable_text_node_owner(Badge {}, *this); MUST(m_placeholder_element->append_child(*m_placeholder_text_node)); MUST(element->append_child(*m_placeholder_element)); @@ -519,7 +518,7 @@ void HTMLInputElement::create_shadow_tree_if_needed() handle_readonly_attribute(deprecated_attribute(HTML::AttributeNames::readonly)); } - m_text_node->set_owner_input_element({}, *this); + m_text_node->set_editable_text_node_owner(Badge {}, *this); if (m_type == TypeAttributeState::Password) m_text_node->set_is_password_input({}, true); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 4e801d4812..755e695cf9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -41,7 +42,8 @@ namespace Web::HTML { class HTMLInputElement final : public HTMLElement - , public FormAssociatedElement { + , public FormAssociatedElement + , public DOM::EditableTextNodeOwner { WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement); FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement) @@ -83,8 +85,6 @@ public: bool is_mutable() const { return m_is_mutable; } - void did_edit_text_node(Badge); - JS::GCPtr files(); void set_files(JS::GCPtr); @@ -101,6 +101,9 @@ public: WebIDL::ExceptionOr show_picker(); + // ^DOM::EditableTextNodeOwner + virtual void did_edit_text_node(Badge) override; + // ^EventTarget // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }