diff --git a/Libraries/LibHTML/Parser/HTMLParser.cpp b/Libraries/LibHTML/Parser/HTMLParser.cpp
index 705e015bf9..25cf52968e 100644
--- a/Libraries/LibHTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibHTML/Parser/HTMLParser.cpp
@@ -2,6 +2,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -33,13 +34,10 @@ static bool is_self_closing_tag(const StringView& tag_name)
|| tag_name == "wbr";
}
-NonnullRefPtr parse_html(const StringView& html, const URL& url)
+static bool parse_html(const StringView& html, Document& document, ParentNode& root)
{
NonnullRefPtrVector node_stack;
-
- auto document = adopt(*new Document);
- document->set_url(url);
- node_stack.append(document);
+ node_stack.append(root);
enum class State {
Free = 0,
@@ -309,6 +307,27 @@ NonnullRefPtr parse_html(const StringView& html, const URL& url)
}
}
+ return true;
+}
+
+RefPtr 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 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 fire_insertion_callbacks = [&](Node& node) {
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
fire_insertion_callbacks(*child);
@@ -316,8 +335,7 @@ NonnullRefPtr parse_html(const StringView& html, const URL& url)
if (node.parent())
node.inserted_into(*node.parent());
};
+ fire_insertion_callbacks(document);
- document->fixup();
- fire_insertion_callbacks(*document);
return document;
}
diff --git a/Libraries/LibHTML/Parser/HTMLParser.h b/Libraries/LibHTML/Parser/HTMLParser.h
index 7e0bf177ab..f852a158a7 100644
--- a/Libraries/LibHTML/Parser/HTMLParser.h
+++ b/Libraries/LibHTML/Parser/HTMLParser.h
@@ -3,5 +3,7 @@
#include
#include
-NonnullRefPtr parse_html(const StringView&, const URL& = URL());
+class DocumentFragment;
+NonnullRefPtr parse_html(const StringView&, const URL& = URL());
+RefPtr parse_html_fragment(Document&, const StringView&);