1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 13:17:36 +00:00

LibWeb: Move element_child_count to ParentNode and add its IDL attribute

I initially had it in Node just because, but then saw it was part of
ParentNode in the spec.
This commit is contained in:
Luke 2021-04-11 17:13:10 +01:00 committed by Andreas Kling
parent fc9abee84b
commit 83d2c3f2f5
7 changed files with 31 additions and 25 deletions

View file

@ -18,12 +18,6 @@ interface Document : Node {
ArrayFromVector getElementsByTagName(DOMString tagName); ArrayFromVector getElementsByTagName(DOMString tagName);
ArrayFromVector getElementsByClassName(DOMString className); ArrayFromVector getElementsByClassName(DOMString className);
readonly attribute Element? firstElementChild;
readonly attribute Element? lastElementChild;
Element? querySelector(DOMString selectors);
ArrayFromVector querySelectorAll(DOMString selectors);
Element createElement(DOMString tagName); Element createElement(DOMString tagName);
Element createElementNS(DOMString? namespace, DOMString qualifiedName); Element createElementNS(DOMString? namespace, DOMString qualifiedName);
DocumentFragment createDocumentFragment(); DocumentFragment createDocumentFragment();
@ -48,6 +42,14 @@ interface Document : Node {
attribute DOMString title; attribute DOMString title;
// FIXME: These should all come from a ParentNode mixin
readonly attribute Element? firstElementChild;
readonly attribute Element? lastElementChild;
readonly attribute unsigned long childElementCount;
Element? querySelector(DOMString selectors);
ArrayFromVector querySelectorAll(DOMString selectors);
// FIXME: These should all come from a GlobalEventHandlers mixin // FIXME: These should all come from a GlobalEventHandlers mixin
attribute EventHandler onabort; attribute EventHandler onabort;
attribute EventHandler onauxclick; attribute EventHandler onauxclick;

View file

@ -2,8 +2,10 @@ interface DocumentFragment : Node {
Element? getElementById(DOMString id); Element? getElementById(DOMString id);
// FIXME: These should all come from a ParentNode mixin
readonly attribute Element? firstElementChild; readonly attribute Element? firstElementChild;
readonly attribute Element? lastElementChild; readonly attribute Element? lastElementChild;
readonly attribute unsigned long childElementCount;
Element? querySelector(DOMString selectors); Element? querySelector(DOMString selectors);
ArrayFromVector querySelectorAll(DOMString selectors); ArrayFromVector querySelectorAll(DOMString selectors);

View file

@ -11,12 +11,6 @@ interface Element : Node {
ArrayFromVector getElementsByTagName(DOMString tagName); ArrayFromVector getElementsByTagName(DOMString tagName);
ArrayFromVector getElementsByClassName(DOMString className); ArrayFromVector getElementsByClassName(DOMString className);
readonly attribute Element? firstElementChild;
readonly attribute Element? lastElementChild;
Element? querySelector(DOMString selectors);
ArrayFromVector querySelectorAll(DOMString selectors);
[LegacyNullToEmptyString] attribute DOMString innerHTML; [LegacyNullToEmptyString] attribute DOMString innerHTML;
[Reflect] attribute DOMString id; [Reflect] attribute DOMString id;
[Reflect=class] attribute DOMString className; [Reflect=class] attribute DOMString className;
@ -25,4 +19,12 @@ interface Element : Node {
readonly attribute Element? previousElementSibling; readonly attribute Element? previousElementSibling;
[ImplementedAs=style_for_bindings] readonly attribute CSSStyleDeclaration style; [ImplementedAs=style_for_bindings] readonly attribute CSSStyleDeclaration style;
// FIXME: These should all come from a ParentNode mixin
readonly attribute Element? firstElementChild;
readonly attribute Element? lastElementChild;
readonly attribute unsigned long childElementCount;
Element? querySelector(DOMString selectors);
ArrayFromVector querySelectorAll(DOMString selectors);
}; };

View file

@ -204,7 +204,7 @@ ExceptionOr<void> Node::ensure_pre_insertion_validity(NonnullRefPtr<Node> node,
if (is<Document>(this)) { if (is<Document>(this)) {
if (is<DocumentFragment>(*node)) { if (is<DocumentFragment>(*node)) {
auto node_element_child_count = node->element_child_count(); auto node_element_child_count = downcast<DocumentFragment>(*node).child_element_count();
if ((node_element_child_count > 1 || node->has_child_of_type<Text>()) if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
|| (node_element_child_count == 1 && (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) /* FIXME: or child is non-null and a doctype is following child. */))) { || (node_element_child_count == 1 && (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) /* FIXME: or child is non-null and a doctype is following child. */))) {
return DOM::HierarchyRequestError::create("Invalid node type for insertion"); return DOM::HierarchyRequestError::create("Invalid node type for insertion");
@ -497,14 +497,4 @@ bool Node::is_host_including_inclusive_ancestor_of(const Node& other) const
return is_inclusive_ancestor_of(other) || (is<DocumentFragment>(other.root()) && downcast<DocumentFragment>(other.root())->host() && is_inclusive_ancestor_of(*downcast<DocumentFragment>(other.root())->host().ptr())); return is_inclusive_ancestor_of(other) || (is<DocumentFragment>(other.root()) && downcast<DocumentFragment>(other.root())->host() && is_inclusive_ancestor_of(*downcast<DocumentFragment>(other.root())->host().ptr()));
} }
size_t Node::element_child_count() const
{
size_t count = 0;
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<Element>(child))
++count;
}
return count;
}
} }

View file

@ -175,8 +175,6 @@ public:
bool is_host_including_inclusive_ancestor_of(const Node&) const; bool is_host_including_inclusive_ancestor_of(const Node&) const;
size_t element_child_count() const;
protected: protected:
Node(Document&, NodeType); Node(Document&, NodeType);

View file

@ -80,4 +80,15 @@ RefPtr<Element> ParentNode::last_element_child()
return last_child_of_type<Element>(); return last_child_of_type<Element>();
} }
// 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;
}
} }

View file

@ -40,6 +40,7 @@ public:
RefPtr<Element> first_element_child(); RefPtr<Element> first_element_child();
RefPtr<Element> last_element_child(); RefPtr<Element> last_element_child();
u32 child_element_count() const;
RefPtr<Element> query_selector(const StringView&); RefPtr<Element> query_selector(const StringView&);
NonnullRefPtrVector<Element> query_selector_all(const StringView&); NonnullRefPtrVector<Element> query_selector_all(const StringView&);