浏览代码

LibHTML: Make sure every DOM Node belongs to a Document

Andreas Kling 5 年之前
父节点
当前提交
1b8509a0c9

+ 3 - 3
Libraries/LibHTML/DOM/Document.cpp

@@ -5,7 +5,7 @@
 #include <stdio.h>
 #include <stdio.h>
 
 
 Document::Document()
 Document::Document()
-    : ParentNode(NodeType::DOCUMENT_NODE)
+    : ParentNode(*this, NodeType::DOCUMENT_NODE)
 {
 {
 }
 }
 
 
@@ -28,8 +28,8 @@ void Document::normalize()
             return;
             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);
     html->append_child(body);
     this->donate_all_children_to(body);
     this->donate_all_children_to(body);
     this->append_child(html);
     this->append_child(html);

+ 2 - 2
Libraries/LibHTML/DOM/Element.cpp

@@ -2,8 +2,8 @@
 #include <LibHTML/Layout/LayoutBlock.h>
 #include <LibHTML/Layout/LayoutBlock.h>
 #include <LibHTML/Layout/LayoutInline.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)
     , m_tag_name(tag_name)
 {
 {
 }
 }

+ 1 - 1
Libraries/LibHTML/DOM/Element.h

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

+ 3 - 2
Libraries/LibHTML/DOM/Node.cpp

@@ -7,8 +7,9 @@
 #include <LibHTML/Layout/LayoutInline.h>
 #include <LibHTML/Layout/LayoutInline.h>
 #include <LibHTML/Layout/LayoutText.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)
 {
 {
 }
 }
 
 

+ 6 - 1
Libraries/LibHTML/DOM/Node.h

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

+ 2 - 2
Libraries/LibHTML/DOM/ParentNode.h

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

+ 2 - 2
Libraries/LibHTML/DOM/Text.cpp

@@ -1,8 +1,8 @@
 #include <LibHTML/DOM/Text.h>
 #include <LibHTML/DOM/Text.h>
 #include <LibHTML/Layout/LayoutText.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)
     , m_data(data)
 {
 {
 }
 }

+ 1 - 1
Libraries/LibHTML/DOM/Text.h

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

+ 7 - 7
Libraries/LibHTML/Parser/HTMLParser.cpp

@@ -6,9 +6,9 @@
 #include <ctype.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <stdio.h>
 
 
-static NonnullRefPtr<Element> create_element(const String& tag_name)
+static NonnullRefPtr<Element> create_element(Document& document, const String& tag_name)
 {
 {
-    return adopt(*new Element(tag_name));
+    return adopt(*new Element(document, tag_name));
 }
 }
 
 
 static bool is_valid_in_attribute_name(char ch)
 static bool is_valid_in_attribute_name(char ch)
@@ -38,8 +38,8 @@ NonnullRefPtr<Document> parse_html(const String& html)
 {
 {
     NonnullRefPtrVector<ParentNode> node_stack;
     NonnullRefPtrVector<ParentNode> node_stack;
 
 
-    auto doc = adopt(*new Document);
-    node_stack.append(doc);
+    auto document = adopt(*new Document);
+    node_stack.append(document);
 
 
     enum class State {
     enum class State {
         Free = 0,
         Free = 0,
@@ -76,7 +76,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
         if (new_state == State::BeforeAttributeValue)
         if (new_state == State::BeforeAttributeValue)
             attribute_value_buffer.clear();
             attribute_value_buffer.clear();
         if (state == State::Free && !text_buffer.string_view().is_empty()) {
         if (state == State::Free && !text_buffer.string_view().is_empty()) {
-            auto text_node = adopt(*new Text(text_buffer.to_string()));
+            auto text_node = adopt(*new Text(document, text_buffer.to_string()));
             node_stack.last().append_child(text_node);
             node_stack.last().append_child(text_node);
         }
         }
         state = new_state;
         state = new_state;
@@ -89,7 +89,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
     };
     };
 
 
     auto open_tag = [&] {
     auto open_tag = [&] {
-        auto new_element = create_element(String::copy(tag_name_buffer));
+        auto new_element = create_element(document, String::copy(tag_name_buffer));
         tag_name_buffer.clear();
         tag_name_buffer.clear();
         new_element->set_attributes(move(attributes));
         new_element->set_attributes(move(attributes));
         node_stack.append(new_element);
         node_stack.append(new_element);
@@ -256,5 +256,5 @@ NonnullRefPtr<Document> parse_html(const String& html)
             ASSERT_NOT_REACHED();
             ASSERT_NOT_REACHED();
         }
         }
     }
     }
-    return doc;
+    return document;
 }
 }