Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / time_ranges.lucb

#==============================================================================================
#
#   time_ranges - HTML::TimeRanges, the ranges a media element's buffered, played and
#   seekable answer
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/TimeRanges.cpp (lines 1-70).
#
#       Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from TimeRanges.cpp:16 Web::HTML::TimeRanges::TimeRanges.
pub func html_time_ranges_construct(this: HtmlTimeRanges*, realm: Realm*):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)

## Ported from TimeRanges.cpp:21 Web::HTML::TimeRanges::initialize (override).
pub func html_time_ranges_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.time_ranges)
    # Base::initialize(realm): PlatformObject inherits JS::Object's.
    js_cell_initialize_impl(base, realm)

## TimeRanges has no visit_edges of its own (PlatformObject's).
pub func html_time_ranges_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    bind_platform_object_visit_edges_impl(base, visitor)

## Ported from TimeRanges.cpp:28 Web::HTML::TimeRanges::length.
# https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-length
pub func html_time_ranges_length(this: HtmlTimeRanges*) -> usize:
    return ak.vector_size(&this.m_ranges)

## Ported from TimeRanges.cpp:34 Web::HTML::TimeRanges::start.
# https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-start
pub func html_time_ranges_start(this: HtmlTimeRanges*, index: u32) -> f64!:
    # These methods must throw "IndexSizeError" DOMExceptions if called with an index argument greater than or equal to the number of ranges represented by the object.
    if (usize)index >= ak.vector_size(&this.m_ranges):
        let realm = bind_platform_object_realm(this.bind_platform_object())
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"))))

    # The start(index) method must return the position of the start of the indexth range represented by the object,
    # in seconds measured from the start of the timeline that the object covers.
    return ak.vector_at(&this.m_ranges, (usize)index).start

## Ported from TimeRanges.cpp:46 Web::HTML::TimeRanges::end.
# https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-end
pub func html_time_ranges_end(this: HtmlTimeRanges*, index: u32) -> f64!:
    # These methods must throw "IndexSizeError" DOMExceptions if called with an index argument greater than or equal to the number of ranges represented by the object.
    if (usize)index >= ak.vector_size(&this.m_ranges):
        let realm = bind_platform_object_realm(this.bind_platform_object())
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"))))

    # The end(index) method must return the position of the end of the indexth range represented by the object,
    # in seconds measured from the start of the timeline that the object covers.
    return ak.vector_at(&this.m_ranges, (usize)index).end

## Ported from TimeRanges.cpp:57 Web::HTML::TimeRanges::add_range.
pub func html_time_ranges_add_range(this: HtmlTimeRanges*, start: f64, end: f64):
    ak.vector_append_t(&this.m_ranges, HtmlTimeRangesRange(start = start, end = end))

## Ported from TimeRanges.cpp:61 Web::HTML::TimeRanges::in_range.
pub func html_time_ranges_in_range(this: HtmlTimeRanges*, point: f64) -> bool:
    for range in ak.vector_span(&this.m_ranges):
        if point >= range.start and point <= range.end:
            return true
    return false