ladybird/Libraries/LibWeb/HTML/TimeRanges.cpp
Andreas Kling a6dfc74e93 LibWeb: Only set prototype once for object with IDL interface
Before this change, we were going through the chain of base classes for
each IDL interface object and having them set the prototype to their
prototype.

Instead of doing that, reorder things so that we set the right prototype
immediately in Foo::initialize(), and then don't bother in all the base
class overrides.

This knocks off a ~1% profile item on Speedometer 3.
2025-04-20 18:43:11 +02:00

70 lines
2.4 KiB
C++

/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/TimeRangesPrototype.h>
#include <LibWeb/HTML/TimeRanges.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(TimeRanges);
TimeRanges::TimeRanges(JS::Realm& realm)
: Base(realm)
{
}
void TimeRanges::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(TimeRanges);
Base::initialize(realm);
}
// 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(realm(), "Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"_string);
// 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(realm(), "Index argument is greater than or equal to the number of ranges represented by this TimeRanges object"_string);
// 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;
}
}