| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/RadioNodeListPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  | #include <LibWeb/HTML/HTMLInputElement.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  | #include <LibWeb/HTML/RadioNodeList.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(RadioNodeList); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<RadioNodeList> RadioNodeList::create(JS::Realm& realm, DOM::Node const& root, Scope scope, Function<bool(DOM::Node const&)> filter) | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<RadioNodeList>(realm, root, scope, move(filter)); | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  | RadioNodeList::RadioNodeList(JS::Realm& realm, DOM::Node const& root, Scope scope, Function<bool(DOM::Node const&)> filter) | 
					
						
							|  |  |  |     : DOM::LiveNodeList(realm, root, scope, move(filter)) | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RadioNodeList::~RadioNodeList() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RadioNodeList::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(RadioNodeList); | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  | static HTMLInputElement const* radio_button(DOM::Node const& node) | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |     if (!is<HTMLInputElement>(node)) | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |     auto const& input_element = verify_cast<HTMLInputElement>(node); | 
					
						
							|  |  |  |     if (input_element.type_state() != HTMLInputElement::TypeAttributeState::RadioButton) | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return &input_element; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-radionodelist-value
 | 
					
						
							|  |  |  | FlyString RadioNodeList::value() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. Let element be the first element in tree order represented by the RadioNodeList object that is an input element whose type
 | 
					
						
							|  |  |  |     //    attribute is in the Radio Button state and whose checkedness is true. Otherwise, let it be null.
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |     auto* element = verify_cast<HTMLInputElement>(first_matching([](DOM::Node const& node) -> bool { | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |         auto const* button = radio_button(node); | 
					
						
							|  |  |  |         if (!button) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return button->checked(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. If element is null, return the empty string.
 | 
					
						
							|  |  |  |     if (!element) | 
					
						
							|  |  |  |         return String {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. If element is an element with no value attribute, return the string "on".
 | 
					
						
							|  |  |  |     // 4. Otherwise, return the value of element's value attribute.
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |     return element->get_attribute(AttributeNames::value).value_or("on"_string); | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RadioNodeList::set_value(FlyString const& value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |     HTMLInputElement* element = nullptr; | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 1. If the new value is the string "on": let element be the first element in tree order represented by the RadioNodeList object
 | 
					
						
							|  |  |  |     //    that is an input element whose type attribute is in the Radio Button state and whose value content attribute is either absent,
 | 
					
						
							|  |  |  |     //    or present and equal to the new value, if any. If no such element exists, then instead let element be null.
 | 
					
						
							|  |  |  |     if (value == "on"sv) { | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |         element = verify_cast<HTMLInputElement>(first_matching([&value](auto const& node) { | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |             auto const* button = radio_button(node); | 
					
						
							|  |  |  |             if (!button) | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |             auto const maybe_value = button->get_attribute(AttributeNames::value); | 
					
						
							| 
									
										
										
										
											2023-10-01 17:46:26 +13:00
										 |  |  |             return !maybe_value.has_value() || maybe_value.value() == value; | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // 2. Otherwise: let element be the first element in tree order represented by the RadioNodeList object that is an input element whose
 | 
					
						
							| 
									
										
										
										
											2023-10-01 17:46:26 +13:00
										 |  |  |     //    type attribute is in the Radio Button state and whose value content attribute is present and equal to the new value, if any. If
 | 
					
						
							|  |  |  |     //    no such element exists, then instead let element be null.
 | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |         element = verify_cast<HTMLInputElement>(first_matching([&value](auto const& node) { | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |             auto const* button = radio_button(node); | 
					
						
							|  |  |  |             if (!button) | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 09:22:13 -04:00
										 |  |  |             auto const maybe_value = button->get_attribute(AttributeNames::value); | 
					
						
							| 
									
										
										
										
											2023-10-01 17:46:26 +13:00
										 |  |  |             return maybe_value.has_value() && maybe_value.value() == value; | 
					
						
							| 
									
										
										
										
											2023-08-25 12:28:36 +12:00
										 |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. If element is not null, then set its checkedness to true.
 | 
					
						
							|  |  |  |     if (element) | 
					
						
							|  |  |  |         element->set_checked(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 14:39:57 +12:00
										 |  |  | } |