From 1a012f279a9791c685781b7189fba6cddb973f96 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Thu, 19 Sep 2024 19:18:00 +0100 Subject: [PATCH] LibWeb/WebVTT: Implement VTTRegion idl interface --- .../BindingsGenerator/IDLGenerators.cpp | 2 + .../Text/expected/all-window-properties.txt | 1 + Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/Forward.h | 4 + .../Libraries/LibWeb/WebVTT/VTTRegion.cpp | 120 ++++++++++++++++++ Userland/Libraries/LibWeb/WebVTT/VTTRegion.h | 74 +++++++++++ .../Libraries/LibWeb/WebVTT/VTTRegion.idl | 16 +++ Userland/Libraries/LibWeb/idl_files.cmake | 1 + 8 files changed, 219 insertions(+) create mode 100644 Userland/Libraries/LibWeb/WebVTT/VTTRegion.cpp create mode 100644 Userland/Libraries/LibWeb/WebVTT/VTTRegion.h create mode 100644 Userland/Libraries/LibWeb/WebVTT/VTTRegion.idl diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 1c27d746544..38f4014245d 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -94,6 +94,7 @@ static bool is_platform_object(Type const& type) "TextMetrics"sv, "TextTrack"sv, "URLSearchParams"sv, + "VTTRegion"sv, "VideoTrack"sv, "VideoTrackList"sv, "WebGLRenderingContext"sv, @@ -4212,6 +4213,7 @@ static void generate_using_namespace_definitions(SourceGenerator& generator) using namespace Web::WebAudio; using namespace Web::WebGL; using namespace Web::WebIDL; + using namespace Web::WebVTT; using namespace Web::XHR; )~~~"sv); } diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 8a93afca788..63421da93cc 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -363,6 +363,7 @@ Uint32Array Uint8Array Uint8ClampedArray UserActivation +VTTRegion ValidityState VideoTrack VideoTrackList diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 603f4bbfe51..30b8ea3b301 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -755,6 +755,7 @@ set(SOURCES WebIDL/Promise.cpp WebIDL/Tracing.cpp WebSockets/WebSocket.cpp + WebVTT/VTTRegion.cpp XHR/EventNames.cpp XHR/FormData.cpp XHR/FormDataIterator.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index e5fd8b4e227..ee88ea6c5aa 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -790,6 +790,10 @@ namespace Web::WebSockets { class WebSocket; } +namespace Web::WebVTT { +class VTTRegion; +} + namespace Web::XHR { class FormData; class FormDataIterator; diff --git a/Userland/Libraries/LibWeb/WebVTT/VTTRegion.cpp b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.cpp new file mode 100644 index 00000000000..5c85b8ca7c9 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2024, Jamie Mansfield + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::WebVTT { + +JS_DEFINE_ALLOCATOR(VTTRegion); + +// https://w3c.github.io/webvtt/#dom-vttregion-vttregion +WebIDL::ExceptionOr> VTTRegion::construct_impl(JS::Realm& realm) +{ + // 1. Create a new WebVTT region. Let region be that WebVTT region. + auto region = realm.heap().allocate(realm, realm); + + // 2. Let region’s WebVTT region identifier be the empty string. + region->m_identifier = ""_string; + + // 3. Let region’s WebVTT region width be 100. + region->m_width = 100; + + // 4. Let region’s WebVTT region lines be 3. + region->m_lines = 3; + + // 5. Let region’s text track region regionAnchorX be 0. + region->m_anchor_x = 0; + + // 6. Let region’s text track region regionAnchorY be 100. + region->m_anchor_y = 100; + + // 7. Let region’s text track region viewportAnchorX be 0. + region->m_viewport_anchor_x = 0; + + // 8. Let region’s text track region viewportAnchorY be 100. + region->m_viewport_anchor_y = 100; + + // 9. Let region’s WebVTT region scroll be the empty string. + region->m_scroll_setting = Bindings::ScrollSetting::Empty; + + // 10. Return the VTTRegion object representing region. + return region; +} + +VTTRegion::VTTRegion(JS::Realm& realm) + : PlatformObject(realm) +{ +} + +void VTTRegion::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(VTTRegion); +} + +// https://w3c.github.io/webvtt/#dom-vttregion-width +WebIDL::ExceptionOr VTTRegion::set_width(double width) +{ + // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown. + if (width < 0 || width > 100) + return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_fly_string); + + // Otherwise, the WebVTT region width must be set to the new value. + m_width = width; + return {}; +} + +// https://w3c.github.io/webvtt/#dom-vttregion-regionanchorx +WebIDL::ExceptionOr VTTRegion::set_region_anchor_x(double region_anchor_x) +{ + // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown. + if (region_anchor_x < 0 || region_anchor_x > 100) + return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_fly_string); + + // Otherwise, the WebVTT region anchor X distance must be set to the new value. + m_anchor_x = region_anchor_x; + return {}; +} + +// https://w3c.github.io/webvtt/#dom-vttregion-regionanchory +WebIDL::ExceptionOr VTTRegion::set_region_anchor_y(double region_anchor_y) +{ + // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown. + if (region_anchor_y < 0 || region_anchor_y > 100) + return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_fly_string); + + // Otherwise, the WebVTT region anchor Y distance must be set to the new value. + m_anchor_y = region_anchor_y; + return {}; +} + +// https://w3c.github.io/webvtt/#dom-vttregion-viewportanchorx +WebIDL::ExceptionOr VTTRegion::set_viewport_anchor_x(double viewport_anchor_x) +{ + // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown. + if (viewport_anchor_x < 0 || viewport_anchor_x > 100) + return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_fly_string); + + // Otherwise, the WebVTT region viewport anchor X distance must be set to the new value. + m_viewport_anchor_x = viewport_anchor_x; + return {}; +} + +// https://w3c.github.io/webvtt/#dom-vttregion-viewportanchory +WebIDL::ExceptionOr VTTRegion::set_viewport_anchor_y(double viewport_anchor_y) +{ + // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown. + if (viewport_anchor_y < 0 || viewport_anchor_y > 100) + return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_fly_string); + + // Otherwise, the WebVTT region viewport anchor Y distance must be set to the new value. + m_viewport_anchor_y = viewport_anchor_y; + return {}; +} + +} diff --git a/Userland/Libraries/LibWeb/WebVTT/VTTRegion.h b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.h new file mode 100644 index 00000000000..52f8d232af3 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2024, Jamie Mansfield + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::WebVTT { + +// https://w3c.github.io/webvtt/#vttregion +class VTTRegion final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(VTTRegion, Bindings::PlatformObject); + JS_DECLARE_ALLOCATOR(VTTRegion); + +public: + static WebIDL::ExceptionOr> construct_impl(JS::Realm&); + virtual ~VTTRegion() override = default; + + String const& id() const { return m_identifier; } + void set_id(String const& id) { m_identifier = id; } + + double width() const { return m_width; } + WebIDL::ExceptionOr set_width(double width); + + WebIDL::UnsignedLong lines() const { return m_lines; } + void set_lines(WebIDL::UnsignedLong lines) { m_lines = lines; } + + double region_anchor_x() const { return m_anchor_x; } + WebIDL::ExceptionOr set_region_anchor_x(double region_anchor_x); + + double region_anchor_y() const { return m_anchor_y; } + WebIDL::ExceptionOr set_region_anchor_y(double region_anchor_y); + + double viewport_anchor_x() const { return m_viewport_anchor_x; } + WebIDL::ExceptionOr set_viewport_anchor_x(double viewport_anchor_x); + + double viewport_anchor_y() const { return m_viewport_anchor_y; } + WebIDL::ExceptionOr set_viewport_anchor_y(double viewport_anchor_y); + + Bindings::ScrollSetting scroll() const { return m_scroll_setting; } + void set_scroll(Bindings::ScrollSetting scroll) { m_scroll_setting = scroll; } + +private: + VTTRegion(JS::Realm&); + + virtual void initialize(JS::Realm&) override; + + // https://w3c.github.io/webvtt/#webvtt-region-identifier + String m_identifier {}; + + // https://w3c.github.io/webvtt/#webvtt-region-width + double m_width { 100 }; + + // https://w3c.github.io/webvtt/#webvtt-region-lines + WebIDL::UnsignedLong m_lines { 3 }; + + // https://w3c.github.io/webvtt/#webvtt-region-anchor + double m_anchor_x { 0 }; + double m_anchor_y { 100 }; + + // https://w3c.github.io/webvtt/#webvtt-region-viewport-anchor + double m_viewport_anchor_x { 0 }; + double m_viewport_anchor_y { 100 }; + + // https://w3c.github.io/webvtt/#webvtt-region-scroll + Bindings::ScrollSetting m_scroll_setting { Bindings::ScrollSetting::Empty }; +}; + +} diff --git a/Userland/Libraries/LibWeb/WebVTT/VTTRegion.idl b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.idl new file mode 100644 index 00000000000..0561f094317 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebVTT/VTTRegion.idl @@ -0,0 +1,16 @@ +// https://w3c.github.io/webvtt/#enumdef-scrollsetting +enum ScrollSetting { "", "up" }; + +// https://w3c.github.io/webvtt/#vttregion +[Exposed=Window] +interface VTTRegion { + constructor(); + attribute DOMString id; + attribute double width; + attribute unsigned long lines; + attribute double regionAnchorX; + attribute double regionAnchorY; + attribute double viewportAnchorX; + attribute double viewportAnchorY; + attribute ScrollSetting scroll; +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index f7f564122e0..9340a6aaf15 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -345,6 +345,7 @@ libweb_js_bindings(WebGL/WebGLContextEvent) libweb_js_bindings(WebGL/WebGLRenderingContext) libweb_js_bindings(WebIDL/DOMException) libweb_js_bindings(WebSockets/WebSocket) +libweb_js_bindings(WebVTT/VTTRegion) libweb_js_bindings(XHR/FormData ITERABLE) libweb_js_bindings(XHR/ProgressEvent) libweb_js_bindings(XHR/XMLHttpRequest)