Skip to content

Commit

Permalink
web-xml: Pase text elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Apr 16, 2024
1 parent f34cce1 commit bf16ef4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/web/web-xml/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ Res<> Parser::_parseCharData(Io::SScan &s) {
logDebug("Parsing character data");

while (
s.match(RE_CHARDATA) == Match::YES and
s.match("]]>"_re) == Match::NO and not s.ended()
s.ahead(RE_CHARDATA) and
not s.ahead("]]>"_re) and
not s.ended()
)
_append(s.next());

Expand Down Expand Up @@ -229,9 +230,8 @@ Res<> Parser::_parseMisc(Io::SScan &s, Dom::Node &parent) {
try$(_parsePi(s));
else if (s.match(RE_S) != Match::NO)
try$(_parseS(s));
else {
else
return Error::invalidData("unexpected character");
}

rollback.disarm();
return Ok();
Expand Down Expand Up @@ -277,6 +277,10 @@ Res<Strong<Dom::Element>> Parser::_parseElement(Io::SScan &s, Ns ns) {
auto el = r.unwrap();
try$(_parseContent(s, ns, *el));
try$(_parseEndTag(s, *el));
auto te = _flush();
if (te.len())
el->appendChild(makeStrong<Dom::Text>(te));

rollback.disarm();
return Ok(el);
}
Expand Down Expand Up @@ -337,6 +341,8 @@ Res<String> Parser::_parseAttValue(Io::SScan &s) {

logDebug("Parsing attribute value");

StringBuilder sb;

auto rollback = s.rollbackPoint();

auto quote = s.next();
Expand All @@ -345,9 +351,9 @@ Res<String> Parser::_parseAttValue(Io::SScan &s) {

while (s.curr() != quote and not s.ended()) {
if (auto r = _parseReference(s))
_append(r.unwrap());
sb.append(r.unwrap());
else
_append(s.next());
sb.append(s.next());
}

if (s.curr() != quote)
Expand All @@ -357,7 +363,7 @@ Res<String> Parser::_parseAttValue(Io::SScan &s) {

rollback.disarm();

return Ok(_flush());
return Ok(sb.take());
}

Res<> Parser::_parseEndTag(Io::SScan &s, Dom::Element &el) {
Expand Down Expand Up @@ -392,13 +398,19 @@ Res<> Parser::_parseContentItem(Io::SScan &s, Ns ns, Dom::Element &el) {
el.appendChild(r.unwrap());
return Ok();
} else if (auto r = _parseReference(s)) {
auto te = _flush();
if (te.len())
el.appendChild(makeStrong<Dom::Text>(te));
_append(r.unwrap());
return Ok();
} else if (auto r = _parseCDSect(s)) {
return Ok();
} else if (auto r = _parsePi(s)) {
return Ok();
} else if (auto r = _parseComment(s)) {
auto te = _flush();
if (te.len())
el.appendChild(makeStrong<Dom::Text>(te));
el.appendChild(r.unwrap());
return Ok();
} else {
Expand Down
1 change: 1 addition & 0 deletions src/web/web-xml/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <web-dom/comment.h>
#include <web-dom/document.h>
#include <web-dom/element.h>
#include <web-dom/text.h>

namespace Web::Xml {

Expand Down
13 changes: 13 additions & 0 deletions src/web/web-xml/tests/test-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ test$("parse-attr") {
return Ok();
}

test$("parse-text") {
auto s = Io::SScan("<html>text</html>");
auto p = Parser();
auto doc = try$(p.parse(s, Web::HTML));
auto first = doc->firstChild();
auto el = try$(first.cast<Dom::Element>());
expect$(el->hasChildren());
auto text = el->firstChild();
expect$(text->nodeType() == Dom::NodeType::TEXT);
expect$(try$(text.cast<Dom::Text>())->data == "text");
return Ok();
}

test$("parse-nested-tags") {
auto s = Io::SScan("<html><head></head><body></body></html>");
auto p = Parser();
Expand Down

0 comments on commit bf16ef4

Please sign in to comment.