diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-09-29 11:43:07 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-29 11:43:07 +0200 |
commit | 1b8509a0c992d9fcdb47e1618b3b1b0de89b8f21 (patch) | |
tree | c6ddc75f6e749d6ee92f2f3beb7fea4d5e52ab46 /Libraries/LibHTML | |
parent | 13860e4dd843d84f78b7956cba07dec083ddf0b3 (diff) | |
download | serenity-1b8509a0c992d9fcdb47e1618b3b1b0de89b8f21.zip |
LibHTML: Make sure every DOM Node belongs to a Document
Diffstat (limited to 'Libraries/LibHTML')
-rw-r--r-- | Libraries/LibHTML/DOM/Document.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Element.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Element.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Node.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Node.h | 7 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/ParentNode.h | 4 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Text.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Text.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTML/Parser/HTMLParser.cpp | 14 |
9 files changed, 27 insertions, 21 deletions
diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp index 0e24019598..766be050ba 100644 --- a/Libraries/LibHTML/DOM/Document.cpp +++ b/Libraries/LibHTML/DOM/Document.cpp @@ -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); diff --git a/Libraries/LibHTML/DOM/Element.cpp b/Libraries/LibHTML/DOM/Element.cpp index 40f3fc53fe..780dc1b08e 100644 --- a/Libraries/LibHTML/DOM/Element.cpp +++ b/Libraries/LibHTML/DOM/Element.cpp @@ -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) { } diff --git a/Libraries/LibHTML/DOM/Element.h b/Libraries/LibHTML/DOM/Element.h index 1b2d2b0ce7..42e564ef2e 100644 --- a/Libraries/LibHTML/DOM/Element.h +++ b/Libraries/LibHTML/DOM/Element.h @@ -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; } diff --git a/Libraries/LibHTML/DOM/Node.cpp b/Libraries/LibHTML/DOM/Node.cpp index 6e60448850..b337cb1b90 100644 --- a/Libraries/LibHTML/DOM/Node.cpp +++ b/Libraries/LibHTML/DOM/Node.cpp @@ -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) { } diff --git a/Libraries/LibHTML/DOM/Node.h b/Libraries/LibHTML/DOM/Node.h index 506da734a5..3838efdfa0 100644 --- a/Libraries/LibHTML/DOM/Node.h +++ b/Libraries/LibHTML/DOM/Node.h @@ -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; + Document& document() { return m_document; } + const Document& document() const { return m_document; } + protected: - explicit Node(NodeType); + Node(Document&, NodeType); + Document& m_document; NodeType m_type { NodeType::INVALID }; }; diff --git a/Libraries/LibHTML/DOM/ParentNode.h b/Libraries/LibHTML/DOM/ParentNode.h index 9773cb31a2..b563320819 100644 --- a/Libraries/LibHTML/DOM/ParentNode.h +++ b/Libraries/LibHTML/DOM/ParentNode.h @@ -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) { } }; diff --git a/Libraries/LibHTML/DOM/Text.cpp b/Libraries/LibHTML/DOM/Text.cpp index 8d8d8ace25..6b41f7c8f8 100644 --- a/Libraries/LibHTML/DOM/Text.cpp +++ b/Libraries/LibHTML/DOM/Text.cpp @@ -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) { } diff --git a/Libraries/LibHTML/DOM/Text.h b/Libraries/LibHTML/DOM/Text.h index d09ab12740..79b3d6730d 100644 --- a/Libraries/LibHTML/DOM/Text.h +++ b/Libraries/LibHTML/DOM/Text.h @@ -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; } diff --git a/Libraries/LibHTML/Parser/HTMLParser.cpp b/Libraries/LibHTML/Parser/HTMLParser.cpp index f53474fdad..a7c39692b2 100644 --- a/Libraries/LibHTML/Parser/HTMLParser.cpp +++ b/Libraries/LibHTML/Parser/HTMLParser.cpp @@ -6,9 +6,9 @@ #include <ctype.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) @@ -38,8 +38,8 @@ NonnullRefPtr<Document> parse_html(const String& html) { 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 { Free = 0, @@ -76,7 +76,7 @@ NonnullRefPtr<Document> parse_html(const String& html) if (new_state == State::BeforeAttributeValue) attribute_value_buffer.clear(); 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); } state = new_state; @@ -89,7 +89,7 @@ NonnullRefPtr<Document> parse_html(const String& html) }; 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(); new_element->set_attributes(move(attributes)); node_stack.append(new_element); @@ -256,5 +256,5 @@ NonnullRefPtr<Document> parse_html(const String& html) ASSERT_NOT_REACHED(); } } - return doc; + return document; } |