LibWeb: Implement the request-close command

See: https://github.com/whatwg/html/pull/11045
This commit is contained in:
Gingeh 2025-06-04 15:35:43 +10:00 committed by Tim Ledbetter
commit 9ef9f8d38f
Notes: github-actions[bot] 2025-06-07 03:07:08 +00:00
5 changed files with 358 additions and 13 deletions

View file

@ -289,9 +289,10 @@ String HTMLButtonElement::command() const
// show-popover Show Popover Shows the targeted popover element.
// hide-popover Hide Popover Hides the targeted popover element.
// close Close Closes the targeted dialog element.
// request-close Request Close Requests to close the targeted dialog element.
// show-modal Show Modal Opens the targeted dialog element as modal.
// A custom command keyword Custom Only dispatches the command event on the targeted element.
Array valid_values { "toggle-popover"_string, "show-popover"_string, "hide-popover"_string, "close"_string, "show-modal"_string };
Array valid_values { "toggle-popover"_string, "show-popover"_string, "hide-popover"_string, "close"_string, "request-close"_string, "show-modal"_string };
// 2. If command is in the Custom state, then return command's value.
// A custom command keyword is a string that starts with "--".

View file

@ -285,11 +285,14 @@ void HTMLDialogElement::close(Optional<String> return_value)
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-requestclose
void HTMLDialogElement::request_close(Optional<String> return_value)
{
// AD-HOC: This method is an amalgamation of the requestClose() and "request to close" algorithms from the spec.
// FIXME: Once the "request-close" command is implemented, this will need to be split into two methods.
// For now this implementation is only used for the requestClose() method, which sets source to null.
auto source = nullptr;
// 1. If returnValue is not given, then set it to null.
// 2. Request to close the dialog this with returnValue and null.
request_close_the_dialog(move(return_value), nullptr);
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-request-close
void HTMLDialogElement::request_close_the_dialog(Optional<String> return_value, GC::Ptr<DOM::Element> source)
{
// 1. If this does not have an open attribute, then return.
if (!has_attribute(AttributeNames::open))
return;
@ -470,8 +473,8 @@ void HTMLDialogElement::set_is_modal(bool is_modal)
// https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element:is-valid-invoker-command-steps
bool HTMLDialogElement::is_valid_invoker_command(String& command)
{
// 1. If command is in the Close state or in the Show Modal state, then return true.
if (command == "close" || command == "show-modal")
// 1. If command is in the Close state, the Request Close state, or the Show Modal state, then return true.
if (command == "close" || command == "request-close" || command == "show-modal")
return true;
// 2. Return false.
@ -494,7 +497,15 @@ void HTMLDialogElement::invoker_command_steps(DOM::Element& invoker, String& com
close_the_dialog(optional_value, invoker);
}
// 3. If command is the Show Modal state and element does not have an open attribute, then show a modal dialog given element and invoker.
// 3. If command is in the Request Close state and element has an open attribute,
// then request to close the dialog element with invoker's optional value and invoker.
if (command == "request-close" && has_attribute(AttributeNames::open)) {
// FIXME: This assumes invoker is a button.
auto optional_value = invoker.get_attribute(AttributeNames::value);
request_close_the_dialog(optional_value, invoker);
}
// 4. If command is the Show Modal state and element does not have an open attribute, then show a modal dialog given element and invoker.
if (command == "show-modal" && !has_attribute(AttributeNames::open)) {
MUST(show_a_modal_dialog(*this, invoker));
}

View file

@ -29,6 +29,7 @@ public:
static WebIDL::ExceptionOr<void> show_a_modal_dialog(HTMLDialogElement&, GC::Ptr<DOM::Element> source);
void close_the_dialog(Optional<String> result, GC::Ptr<DOM::Element> source);
void request_close_the_dialog(Optional<String> return_value, GC::Ptr<DOM::Element> source);
WebIDL::ExceptionOr<void> show();
WebIDL::ExceptionOr<void> show_modal();