2021-10-02 20:33:45 +01:00
/*
* Copyright ( c ) 2021 , Luke Wilde < lukew @ serenityos . org >
2022-09-01 16:30:26 +02:00
* Copyright ( c ) 2022 , Andreas Kling < kling @ serenityos . org >
2021-10-02 20:33:45 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <LibWeb/DOM/LiveNodeList.h>
# include <LibWeb/DOM/Node.h>
namespace Web : : DOM {
2023-05-23 12:28:16 +02:00
WebIDL : : ExceptionOr < JS : : NonnullGCPtr < NodeList > > LiveNodeList : : create ( JS : : Realm & realm , Node & root , Scope scope , Function < bool ( Node const & ) > filter )
2022-09-01 16:30:26 +02:00
{
2023-05-23 12:28:16 +02:00
return MUST_OR_THROW_OOM ( realm . heap ( ) . allocate < LiveNodeList > ( realm , realm , root , scope , move ( filter ) ) ) ;
2022-09-01 16:30:26 +02:00
}
2023-05-23 12:28:16 +02:00
LiveNodeList : : LiveNodeList ( JS : : Realm & realm , Node & root , Scope scope , Function < bool ( Node const & ) > filter )
2022-09-25 16:15:49 -06:00
: NodeList ( realm )
2022-09-01 16:30:26 +02:00
, m_root ( root )
2021-10-02 20:33:45 +01:00
, m_filter ( move ( filter ) )
2023-05-23 12:28:16 +02:00
, m_scope ( scope )
2021-10-02 20:33:45 +01:00
{
}
2022-09-01 16:30:26 +02:00
LiveNodeList : : ~ LiveNodeList ( ) = default ;
void LiveNodeList : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( m_root . ptr ( ) ) ;
}
2022-08-28 13:42:07 +02:00
JS : : MarkedVector < Node * > LiveNodeList : : collection ( ) const
2021-10-02 20:33:45 +01:00
{
2022-09-01 16:30:26 +02:00
JS : : MarkedVector < Node * > nodes ( heap ( ) ) ;
2023-05-23 12:28:16 +02:00
if ( m_scope = = Scope : : Descendants ) {
m_root - > for_each_in_subtree ( [ & ] ( auto & node ) {
if ( m_filter ( node ) )
nodes . append ( const_cast < Node * > ( & node ) ) ;
return IterationDecision : : Continue ;
} ) ;
} else {
m_root - > for_each_child ( [ & ] ( auto & node ) {
if ( m_filter ( node ) )
nodes . append ( const_cast < Node * > ( & node ) ) ;
return IterationDecision : : Continue ;
} ) ;
}
2021-10-02 20:33:45 +01:00
return nodes ;
}
// https://dom.spec.whatwg.org/#dom-nodelist-length
u32 LiveNodeList : : length ( ) const
{
return collection ( ) . size ( ) ;
}
// https://dom.spec.whatwg.org/#dom-nodelist-item
Node const * LiveNodeList : : item ( u32 index ) const
{
// The item(index) method must return the indexth node in the collection. If there is no indexth node in the collection, then the method must return null.
auto nodes = collection ( ) ;
if ( index > = nodes . size ( ) )
return nullptr ;
2022-08-28 13:42:07 +02:00
return nodes [ index ] ;
2021-10-02 20:33:45 +01:00
}
// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
bool LiveNodeList : : is_supported_property_index ( u32 index ) const
{
// The object’ s supported property indices are the numbers in the range zero to one less than the number of nodes represented by the collection.
// If there are no such elements, then there are no supported property indices.
return index < length ( ) ;
}
}