2020-08-17 21:14:30 +03:00
/*
2021-04-23 00:12:55 +03:00
* Copyright ( c ) 2020 , Luke Wilde < lukew @ serenityos . org >
2022-09-18 01:39:42 +03:00
* Copyright ( c ) 2022 , Andreas Kling < kling @ serenityos . org >
2020-08-17 21:14:30 +03:00
*
2021-04-22 11:24:48 +03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-08-17 21:14:30 +03:00
*/
2021-07-30 21:31:46 +03:00
# include <LibWeb/CSS/Parser/Parser.h>
2020-08-17 21:14:30 +03:00
# include <LibWeb/CSS/SelectorEngine.h>
2022-09-18 01:39:42 +03:00
# include <LibWeb/DOM/Document.h>
2021-09-14 01:01:17 +03:00
# include <LibWeb/DOM/HTMLCollection.h>
2022-01-29 23:23:48 +03:00
# include <LibWeb/DOM/NodeOperations.h>
2020-08-17 21:14:30 +03:00
# include <LibWeb/DOM/ParentNode.h>
2021-10-02 22:36:39 +03:00
# include <LibWeb/DOM/StaticNodeList.h>
2020-08-17 21:14:30 +03:00
# include <LibWeb/Dump.h>
2021-09-22 20:06:19 +03:00
# include <LibWeb/Namespace.h>
2020-08-17 21:14:30 +03:00
namespace Web : : DOM {
2023-03-21 01:48:59 +03:00
// https://dom.spec.whatwg.org/#dom-parentnode-queryselector
2022-09-25 19:03:42 +03:00
WebIDL : : ExceptionOr < JS : : GCPtr < Element > > ParentNode : : query_selector ( StringView selector_text )
2020-08-17 21:14:30 +03:00
{
2023-03-21 01:48:59 +03:00
// The querySelector(selectors) method steps are to return the first result of running scope-match a selectors string selectors against this,
// if the result is not an empty list; otherwise null.
// https://dom.spec.whatwg.org/#scope-match-a-selectors-string
// To scope-match a selectors string selectors against a node, run these steps:
// 1. Let s be the result of parse a selector selectors.
2022-04-12 14:00:07 +03:00
auto maybe_selectors = parse_selector ( CSS : : Parser : : ParsingContext ( * this ) , selector_text ) ;
2023-03-21 01:48:59 +03:00
// 2. If s is failure, then throw a "SyntaxError" DOMException.
2021-07-30 21:31:46 +03:00
if ( ! maybe_selectors . has_value ( ) )
2022-09-26 01:15:49 +03:00
return WebIDL : : SyntaxError : : create ( realm ( ) , " Failed to parse selector " ) ;
2020-08-17 21:14:30 +03:00
2021-07-30 21:31:46 +03:00
auto selectors = maybe_selectors . value ( ) ;
2023-03-21 01:48:59 +03:00
// 3. Return the result of match a selector against a tree with s and node’ s root using scoping root node.
2022-08-28 14:42:07 +03:00
JS : : GCPtr < Element > result ;
2022-01-21 00:33:16 +03: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 21:31:46 +03:00
for ( auto & selector : selectors ) {
2023-03-21 01:48:59 +03:00
if ( SelectorEngine : : matches ( selector , element , { } , this ) ) {
2022-08-28 14:42:07 +03:00
result = & element ;
2021-07-30 21:31:46 +03:00
return IterationDecision : : Break ;
}
2020-08-17 21:14:30 +03:00
}
return IterationDecision : : Continue ;
} ) ;
return result ;
}
2023-03-21 01:49:27 +03:00
// https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall
2022-09-25 19:03:42 +03:00
WebIDL : : ExceptionOr < JS : : NonnullGCPtr < NodeList > > ParentNode : : query_selector_all ( StringView selector_text )
2020-08-17 21:14:30 +03:00
{
2023-03-21 01:49:27 +03:00
// The querySelectorAll(selectors) method steps are to return the static result of running scope-match a selectors string selectors against this.
// https://dom.spec.whatwg.org/#scope-match-a-selectors-string
// To scope-match a selectors string selectors against a node, run these steps:
// 1. Let s be the result of parse a selector selectors.
2022-04-12 14:00:07 +03:00
auto maybe_selectors = parse_selector ( CSS : : Parser : : ParsingContext ( * this ) , selector_text ) ;
2023-03-21 01:49:27 +03:00
// 2. If s is failure, then throw a "SyntaxError" DOMException.
2021-07-30 21:31:46 +03:00
if ( ! maybe_selectors . has_value ( ) )
2022-09-26 01:15:49 +03:00
return WebIDL : : SyntaxError : : create ( realm ( ) , " Failed to parse selector " ) ;
2020-08-17 21:14:30 +03:00
2021-07-30 21:31:46 +03:00
auto selectors = maybe_selectors . value ( ) ;
2023-03-21 01:49:27 +03:00
// 3. Return the result of match a selector against a tree with s and node’ s root using scoping root node.
2022-08-28 14:42:07 +03:00
Vector < JS : : Handle < Node > > elements ;
2022-01-21 00:33:16 +03: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 21:31:46 +03:00
for ( auto & selector : selectors ) {
2023-03-21 01:49:27 +03:00
if ( SelectorEngine : : matches ( selector , element , { } , this ) ) {
2022-08-28 14:42:07 +03:00
elements . append ( & element ) ;
2021-07-30 21:31:46 +03:00
}
2020-08-17 21:14:30 +03:00
}
return IterationDecision : : Continue ;
} ) ;
2022-09-26 01:15:49 +03:00
return StaticNodeList : : create ( realm ( ) , move ( elements ) ) ;
2020-08-17 21:14:30 +03:00
}
2022-08-28 14:42:07 +03:00
JS : : GCPtr < Element > ParentNode : : first_element_child ( )
2020-11-21 21:49:09 +03:00
{
return first_child_of_type < Element > ( ) ;
}
2022-08-28 14:42:07 +03:00
JS : : GCPtr < Element > ParentNode : : last_element_child ( )
2020-11-21 21:49:09 +03:00
{
return last_child_of_type < Element > ( ) ;
}
2021-04-11 19:13:10 +03: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 ;
}
2022-09-18 01:42:33 +03:00
void ParentNode : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( m_children ) ;
}
2021-09-14 01:01:17 +03:00
// https://dom.spec.whatwg.org/#dom-parentnode-children
2022-09-01 21:50:16 +03:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : children ( )
2021-09-14 01:01:17 +03:00
{
// The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
2022-09-18 01:42:33 +03:00
if ( ! m_children ) {
2023-05-23 12:25:07 +03:00
m_children = HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Children , [ ] ( Element const & ) {
return true ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2022-09-18 01:42:33 +03:00
}
return * m_children ;
2021-09-14 01:01:17 +03:00
}
2021-09-22 20:06:19 +03: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.
2023-01-09 03:23:00 +03:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : get_elements_by_tag_name ( DeprecatedFlyString const & qualified_name )
2021-09-22 20:06:19 +03:00
{
// 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
if ( qualified_name = = " * " ) {
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ ] ( Element const & ) {
2022-09-18 01:37:24 +03:00
return true ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
2022-09-18 01:39:42 +03:00
// 2. Otherwise, if root’ s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
if ( root ( ) . document ( ) . document_type ( ) = = Document : : Type : : HTML ) {
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ qualified_name ] ( Element const & element ) {
2022-09-18 01:39:42 +03:00
// - 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 ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2022-09-18 01:39:42 +03:00
}
2021-09-22 20:06:19 +03:00
2022-09-18 01:39:42 +03:00
// 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ qualified_name ] ( Element const & element ) {
2021-09-22 20:06:19 +03:00
return element . qualified_name ( ) = = qualified_name ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
// 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.
2023-01-09 03:23:00 +03:00
JS : : NonnullGCPtr < HTMLCollection > ParentNode : : get_elements_by_tag_name_ns ( DeprecatedFlyString const & nullable_namespace , DeprecatedFlyString const & local_name )
2021-09-22 20:06:19 +03:00
{
// 1. If namespace is the empty string, set it to null.
2022-12-04 21:02:33 +03:00
DeprecatedString namespace_ = nullable_namespace ;
2021-09-22 20:06:19 +03:00
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 = = " * " ) {
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ ] ( Element const & ) {
2022-09-18 01:37:24 +03:00
return true ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
// 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_ = = " * " ) {
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ local_name ] ( Element const & element ) {
2022-09-18 01:37:24 +03:00
return element . local_name ( ) = = local_name ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
// 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 = = " * " ) {
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ namespace_ ] ( Element const & element ) {
2022-09-18 01:37:24 +03:00
return element . namespace_ ( ) = = namespace_ ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
// 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
2023-05-23 12:25:07 +03:00
return HTMLCollection : : create ( * this , HTMLCollection : : Scope : : Descendants , [ namespace_ , local_name ] ( Element const & element ) {
2022-09-18 01:37:24 +03:00
return element . namespace_ ( ) = = namespace_ & & element . local_name ( ) = = local_name ;
2023-02-15 00:53:08 +03:00
} ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-09-22 20:06:19 +03:00
}
2022-01-29 23:23:48 +03:00
// https://dom.spec.whatwg.org/#dom-parentnode-prepend
2022-12-04 21:02:33 +03:00
WebIDL : : ExceptionOr < void > ParentNode : : prepend ( Vector < Variant < JS : : Handle < Node > , DeprecatedString > > const & nodes )
2022-01-29 23:23:48 +03:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 15:39:49 +03:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-29 23:23:48 +03:00
// 2. Pre-insert node into this before this’ s first child.
2022-03-22 15:39:49 +03:00
( void ) TRY ( pre_insert ( node , first_child ( ) ) ) ;
2022-01-29 23:23:48 +03:00
return { } ;
}
2022-12-04 21:02:33 +03:00
WebIDL : : ExceptionOr < void > ParentNode : : append ( Vector < Variant < JS : : Handle < Node > , DeprecatedString > > const & nodes )
2022-01-29 23:45:17 +03:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 15:39:49 +03:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-29 23:45:17 +03:00
// 2. Append node to this.
2022-03-22 15:39:49 +03:00
( void ) TRY ( append_child ( node ) ) ;
2022-01-29 23:45:17 +03:00
return { } ;
}
2022-12-04 21:02:33 +03:00
WebIDL : : ExceptionOr < void > ParentNode : : replace_children ( Vector < Variant < JS : : Handle < Node > , DeprecatedString > > const & nodes )
2022-01-30 00:01:09 +03:00
{
// 1. Let node be the result of converting nodes into a node given nodes and this’ s node document.
2022-03-22 15:39:49 +03:00
auto node = TRY ( convert_nodes_to_single_node ( nodes , document ( ) ) ) ;
2022-01-30 00:01:09 +03:00
// 2. Ensure pre-insertion validity of node into this before null.
2022-03-22 15:39:49 +03:00
TRY ( ensure_pre_insertion_validity ( node , nullptr ) ) ;
2022-01-30 00:01:09 +03:00
// 3. Replace all with node within this.
2022-08-28 14:42:07 +03:00
replace_all ( * node ) ;
2022-01-30 00:01:09 +03:00
return { } ;
}
2020-08-17 21:14:30 +03:00
}