2020-08-01 03:07:00 +01:00
/*
2021-04-28 22:46:44 +02:00
* Copyright ( c ) 2020 , the SerenityOS developers .
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2021 - 2022 , Andreas Kling < andreas @ ladybird . org >
2023-12-07 15:53:49 +01:00
* Copyright ( c ) 2023 , Bastiaan van der Plaat < bastiaan . v . d . plaat @ gmail . com >
2025-01-27 13:11:06 +13:00
* Copyright ( c ) 2025 , Shannon Booth < shannon @ serenityos . org >
2020-08-01 03:07:00 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-08-01 03:07:00 +01:00
*/
2024-04-27 12:09:58 +12:00
# include <LibWeb/Bindings/HTMLSelectElementPrototype.h>
2022-09-30 17:16:16 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2026-02-11 07:33:58 +01:00
# include <LibWeb/CSS/CSSStyleProperties.h>
2025-02-18 09:19:56 +01:00
# include <LibWeb/CSS/ComputedProperties.h>
2023-12-07 15:53:49 +01:00
# include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
2026-02-04 10:30:53 +00:00
# include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
2023-12-07 15:53:49 +01:00
# include <LibWeb/DOM/Document.h>
# include <LibWeb/DOM/ElementFactory.h>
# include <LibWeb/DOM/Event.h>
# include <LibWeb/DOM/ShadowRoot.h>
# include <LibWeb/HTML/EventNames.h>
2025-11-17 20:46:54 -08:00
# include <LibWeb/HTML/HTMLDataListElement.h>
2021-04-20 22:52:55 +02:00
# include <LibWeb/HTML/HTMLFormElement.h>
2023-12-07 15:53:49 +01:00
# include <LibWeb/HTML/HTMLHRElement.h>
2022-03-20 16:13:23 +01:00
# include <LibWeb/HTML/HTMLOptGroupElement.h>
2022-03-16 13:08:12 +01:00
# include <LibWeb/HTML/HTMLOptionElement.h>
2020-08-01 03:07:00 +01:00
# include <LibWeb/HTML/HTMLSelectElement.h>
2025-12-07 16:37:54 -08:00
# include <LibWeb/HTML/HTMLSelectedContentElement.h>
2025-10-16 14:07:09 +01:00
# include <LibWeb/HTML/Navigable.h>
2024-04-08 21:34:19 +02:00
# include <LibWeb/HTML/Numbers.h>
2024-06-13 18:11:45 +02:00
# include <LibWeb/HTML/Window.h>
2023-12-07 15:53:49 +01:00
# include <LibWeb/Infra/Strings.h>
# include <LibWeb/Layout/Node.h>
# include <LibWeb/Namespace.h>
# include <LibWeb/Page/Page.h>
2024-09-07 14:22:17 -04:00
# include <LibWeb/Painting/Paintable.h>
2020-08-01 03:07:00 +01:00
namespace Web : : HTML {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( HTMLSelectElement ) ;
2023-11-19 19:47:52 +01:00
2022-02-18 21:00:52 +01:00
HTMLSelectElement : : HTMLSelectElement ( DOM : : Document & document , DOM : : QualifiedName qualified_name )
2022-03-23 18:55:54 -04:00
: HTMLElement ( document , move ( qualified_name ) )
2020-08-01 03:07:00 +01:00
{
2025-11-06 17:51:35 +01:00
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
. supports_indexed_properties = true ,
. has_indexed_property_setter = true ,
. indexed_property_setter_has_identifier = true ,
} ;
2020-08-01 03:07:00 +01:00
}
2022-03-14 13:21:51 -06:00
HTMLSelectElement : : ~ HTMLSelectElement ( ) = default ;
2020-08-01 03:07:00 +01:00
2023-08-07 08:41:28 +02:00
void HTMLSelectElement : : initialize ( JS : : Realm & realm )
2023-01-10 06:28:20 -05:00
{
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( HTMLSelectElement ) ;
2025-04-20 16:22:57 +02:00
Base : : initialize ( realm ) ;
2023-01-10 06:28:20 -05:00
}
2022-09-01 20:50:16 +02:00
void HTMLSelectElement : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
2023-11-19 16:18:00 +13:00
visitor . visit ( m_options ) ;
2024-04-08 21:58:39 +02:00
visitor . visit ( m_selected_options ) ;
2023-12-07 15:53:49 +01:00
visitor . visit ( m_inner_text_element ) ;
2023-12-20 18:21:32 +01:00
visitor . visit ( m_chevron_icon_element ) ;
2025-01-26 15:16:07 +13:00
visitor . visit ( m_cached_list_of_options ) ;
2024-04-03 19:19:08 +02:00
for ( auto const & item : m_select_items ) {
if ( item . has < SelectItemOption > ( ) )
visitor . visit ( item . get < SelectItemOption > ( ) . option_element ) ;
if ( item . has < SelectItemOptionGroup > ( ) ) {
auto item_option_group = item . get < SelectItemOptionGroup > ( ) ;
for ( auto const & item : item_option_group . items )
visitor . visit ( item . option_element ) ;
}
}
2023-12-07 15:53:49 +01:00
}
2024-12-20 11:32:17 +01:00
void HTMLSelectElement : : adjust_computed_style ( CSS : : ComputedProperties & style )
2023-12-07 15:53:49 +01:00
{
2024-11-08 20:14:37 +08:00
// https://drafts.csswg.org/css-display-3/#unbox
if ( style . display ( ) . is_contents ( ) )
style . set_property ( CSS : : PropertyID : : Display , CSS : : DisplayStyleValue : : create ( CSS : : Display : : from_short ( CSS : : Display : : Short : : None ) ) ) ;
2023-12-07 15:53:49 +01:00
// AD-HOC: We rewrite `display: inline` to `display: inline-block`.
// This is required for the internal shadow tree to work correctly in layout.
2024-03-07 21:27:37 +01:00
if ( style . display ( ) . is_inline_outside ( ) & & style . display ( ) . is_flow_inside ( ) )
style . set_property ( CSS : : PropertyID : : Display , CSS : : DisplayStyleValue : : create ( CSS : : Display : : from_short ( CSS : : Display : : Short : : InlineBlock ) ) ) ;
2026-02-04 10:30:53 +00:00
// AD-HOC: Enforce normal line-height for select elements. This matches the behavior of other engines.
style . set_property ( CSS : : PropertyID : : LineHeight , CSS : : KeywordStyleValue : : create ( CSS : : Keyword : : Normal ) ) ;
2022-09-01 20:50:16 +02:00
}
2024-07-25 21:20:04 +04:00
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-size
2024-11-29 11:19:31 +00:00
u32 HTMLSelectElement : : display_size ( ) const
2024-04-08 21:34:19 +02:00
{
// The size IDL attribute must reflect the respective content attributes of the same name. The size IDL attribute has a default value of 0.
if ( auto size_string = get_attribute ( HTML : : AttributeNames : : size ) ; size_string . has_value ( ) ) {
2024-07-25 21:20:04 +04:00
// The display size of a select element is the result of applying the rules for parsing non-negative integers
// to the value of element's size attribute, if it has one and parsing it is successful.
2024-04-08 21:34:19 +02:00
if ( auto size = parse_non_negative_integer ( * size_string ) ; size . has_value ( ) )
return * size ;
}
2024-07-25 21:20:04 +04:00
// If applying those rules to the attribute's value is not successful or if the size attribute is absent,
// then the element's display size is 4 if the element's multiple content attribute is present, and 1 otherwise.
if ( has_attribute ( AttributeNames : : multiple ) )
return 4 ;
return 1 ;
2024-04-08 21:34:19 +02:00
}
2024-11-29 11:19:31 +00:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-size
WebIDL : : UnsignedLong HTMLSelectElement : : size ( ) const
{
// The multiple, required, and size IDL attributes must reflect the respective content attributes of the same name. The size IDL attribute has a default value of 0.
if ( auto size_string = get_attribute ( HTML : : AttributeNames : : size ) ; size_string . has_value ( ) ) {
if ( auto size = parse_non_negative_integer ( * size_string ) ; size . has_value ( ) & & * size < = 2147483647 )
return * size ;
}
return 0 ;
}
2025-10-31 12:30:47 +00:00
void HTMLSelectElement : : set_size ( WebIDL : : UnsignedLong size )
2024-04-08 21:34:19 +02:00
{
2024-11-29 11:19:31 +00:00
if ( size > 2147483647 )
size = 0 ;
2025-10-31 12:30:47 +00:00
set_attribute_value ( HTML : : AttributeNames : : size , String : : number ( size ) ) ;
2024-04-08 21:34:19 +02:00
}
2022-03-16 13:08:12 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
2025-11-06 17:51:35 +01:00
GC : : Ptr < HTMLOptionsCollection > const & HTMLSelectElement : : options ( ) const
2022-03-16 13:08:12 +01:00
{
2025-11-17 20:46:54 -08:00
// The options IDL attribute must return an HTMLOptionsCollection rooted at the select node,
// whose filter matches the elements in the list of options.
2022-03-16 13:08:12 +01:00
if ( ! m_options ) {
2025-11-06 17:51:35 +01:00
m_options = HTMLOptionsCollection : : create ( const_cast < HTMLSelectElement & > ( * this ) , [ this ] ( DOM : : Element const & element ) {
2025-11-17 20:46:54 -08:00
auto const * maybe_option = as_if < HTML : : HTMLOptionElement > ( element ) ;
return maybe_option & & maybe_option - > nearest_select_element ( ) = = this ;
2023-08-13 13:05:26 +02:00
} ) ;
2022-03-16 13:08:12 +01:00
}
return m_options ;
}
2022-10-18 03:22:55 -05:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-length
2024-04-08 21:50:35 +02:00
WebIDL : : UnsignedLong HTMLSelectElement : : length ( )
2022-10-18 03:22:55 -05:00
{
// The length IDL attribute must return the number of nodes represented by the options collection. On setting, it must act like the attribute of the same name on the options collection.
return const_cast < HTMLOptionsCollection & > ( * options ( ) ) . length ( ) ;
}
2024-04-08 21:50:35 +02:00
WebIDL : : ExceptionOr < void > HTMLSelectElement : : set_length ( WebIDL : : UnsignedLong length )
{
// On setting, it must act like the attribute of the same name on the options collection.
return const_cast < HTMLOptionsCollection & > ( * options ( ) ) . set_length ( length ) ;
}
2022-10-18 03:22:55 -05:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
2024-04-08 22:01:21 +02:00
HTMLOptionElement * HTMLSelectElement : : item ( WebIDL : : UnsignedLong index )
2022-10-18 03:22:55 -05:00
{
// The item(index) method must return the value returned by the method of the same name on the options collection, when invoked with the same argument.
2025-01-21 09:12:05 -05:00
return as < HTMLOptionElement > ( const_cast < HTMLOptionsCollection & > ( * options ( ) ) . item ( index ) ) ;
2022-10-18 03:22:55 -05:00
}
2025-11-06 17:51:35 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:htmlselectelement
Optional < JS : : Value > HTMLSelectElement : : item_value ( size_t index ) const
{
// The options collection is also mirrored on the HTMLSelectElement object. The supported property indices at any
// instant are the indices supported by the object returned by the options attribute at that instant.
return ( const_cast < HTMLOptionsCollection & > ( * options ( ) ) . item_value ( index ) ) ;
}
2022-10-18 03:22:55 -05:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
2024-04-08 22:01:21 +02:00
HTMLOptionElement * HTMLSelectElement : : named_item ( FlyString const & name )
2022-10-18 03:22:55 -05:00
{
// The namedItem(name) method must return the value returned by the method of the same name on the options collection, when invoked with the same argument.
2025-01-21 09:12:05 -05:00
return as < HTMLOptionElement > ( const_cast < HTMLOptionsCollection & > ( * options ( ) ) . named_item ( name ) ) ;
2022-10-18 03:22:55 -05:00
}
2022-03-21 20:03:37 -04:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
2022-09-25 17:03:42 +01:00
WebIDL : : ExceptionOr < void > HTMLSelectElement : : add ( HTMLOptionOrOptGroupElement element , Optional < HTMLElementOrElementIndex > before )
2022-03-21 20:03:37 -04:00
{
// Similarly, the add(element, before) method must act like its namesake method on that same options collection.
2024-07-24 22:32:05 +04:00
TRY ( const_cast < HTMLOptionsCollection & > ( * options ( ) ) . add ( move ( element ) , move ( before ) ) ) ;
2024-07-25 21:13:22 +04:00
update_selectedness ( ) ; // Not in spec
2024-07-24 22:32:05 +04:00
return { } ;
2022-03-21 20:03:37 -04:00
}
2025-11-06 17:51:35 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:set-the-value-of-a-new-indexed-property
WebIDL : : ExceptionOr < void > HTMLSelectElement : : set_value_of_indexed_property ( u32 n , JS : : Value new_value )
{
// When the user agent is to set the value of a new indexed property or set the value of an existing indexed property
// for a select element, it must instead run the corresponding algorithm on the select element's options collection.
TRY ( const_cast < HTMLOptionsCollection & > ( * options ( ) ) . set_value_of_indexed_property ( n , new_value ) ) ;
return { } ;
}
2024-04-08 21:56:33 +02:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-remove
void HTMLSelectElement : : remove ( )
{
// The remove() method must act like its namesake method on that same options collection when it has arguments,
// and like its namesake method on the ChildNode interface implemented by the HTMLSelectElement ancestor interface Element when it has no arguments.
ChildNode : : remove_binding ( ) ;
}
void HTMLSelectElement : : remove ( WebIDL : : Long index )
{
const_cast < HTMLOptionsCollection & > ( * options ( ) ) . remove ( index ) ;
}
2024-04-08 21:58:39 +02:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedoptions
2024-11-15 04:01:23 +13:00
GC : : Ref < DOM : : HTMLCollection > HTMLSelectElement : : selected_options ( )
2024-04-08 21:58:39 +02:00
{
// The selectedOptions IDL attribute must return an HTMLCollection rooted at the select node,
// whose filter matches the elements in the list of options that have their selectedness set to true.
if ( ! m_selected_options ) {
2025-11-17 20:46:54 -08:00
m_selected_options = DOM : : HTMLCollection : : create ( * this , DOM : : HTMLCollection : : Scope : : Descendants , [ this ] ( Element const & element ) {
auto const * maybe_option = as_if < HTML : : HTMLOptionElement > ( element ) ;
if ( maybe_option & & maybe_option - > nearest_select_element ( ) = = this ) {
return maybe_option - > selected ( ) ;
2024-04-08 21:58:39 +02:00
}
return false ;
} ) ;
}
return * m_selected_options ;
}
2022-03-20 16:13:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
2025-01-26 15:16:07 +13:00
void HTMLSelectElement : : update_cached_list_of_options ( ) const
2022-03-20 16:13:23 +01:00
{
2025-11-17 20:46:54 -08:00
// 1. Let options be « ».
2025-01-26 15:16:07 +13:00
m_cached_list_of_options . clear ( ) ;
m_cached_number_of_selected_options = 0 ;
2022-03-20 16:13:23 +01:00
2025-11-17 20:46:54 -08:00
// Check if node is an optgroup element and node has an ancestor optgroup in between itself and this select
auto is_nested_optgroup = [ this ] ( DOM : : Node const & node ) {
if ( ! is < HTMLOptGroupElement > ( node ) )
return false ;
for ( auto const * ancestor = node . parent ( ) ; ancestor ; ancestor = ancestor - > parent ( ) ) {
if ( ancestor = = this )
return false ; // reached the select without another optgroup
if ( is < HTMLOptGroupElement > ( * ancestor ) )
return true ; // found an optgroup above us
}
return false ;
} ;
// 2. Let node be the first child of select in tree order.
// 3. While node is not null:
for_each_in_subtree ( [ & ] ( auto & node ) {
// 1. If node is an option element, then append node to options.
if ( auto maybe_option = as_if < HTMLOptionElement > ( node ) ) {
2025-01-26 15:16:07 +13:00
if ( maybe_option - > selected ( ) )
+ + m_cached_number_of_selected_options ;
m_cached_list_of_options . append ( const_cast < HTMLOptionElement & > ( * maybe_option ) ) ;
2025-01-26 14:34:12 +13:00
}
2025-11-17 20:46:54 -08:00
// 2. If any of the following conditions are true:
// - node is a select element;
// - node is an hr element;
// - node is an option element;
// - node is a datalist element;
// - node is an optgroup element and node has an ancestor optgroup in between itself and select,
if ( is < HTMLSelectElement > ( node )
| | is < HTMLHRElement > ( node )
| | is < HTMLOptionElement > ( node )
| | is < HTMLDataListElement > ( node )
| | is_nested_optgroup ( node ) ) {
// then set node to the next descendant of select in tree order, excluding node's descendants, if any such
// node exists; otherwise null.
return TraversalDecision : : SkipChildrenAndContinue ;
2025-01-26 14:34:12 +13:00
}
2025-11-17 20:46:54 -08:00
// Otherwise, set node to the next descendant of select in tree order, if any such node exists; otherwise null.
return TraversalDecision : : Continue ;
} ) ;
// 4. Return options.
// (Implicit by updating m_cached_list_of_options)
2025-01-26 15:16:07 +13:00
}
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
Vector < GC : : Root < HTMLOptionElement > > HTMLSelectElement : : list_of_options ( ) const
{
update_cached_list_of_options ( ) ;
Vector < GC : : Root < HTMLOptionElement > > list ;
list . ensure_capacity ( m_cached_list_of_options . size ( ) ) ;
for ( auto & item : m_cached_list_of_options )
list . unchecked_append ( GC : : make_root ( item ) ) ;
2022-03-20 16:13:23 +01:00
return list ;
}
2022-12-22 19:51:46 -05:00
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-form-reset-control
void HTMLSelectElement : : reset_algorithm ( )
{
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2025-02-02 20:41:23 +01:00
// The reset algorithm for a select element selectElement is:
2025-01-26 15:16:07 +13:00
2025-02-02 20:41:23 +01:00
// 1. Set selectElement's user validity to false.
m_user_validity = false ;
// 2. For each optionElement of selectElement's list of options:
2025-01-26 15:16:07 +13:00
for ( auto const & option_element : m_cached_list_of_options ) {
2025-02-02 20:41:23 +01:00
// 1. If optionElement has a selected attribute, then set optionElement's selectedness to true; otherwise set it to false.
2024-11-14 00:05:38 +01:00
option_element - > set_selected_internal ( option_element - > has_attribute ( AttributeNames : : selected ) ) ;
2025-02-02 20:41:23 +01:00
// 2. Set optionElement's dirtiness to false.
2022-12-22 19:51:46 -05:00
option_element - > m_dirty = false ;
}
2025-02-02 20:41:23 +01:00
// 3. Run the selectedness setting algorithm given selectElement.
update_selectedness ( ) ;
2022-12-22 19:51:46 -05:00
}
2022-03-20 16:13:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
2024-04-08 21:58:39 +02:00
WebIDL : : Long HTMLSelectElement : : selected_index ( ) const
2022-03-20 16:13:23 +01:00
{
2025-12-07 16:37:54 -08:00
// The selectedIndex getter steps are to return the index of the first option element in this's list of options
// in tree order that has its selectedness set to true, if any. If there isn't one, then return − 1.
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2022-03-20 16:13:23 +01:00
2024-04-08 21:58:39 +02:00
WebIDL : : Long index = 0 ;
2025-01-26 15:16:07 +13:00
for ( auto const & option_element : m_cached_list_of_options ) {
2022-08-28 13:42:07 +02:00
if ( option_element - > selected ( ) )
2022-03-20 16:13:23 +01:00
return index ;
+ + index ;
}
return - 1 ;
}
2025-12-07 16:37:54 -08:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
WebIDL : : ExceptionOr < void > HTMLSelectElement : : set_selected_index ( WebIDL : : Long index )
2022-03-20 16:13:23 +01:00
{
2025-12-07 16:37:54 -08:00
// The selectedIndex setter steps are:
ScopeGuard guard { [ & ] ( ) { update_inner_text_element ( ) ; } } ;
// 1. Let firstMatchingOption be null.
GC : : Ptr < HTMLOptionElement > first_matching_option ;
// 2. For each option of this's list of options:
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2025-12-07 16:37:54 -08:00
WebIDL : : Long current_index = 0 ;
for ( auto const & option : m_cached_list_of_options ) {
// 1. Set option's selectedness to false.
2024-11-14 00:05:38 +01:00
option - > set_selected_internal ( false ) ;
2022-03-20 16:13:23 +01:00
2025-12-07 16:37:54 -08:00
// 2. If firstMatchingOption is null and option's index is equal to the given value, then
// set firstMatchingOption to option.
if ( ! first_matching_option & & current_index = = index )
first_matching_option = option ;
2025-03-20 17:25:11 -04:00
2025-12-07 16:37:54 -08:00
current_index + + ;
}
// 3. If firstMatchingOption is not null, then set firstMatchingOption's selectedness to true
// and set firstMatchingOption's dirtiness to true.
if ( first_matching_option ) {
first_matching_option - > set_selected_internal ( true ) ;
first_matching_option - > m_dirty = true ;
}
2022-03-20 16:13:23 +01:00
2025-12-07 16:37:54 -08:00
// 4. Run update a select's selectedcontent given this.
TRY ( update_selectedcontent ( ) ) ;
return { } ;
2022-03-20 16:13:23 +01:00
}
2022-11-05 03:58:14 +00:00
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
i32 HTMLSelectElement : : default_tab_index_value ( ) const
{
// See the base function for the spec comments.
return 0 ;
}
2025-01-27 13:35:18 +13:00
bool HTMLSelectElement : : can_skip_selectedness_update_for_inserted_option ( HTMLOptionElement const & option ) const
{
if ( option . selected ( ) )
return false ;
if ( m_cached_number_of_selected_options > = 2 )
return false ;
if ( display_size ( ) = = 1 & & m_cached_number_of_selected_options = = 0 )
return false ;
return true ;
}
2025-01-27 13:11:06 +13:00
bool HTMLSelectElement : : can_skip_children_changed_selectedness_update ( ChildrenChangedMetadata const & metadata ) const
{
// If the following criteria are met, there is no need to re-run the selectedness algorithm.
// FIXME: We can tighten up these conditions and skip even more work!
if ( metadata . type ! = ChildrenChangedMetadata : : Type : : Inserted )
return false ;
2025-01-27 13:35:18 +13:00
if ( auto * option = as_if < HTMLOptionElement > ( * metadata . node ) )
return can_skip_selectedness_update_for_inserted_option ( * option ) ;
2025-01-27 13:11:06 +13:00
return false ;
}
2026-03-18 13:36:27 +01:00
void HTMLSelectElement : : children_changed ( ChildrenChangedMetadata const & metadata )
2024-11-14 00:05:38 +01:00
{
2025-01-27 01:16:33 +13:00
Base : : children_changed ( metadata ) ;
2025-01-27 13:11:06 +13:00
2026-03-18 13:36:27 +01:00
if ( can_skip_children_changed_selectedness_update ( metadata ) )
2025-01-27 13:11:06 +13:00
return ;
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2024-11-14 00:05:38 +01:00
update_selectedness ( ) ;
}
2022-11-05 04:51:42 +00:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
2023-08-12 21:30:19 +12:00
String const & HTMLSelectElement : : type ( ) const
2022-11-05 04:51:42 +00:00
{
// The type IDL attribute, on getting, must return the string "select-one" if the multiple attribute is absent, and the string "select-multiple" if the multiple attribute is present.
2023-08-12 21:30:19 +12:00
static String const select_one = " select-one " _string ;
static String const select_multiple = " select-multiple " _string ;
2022-11-05 04:51:42 +00:00
if ( ! has_attribute ( AttributeNames : : multiple ) )
return select_one ;
return select_multiple ;
}
2023-01-28 22:23:16 +00:00
Optional < ARIA : : Role > HTMLSelectElement : : default_role ( ) const
2022-11-28 17:58:13 -06:00
{
// https://www.w3.org/TR/html-aria/#el-select-multiple-or-size-greater-1
2023-09-21 20:26:19 +12:00
if ( has_attribute ( AttributeNames : : multiple ) )
2023-01-28 22:23:16 +00:00
return ARIA : : Role : : listbox ;
2025-01-27 12:08:36 +13:00
if ( auto size_string = get_attribute ( HTML : : AttributeNames : : size ) ; size_string . has_value ( ) ) {
if ( auto size = size_string - > to_number < int > ( ) ; size . has_value ( ) & & * size > 1 )
return ARIA : : Role : : listbox ;
2022-11-28 17:58:13 -06:00
}
// https://www.w3.org/TR/html-aria/#el-select
2023-01-28 22:23:16 +00:00
return ARIA : : Role : : combobox ;
2022-11-28 17:58:13 -06:00
}
2025-12-07 16:37:54 -08:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-value
2025-07-26 12:19:56 -04:00
Utf16String HTMLSelectElement : : value ( ) const
2023-12-07 15:53:49 +01:00
{
2025-12-07 16:37:54 -08:00
// The value getter steps are to return the value of the first option element in this's
// list of options in tree order that has its selectedness set to true, if any. If there
// isn't one, then return the empty string.
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
for ( auto const & option_element : m_cached_list_of_options )
2023-12-07 15:53:49 +01:00
if ( option_element - > selected ( ) )
return option_element - > value ( ) ;
2025-07-26 12:19:56 -04:00
return { } ;
2023-12-07 15:53:49 +01:00
}
2025-12-07 16:37:54 -08:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-value
2025-07-26 12:19:56 -04:00
WebIDL : : ExceptionOr < void > HTMLSelectElement : : set_value ( Utf16String const & value )
2023-12-07 15:53:49 +01:00
{
2025-12-07 16:37:54 -08:00
// The value setter steps are:
ScopeGuard guard { [ & ] ( ) { update_inner_text_element ( ) ; } } ;
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2025-12-07 16:37:54 -08:00
// 1. Let firstMatchingOption be null.
GC : : Ptr < HTMLOptionElement > first_matching_option ;
// 2. For each option of this's list of options:
for ( auto const & option_element : m_cached_list_of_options ) {
// 1. Set option's selectedness to false.
option_element - > set_selected_internal ( false ) ;
// 2. If firstMatchingOption is null and option's value is equal to the given value, then set
// firstMatchingOption to option.
if ( ! first_matching_option & & option_element - > value ( ) = = value )
first_matching_option = option_element ;
}
// 3. If firstMatchingOption is not null, then set firstMatchingOption's selectedness to true and set
// firstMatchingOption's dirtiness to true.
if ( first_matching_option ) {
first_matching_option - > set_selected_internal ( true ) ;
first_matching_option - > m_dirty = true ;
}
// 4. Run update a select's selectedcontent given this.
TRY ( update_selectedcontent ( ) ) ;
2024-04-03 19:19:08 +02:00
return { } ;
}
2023-12-07 15:53:49 +01:00
2025-12-07 16:37:54 -08:00
// https://html.spec.whatwg.org/multipage/form-elements.html#send-select-update-notifications
void HTMLSelectElement : : send_select_update_notifications ( )
2024-04-03 19:19:08 +02:00
{
2025-12-07 16:37:54 -08:00
// To send select update notifications for a select element element, queue an element task on
// the user interaction task source given element to run these steps:
2023-12-07 15:53:49 +01:00
queue_an_element_task ( HTML : : Task : : Source : : UserInteraction , [ this ] {
2025-02-02 20:41:23 +01:00
// 1. Set the select element's user validity to true.
m_user_validity = true ;
2023-12-07 15:53:49 +01:00
2025-12-07 16:37:54 -08:00
// 2. Run update a select's selectedcontent given element.
MUST ( update_selectedcontent ( ) ) ;
// FIXME: 3. Run clone selected option into select button given element.
// 4. Fire an event named input at element, with the bubbles and composed attributes initialized to true.
2023-12-07 15:53:49 +01:00
auto input_event = DOM : : Event : : create ( realm ( ) , HTML : : EventNames : : input ) ;
input_event - > set_bubbles ( true ) ;
input_event - > set_composed ( true ) ;
dispatch_event ( input_event ) ;
2025-12-07 16:37:54 -08:00
// 5. Fire an event named change at element, with the bubbles attribute initialized to true.
2023-12-07 15:53:49 +01:00
auto change_event = DOM : : Event : : create ( realm ( ) , HTML : : EventNames : : change ) ;
change_event - > set_bubbles ( true ) ;
dispatch_event ( * change_event ) ;
} ) ;
}
2023-09-13 17:15:27 +01:00
void HTMLSelectElement : : set_is_open ( bool open )
{
if ( open = = m_is_open )
return ;
m_is_open = open ;
2024-09-04 10:01:08 +02:00
invalidate_style ( DOM : : StyleInvalidationReason : : HTMLSelectElementSetIsOpen ) ;
2023-09-13 17:15:27 +01:00
}
2023-12-07 15:53:49 +01:00
bool HTMLSelectElement : : has_activation_behavior ( ) const
{
return true ;
}
2024-06-13 18:11:45 +02:00
// https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
void HTMLSelectElement : : show_the_picker_if_applicable ( )
2023-12-07 15:53:49 +01:00
{
2024-06-13 18:11:45 +02:00
// FIXME: Deduplicate with HTMLInputElement
2025-03-14 19:43:45 +00:00
// To show the picker, if applicable for a select element element:
2024-06-13 18:11:45 +02:00
// 1. If element's relevant global object does not have transient activation, then return.
2025-01-26 18:41:47 +13:00
auto & relevant_global = as < HTML : : Window > ( relevant_global_object ( * this ) ) ;
if ( ! relevant_global . has_transient_activation ( ) )
2024-06-13 18:11:45 +02:00
return ;
// 2. If element is not mutable, then return.
2025-08-10 00:29:35 +02:00
if ( ! is_mutable ( ) )
2024-06-13 18:11:45 +02:00
return ;
// 3. Consume user activation given element's relevant global object.
2025-01-26 18:41:47 +13:00
relevant_global . consume_user_activation ( ) ;
2024-06-13 18:11:45 +02:00
2025-03-14 19:43:45 +00:00
// 4. If element does not support a picker, then return.
// NB: Select elements always support a picker.
2024-06-13 18:11:45 +02:00
2025-03-14 19:43:45 +00:00
// 5. If element is an input element and element's type attribute is in the File Upload state, then run these steps
// in parallel:
// NB: Not applicable to select elements.
// 6. Otherwise, the user agent should show the relevant user interface for selecting a value for element, in the
// way it normally would when the user interacts with the control.
// When showing such a user interface, it must respect the requirements stated in the relevant parts of the
// specification for how element behaves given its type attribute state. (For example, various sections describe
// restrictions on the resulting value string.)
2024-06-13 18:11:45 +02:00
// This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
2025-03-14 19:43:45 +00:00
// (If this closes a file selection picker, then per the above that will lead to firing either input and change
// events, or a cancel event.)
2024-06-13 18:11:45 +02:00
2023-12-07 15:53:49 +01:00
// Populate select items
2024-04-03 19:19:08 +02:00
m_select_items . clear ( ) ;
u32 id_counter = 1 ;
2023-12-07 15:53:49 +01:00
for ( auto const & child : children_as_vector ( ) ) {
2025-02-03 16:57:27 +01:00
if ( auto const * opt_group_element = as_if < HTMLOptGroupElement > ( * child ) ) {
if ( ! opt_group_element - > has_attribute ( Web : : HTML : : AttributeNames : : hidden ) ) {
Vector < SelectItemOption > option_group_items ;
for ( auto const & child : opt_group_element - > children_as_vector ( ) ) {
if ( auto const & option_element = as_if < HTMLOptionElement > ( * child ) ) {
if ( ! option_element - > has_attribute ( Web : : HTML : : AttributeNames : : hidden ) )
2025-07-28 09:03:03 -04:00
option_group_items . append ( SelectItemOption { id_counter + + , option_element - > selected ( ) , option_element - > disabled ( ) , option_element , MUST ( Infra : : strip_and_collapse_whitespace ( option_element - > label ( ) ) ) , option_element - > value ( ) . to_utf8_but_should_be_ported_to_utf16 ( ) } ) ;
2025-02-03 16:57:27 +01:00
}
2023-12-07 15:53:49 +01:00
}
2025-02-03 16:57:27 +01:00
m_select_items . append ( SelectItemOptionGroup { opt_group_element - > get_attribute ( AttributeNames : : label ) . value_or ( String { } ) , option_group_items } ) ;
2023-12-07 15:53:49 +01:00
}
}
2025-02-03 16:57:27 +01:00
if ( auto const & option_element = as_if < HTMLOptionElement > ( * child ) ) {
if ( ! option_element - > has_attribute ( Web : : HTML : : AttributeNames : : hidden ) )
2025-07-28 09:03:03 -04:00
m_select_items . append ( SelectItemOption { id_counter + + , option_element - > selected ( ) , option_element - > disabled ( ) , option_element , MUST ( Infra : : strip_and_collapse_whitespace ( option_element - > label ( ) ) ) , option_element - > value ( ) . to_utf8_but_should_be_ported_to_utf16 ( ) } ) ;
2023-12-07 15:53:49 +01:00
}
2024-04-03 19:19:08 +02:00
2025-02-03 16:57:27 +01:00
if ( auto const * hr_element = as_if < HTMLHRElement > ( * child ) ) {
if ( ! hr_element - > has_attribute ( Web : : HTML : : AttributeNames : : hidden ) )
m_select_items . append ( SelectItemSeparator { } ) ;
}
2023-12-07 15:53:49 +01:00
}
// Request select dropdown
2025-10-16 11:13:54 +02:00
auto weak_element = GC : : Weak < HTMLSelectElement > { * this } ;
2023-12-07 15:53:49 +01:00
auto rect = get_bounding_client_rect ( ) ;
2025-03-21 19:38:12 -05:00
auto position = document ( ) . navigable ( ) - > to_top_level_position ( Web : : CSSPixelPoint { rect . x ( ) , rect . bottom ( ) } ) ;
document ( ) . page ( ) . did_request_select_dropdown ( weak_element , position , rect . width ( ) , m_select_items ) ;
2023-12-07 15:53:49 +01:00
set_is_open ( true ) ;
}
2024-06-13 18:11:45 +02:00
// https://html.spec.whatwg.org/multipage/input.html#dom-select-showpicker
WebIDL : : ExceptionOr < void > HTMLSelectElement : : show_picker ( )
{
// FIXME: Deduplicate with HTMLInputElement
// The showPicker() method steps are:
// 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
2025-08-10 00:29:35 +02:00
if ( ! is_mutable ( ) )
2025-08-07 19:31:52 -04:00
return WebIDL : : InvalidStateError : : create ( realm ( ) , " Element is not mutable " _utf16 ) ;
2024-06-13 18:11:45 +02:00
// 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
2025-05-25 16:47:37 +12:00
// and this is a select element, then throw a "SecurityError" DOMException.
if ( ! relevant_settings_object ( * this ) . origin ( ) . is_same_origin ( relevant_settings_object ( * this ) . top_level_origin . value ( ) ) ) {
2025-08-07 19:31:52 -04:00
return WebIDL : : SecurityError : : create ( realm ( ) , " Cross origin pickers are not allowed " _utf16 ) ;
2024-06-13 18:11:45 +02:00
}
// 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
auto & global_object = relevant_global_object ( * this ) ;
2025-01-26 18:41:47 +13:00
if ( ! as < HTML : : Window > ( global_object ) . has_transient_activation ( ) ) {
2025-08-07 19:31:52 -04:00
return WebIDL : : NotAllowedError : : create ( realm ( ) , " Too long since user activation to show picker " _utf16 ) ;
2024-06-13 18:11:45 +02:00
}
// FIXME: 4. If this is a select element, and this is not being rendered, then throw a "NotSupportedError" DOMException.
// 5. Show the picker, if applicable, for this.
show_the_picker_if_applicable ( ) ;
return { } ;
}
2024-06-13 18:36:18 +02:00
void HTMLSelectElement : : activation_behavior ( DOM : : Event const & event )
2024-06-13 18:11:45 +02:00
{
2024-06-13 18:36:18 +02:00
if ( event . is_trusted ( ) )
show_the_picker_if_applicable ( ) ;
2024-06-13 18:11:45 +02:00
}
2023-12-07 15:53:49 +01:00
2024-04-03 19:19:08 +02:00
void HTMLSelectElement : : did_select_item ( Optional < u32 > const & id )
2023-12-07 15:53:49 +01:00
{
set_is_open ( false ) ;
2024-04-03 19:19:08 +02:00
if ( ! id . has_value ( ) )
return ;
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
for ( auto const & option_element : m_cached_list_of_options )
2024-04-03 19:19:08 +02:00
option_element - > set_selected ( false ) ;
for ( auto const & item : m_select_items ) {
if ( item . has < SelectItemOption > ( ) ) {
auto const & item_option = item . get < SelectItemOption > ( ) ;
if ( item_option . id = = * id )
item_option . option_element - > set_selected ( true ) ;
}
if ( item . has < SelectItemOptionGroup > ( ) ) {
auto item_option_group = item . get < SelectItemOptionGroup > ( ) ;
for ( auto const & item_option : item_option_group . items ) {
if ( item_option . id = = * id )
item_option . option_element - > set_selected ( true ) ;
}
}
2023-12-07 15:53:49 +01:00
}
2024-04-03 19:19:08 +02:00
update_inner_text_element ( ) ;
2025-12-07 16:37:54 -08:00
send_select_update_notifications ( ) ;
2023-12-07 15:53:49 +01:00
}
void HTMLSelectElement : : form_associated_element_was_inserted ( )
{
create_shadow_tree_if_needed ( ) ;
}
2025-07-22 00:58:28 +02:00
void HTMLSelectElement : : form_associated_element_attribute_changed ( FlyString const & name , Optional < String > const & , Optional < String > const & value , Optional < FlyString > const & )
2025-05-26 09:34:12 +02:00
{
if ( name = = HTML : : AttributeNames : : multiple ) {
// If the multiple attribute is absent then update the selectedness of the option elements.
if ( ! value . has_value ( ) ) {
update_selectedness ( ) ;
}
}
}
2024-12-20 16:35:12 +01:00
void HTMLSelectElement : : computed_properties_changed ( )
2023-12-20 18:21:32 +01:00
{
// Hide chevron icon when appearance is none
if ( m_chevron_icon_element ) {
2024-12-20 16:35:12 +01:00
auto appearance = computed_properties ( ) - > appearance ( ) ;
2025-02-05 12:55:02 +00:00
if ( appearance = = CSS : : Appearance : : None ) {
2023-12-20 18:21:32 +01:00
MUST ( m_chevron_icon_element - > style_for_bindings ( ) - > set_property ( CSS : : PropertyID : : Display , " none " _string ) ) ;
} else {
MUST ( m_chevron_icon_element - > style_for_bindings ( ) - > set_property ( CSS : : PropertyID : : Display , " block " _string ) ) ;
}
}
}
2023-12-07 15:53:49 +01:00
void HTMLSelectElement : : create_shadow_tree_if_needed ( )
{
2024-06-25 11:28:58 +02:00
if ( shadow_root ( ) )
2023-12-07 15:53:49 +01:00
return ;
2024-11-14 05:50:17 +13:00
auto shadow_root = realm ( ) . create < DOM : : ShadowRoot > ( document ( ) , * this , Bindings : : ShadowRootMode : : Closed ) ;
2026-02-02 09:49:39 +00:00
shadow_root - > set_user_agent_internal ( true ) ;
2023-12-07 15:53:49 +01:00
set_shadow_root ( shadow_root ) ;
auto border = DOM : : create_element ( document ( ) , HTML : : TagNames : : div , Namespace : : HTML ) . release_value_but_fixme_should_propagate_errors ( ) ;
2025-10-31 12:30:47 +00:00
border - > set_attribute_value ( HTML : : AttributeNames : : style , R " ~~~(
2023-12-07 15:53:49 +01:00
display : flex ;
2023-12-10 19:59:25 +01:00
align - items : center ;
2023-12-07 15:53:49 +01:00
height : 100 % ;
2025-10-31 12:30:47 +00:00
) ~ ~ ~ " _string);
2023-12-07 15:53:49 +01:00
MUST ( shadow_root - > append_child ( border ) ) ;
m_inner_text_element = DOM : : create_element ( document ( ) , HTML : : TagNames : : div , Namespace : : HTML ) . release_value_but_fixme_should_propagate_errors ( ) ;
2025-10-31 12:30:47 +00:00
m_inner_text_element - > set_attribute_value ( HTML : : AttributeNames : : style , R " ~~~(
2023-12-07 15:53:49 +01:00
flex : 1 ;
2025-10-31 12:30:47 +00:00
) ~ ~ ~ " _string);
2023-12-07 15:53:49 +01:00
MUST ( border - > append_child ( * m_inner_text_element ) ) ;
2023-12-20 18:21:32 +01:00
m_chevron_icon_element = DOM : : create_element ( document ( ) , HTML : : TagNames : : div , Namespace : : HTML ) . release_value_but_fixme_should_propagate_errors ( ) ;
2025-10-31 12:30:47 +00:00
m_chevron_icon_element - > set_attribute_value ( HTML : : AttributeNames : : style , R " ~~~(
2023-12-10 19:59:25 +01:00
width : 16 px ;
height : 16 px ;
margin - left : 4 px ;
2025-10-31 12:30:47 +00:00
) ~ ~ ~ " _string);
auto chevron_svg_element = DOM : : create_element ( document ( ) , SVG : : TagNames : : svg , Namespace : : SVG ) . release_value_but_fixme_should_propagate_errors ( ) ;
chevron_svg_element - > set_attribute_value ( SVG : : AttributeNames : : xmlns , Namespace : : SVG . to_string ( ) ) ;
chevron_svg_element - > set_attribute_value ( SVG : : AttributeNames : : viewBox , " 0 0 24 24 " _string ) ;
MUST ( m_chevron_icon_element - > append_child ( chevron_svg_element ) ) ;
auto chevron_path_element = DOM : : create_element ( document ( ) , SVG : : TagNames : : path , Namespace : : SVG ) . release_value_but_fixme_should_propagate_errors ( ) ;
chevron_path_element - > set_attribute_value ( SVG : : AttributeNames : : fill , " currentcolor " _string ) ;
chevron_path_element - > set_attribute_value ( SVG : : AttributeNames : : d , " M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z " _string ) ;
MUST ( chevron_svg_element - > append_child ( chevron_path_element ) ) ;
2023-12-20 18:21:32 +01:00
MUST ( border - > append_child ( * m_chevron_icon_element ) ) ;
2023-12-07 15:53:49 +01:00
update_inner_text_element ( ) ;
}
2024-11-16 22:14:42 +01:00
void HTMLSelectElement : : update_inner_text_element ( Badge < HTMLOptionElement > )
{
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2024-11-16 22:14:42 +01:00
update_inner_text_element ( ) ;
}
// FIXME: This needs to be called any time the selected option's children are modified.
2023-12-07 15:53:49 +01:00
void HTMLSelectElement : : update_inner_text_element ( )
{
2024-01-15 22:41:24 +01:00
if ( ! m_inner_text_element )
return ;
2024-11-16 22:14:42 +01:00
// Update inner text element to the label of the selected option
2025-01-26 15:16:07 +13:00
for ( auto const & option_element : m_cached_list_of_options ) {
2023-12-07 15:53:49 +01:00
if ( option_element - > selected ( ) ) {
2025-10-31 12:30:47 +00:00
m_inner_text_element - > string_replace_all ( Infra : : strip_and_collapse_whitespace ( Utf16String : : from_utf8 ( option_element - > label ( ) ) ) ) ;
2023-12-07 15:53:49 +01:00
return ;
}
}
}
2024-07-25 21:13:22 +04:00
// https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
2025-12-07 16:37:54 -08:00
// https://whatpr.org/html/11890/form-elements.html#selectedness-setting-algorithm
2024-11-14 00:05:38 +01:00
void HTMLSelectElement : : update_selectedness ( )
2024-07-25 21:13:22 +04:00
{
2025-12-07 16:37:54 -08:00
// The selectedness setting algorithm, given a select element element, is to run the following steps:
2025-01-26 15:16:07 +13:00
update_cached_list_of_options ( ) ;
2025-01-26 13:59:45 +13:00
2025-12-07 16:37:54 -08:00
// 1. Let updateSelectedcontent be false.
auto should_update_selectedcontent = false ;
// 2. If element 's multiple attribute is absent, and element's display size is 1,
// and no option elements in the element's list of options have their selectedness set to true, then
if ( ! has_attribute ( AttributeNames : : multiple ) & & display_size ( ) = = 1 & & m_cached_number_of_selected_options = = 0 ) {
// 1. Set the selectedness of the first option element in the list of options in tree order
// that is not disabled, if any, to true.
for ( auto const & option_element : m_cached_list_of_options ) {
if ( ! option_element - > disabled ( ) ) {
option_element - > set_selected_internal ( true ) ;
break ;
2024-07-25 21:13:22 +04:00
}
}
2025-12-07 16:37:54 -08:00
// 2. Set updateSelectedcontent to true.
should_update_selectedcontent = true ;
}
// Otherwise, if element's multiple attribute is absent,
// and two or more option elements in element's list of options have their selectedness set to true, then:
else if ( ! has_attribute ( AttributeNames : : multiple ) & & m_cached_number_of_selected_options > = 2 ) {
// 1. Set the selectedness of all but the last option element with its selectedness set to true
// in the list of options in tree order to false.
2024-11-15 04:01:23 +13:00
GC : : Ptr < HTML : : HTMLOptionElement > last_selected_option ;
2024-11-14 00:05:38 +01:00
u64 last_selected_option_update_index = 0 ;
2025-01-26 15:16:07 +13:00
for ( auto const & option_element : m_cached_list_of_options ) {
2024-11-14 00:05:38 +01:00
if ( ! option_element - > selected ( ) )
2024-10-27 16:39:07 +01:00
continue ;
2024-11-14 00:05:38 +01:00
if ( ! last_selected_option
| | option_element - > selectedness_update_index ( ) > last_selected_option_update_index ) {
last_selected_option = option_element ;
last_selected_option_update_index = option_element - > selectedness_update_index ( ) ;
2024-07-25 21:13:22 +04:00
}
2024-11-14 00:05:38 +01:00
}
2025-01-26 15:16:07 +13:00
for ( auto const & option_element : m_cached_list_of_options ) {
2024-11-14 00:05:38 +01:00
if ( option_element ! = last_selected_option )
option_element - > set_selected_internal ( false ) ;
2024-07-25 21:13:22 +04:00
}
2025-12-07 16:37:54 -08:00
// 2. Set updateSelectedcontent to true.
should_update_selectedcontent = true ;
}
// 4. If updateSelectedcontent is true, then run update a select's selectedcontent given element.
if ( should_update_selectedcontent ) {
MUST ( update_selectedcontent ( ) ) ;
update_inner_text_element ( ) ;
2024-07-25 21:13:22 +04:00
}
}
2024-11-02 19:17:20 +01:00
bool HTMLSelectElement : : is_focusable ( ) const
{
return enabled ( ) ;
}
2025-02-02 20:41:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#placeholder-label-option
HTMLOptionElement * HTMLSelectElement : : placeholder_label_option ( ) const
{
// If a select element has a required attribute specified, does not have a multiple attribute specified, and has a display size of 1;
if ( has_attribute ( HTML : : AttributeNames : : required ) & & ! has_attribute ( HTML : : AttributeNames : : multiple ) & & display_size ( ) = = 1 ) {
// and if the value of the first option element in the
// select element's list of options (if any) is the empty string, and that option element's parent node is the select element (and not an optgroup element), then that option is the
// select element's placeholder label option.
auto first_option_element = list_of_options ( ) [ 0 ] ;
if ( first_option_element - > value ( ) . is_empty ( ) & & first_option_element - > parent ( ) = = this )
return first_option_element ;
}
return { } ;
}
2025-12-07 16:37:54 -08:00
// https://html.spec.whatwg.org/multipage/form-elements.html#select-enabled-selectedcontent
GC : : Ptr < HTMLSelectedContentElement > HTMLSelectElement : : enabled_selectedcontent ( ) const
{
// To get a select's enabled selectedcontent given a select element select:
// 1. If select has the multiple attribute, then return null.
if ( has_attribute ( AttributeNames : : multiple ) )
return nullptr ;
// 2. Let selectedcontent be the first selectedcontent element descendant of select in tree order if any such
// element exists; otherwise return null.
GC : : Ptr < HTMLSelectedContentElement > selectedcontent ;
for_each_in_subtree_of_type < HTMLSelectedContentElement > ( [ & ] ( auto & element ) {
selectedcontent = const_cast < HTMLSelectedContentElement * > ( & element ) ;
return TraversalDecision : : Break ;
} ) ;
if ( ! selectedcontent )
return nullptr ;
// 3. If selectedcontent is disabled, then return null.
if ( selectedcontent - > disabled ( ) )
return nullptr ;
// 4. Return selectedcontent.
return selectedcontent ;
}
// https://html.spec.whatwg.org/multipage/form-elements.html#clear-a-select%27s-non-primary-selectedcontent-elements
void HTMLSelectElement : : clear_non_primary_selectedcontent ( )
{
// To clear a select's non-primary selectedcontent elements, given a select element select:
// 1. Let passedFirstSelectedcontent be false.
bool passed_first_selectedcontent = false ;
// 2. For each descendant of select's descendants in tree order that is a selectedcontent element:
for_each_in_subtree_of_type < HTMLSelectedContentElement > ( [ & ] ( auto & element ) {
// 1. If passedFirstSelectedcontent is false, then set passedFirstSelectedcontent to true.
if ( ! passed_first_selectedcontent )
passed_first_selectedcontent = true ;
// 2. Otherwise, run clear a selectedcontent given descendant.
else
element . clear_selectedcontent ( ) ;
return TraversalDecision : : Continue ;
} ) ;
}
// https://html.spec.whatwg.org/multipage/form-elements.html#update-a-select%27s-selectedcontent
WebIDL : : ExceptionOr < void > HTMLSelectElement : : update_selectedcontent ( )
{
// To update a select's selectedcontent given a select element select:
// 1. Let selectedcontent be the result of get a select's enabled selectedcontent given select.
auto selectedcontent = enabled_selectedcontent ( ) ;
// 2. If selectedcontent is null, then return.
if ( ! selectedcontent )
return { } ;
// 3. Let option be the first option in select's list of options whose selectedness is true,
// if any such option exists, otherwise null.
update_cached_list_of_options ( ) ;
GC : : Ptr < HTML : : HTMLOptionElement > option ;
for ( auto const & candidate : m_cached_list_of_options ) {
if ( candidate - > selected ( ) ) {
option = candidate ;
break ;
}
}
// 4. If option is null, then run clear a selectedcontent given selectedcontent.
if ( ! option ) {
selectedcontent - > clear_selectedcontent ( ) ;
return { } ;
}
// 5. Otherwise, run clone an option into a selectedcontent given option and selectedcontent.
TRY ( option - > clone_into_selectedcontent ( * selectedcontent ) ) ;
return { } ;
}
2025-02-02 20:41:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element%3Asuffering-from-being-missing
bool HTMLSelectElement : : suffering_from_being_missing ( ) const
{
// If the element has its required attribute specified, and either none of the option elements in the select element's list of options have their selectedness
// set to true, or the only option element in the select element's list of options with its selectedness set to true is the placeholder label option, then the element is suffering from being
// missing.
2025-02-06 11:14:55 +00:00
auto selected_options = this - > selected_options ( ) ;
return has_attribute ( HTML : : AttributeNames : : required ) & & ( selected_options - > length ( ) = = 0 | | ( selected_options - > length ( ) = = 1 & & selected_options - > item ( 0 ) = = placeholder_label_option ( ) ) ) ;
2025-02-02 20:41:23 +01:00
}
2025-08-10 00:29:35 +02:00
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-fe-mutable
bool HTMLSelectElement : : is_mutable ( ) const
{
// A select element that is not disabled is mutable.
return enabled ( ) ;
}
2020-08-01 03:07:00 +01:00
}