mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:52:45 +00:00 
			
		
		
		
	LibHTML: Make sure every DOM Node belongs to a Document
This commit is contained in:
		
							parent
							
								
									13860e4dd8
								
							
						
					
					
						commit
						1b8509a0c9
					
				
					 9 changed files with 28 additions and 22 deletions
				
			
		|  | @ -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> body = adopt(*new Element(*this, "body")); | ||||||
|     NonnullRefPtr<Element> html = adopt(*new Element("html")); |     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,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) | Element::Element(Document& document, const String& tag_name) | ||||||
|     : ParentNode(NodeType::ELEMENT_NODE) |     : ParentNode(document, NodeType::ELEMENT_NODE) | ||||||
|     , m_tag_name(tag_name) |     , m_tag_name(tag_name) | ||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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; } | ||||||
|  |  | ||||||
|  | @ -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) | Node::Node(Document& document, NodeType type) | ||||||
|     : m_type(type) |     : m_document(document) | ||||||
|  |     , m_type(type) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -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; | ||||||
| 
 | 
 | ||||||
| protected: |     Document& document() { return m_document; } | ||||||
|     explicit Node(NodeType); |     const Document& document() const { return m_document; } | ||||||
| 
 | 
 | ||||||
|  | protected: | ||||||
|  |     Node(Document&, NodeType); | ||||||
|  | 
 | ||||||
|  |     Document& m_document; | ||||||
|     NodeType m_type { NodeType::INVALID }; |     NodeType m_type { NodeType::INVALID }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -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) |     explicit ParentNode(Document& document, NodeType type) | ||||||
|         : Node(type) |         : Node(document, type) | ||||||
|     { |     { | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -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) | Text::Text(Document& document, const String& data) | ||||||
|     : Node(NodeType::TEXT_NODE) |     : Node(document, NodeType::TEXT_NODE) | ||||||
|     , m_data(data) |     , m_data(data) | ||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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; } | ||||||
|  |  | ||||||
|  | @ -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); |     auto document = adopt(*new Document); | ||||||
|     node_stack.append(doc); |     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; | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling