LibWeb/HTML: Port Window.prompt() to IDL

This commit is contained in:
Linus Groh 2023-03-05 17:28:17 +00:00
parent bbffda5f55
commit eb4842dfa1
3 changed files with 15 additions and 25 deletions

View file

@ -430,13 +430,6 @@ WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> Window::open_impl(StringView u
return target_browsing_context->window_proxy();
}
DeprecatedString Window::prompt_impl(DeprecatedString const& message, DeprecatedString const& default_)
{
if (auto* page = this->page())
return page->did_request_prompt(message, default_);
return {};
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
i32 Window::set_timeout_impl(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
{
@ -1129,7 +1122,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_accessor(realm, "devicePixelRatio", device_pixel_ratio_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
define_native_function(realm, "open", open, 0, attr);
define_native_function(realm, "prompt", prompt, 0, attr);
define_native_function(realm, "setInterval", set_interval, 1, attr);
define_native_function(realm, "setTimeout", set_timeout, 1, attr);
define_native_function(realm, "clearInterval", clear_interval, 1, attr);
@ -1252,6 +1244,19 @@ bool Window::confirm(Optional<String> const& message)
return false;
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-prompt
Optional<String> Window::prompt(Optional<String> const& message, Optional<String> const& default_)
{
// FIXME: Make this fully spec compliant.
if (auto* page = this->page()) {
auto response = page->did_request_prompt(message->to_deprecated_string(), default_->to_deprecated_string());
if (response.is_null())
return {};
return String::from_deprecated_string(response).release_value_but_fixme_should_propagate_errors();
}
return {};
}
JS_DEFINE_NATIVE_FUNCTION(Window::open)
{
auto* impl = TRY(impl_from(vm));
@ -1274,21 +1279,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::open)
return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->open_impl(url, target, features); }));
}
JS_DEFINE_NATIVE_FUNCTION(Window::prompt)
{
auto* impl = TRY(impl_from(vm));
DeprecatedString message = "";
DeprecatedString default_ = "";
if (!vm.argument(0).is_undefined())
message = TRY(vm.argument(0).to_deprecated_string(vm));
if (!vm.argument(1).is_undefined())
default_ = TRY(vm.argument(1).to_deprecated_string(vm));
auto response = impl->prompt_impl(message, default_);
if (response.is_null())
return JS::js_null();
return JS::PrimitiveString::create(vm, response);
}
static JS::ThrowCompletionOr<TimerHandler> make_timer_handler(JS::VM& vm, JS::Value handler)
{
if (handler.is_function())

View file

@ -66,7 +66,6 @@ public:
void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open_impl(StringView url, StringView target, StringView features);
DeprecatedString prompt_impl(DeprecatedString const&, DeprecatedString const&);
i32 request_animation_frame_impl(WebIDL::CallbackType& js_callback);
void cancel_animation_frame_impl(i32);
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
@ -144,6 +143,7 @@ public:
// JS API functions
void alert(String const& message = {});
bool confirm(Optional<String> const& message);
Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
private:
explicit Window(JS::Realm&);
@ -267,7 +267,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(is_secure_context_getter);
JS_DECLARE_NATIVE_FUNCTION(open);
JS_DECLARE_NATIVE_FUNCTION(prompt);
JS_DECLARE_NATIVE_FUNCTION(set_interval);
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(clear_interval);

View file

@ -8,6 +8,7 @@ interface Window : EventTarget {
undefined alert();
undefined alert(DOMString message);
boolean confirm(optional DOMString message = "");
DOMString? prompt(optional DOMString message = "", optional DOMString default = "");
};
Window includes GlobalEventHandlers;
Window includes WindowEventHandlers;