2020-08-01 03:07:00 +01:00
/*
2021-04-28 22:46:44 +02:00
* Copyright ( c ) 2020 , the SerenityOS developers .
2022-03-20 16:13:23 +01:00
* Copyright ( c ) 2021 - 2022 , Andreas Kling < kling @ serenityos . org >
2023-12-07 15:53:49 +01:00
* Copyright ( c ) 2023 , Bastiaan van der Plaat < bastiaan . v . d . plaat @ gmail . com >
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
*/
2022-09-30 17:16:16 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2023-12-07 15:53:49 +01:00
# include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
# 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>
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>
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>
2020-08-01 03:07:00 +01:00
namespace Web : : HTML {
2023-11-19 19:47:52 +01:00
JS_DEFINE_ALLOCATOR ( HTMLSelectElement ) ;
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
{
}
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
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( HTMLSelectElement ) ;
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 ) ;
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 ) ;
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-03-07 21:27:37 +01:00
void HTMLSelectElement : : adjust_computed_style ( CSS : : StyleProperties & style )
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 ) ) ) ;
2022-09-01 20:50:16 +02:00
}
2022-03-16 13:08:12 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
2022-09-01 20:50:16 +02:00
JS : : GCPtr < HTMLOptionsCollection > const & HTMLSelectElement : : options ( )
2022-03-16 13:08:12 +01:00
{
if ( ! m_options ) {
m_options = HTMLOptionsCollection : : create ( * this , [ ] ( DOM : : Element const & element ) {
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
// The list of options for a select element consists of all the option element children of
// the select element, and all the option element children of all the optgroup element children
// of the select element, in tree order.
return is < HTMLOptionElement > ( element ) ;
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
size_t HTMLSelectElement : : length ( )
{
// 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 ( ) ;
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
DOM : : Element * HTMLSelectElement : : item ( size_t index )
{
// 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.
return const_cast < HTMLOptionsCollection & > ( * options ( ) ) . item ( index ) ;
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
2023-08-12 21:30:19 +12:00
DOM : : Element * 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.
2023-08-12 21:30:19 +12:00
return 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.
2022-09-01 20:50:16 +02:00
return const_cast < HTMLOptionsCollection & > ( * options ( ) ) . add ( move ( element ) , move ( before ) ) ;
2022-03-21 20:03:37 -04:00
}
2022-03-20 16:13:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
2022-08-28 13:42:07 +02:00
Vector < JS : : Handle < HTMLOptionElement > > HTMLSelectElement : : list_of_options ( ) const
2022-03-20 16:13:23 +01:00
{
// The list of options for a select element consists of all the option element children of the select element,
// and all the option element children of all the optgroup element children of the select element, in tree order.
2022-08-28 13:42:07 +02:00
Vector < JS : : Handle < HTMLOptionElement > > list ;
2022-03-20 16:13:23 +01:00
2023-02-25 10:44:31 -07:00
for_each_child_of_type < HTMLOptionElement > ( [ & ] ( HTMLOptionElement & option_element ) {
list . append ( JS : : make_handle ( option_element ) ) ;
2022-03-20 16:13:23 +01:00
} ) ;
for_each_child_of_type < HTMLOptGroupElement > ( [ & ] ( HTMLOptGroupElement const & optgroup_element ) {
2023-02-25 10:44:31 -07:00
optgroup_element . for_each_child_of_type < HTMLOptionElement > ( [ & ] ( HTMLOptionElement & option_element ) {
list . append ( JS : : make_handle ( option_element ) ) ;
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 ( )
{
// The reset algorithm for select elements is to go through all the option elements in the element's list of options,
for ( auto const & option_element : list_of_options ( ) ) {
// set their selectedness to true if the option element has a selected attribute, and false otherwise,
option_element - > m_selected = option_element - > has_attribute ( AttributeNames : : selected ) ;
// set their dirtiness to false,
option_element - > m_dirty = false ;
// and then have the option elements ask for a reset.
option_element - > ask_for_a_reset ( ) ;
}
}
2022-03-20 16:13:23 +01:00
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
int HTMLSelectElement : : selected_index ( ) const
{
// The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
// in tree order that has its selectedness set to true, if any. If there isn't one, then it must return − 1.
int index = 0 ;
for ( auto const & option_element : 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 ;
}
void HTMLSelectElement : : set_selected_index ( int index )
{
// On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
// and then the option element in the list of options whose index is the given new value,
// if any, must have its selectedness set to true and its dirtiness set to true.
auto options = list_of_options ( ) ;
for ( auto & option : options )
2022-08-28 13:42:07 +02:00
option - > m_selected = false ;
2022-03-20 16:13:23 +01:00
if ( index < 0 | | index > = static_cast < int > ( options . size ( ) ) )
return ;
auto & selected_option = options [ index ] ;
2022-08-28 13:42:07 +02:00
selected_option - > m_selected = true ;
selected_option - > m_dirty = true ;
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 ;
}
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 ;
2023-09-21 20:26:19 +12:00
if ( has_attribute ( AttributeNames : : size ) ) {
2024-01-16 19:04:45 +01: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
}
2023-12-07 15:53:49 +01:00
String HTMLSelectElement : : value ( ) const
{
for ( auto const & option_element : list_of_options ( ) )
if ( option_element - > selected ( ) )
return option_element - > value ( ) ;
return " " _string ;
}
WebIDL : : ExceptionOr < void > HTMLSelectElement : : set_value ( String const & value )
{
for ( auto const & option_element : list_of_options ( ) )
option_element - > set_selected ( option_element - > value ( ) = = value ) ;
update_inner_text_element ( ) ;
2024-04-03 19:19:08 +02:00
queue_input_and_change_events ( ) ;
return { } ;
}
2023-12-07 15:53:49 +01:00
2024-04-03 19:19:08 +02:00
void HTMLSelectElement : : queue_input_and_change_events ( )
{
2023-12-07 15:53:49 +01:00
// When the user agent is to send select update notifications, queue an element task on the user interaction task source given the select element to run these steps:
queue_an_element_task ( HTML : : Task : : Source : : UserInteraction , [ this ] {
// FIXME: 1. Set the select element's user interacted to true.
// 2. Fire an event named input at the select element, with the bubbles and composed attributes initialized to true.
auto input_event = DOM : : Event : : create ( realm ( ) , HTML : : EventNames : : input ) ;
input_event - > set_bubbles ( true ) ;
input_event - > set_composed ( true ) ;
dispatch_event ( input_event ) ;
// 3. Fire an event named change at the select element, with the bubbles attribute initialized to true.
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 ;
invalidate_style ( ) ;
}
2023-12-07 15:53:49 +01:00
bool HTMLSelectElement : : has_activation_behavior ( ) const
{
return true ;
}
2024-04-03 19:19:08 +02:00
static String strip_newlines ( Optional < String > string )
2023-12-07 15:53:49 +01:00
{
// FIXME: Move this to a more general function
if ( ! string . has_value ( ) )
return { } ;
StringBuilder builder ;
for ( auto c : string . value ( ) . bytes_as_string_view ( ) ) {
if ( c = = ' \r ' | | c = = ' \n ' ) {
builder . append ( ' ' ) ;
} else {
builder . append ( c ) ;
}
}
return MUST ( Infra : : strip_and_collapse_whitespace ( MUST ( builder . to_string ( ) ) ) ) ;
}
void HTMLSelectElement : : activation_behavior ( DOM : : Event const & )
{
// 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 ( ) ) {
if ( is < HTMLOptGroupElement > ( * child ) ) {
auto & opt_group_element = verify_cast < HTMLOptGroupElement > ( * child ) ;
2024-04-03 19:19:08 +02:00
Vector < SelectItemOption > option_group_items ;
2023-12-07 15:53:49 +01:00
for ( auto const & child : opt_group_element . children_as_vector ( ) ) {
if ( is < HTMLOptionElement > ( * child ) ) {
auto & option_element = verify_cast < HTMLOptionElement > ( * child ) ;
2024-04-03 19:20:15 +02:00
option_group_items . append ( SelectItemOption { id_counter + + , strip_newlines ( option_element . text_content ( ) ) , option_element . value ( ) , option_element . selected ( ) , option_element . disabled ( ) , option_element } ) ;
2023-12-07 15:53:49 +01:00
}
}
2024-04-03 19:19:08 +02: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
}
if ( is < HTMLOptionElement > ( * child ) ) {
auto & option_element = verify_cast < HTMLOptionElement > ( * child ) ;
2024-04-03 19:20:15 +02:00
m_select_items . append ( SelectItemOption { id_counter + + , strip_newlines ( option_element . text_content ( ) ) , option_element . value ( ) , option_element . selected ( ) , option_element . disabled ( ) , option_element } ) ;
2023-12-07 15:53:49 +01:00
}
2024-04-03 19:19:08 +02:00
if ( is < HTMLHRElement > ( * child ) )
m_select_items . append ( SelectItemSeparator { } ) ;
2023-12-07 15:53:49 +01:00
}
// Request select dropdown
auto weak_element = make_weak_ptr < HTMLSelectElement > ( ) ;
auto rect = get_bounding_client_rect ( ) ;
2024-02-04 03:36:24 -07:00
auto position = document ( ) . navigable ( ) - > to_top_level_position ( Web : : CSSPixelPoint { rect - > x ( ) , rect - > y ( ) } ) ;
2024-04-03 19:19:08 +02:00
document ( ) . page ( ) . did_request_select_dropdown ( weak_element , position , CSSPixels ( rect - > width ( ) ) , m_select_items ) ;
2023-12-07 15:53:49 +01:00
set_is_open ( true ) ;
}
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 ;
for ( auto const & option_element : list_of_options ( ) )
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 ( ) ;
queue_input_and_change_events ( ) ;
2023-12-07 15:53:49 +01:00
}
void HTMLSelectElement : : form_associated_element_was_inserted ( )
{
create_shadow_tree_if_needed ( ) ;
// Wait until children are ready
queue_an_element_task ( HTML : : Task : : Source : : Microtask , [ this ] {
// Select first option when no other option is selected
if ( selected_index ( ) = = - 1 ) {
auto options = list_of_options ( ) ;
if ( options . size ( ) > 0 ) {
options . at ( 0 ) - > set_selected ( true ) ;
}
}
2024-01-29 17:52:30 +00:00
update_inner_text_element ( ) ;
2023-12-07 15:53:49 +01:00
} ) ;
}
void HTMLSelectElement : : form_associated_element_was_removed ( DOM : : Node * )
{
set_shadow_root ( nullptr ) ;
}
2023-12-20 18:21:32 +01:00
void HTMLSelectElement : : computed_css_values_changed ( )
{
// Hide chevron icon when appearance is none
if ( m_chevron_icon_element ) {
auto appearance = computed_css_values ( ) - > appearance ( ) ;
if ( appearance . has_value ( ) & & * appearance = = CSS : : Appearance : : None ) {
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 ( )
{
if ( shadow_root_internal ( ) )
return ;
auto shadow_root = heap ( ) . allocate < DOM : : ShadowRoot > ( realm ( ) , document ( ) , * this , Bindings : : ShadowRootMode : : Closed ) ;
set_shadow_root ( shadow_root ) ;
auto border = DOM : : create_element ( document ( ) , HTML : : TagNames : : div , Namespace : : HTML ) . release_value_but_fixme_should_propagate_errors ( ) ;
MUST ( border - > set_attribute ( HTML : : AttributeNames : : style , R " ~~~(
display : flex ;
2023-12-10 19:59:25 +01:00
align - items : center ;
2023-12-07 15:53:49 +01:00
height : 100 % ;
2023-12-10 19:59:25 +01: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 ( ) ;
MUST ( m_inner_text_element - > set_attribute ( HTML : : AttributeNames : : style , R " ~~~(
flex : 1 ;
2023-12-10 19:59:25 +01:00
) ~ ~ ~ " _string));
2023-12-07 15:53:49 +01:00
MUST ( border - > append_child ( * m_inner_text_element ) ) ;
// FIXME: Find better way to add chevron icon
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 ( ) ;
MUST ( m_chevron_icon_element - > set_attribute ( HTML : : AttributeNames : : style , R " ~~~(
2023-12-10 19:59:25 +01:00
width : 16 px ;
height : 16 px ;
margin - left : 4 px ;
) ~ ~ ~ " _string));
2023-12-20 18:21:32 +01:00
MUST ( m_chevron_icon_element - > set_inner_html ( " <svg xmlns= \" http://www.w3.org/2000/svg \" viewBox= \" 0 0 24 24 \" ><path d= \" M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z \" /></svg> " sv ) ) ;
MUST ( border - > append_child ( * m_chevron_icon_element ) ) ;
2023-12-07 15:53:49 +01:00
update_inner_text_element ( ) ;
}
void HTMLSelectElement : : update_inner_text_element ( )
{
2024-01-15 22:41:24 +01:00
if ( ! m_inner_text_element )
return ;
2023-12-07 15:53:49 +01:00
// Update inner text element to text content of selected option
for ( auto const & option_element : list_of_options ( ) ) {
if ( option_element - > selected ( ) ) {
m_inner_text_element - > set_text_content ( strip_newlines ( option_element - > text_content ( ) ) ) ;
return ;
}
}
}
2020-08-01 03:07:00 +01:00
}