Skip to content

Commit

Permalink
LibHTML: Add parse_html_fragment()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Nov 6, 2019
1 parent f404a1d commit 635717e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 25 additions & 7 deletions Libraries/LibHTML/Parser/HTMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/StringBuilder.h>
#include <LibHTML/DOM/Comment.h>
#include <LibHTML/DOM/DocumentFragment.h>
#include <LibHTML/DOM/DocumentType.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/ElementFactory.h>
Expand Down Expand Up @@ -33,13 +34,10 @@ static bool is_self_closing_tag(const StringView& tag_name)
|| 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;

auto document = adopt(*new Document);
document->set_url(url);
node_stack.append(document);
node_stack.append(root);

enum class State {
Free = 0,
Expand Down Expand Up @@ -309,15 +307,35 @@ 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) {
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
fire_insertion_callbacks(*child);
}
if (node.parent())
node.inserted_into(*node.parent());
};
fire_insertion_callbacks(document);

document->fixup();
fire_insertion_callbacks(*document);
return document;
}
4 changes: 3 additions & 1 deletion Libraries/LibHTML/Parser/HTMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#include <AK/NonnullRefPtr.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&);

0 comments on commit 635717e

Please sign in to comment.