mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 22:28:11 +00:00
LibHTML: Add is<ElementType> and to<ElementType> helper functions
These will help us write node-type-aware template functions.
This commit is contained in:
parent
bedb00603c
commit
f52f2736e1
12 changed files with 93 additions and 22 deletions
|
@ -29,11 +29,8 @@ StyleResolver& Document::style_resolver()
|
|||
|
||||
void Document::normalize()
|
||||
{
|
||||
if (first_child() != nullptr && first_child()->is_element()) {
|
||||
const Element& el = static_cast<const Element&>(*first_child());
|
||||
if (el.tag_name() == "html")
|
||||
return;
|
||||
}
|
||||
if (is<HTMLHtmlElement>(first_child()))
|
||||
return;
|
||||
|
||||
NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
|
||||
NonnullRefPtr<Element> html = adopt(*new Element(*this, "html"));
|
||||
|
|
|
@ -80,3 +80,9 @@ private:
|
|||
Color m_active_link_color { Color::Red };
|
||||
Color m_visited_link_color { Color::Magenta };
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<Document>(const Node& node)
|
||||
{
|
||||
return node.is_document();
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ public:
|
|||
Element(Document&, const String& tag_name);
|
||||
virtual ~Element() override;
|
||||
|
||||
virtual String tag_name() const override { return m_tag_name; }
|
||||
virtual String tag_name() const final { return m_tag_name; }
|
||||
|
||||
String attribute(const String& name) const;
|
||||
void set_attribute(const String& name, const String& value);
|
||||
|
@ -54,3 +54,9 @@ private:
|
|||
String m_tag_name;
|
||||
Vector<Attribute> m_attributes;
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<Element>(const Node& node)
|
||||
{
|
||||
return node.is_element();
|
||||
}
|
||||
|
|
|
@ -9,3 +9,9 @@ public:
|
|||
|
||||
String href() const { return attribute("href"); }
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<HTMLAnchorElement>(const Node& node)
|
||||
{
|
||||
return is<Element>(node) && to<Element>(node).tag_name().to_lowercase() == "a";
|
||||
}
|
||||
|
|
|
@ -12,3 +12,9 @@ public:
|
|||
private:
|
||||
virtual bool is_html_element() const final { return true; }
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<HTMLElement>(const Node& node)
|
||||
{
|
||||
return node.is_html_element();
|
||||
}
|
||||
|
|
|
@ -7,3 +7,9 @@ public:
|
|||
HTMLHtmlElement(Document&, const String& tag_name);
|
||||
virtual ~HTMLHtmlElement() override;
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<HTMLHtmlElement>(const Node& node)
|
||||
{
|
||||
return is<Element>(node) && to<Element>(node).tag_name().to_lowercase() == "html";
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const
|
|||
|
||||
const HTMLAnchorElement* Node::enclosing_link_element() const
|
||||
{
|
||||
if (is_element() && tag_name().to_lowercase() == "a")
|
||||
if (is<HTMLAnchorElement>(*this))
|
||||
return static_cast<const HTMLAnchorElement*>(this);
|
||||
return parent() ? parent()->enclosing_link_element() : nullptr;
|
||||
}
|
||||
|
|
|
@ -74,3 +74,41 @@ protected:
|
|||
mutable LayoutNode* m_layout_node { nullptr };
|
||||
NodeType m_type { NodeType::INVALID };
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -19,3 +19,9 @@ private:
|
|||
|
||||
String m_data;
|
||||
};
|
||||
|
||||
template<>
|
||||
inline bool is<Text>(const Node& node)
|
||||
{
|
||||
return node.is_text();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue