2021-04-20 11:50:29 +02:00
/*
* Copyright ( c ) 2021 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2021-04-20 11:50:29 +02:00
*/
# include <LibWeb/HTML/FormAssociatedElement.h>
2022-03-01 20:59:46 +00:00
# include <LibWeb/HTML/HTMLButtonElement.h>
# include <LibWeb/HTML/HTMLFieldSetElement.h>
2021-04-20 11:50:29 +02:00
# include <LibWeb/HTML/HTMLFormElement.h>
2022-03-01 20:59:46 +00:00
# include <LibWeb/HTML/HTMLInputElement.h>
# include <LibWeb/HTML/HTMLLegendElement.h>
# include <LibWeb/HTML/HTMLSelectElement.h>
# include <LibWeb/HTML/HTMLTextAreaElement.h>
2021-04-20 11:50:29 +02:00
namespace Web : : HTML {
void FormAssociatedElement : : set_form ( HTMLFormElement * form )
{
2021-04-20 23:34:49 +02:00
if ( m_form )
2021-10-14 16:18:49 +01:00
m_form - > remove_associated_element ( { } , * this ) ;
2021-04-20 11:50:29 +02:00
m_form = form ;
2021-04-20 23:34:49 +02:00
if ( m_form )
2021-10-14 16:18:49 +01:00
m_form - > add_associated_element ( { } , * this ) ;
2021-04-20 11:50:29 +02:00
}
2022-03-01 20:59:46 +00:00
bool FormAssociatedElement : : enabled ( ) const
{
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled
// A form control is disabled if any of the following conditions are met:
// 1. The element is a button, input, select, textarea, or form-associated custom element, and the disabled attribute is specified on this element (regardless of its value).
// FIXME: This doesn't check for form-associated custom elements.
if ( ( is < HTMLButtonElement > ( this ) | | is < HTMLInputElement > ( this ) | | is < HTMLSelectElement > ( this ) | | is < HTMLTextAreaElement > ( this ) ) & & has_attribute ( HTML : : AttributeNames : : disabled ) )
return false ;
// 2. The element is a descendant of a fieldset element whose disabled attribute is specified, and is not a descendant of that fieldset element's first legend element child, if any.
auto * fieldset_ancestor = first_ancestor_of_type < HTMLFieldSetElement > ( ) ;
if ( fieldset_ancestor & & fieldset_ancestor - > has_attribute ( HTML : : AttributeNames : : disabled ) ) {
auto * first_legend_element_child = fieldset_ancestor - > first_child_of_type < HTMLLegendElement > ( ) ;
if ( ! first_legend_element_child | | ! is_descendant_of ( * first_legend_element_child ) )
return false ;
}
return true ;
}
2021-04-20 11:50:29 +02:00
}