Skip to content

Truly independent web browser

2.2 KB
Raw Blame
/*
 * Copyright (c) 2023, Tim Flynn <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGC/Heap.h>
#include <LibWeb/HTML/TimeRanges.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(TimeRanges);

GC::Ref<TimeRanges> TimeRanges::create()
{
    return GC::Heap::the().allocate<TimeRanges>();
}

TimeRanges::TimeRanges()
{
}

// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-length
size_t TimeRanges::length() const
{
    return m_ranges.size();
}

// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-start
WebIDL::ExceptionOr<double> TimeRanges::start(u32 index) const
{
    // 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 (index >= m_ranges.size())
        return WebIDL::IndexSizeError::create("Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"_utf16);

    // 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 m_ranges[index].start;
}

// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-end
WebIDL::ExceptionOr<double> TimeRanges::end(u32 index) const
{
    // 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 (index >= m_ranges.size())
        return WebIDL::IndexSizeError::create("Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"_utf16);

    // 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 m_ranges[index].end;
}

void TimeRanges::add_range(double start, double end)
{
    m_ranges.append({ start, end });
}
bool TimeRanges::in_range(double point)
{
    for (auto range : m_ranges) {
        if (point >= range.start && point <= range.end)
            return true;
    }
    return false;
}

}