LibWeb: Register FormAssociatedElement with their owner form

This will eventually allow us to implement HTMLFormControlsCollection.
This commit is contained in:
Andreas Kling 2021-04-20 23:34:49 +02:00
parent e454e1a45d
commit 78733417a4
Notes: sideshowbarker 2024-07-18 19:19:19 +09:00
6 changed files with 32 additions and 2 deletions

View file

@ -31,7 +31,11 @@ namespace Web::HTML {
void FormAssociatedElement::set_form(HTMLFormElement* form)
{
if (m_form)
m_form->remove_associated_element({}, form_associated_element_to_html_element());
m_form = form;
if (m_form)
m_form->add_associated_element({}, form_associated_element_to_html_element());
}
}

View file

@ -39,7 +39,10 @@ public:
void set_form(HTMLFormElement*);
protected:
FormAssociatedElement() { }
FormAssociatedElement() = default;
virtual ~FormAssociatedElement() = default;
virtual HTMLElement& form_associated_element_to_html_element() = 0;
private:
WeakPtr<HTMLFormElement> m_form;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -148,4 +148,14 @@ void HTMLFormElement::submit()
submit_form(this, true);
}
void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
{
m_associated_elements.append(element);
}
void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
{
m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
}
}

View file

@ -46,8 +46,13 @@ public:
// NOTE: This is for the JS bindings. Use submit_form instead.
void submit();
void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
private:
bool m_firing_submission_events { false };
Vector<WeakPtr<HTMLElement>> m_associated_elements;
};
}

View file

@ -57,9 +57,13 @@ public:
void did_click_button(Badge<Layout::ButtonBox>);
private:
// ^DOM::Node
virtual void inserted() override;
virtual void removed_from(Node*) override;
// ^HTML::FormAssociatedElement
virtual HTMLElement& form_associated_element_to_html_element() override { return *this; }
void create_shadow_tree_if_needed();
RefPtr<DOM::Text> m_text_node;

View file

@ -42,8 +42,12 @@ public:
virtual ~HTMLSelectElement() override;
private:
// ^DOM::Node
virtual void inserted() override;
virtual void removed_from(DOM::Node*) override;
// ^HTML::FormAssociatedElement
virtual HTMLElement& form_associated_element_to_html_element() override { return *this; }
};
}