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

LibHTML: Make sure every DOM Node belongs to a Document

This commit is contained in:
Andreas Kling 2019-09-29 11:43:07 +02:00
parent 13860e4dd8
commit 1b8509a0c9
9 changed files with 28 additions and 22 deletions

View file

@ -5,7 +5,7 @@
#include <stdio.h>
Document::Document()
: ParentNode(NodeType::DOCUMENT_NODE)
: ParentNode(*this, NodeType::DOCUMENT_NODE)
{
}
@ -28,8 +28,8 @@ void Document::normalize()
return;
}
NonnullRefPtr<Element> body = adopt(*new Element("body"));
NonnullRefPtr<Element> html = adopt(*new Element("html"));
NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
NonnullRefPtr<Element> html = adopt(*new Element(*this, "html"));
html->append_child(body);
this->donate_all_children_to(body);
this->append_child(html);

View file

@ -2,8 +2,8 @@
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutInline.h>
Element::Element(const String& tag_name)
: ParentNode(NodeType::ELEMENT_NODE)
Element::Element(Document& document, const String& tag_name)
: ParentNode(document, NodeType::ELEMENT_NODE)
, m_tag_name(tag_name)
{
}

View file

@ -23,7 +23,7 @@ private:
class Element : public ParentNode {
public:
explicit Element(const String& tag_name);
Element(Document&, const String& tag_name);
virtual ~Element() override;
virtual String tag_name() const override { return m_tag_name; }

View file

@ -7,8 +7,9 @@
#include <LibHTML/Layout/LayoutInline.h>
#include <LibHTML/Layout/LayoutText.h>
Node::Node(NodeType type)
: m_type(type)
Node::Node(Document& document, NodeType type)
: m_document(document)
, m_type(type)
{
}

View file

@ -13,6 +13,7 @@ enum class NodeType : unsigned {
DOCUMENT_NODE = 9,
};
class Document;
class ParentNode;
class LayoutNode;
class StyleResolver;
@ -33,8 +34,12 @@ public:
virtual String tag_name() const = 0;
protected:
explicit Node(NodeType);
Document& document() { return m_document; }
const Document& document() const { return m_document; }
protected:
Node(Document&, NodeType);
Document& m_document;
NodeType m_type { NodeType::INVALID };
};

View file

@ -8,8 +8,8 @@ public:
template<typename F> void for_each_child(F);
protected:
explicit ParentNode(NodeType type)
: Node(type)
explicit ParentNode(Document& document, NodeType type)
: Node(document, type)
{
}
};

View file

@ -1,8 +1,8 @@
#include <LibHTML/DOM/Text.h>
#include <LibHTML/Layout/LayoutText.h>
Text::Text(const String& data)
: Node(NodeType::TEXT_NODE)
Text::Text(Document& document, const String& data)
: Node(document, NodeType::TEXT_NODE)
, m_data(data)
{
}

View file

@ -5,7 +5,7 @@
class Text final : public Node {
public:
explicit Text(const String&);
explicit Text(Document&, const String&);
virtual ~Text() override;
const String& data() const { return m_data; }