From b092353e4d1f74117d7530e8db3a09343b402d93 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 20 Apr 2021 11:50:29 +0200 Subject: [PATCH] LibWeb: Add basic support for HTMLInputElement.form HTMLInputElement now inherits from FormAssociatedElement, which will be a common base for the handful of elements that need to track their owner form (and register with it for the form.elements collection.) At the moment, the owner form is assigned during DOM insertion/removal of an HTMLInputElement. I didn't implement any of the legacy behaviors defined by the HTML parsing spec yet. --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../CodeGenerators/WrapperGenerator.cpp | 2 + .../LibWeb/HTML/FormAssociatedElement.cpp | 37 ++++++++++++++ .../LibWeb/HTML/FormAssociatedElement.h | 48 +++++++++++++++++++ .../Libraries/LibWeb/HTML/HTMLFormElement.h | 2 +- .../LibWeb/HTML/HTMLInputElement.cpp | 12 ++++- .../Libraries/LibWeb/HTML/HTMLInputElement.h | 10 +++- .../LibWeb/HTML/HTMLInputElement.idl | 2 + 8 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp create mode 100644 Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 110d3247f1..5361b96482 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -67,6 +67,7 @@ set(SOURCES HTML/AttributeNames.cpp HTML/CanvasRenderingContext2D.cpp HTML/EventNames.cpp + HTML/FormAssociatedElement.cpp HTML/FrameHostElement.cpp HTML/GlobalEventHandlers.cpp HTML/HTMLAnchorElement.cpp diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 9a4311ad3b..bf7f4660da 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -865,6 +865,7 @@ void generate_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include @@ -1205,6 +1206,7 @@ void generate_prototype_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp new file mode 100644 index 0000000000..5026c6067e --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +namespace Web::HTML { + +void FormAssociatedElement::set_form(HTMLFormElement* form) +{ + m_form = form; +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h new file mode 100644 index 0000000000..2e7a405adf --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +namespace Web::HTML { + +class FormAssociatedElement { +public: + HTMLFormElement* form() { return m_form; } + HTMLFormElement const* form() const { return m_form; } + + void set_form(HTMLFormElement*); + +protected: + FormAssociatedElement() { } + +private: + WeakPtr m_form; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.h b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.h index 8c5b3d1669..0368880494 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 25efe4d7c5..350ff84014 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -138,4 +138,14 @@ void HTMLInputElement::create_shadow_tree_if_needed() set_shadow_root(move(shadow_root)); } +void HTMLInputElement::inserted() +{ + set_form(first_ancestor_of_type()); +} + +void HTMLInputElement::removed_from(DOM::Node*) +{ + set_form(nullptr); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 51387371df..ff8cf6d8c7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,11 +26,14 @@ #pragma once +#include #include namespace Web::HTML { -class HTMLInputElement final : public HTMLElement { +class HTMLInputElement final + : public HTMLElement + , public FormAssociatedElement { public: using WrapperType = Bindings::HTMLInputElementWrapper; @@ -54,6 +57,9 @@ public: void did_click_button(Badge); private: + virtual void inserted() override; + virtual void removed_from(Node*) override; + void create_shadow_tree_if_needed(); RefPtr m_text_node; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl index 1fe526d6cc..361de54d65 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl @@ -1,5 +1,7 @@ interface HTMLInputElement : HTMLElement { + readonly attribute HTMLFormElement? form; + [Reflect] attribute DOMString accept; [Reflect] attribute DOMString alt; [Reflect] attribute DOMString max;