2020-08-17 19:14:30 +01:00
/*
2021-04-22 22:12:55 +01:00
* Copyright ( c ) 2020 , Luke Wilde < lukew @ serenityos . org >
2020-08-17 19:14:30 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-08-17 19:14:30 +01:00
*/
2021-07-30 19:31:46 +01:00
# include <LibWeb/CSS/Parser/Parser.h>
2020-08-17 19:14:30 +01:00
# include <LibWeb/CSS/SelectorEngine.h>
2021-09-13 23:01:17 +01:00
# include <LibWeb/DOM/HTMLCollection.h>
2022-01-29 20:23:48 +00:00
# include <LibWeb/DOM/NodeOperations.h>
2020-08-17 19:14:30 +01:00
# include <LibWeb/DOM/ParentNode.h>
2021-10-02 20:36:39 +01:00
# include <LibWeb/DOM/StaticNodeList.h>
2020-08-17 19:14:30 +01:00
# include <LibWeb/Dump.h>
2022-09-04 16:56:15 +02:00
# include <LibWeb/HTML/Window.h>
2021-09-22 18:06:19 +01:00
# include <LibWeb/Namespace.h>
2020-08-17 19:14:30 +01:00
namespace Web : : DOM {
2022-08-28 13:42:07 +02:00
ExceptionOr < JS : : GCPtr < Element > > ParentNode : : query_selector ( StringView selector_text )
2020-08-17 19:14:30 +01:00
{
2022-04-12 12:00:07 +01:00
auto maybe_selectors = parse_selector ( CSS : : Parser : : ParsingContext ( * this ) , selector_text ) ;
2021-07-30 19:31:46 +01:00
if ( ! maybe_selectors . has_value ( ) )
2022-09-04 16:56:15 +02:00
return DOM : : SyntaxError : : create ( global_object ( ) , " Failed to parse selector " ) ;
2020-08-17 19:14:30 +01:00
2021-07-30 19:31:46 +01:00
auto selectors = maybe_selectors . value ( ) ;
2022-08-28 13:42:07 +02:00
JS : : GCPtr < Element > result ;
2022-01-20 21:33:16 +00:00
// FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
for_each_in_subtree_of_type < Element > ( [ & ] ( auto & element ) {
2021-07-30 19:31:46 +01:00
for ( auto & selector : selectors ) {
if ( SelectorEngine : : matches ( selector , element ) ) {
2022-08-28 13:42:07 +02:00
result = & element ;
2021-07-30 19:31:46 +01:00
return IterationDecision : : Break ;
}
2020-08-17 19:14:30 +01:00
}
return IterationDecision : : Continue ;
} ) ;
return result ;
}
2022-09-01 16:30:26 +02:00
ExceptionOr < JS : : NonnullGCPtr < NodeList > > ParentNode : : query_selector_all ( StringView selector_text )
2020-08-17 19:14:30 +01:00
{
2022-04-12 12:00:07 +01:00
auto maybe_selectors = parse_selector ( CSS : : Parser : : ParsingContext ( * this ) , selector_text ) ;
2021-07-30 19:31:46 +01:00
if ( ! maybe_selectors . has_value ( ) )
2022-09-04 16:56:15 +02:00
return DOM : : SyntaxError : : create ( global_object ( ) , " Failed to parse selector " ) ;
2020-08-17 19:14:30 +01:00
2021-07-30 19:31:46 +01:00
auto selectors = maybe_selectors . value ( ) ;
2022-08-28 13:42:07 +02:00
Vector < JS : : Handle < Node > > elements ;
2022-01-20 21:33:16 +00:00
// FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
for_each_in_subtree_of_type < Element > ( [ & ] ( auto & element ) {
2021-07-30 19:31:46 +01:00
for ( auto & selector : selectors ) {
if ( SelectorEngine : : matches ( selector , element ) ) {
2022-08-28 13:42:07 +02:00
elements . append ( & element ) ;
2021-07-30 19:31:46 +01:00
}
2020-08-17 19:14:30 +01:00
}
return IterationDecision : : Continue ;
} ) ;
2022-09-01 16:30:26 +02:00
return StaticNodeList : : create ( window ( ) , move ( elements ) ) ;
2020-08-17 19:14:30 +01:00
}
2022-08-28 13:42:07 +02:00
JS : : GCPtr < Element > ParentNode : : first_element_child ( )
2020-11-21 18:49:09 +00:00
{
return first_child_of_type < Element > ( ) ;
}
2022-08-28 13:42:07 +02:00
JS : : GCPtr < Element > ParentNode : : last_element_child ( )
2020-11-21 18:49:09 +00:00
{
return last_child_of_type < Element > ( ) ;
}
2021-04-11 17:13:10 +01:00
// https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
u32 ParentNode : : child_element_count ( ) const
{
u32 count = 0 ;
for ( auto * child = first_child ( ) ; child ; child = child - > next_sibling ( ) ) {
if ( is < Element > ( child ) )
+ + count ;
}
return count ;
}
2021-09-13 23:01:17 +01:00
// https://dom.spec.whatwg.org/#dom-parentnode-children
2022-09-01 20:50:16 +02:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : children ( )
2021-09-13 23:01:17 +01:00
{
// The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
// FIXME: This should return the same HTMLCollection object every time,
// but that would cause a reference cycle since HTMLCollection refs the root.
return HTMLCollection : : create ( * this , [ this ] ( Element const & element ) {
return is_parent_of ( element ) ;
} ) ;
}
2021-09-22 18:06:19 +01:00
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
2022-09-01 20:50:16 +02:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : get_elements_by_tag_name ( FlyString const & qualified_name )
2021-09-22 18:06:19 +01:00
{
// 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
if ( qualified_name = = " * " ) {
return HTMLCollection : : create ( * this , [ this ] ( Element const & element ) {
return element . is_descendant_of ( * this ) ;
} ) ;
}
// FIXME: 2. Otherwise, if root’ s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
// (It is currently always a HTML document)
return HTMLCollection : : create ( * this , [ this , qualified_name ] ( Element const & element ) {
if ( ! element . is_descendant_of ( * this ) )
return false ;
// - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
if ( element . namespace_ ( ) = = Namespace : : HTML )
return element . qualified_name ( ) . to_lowercase ( ) = = qualified_name . to_lowercase ( ) ;
// - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
return element . qualified_name ( ) = = qualified_name ;
} ) ;
// FIXME: 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
}
// https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
2022-09-01 20:50:16 +02:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : get_elements_by_tag_name_ns ( FlyString const & nullable_namespace , FlyString const & local_name )
2021-09-22 18:06:19 +01:00
{
// 1. If namespace is the empty string, set it to null.
String namespace_ = nullable_namespace ;
if ( namespace_ . is_empty ( ) )
namespace_ = { } ;
// 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
if ( namespace_ = = " * " & & local_name = = " * " ) {
return HTMLCollection : : create ( * this , [ this ] ( Element const & element ) {
return element . is_descendant_of ( * this ) ;
} ) ;
}
// 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
if ( namespace_ = = " * " ) {
return HTMLCollection : : create ( * this , [ this , local_name ] ( Element const & element ) {
return element . is_descendant_of ( * this ) & & element . local_name ( ) = = local_name ;
} ) ;
}
// 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
if ( local_name = = " * " ) {
return HTMLCollection : : create ( * this , [ this , namespace_ ] ( Element const & element ) {
return element . is_descendant_of ( * this ) & & element . namespace_ ( ) = = namespace_ ;
} ) ;
}
// 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
return HTMLCollection : : create ( * this , [ this , namespace_ , local_name ] ( Element const & element ) {
return element . is_descendant_of ( * this ) & & element . namespace_ ( ) = = namespace_ & & element . local_name ( ) = = local_name ;
} ) ;
}
2022-01-29 20:23:48 +00:00
// https://dom.spec.whatwg.org/#dom-parentnode-prepend
2022-08-28 13:42:07 +02:00
ExceptionOr < void > ParentNode : : prepend ( Vector < Variant < JS : : Handle < Node > , String > > const & nodes )
2022-01-29 20:23:48 +00:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 12:39:49 +00:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-29 20:23:48 +00:00
// 2. Pre-insert node into this before this’ s first child.
2022-03-22 12:39:49 +00:00
( void ) TRY ( pre_insert ( node , first_child ( ) ) ) ;
2022-01-29 20:23:48 +00:00
return { } ;
}
2022-08-28 13:42:07 +02:00
ExceptionOr < void > ParentNode : : append ( Vector < Variant < JS : : Handle < Node > , String > > const & nodes )
2022-01-29 20:45:17 +00:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 12:39:49 +00:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-29 20:45:17 +00:00
// 2. Append node to this.
2022-03-22 12:39:49 +00:00
( void ) TRY ( append_child ( node ) ) ;
2022-01-29 20:45:17 +00:00
return { } ;
}
2022-08-28 13:42:07 +02:00
ExceptionOr < void > ParentNode : : replace_children ( Vector < Variant < JS : : Handle < Node > , String > > const & nodes )
2022-01-29 21:01:09 +00:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 12:39:49 +00:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-29 21:01:09 +00:00
// 2. Ensure pre-insertion validity of node into this before null.
2022-03-22 12:39:49 +00:00
TRY ( ensure_pre_insertion_validity ( node , nullptr ) ) ;
2022-01-29 21:01:09 +00:00
// 3. Replace all with node within this.
2022-08-28 13:42:07 +02:00
replace_all ( * node ) ;
2022-01-29 21:01:09 +00:00
return { } ;
}
2020-08-17 19:14:30 +01:00
}