1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibWeb: Switch to using AK::is and AK::downcast

This commit is contained in:
Andreas Kling 2020-07-26 17:16:18 +02:00
parent fe6474e692
commit 71556e39a4
73 changed files with 249 additions and 433 deletions

View file

@ -47,10 +47,8 @@ private:
String m_data;
};
template<>
inline bool is<CharacterData>(const Node& node)
{
return node.is_character_data();
}
}
AK_BEGIN_TYPE_TRAITS(Web::CharacterData)
static bool is_type(const Web::Node& node) { return node.is_character_data(); }
AK_END_TYPE_TRAITS()

View file

@ -39,10 +39,8 @@ public:
virtual FlyString node_name() const override { return "#comment"; }
};
template<>
inline bool is<Comment>(const Node& node)
{
return node.is_comment();
}
}
AK_BEGIN_TYPE_TRAITS(Web::Comment)
static bool is_type(const Web::Node& node) { return node.is_comment(); }
AK_END_TYPE_TRAITS()

View file

@ -188,10 +188,9 @@ private:
QuirksMode m_quirks_mode { QuirksMode::No };
};
template<>
inline bool is<Document>(const Node& node)
{
return node.is_document();
}
}
AK_BEGIN_TYPE_TRAITS(Web::Document)
static bool is_type(const Web::Node& node) { return node.is_document(); }
AK_END_TYPE_TRAITS()

View file

@ -44,10 +44,8 @@ public:
virtual FlyString node_name() const override { return "#document-fragment"; }
};
template<>
inline bool is<DocumentFragment>(const Node& node)
{
return node.is_document_fragment();
}
}
AK_BEGIN_TYPE_TRAITS(Web::DocumentFragment)
static bool is_type(const Web::Node& node) { return node.is_document_fragment(); }
AK_END_TYPE_TRAITS()

View file

@ -55,10 +55,8 @@ private:
String m_system_id;
};
template<>
inline bool is<DocumentType>(const Node& node)
{
return node.type() == NodeType::DOCUMENT_TYPE_NODE;
}
}
AK_BEGIN_TYPE_TRAITS(Web::DocumentType)
static bool is_type(const Web::Node& node) { return node.type() == Web::NodeType::DOCUMENT_TYPE_NODE; }
AK_END_TYPE_TRAITS()

View file

@ -271,17 +271,17 @@ String Element::inner_html() const
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
if (child->is_element()) {
builder.append('<');
builder.append(to<Element>(*child).local_name());
builder.append(downcast<Element>(*child).local_name());
builder.append('>');
recurse(*child);
builder.append("</");
builder.append(to<Element>(*child).local_name());
builder.append(downcast<Element>(*child).local_name());
builder.append('>');
}
if (child->is_text()) {
builder.append(to<Text>(*child).data());
builder.append(downcast<Text>(*child).data());
}
}
};

View file

@ -99,10 +99,8 @@ private:
Vector<FlyString> m_classes;
};
template<>
inline bool is<Element>(const Node& node)
{
return node.is_element();
}
}
AK_BEGIN_TYPE_TRAITS(Web::Element)
static bool is_type(const Web::Node& node) { return node.is_element(); }
AK_END_TYPE_TRAITS()

View file

@ -65,8 +65,8 @@ Node::~Node()
const HTMLAnchorElement* Node::enclosing_link_element() const
{
for (auto* node = this; node; node = node->parent()) {
if (is<HTMLAnchorElement>(*node) && to<HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
return to<HTMLAnchorElement>(node);
if (is<HTMLAnchorElement>(*node) && downcast<HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
return downcast<HTMLAnchorElement>(node);
}
return nullptr;
}
@ -160,9 +160,9 @@ String Node::child_text_content() const
return String::empty();
StringBuilder builder;
to<ParentNode>(*this).for_each_child([&](auto& child) {
downcast<ParentNode>(*this).for_each_child([&](auto& child) {
if (is<Text>(child))
builder.append(to<Text>(child).text_content());
builder.append(downcast<Text>(child).text_content());
});
return builder.build();
}
@ -184,14 +184,14 @@ Element* Node::parent_element()
{
if (!parent() || !is<Element>(parent()))
return nullptr;
return to<Element>(parent());
return downcast<Element>(parent());
}
const Element* Node::parent_element() const
{
if (!parent() || !is<Element>(parent()))
return nullptr;
return to<Element>(parent());
return downcast<Element>(parent());
}
RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)

View file

@ -29,6 +29,7 @@
#include <AK/Badge.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/TypeCasts.h>
#include <AK/Vector.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/EventTarget.h>
@ -116,9 +117,9 @@ public:
template<typename T>
const T* first_ancestor_of_type() const;
virtual void inserted_into(Node&) {}
virtual void removed_from(Node&) {}
virtual void children_changed() {}
virtual void inserted_into(Node&) { }
virtual void removed_from(Node&) { }
virtual void children_changed() { }
const LayoutNode* layout_node() const { return m_layout_node; }
LayoutNode* layout_node() { return m_layout_node; }
@ -137,8 +138,8 @@ public:
bool is_link() const;
virtual void document_did_attach_to_frame(Frame&) {}
virtual void document_will_detach_from_frame(Frame&) {}
virtual void document_did_attach_to_frame(Frame&) { }
virtual void document_will_detach_from_frame(Frame&) { }
void set_document(Badge<Document>, Document&);
@ -151,64 +152,12 @@ protected:
bool m_needs_style_update { true };
};
template<typename T>
inline bool is(const Node&)
{
return false;
}
template<typename T>
inline bool is(const Node* node)
{
return !node || is<T>(*node);
}
template<>
inline bool is<Node>(const Node&)
{
return true;
}
template<>
inline bool is<ParentNode>(const Node& node)
{
return node.is_parent_node();
}
template<typename T>
inline const T& to(const Node& node)
{
ASSERT(is<T>(node));
return static_cast<const T&>(node);
}
template<typename T>
inline T* to(Node* node)
{
ASSERT(is<T>(node));
return static_cast<T*>(node);
}
template<typename T>
inline const T* to(const Node* node)
{
ASSERT(is<T>(node));
return static_cast<const T*>(node);
}
template<typename T>
inline T& to(Node& node)
{
ASSERT(is<T>(node));
return static_cast<T&>(node);
}
template<typename T>
inline const T* Node::first_child_of_type() const
{
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<T>(*child))
return to<T>(child);
return downcast<T>(child);
}
return nullptr;
}
@ -218,7 +167,7 @@ inline const T* Node::first_ancestor_of_type() const
{
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (is<T>(*ancestor))
return to<T>(ancestor);
return downcast<T>(ancestor);
}
return nullptr;
}

View file

@ -59,3 +59,7 @@ inline void ParentNode::for_each_child(Callback callback)
}
}
AK_BEGIN_TYPE_TRAITS(Web::ParentNode)
static bool is_type(const Web::Node& node) { return node.is_parent_node(); }
AK_END_TYPE_TRAITS()

View file

@ -43,10 +43,8 @@ private:
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) override;
};
template<>
inline bool is<Text>(const Node& node)
{
return node.is_text();
}
}
AK_BEGIN_TYPE_TRAITS(Web::Text)
static bool is_type(const Web::Node& node) { return node.is_text(); }
AK_END_TYPE_TRAITS()