2023-04-18 16:19:37 -04:00
/*
* 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 {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( TimeRanges ) ;
2023-11-19 19:47:52 +01:00
2023-04-18 16:19:37 -04:00
TimeRanges : : TimeRanges ( JS : : Realm & realm )
: Base ( realm )
{
}
2023-08-07 08:41:28 +02:00
void TimeRanges : : initialize ( JS : : Realm & realm )
2023-04-18 16:19:37 -04:00
{
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( TimeRanges ) ;
2025-04-20 16:22:57 +02:00
Base : : initialize ( realm ) ;
2023-04-18 16:19:37 -04:00
}
// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-length
size_t TimeRanges : : length ( ) const
{
2025-02-14 11:22:12 +01:00
return m_ranges . size ( ) ;
2023-04-18 16:19:37 -04:00
}
// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-start
2025-02-14 11:22:12 +01:00
WebIDL : : ExceptionOr < double > TimeRanges : : start ( u32 index ) const
2023-04-18 16:19:37 -04:00
{
2025-02-14 11:22:12 +01:00
// 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 ( ) )
2025-08-07 19:31:52 -04:00
return WebIDL : : IndexSizeError : : create ( realm ( ) , " Index argument is greater than or equal to the number of ranges represented by this TimeRanges object " _utf16 ) ;
2025-02-14 11:22:12 +01:00
// 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 ;
2023-04-18 16:19:37 -04:00
}
// https://html.spec.whatwg.org/multipage/media.html#dom-timeranges-end
2025-02-14 11:22:12 +01:00
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 ( ) )
2025-08-07 19:31:52 -04:00
return WebIDL : : IndexSizeError : : create ( realm ( ) , " Index argument is greater than or equal to the number of ranges represented by this TimeRanges object " _utf16 ) ;
2025-02-14 11:22:12 +01:00
// 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 )
2023-04-18 16:19:37 -04:00
{
2025-02-14 11:22:12 +01:00
for ( auto range : m_ranges ) {
if ( point > = range . start & & point < = range . end )
return true ;
}
return false ;
2023-04-18 16:19:37 -04:00
}
}