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

This commit is contained in:
Linus Groh 2023-03-06 11:14:45 +00:00
parent 94f1eff291
commit 192f5e61f6
3 changed files with 3 additions and 20 deletions

View file

@ -1064,7 +1064,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "requestAnimationFrame", request_animation_frame, 1, attr);
define_native_function(realm, "cancelAnimationFrame", cancel_animation_frame, 1, attr);
define_native_function(realm, "atob", atob, 1, attr);
define_native_function(realm, "btoa", btoa, 1, attr);
define_native_function(realm, "focus", focus, 0, attr);
define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr);
@ -1502,24 +1501,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::atob)
return JS::PrimitiveString::create(vm, text);
}
JS_DEFINE_NATIVE_FUNCTION(Window::btoa)
{
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "btoa");
auto string = TRY(vm.argument(0).to_deprecated_string(vm));
Vector<u8> byte_string;
byte_string.ensure_capacity(string.length());
for (u32 code_point : Utf8View(string)) {
if (code_point > 0xff)
return throw_completion(WebIDL::InvalidCharacterError::create(*vm.current_realm(), "Data contains characters outside the range U+0000 and U+00FF"));
byte_string.append(code_point);
}
auto encoded = MUST(encode_base64(byte_string.span()));
return JS::PrimitiveString::create(vm, encoded.to_deprecated_string());
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus
JS_DEFINE_NATIVE_FUNCTION(Window::focus)
{

View file

@ -46,6 +46,8 @@ public:
~Window();
using WindowOrWorkerGlobalScopeMixin::btoa;
// ^DOM::EventTarget
virtual bool dispatch_event(DOM::Event&) override;
@ -265,7 +267,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(atob);
JS_DECLARE_NATIVE_FUNCTION(btoa);
JS_DECLARE_NATIVE_FUNCTION(focus);
JS_DECLARE_NATIVE_FUNCTION(get_computed_style);

View file

@ -39,6 +39,7 @@ interface Window : EventTarget {
// FIXME: Replace these with 'Window includes WindowOrWorkerGlobalScope;' once we have feature parity
[Replaceable] readonly attribute USVString origin;
readonly attribute boolean isSecureContext;
DOMString btoa(DOMString data);
};
Window includes GlobalEventHandlers;
Window includes WindowEventHandlers;