srctree

Robin Linden parent dba944b3 3cdb1cfb
html: Make sure any plain text content is emitted on eof

inlinesplit
html/parser.cpp added: 23, removed: 3, total 20
@@ -264,7 +264,12 @@ void Parser::operator()(html2::EndOfFileToken const &) {
}
 
if (!open_elements_.empty() && open_elements_.top()->name == "html" && open_elements_.top()->children.size() == 1) {
doc_.html().children.emplace_back(dom::Element{.name = "body"});
auto &body = doc_.html().children.emplace_back(dom::Element{.name = "body"});
open_elements_.push(&std::get<dom::Element>(body));
}
 
if (!open_elements_.empty()) {
generate_text_node_if_needed();
}
 
if (!open_elements_.empty() && open_elements_.top()->name == "body") {
 
html/parser_test.cpp added: 23, removed: 3, total 20
@@ -270,5 +270,20 @@ int main() {
expect_eq(title.name, "title");
});
 
etest::test("special rules, just text is fine too", [] {
auto html = html::parse("hello?"sv).html();
require_eq(html.children.size(), std::size_t{2});
 
auto const &head = std::get<dom::Element>(html.children[0]);
expect_eq(head.name, "head");
 
auto const &body = std::get<dom::Element>(html.children[1]);
expect_eq(body.name, "body");
 
require_eq(body.children.size(), std::size_t{1});
auto const &body_text = std::get<dom::Text>(body.children[0]);
expect_eq(body_text, dom::Text{"hello?"});
});
 
return etest::run_all_tests();
}