LibWeb/WebVTT: Implement VTTRegion idl interface

This commit is contained in:
Jamie Mansfield 2024-09-19 19:18:00 +01:00 committed by Tim Ledbetter
parent cfec88feb3
commit 1a012f279a
Notes: github-actions[bot] 2024-09-24 22:49:56 +00:00
8 changed files with 219 additions and 0 deletions

View file

@ -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);
}

View file

@ -363,6 +363,7 @@ Uint32Array
Uint8Array
Uint8ClampedArray
UserActivation
VTTRegion
ValidityState
VideoTrack
VideoTrackList

View file

@ -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

View file

@ -790,6 +790,10 @@ namespace Web::WebSockets {
class WebSocket;
}
namespace Web::WebVTT {
class VTTRegion;
}
namespace Web::XHR {
class FormData;
class FormDataIterator;

View file

@ -0,0 +1,120 @@
/*
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebVTT/VTTRegion.h>
namespace Web::WebVTT {
JS_DEFINE_ALLOCATOR(VTTRegion);
// https://w3c.github.io/webvtt/#dom-vttregion-vttregion
WebIDL::ExceptionOr<JS::NonnullGCPtr<VTTRegion>> VTTRegion::construct_impl(JS::Realm& realm)
{
// 1. Create a new WebVTT region. Let region be that WebVTT region.
auto region = realm.heap().allocate<VTTRegion>(realm, realm);
// 2. Let regions WebVTT region identifier be the empty string.
region->m_identifier = ""_string;
// 3. Let regions WebVTT region width be 100.
region->m_width = 100;
// 4. Let regions WebVTT region lines be 3.
region->m_lines = 3;
// 5. Let regions text track region regionAnchorX be 0.
region->m_anchor_x = 0;
// 6. Let regions text track region regionAnchorY be 100.
region->m_anchor_y = 100;
// 7. Let regions text track region viewportAnchorX be 0.
region->m_viewport_anchor_x = 0;
// 8. Let regions text track region viewportAnchorY be 100.
region->m_viewport_anchor_y = 100;
// 9. Let regions 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<void> 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<void> 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<void> 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<void> 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<void> 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 {};
}
}

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/VTTRegionPrototype.h>
#include <LibWeb/WebIDL/Types.h>
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<JS::NonnullGCPtr<VTTRegion>> 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<void> 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<void> set_region_anchor_x(double region_anchor_x);
double region_anchor_y() const { return m_anchor_y; }
WebIDL::ExceptionOr<void> set_region_anchor_y(double region_anchor_y);
double viewport_anchor_x() const { return m_viewport_anchor_x; }
WebIDL::ExceptionOr<void> set_viewport_anchor_x(double viewport_anchor_x);
double viewport_anchor_y() const { return m_viewport_anchor_y; }
WebIDL::ExceptionOr<void> 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 };
};
}

View file

@ -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;
};

View file

@ -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)