Skip to content

Commit ca1c928

Browse files
committed
feat(html): implement HTMLDocument::title
Reads the title via lxb_html_document_title; a missing title yields an empty string per the declared contract. Adds a test.
1 parent 94d3e95 commit ca1c928

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/html/HTMLDocument.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ DOMElementView HTMLDocument::body() const {
5252
return DOMElementView(body);
5353
}
5454

55+
std::string_view HTMLDocument::title() const {
56+
// Points into the document's <title> text, which outlives this view; NULL
57+
// (no <title>) maps to an empty string per the declared contract.
58+
size_t length = 0;
59+
const lxb_char_t* title = lxb_html_document_title(document_, &length);
60+
if (!title) {
61+
return {};
62+
}
63+
return std::string_view(reinterpret_cast<const char*>(title), length);
64+
}
65+
5566
std::optional<DOMElementView> HTMLDocument::query(const CompiledSelector& selector) const {
5667
return as_element().query(selector);
5768
}

tests/HTMLParseTests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ TEST_CASE("HTML try_parse reports failure as a value, not an exception") {
8787
REQUIRE(parsed->body().contains_class("class1"));
8888
}
8989

90+
TEST_CASE("HTMLDocument title returns the <title> text") {
91+
HTMLParser parser;
92+
93+
HTMLDocument with_title = parser.parse(
94+
"<!DOCTYPE html><html><head><title>Hello World</title></head><body></body></html>");
95+
REQUIRE(with_title.title() == "Hello World");
96+
97+
HTMLDocument without_title = parser.parse(
98+
"<!DOCTYPE html><html><head></head><body></body></html>");
99+
REQUIRE(without_title.title().empty());
100+
}
101+
90102
TEST_CASE("HTML document iterate") {
91103
auto check_tags = [](HTMLDocument& document) -> bool {
92104
bool success = true;

0 commit comments

Comments
 (0)