ladybird/Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
Shannon Booth bad44f8fc9 LibWeb: Remove Bindings/Forward.h from LibWeb/Forward.h
This was resulting in a whole lot of rebuilding whenever a new IDL
interface was added.

Instead, just directly include the prototype in every C++ file which
needs it. While we only really need a forward declaration in each cpp
file; including the full prototype header (which itself only includes
LibJS/Object.h, which is already transitively brought in by
PlatformObject) - it seems like a small price to pay compared to what
feels like a full rebuild of LibWeb whenever a new IDL file is added.

Given all of these includes are only needed for the ::initialize
method, there is probably a smart way of avoiding this problem
altogether. I've considered both using some macro trickery or generating
these functions somehow instead.
2024-04-27 18:29:35 -04:00

191 lines
7.4 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/HTMLDialogElementPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/HTMLDialogElement.h>
namespace Web::HTML {
JS_DEFINE_ALLOCATOR(HTMLDialogElement);
HTMLDialogElement::HTMLDialogElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
{
}
HTMLDialogElement::~HTMLDialogElement() = default;
void HTMLDialogElement::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLDialogElement);
}
void HTMLDialogElement::removed_from(Node* old_parent)
{
HTMLElement::removed_from(old_parent);
// FIXME: 1. If removedNode's close watcher is not null, then:
// 1. Destroy removedNode's close watcher.
// 2. Set removedNode's close watcher to null.
// 2. If removedNode's node document's top layer contains removedNode, then remove an element from the top layer
// immediately given removedNode.
if (document().top_layer_elements().contains(*this))
document().remove_an_element_from_the_top_layer_immediately(*this);
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-show
WebIDL::ExceptionOr<void> HTMLDialogElement::show()
{
// 1. If this has an open attribute and the is modal flag of this is false, then return.
// FIXME: Add modal flag check here when modal dialog support is added
if (has_attribute(AttributeNames::open))
return {};
// FIXME: 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
// 3. Add an open attribute to this, whose value is the empty string.
TRY(set_attribute(AttributeNames::open, {}));
// FIXME 4. Set this's previously focused element to the focused element.
// FIXME 5. Run hide all popovers given this's node document.
// 6. Run the dialog focusing steps given this.
run_dialog_focusing_steps();
return {};
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-showmodal
WebIDL::ExceptionOr<void> HTMLDialogElement::show_modal()
{
// 1. If this has an open attribute and the is modal flag of this is true, then return.
if (has_attribute(AttributeNames::open) && m_is_modal)
return {};
// 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
if (has_attribute(AttributeNames::open))
return WebIDL::InvalidStateError::create(realm(), "Dialog already open"_fly_string);
// 3. If this is not connected, then throw an "InvalidStateError" DOMException.
if (!is_connected())
return WebIDL::InvalidStateError::create(realm(), "Dialog not connected"_fly_string);
// FIXME: 4. If this is in the popover showing state, then throw an "InvalidStateError" DOMException.
// 5. Add an open attribute to this, whose value is the empty string.
TRY(set_attribute(AttributeNames::open, {}));
// 6. Set the is modal flag of this to true.
m_is_modal = true;
// FIXME: 7. Let this's node document be blocked by the modal dialog this.
// 8. If this's node document's top layer does not already contain this, then add an element to the top layer given this.
if (!document().top_layer_elements().contains(*this))
document().add_an_element_to_the_top_layer(*this);
// FIXME: 9. Set this's close watcher to the result of establishing a close watcher given this's relevant global object, with:
// - cancelAction being to return the result of firing an event named cancel at this, with the cancelable
// attribute initialized to true.
// - closeAction being to close the dialog given this and null.
// FIXME: 10. Set this's previously focused element to the focused element.
// FIXME: 11. Let hideUntil be the result of running topmost popover ancestor given this, null, and false.
// FIXME: 12. If hideUntil is null, then set hideUntil to this's node document.
// FIXME: 13. Run hide all popovers until given hideUntil, false, and true.
// FIXME: 14. Run the dialog focusing steps given this.
return {};
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-close
void HTMLDialogElement::close(Optional<String> return_value)
{
// 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
String HTMLDialogElement::return_value() const
{
return m_return_value;
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-returnvalue
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<String> 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);
// 3. If the is modal flag of subject is true, then request an element to be removed from the top layer given subject.
if (m_is_modal)
document().request_an_element_to_be_remove_from_the_top_layer(*this);
// FIXME: 4. Let wasModal be the value of subject's is modal flag.
// 5. Set the is modal flag of subject to false.
m_is_modal = 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.
}
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-focusing-steps
void HTMLDialogElement::run_dialog_focusing_steps()
{
// 1. Let control be null
JS::GCPtr<Element> control = nullptr;
// FIXME 2. If subject has the autofocus attribute, then set control to subject.
// FIXME 3. If control is null, then set control to the focus delegate of subject.
// 4. If control is null, then set control to subject.
if (!control)
control = this;
// 5. Run the focusing steps for control.
run_focusing_steps(control);
}
}