Skip to content

Commit

Permalink
vaev-markup: Added accessor for document title and url.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 12, 2024
1 parent a87c761 commit 82c3ded
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/web/vaev-driver/fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Vaev::Driver {

Res<Strong<Markup::Document>> loadDocument(Mime::Url const &, Mime::Mime const &mime, Io::Reader &reader) {
auto dom = makeStrong<Markup::Document>();
Res<Strong<Markup::Document>> loadDocument(Mime::Url const &url, Mime::Mime const &mime, Io::Reader &reader) {
auto dom = makeStrong<Markup::Document>(url);
auto buf = try$(Io::readAllUtf8(reader));

if (mime.is("text/html"_mime)) {
Expand All @@ -21,13 +21,13 @@ Res<Strong<Markup::Document>> loadDocument(Mime::Url const &, Mime::Mime const &
} else if (mime.is("application/xhtml+xml"_mime)) {
Io::SScan scan{buf};
Markup::XmlParser parser;
dom = try$(parser.parse(scan, HTML));
try$(parser.parse(scan, HTML, *dom));

return Ok(dom);
} else if (mime.is("image/svg+xml"_mime)) {
Io::SScan scan{buf};
Markup::XmlParser parser;
dom = try$(parser.parse(scan, SVG));
try$(parser.parse(scan, SVG, *dom));

return Ok(dom);
} else {
Expand All @@ -40,7 +40,7 @@ Res<Strong<Markup::Document>> viewSource(Mime::Url const &url) {
auto file = try$(Sys::File::open(url));
auto buf = try$(Io::readAllUtf8(file));

auto dom = makeStrong<Markup::Document>();
auto dom = makeStrong<Markup::Document>(url);

auto body = makeStrong<Markup::Element>(Html::BODY);
dom->appendChild(body);
Expand All @@ -55,7 +55,7 @@ Res<Strong<Markup::Document>> viewSource(Mime::Url const &url) {
}

Res<Strong<Markup::Document>> indexOf(Mime::Url const &url) {
auto dom = makeStrong<Markup::Document>();
auto dom = makeStrong<Markup::Document>(url);

auto body = makeStrong<Markup::Element>(Html::BODY);
dom->appendChild(body);
Expand Down Expand Up @@ -106,7 +106,7 @@ Res<Strong<Markup::Document>> fetchDocument(Mime::Url const &url) {
if (not mime.has())
return Error::invalidInput("cannot determine MIME type");

auto dom = makeStrong<Markup::Document>();
auto dom = makeStrong<Markup::Document>(url);
auto file = try$(Sys::File::open(url));
return loadDocument(url, *mime, file);
}
Expand Down
21 changes: 21 additions & 0 deletions src/web/vaev-markup/dom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "dom.h"

namespace Vaev::Markup {

// MARK: Document --------------------------------------------------------------

String Document::title() const {
String res = ""s;
iterDepthFirst([&](auto &node) {
if (auto element = node.template is<Element>()) {
if (element->tagName == Html::TITLE) {
res = element->textContent();
return Iter::BREAK;
}
}
return Iter::CONTINUE;
});
return res;
}

} // namespace Vaev::Markup
38 changes: 34 additions & 4 deletions src/web/vaev-markup/dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <karm-base/rc.h>
#include <karm-base/vec.h>
#include <karm-io/emit.h>
#include <karm-mime/url.h>

#include "tags.h"

Expand All @@ -30,6 +31,11 @@ enum struct NodeType {
_LEN,
};

enum struct Iter {
CONTINUE,
BREAK,
};

// https://dom.spec.whatwg.org/#interface-node
struct Node :
Meta::Static {
Expand Down Expand Up @@ -140,6 +146,20 @@ struct Node :
return _parentIndex() < parentNode()._children.len() - 1;
}

// MARK: Iter

Iter iterDepthFirst(this auto &self, auto f) {
if (f(self) == Iter::BREAK)
return Iter::BREAK;
for (auto &child : self._children) {
if (child->iterDepthFirst(f) == Iter::BREAK)
return Iter::BREAK;
}
return Iter::CONTINUE;
}

// MARK: Repr

virtual void _repr(Io::Emit &) const {}

void repr(Io::Emit &e) const {
Expand Down Expand Up @@ -178,11 +198,22 @@ enum struct QuirkMode {
struct Document : public Node {
static constexpr auto TYPE = NodeType::DOCUMENT;

Mime::Url _url;
QuirkMode quirkMode{QuirkMode::NO};

Document(Mime::Url url)
: _url(url) {
}

NodeType nodeType() const override {
return TYPE;
}

String title() const;

Mime::Url const &url() const {
return _url;
}
};

// MARK: DocumentType ----------------------------------------------------------
Expand Down Expand Up @@ -298,7 +329,7 @@ struct TokenList {
}

void add(Str token) {
if (not ::contains(_tokens, token))
if (not::contains(_tokens, token))
_tokens.pushBack(token);
}

Expand All @@ -316,7 +347,7 @@ struct TokenList {
}

bool replace(Str oldToken, Str newToken) {
if (not ::contains(_tokens, oldToken))
if (not::contains(_tokens, oldToken))
return false;
_tokens.removeAll(oldToken);
_tokens.pushBack(newToken);
Expand Down Expand Up @@ -356,9 +387,8 @@ struct Element : public Node {
panic("textContent is not implemented for elements with multiple children");

auto const &child = *_children[0];
if (auto text = child.is<Text>()) {
if (auto text = child.is<Text>())
return text->data;
}

panic("textContent is not implemented for elements with children other than text nodes");
}
Expand Down
12 changes: 5 additions & 7 deletions src/web/vaev-markup/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ namespace Vaev::Markup {
// 2 MARK: Documents
// https://www.w3.org/TR/xml/#sec-documents

Res<Strong<Document>> XmlParser::parse(Io::SScan &s, Ns ns) {
Res<> XmlParser::parse(Io::SScan &s, Ns ns, Markup::Document &doc) {
// document :: = prolog element Misc *

auto doc = makeStrong<Document>();
try$(_parseProlog(s, *doc));
doc->appendChild(try$(_parseElement(s, ns)));
while (_parseMisc(s, *doc))
try$(_parseProlog(s, doc));
doc.appendChild(try$(_parseElement(s, ns)));
while (_parseMisc(s, doc))
;

return Ok(doc);
return Ok();
}

// 2.2 MARK: Characters
Expand Down
2 changes: 1 addition & 1 deletion src/web/vaev-markup/xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Vaev::Markup {

struct XmlParser {
Res<Strong<Document>> parse(Io::SScan &s, Ns ns);
Res<> parse(Io::SScan &s, Ns ns, Markup::Document &doc);

Res<> _parseS(Io::SScan &s);

Expand Down

0 comments on commit 82c3ded

Please sign in to comment.