feat: Added comment parsing in elements
Some checks failed
test / test (push) Has been cancelled

This commit is contained in:
2025-10-09 18:32:12 +01:00
parent 8e0e6f988a
commit 43d0638fd8

View File

@@ -36,11 +36,12 @@ pub type Element {
EmptyElem(name: String, attrs: List(Attribute))
Element(name: String, attrs: List(Attribute), elements: List(Element))
Text(content: String)
Comment(content: String)
}
pub fn main() {
parse_document(
"<?xml version=\"1.1\" encoding='UTF-8'?>\r\n <!-- hello-world --> \n<b><a attr='ha&#x20;&#38;#38;ha' battr='baba' ref='&amp;'/></b>",
"<?xml version=\"1.1\" encoding='UTF-8'?>\r\n <!-- hello-world --> \n<b><a attr='ha&#x20;&#38;#38;ha' battr='baba' ref='&amp;'/><!-- ma comment --></b>",
)
|> echo
}
@@ -147,7 +148,20 @@ fn parse_content(
case doc {
"<" <> _ -> {
case try_parsers([parse_element(_, doctype)], doc) {
case
try_parsers(
[
parse_element(_, doctype),
fn(doc) {
case parse_comment(doc) {
Ok(#(comment, doc)) -> Ok(#(Comment(comment), doc))
Error(_) -> Error(Nil)
}
},
],
doc,
)
{
Ok(#(element, doc)) -> parse_content(doc, doctype, [element, ..content])
Error(_) -> Ok(#(list.reverse(new_content), doc))
}