mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:22:45 +00:00 
			
		
		
		
	LibHTML: Add parse_html_fragment()
This function parses a partial DOM and returns it wrapped in a document fragment node (DocumentFragment.) There are now two entrances into the HTML parser, one for parsing full documents, and one for parsing fragments. Internally the both wrap the same parsing function.
This commit is contained in:
		
							parent
							
								
									f404a1dc16
								
							
						
					
					
						commit
						635717ed0f
					
				
					 2 changed files with 28 additions and 8 deletions
				
			
		|  | @ -2,6 +2,7 @@ | ||||||
| #include <AK/NonnullRefPtrVector.h> | #include <AK/NonnullRefPtrVector.h> | ||||||
| #include <AK/StringBuilder.h> | #include <AK/StringBuilder.h> | ||||||
| #include <LibHTML/DOM/Comment.h> | #include <LibHTML/DOM/Comment.h> | ||||||
|  | #include <LibHTML/DOM/DocumentFragment.h> | ||||||
| #include <LibHTML/DOM/DocumentType.h> | #include <LibHTML/DOM/DocumentType.h> | ||||||
| #include <LibHTML/DOM/Element.h> | #include <LibHTML/DOM/Element.h> | ||||||
| #include <LibHTML/DOM/ElementFactory.h> | #include <LibHTML/DOM/ElementFactory.h> | ||||||
|  | @ -33,13 +34,10 @@ static bool is_self_closing_tag(const StringView& tag_name) | ||||||
|         || tag_name == "wbr"; |         || tag_name == "wbr"; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| NonnullRefPtr<Document> parse_html(const StringView& html, const URL& url) | static bool parse_html(const StringView& html, Document& document, ParentNode& root) | ||||||
| { | { | ||||||
|     NonnullRefPtrVector<ParentNode> node_stack; |     NonnullRefPtrVector<ParentNode> node_stack; | ||||||
| 
 |     node_stack.append(root); | ||||||
|     auto document = adopt(*new Document); |  | ||||||
|     document->set_url(url); |  | ||||||
|     node_stack.append(document); |  | ||||||
| 
 | 
 | ||||||
|     enum class State { |     enum class State { | ||||||
|         Free = 0, |         Free = 0, | ||||||
|  | @ -309,6 +307,27 @@ NonnullRefPtr<Document> parse_html(const StringView& html, const URL& url) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     return true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | RefPtr<DocumentFragment> parse_html_fragment(Document& document, const StringView& html) | ||||||
|  | { | ||||||
|  |     auto fragment = adopt(*new DocumentFragment(document)); | ||||||
|  |     if (!parse_html(html, document, *fragment)) | ||||||
|  |         return nullptr; | ||||||
|  |     return fragment; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | NonnullRefPtr<Document> parse_html(const StringView& html, const URL& url) | ||||||
|  | { | ||||||
|  |     auto document = adopt(*new Document); | ||||||
|  |     document->set_url(url); | ||||||
|  | 
 | ||||||
|  |     bool success = parse_html(html, *document, *document); | ||||||
|  |     ASSERT(success); | ||||||
|  | 
 | ||||||
|  |     document->fixup(); | ||||||
|  | 
 | ||||||
|     Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) { |     Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) { | ||||||
|         for (auto* child = node.first_child(); child; child = child->next_sibling()) { |         for (auto* child = node.first_child(); child; child = child->next_sibling()) { | ||||||
|             fire_insertion_callbacks(*child); |             fire_insertion_callbacks(*child); | ||||||
|  | @ -316,8 +335,7 @@ NonnullRefPtr<Document> parse_html(const StringView& html, const URL& url) | ||||||
|         if (node.parent()) |         if (node.parent()) | ||||||
|             node.inserted_into(*node.parent()); |             node.inserted_into(*node.parent()); | ||||||
|     }; |     }; | ||||||
|  |     fire_insertion_callbacks(document); | ||||||
| 
 | 
 | ||||||
|     document->fixup(); |  | ||||||
|     fire_insertion_callbacks(*document); |  | ||||||
|     return document; |     return document; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,5 +3,7 @@ | ||||||
| #include <AK/NonnullRefPtr.h> | #include <AK/NonnullRefPtr.h> | ||||||
| #include <LibHTML/DOM/Document.h> | #include <LibHTML/DOM/Document.h> | ||||||
| 
 | 
 | ||||||
| NonnullRefPtr<Document> parse_html(const StringView&, const URL& = URL()); | class DocumentFragment; | ||||||
| 
 | 
 | ||||||
|  | NonnullRefPtr<Document> parse_html(const StringView&, const URL& = URL()); | ||||||
|  | RefPtr<DocumentFragment> parse_html_fragment(Document&, const StringView&); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling