From 8042558805d6a9d9ec822ab89c59f8fc51c26d1c Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 13 Feb 2024 22:37:16 +0000 Subject: [PATCH] LibWeb: Implement `HTMLDialogElement::close()` for non-modal dialogs --- .../Text/expected/close-non-modal-dialog.txt | 3 ++ .../Text/input/close-non-modal-dialog.html | 14 +++++++ .../LibWeb/HTML/HTMLDialogElement.cpp | 42 ++++++++++++++++++- .../Libraries/LibWeb/HTML/HTMLDialogElement.h | 2 + 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/close-non-modal-dialog.txt create mode 100644 Tests/LibWeb/Text/input/close-non-modal-dialog.html diff --git a/Tests/LibWeb/Text/expected/close-non-modal-dialog.txt b/Tests/LibWeb/Text/expected/close-non-modal-dialog.txt new file mode 100644 index 0000000000..e1e7fb329e --- /dev/null +++ b/Tests/LibWeb/Text/expected/close-non-modal-dialog.txt @@ -0,0 +1,3 @@ + dialog.open state before close(): true +dialog.open state after close(): false +dialog.returnValue state after close(): after-close diff --git a/Tests/LibWeb/Text/input/close-non-modal-dialog.html b/Tests/LibWeb/Text/input/close-non-modal-dialog.html new file mode 100644 index 0000000000..21ee6eb268 --- /dev/null +++ b/Tests/LibWeb/Text/input/close-non-modal-dialog.html @@ -0,0 +1,14 @@ + + + diff --git a/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp index 3607098a96..b3535fc291 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp @@ -5,6 +5,7 @@ */ #include +#include #include namespace Web::HTML { @@ -37,9 +38,11 @@ void HTMLDialogElement::show_modal() } // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-close -void HTMLDialogElement::close(Optional) +void HTMLDialogElement::close(Optional return_value) { - dbgln("(STUBBED) HTMLDialogElement::close()"); + // 1. If returnValue is not given, then set it to null. + // 2. Close the dialog this with returnValue. + close_the_dialog(move(return_value)); } // https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue @@ -54,4 +57,39 @@ void HTMLDialogElement::set_return_value(String return_value) m_return_value = move(return_value); } +// https://html.spec.whatwg.org/multipage/interactive-elements.html#close-the-dialog +void HTMLDialogElement::close_the_dialog(Optional result) +{ + // 1. If subject does not have an open attribute, then return. + if (!has_attribute(AttributeNames::open)) + return; + + // 2. Remove subject's open attribute. + remove_attribute(AttributeNames::open); + + // FIXME: 3. If the is modal flag of subject is true, then request an element to be removed from the top layer given subject. + // FIXME: 4. Let wasModal be the value of subject's is modal flag. + // FIXME: 5. Set the is modal flag of subject to false. + + // 6. If result is not null, then set the returnValue attribute to result. + if (result.has_value()) + set_return_value(result.release_value()); + + // FIXME: 7. If subject's previously focused element is not null, then: + // 1. Let element be subject's previously focused element. + // 2. Set subject's previously focused element to null. + // 3. If subject's node document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of element, + // or wasModal is true, then run the focusing steps for element; the viewport should not be scrolled by doing this step. + + // 8. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject. + queue_an_element_task(HTML::Task::Source::UserInteraction, [this] { + auto close_event = DOM::Event::create(realm(), HTML::EventNames::close); + dispatch_event(close_event); + }); + + // FIXME: 9. If subject's close watcher is not null, then: + // 1. Destroy subject's close watcher. + // 2. Set subject's close watcher to null. +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.h b/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.h index 1780e3a604..ebc3965e8e 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.h @@ -33,6 +33,8 @@ private: virtual void initialize(JS::Realm&) override; + void close_the_dialog(Optional result); + String m_return_value; };