fix: Initial chardata was being thrown away
Some checks failed
test / test (push) Has been cancelled

This commit is contained in:
2025-10-09 22:20:18 +01:00
parent 489590bdac
commit 1e5227f60c

View File

@@ -44,12 +44,19 @@ pub type Element {
pub fn main() { pub fn main() {
parse_document( 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;'/><!-- ma comment --><![CDATA[Testing&&<haha>]]><?test asuhashd ?></b>", //"<?xml version=\"1.1\" encoding='UTF-8'?>\r\n <!-- hello-world --> \n<b blah:test='1'><a attr='ha&#x20;&#38;#38;ha' battr='baba' ref='&amp;'/><!-- ma comment --><![CDATA[Testing&&<haha>]]><?test asuhashd ?></b>",
// "<doc>
"<doc>
<A a=\"asdf>'&#34;>
asdf
?>%\"/>
<A a='\"\">&#39;&#34;'/>
</doc>",
) )
|> echo |> echo
} }
pub fn default_entities() -> dict.Dict(String, Entity) { fn default_entities() -> dict.Dict(String, Entity) {
dict.from_list([ dict.from_list([
#("lt", InternalEntity("&#60;")), #("lt", InternalEntity("&#60;")),
#("gt", InternalEntity("&#62;")), #("gt", InternalEntity("&#62;")),
@@ -144,7 +151,7 @@ fn parse_content(
content: List(Element), content: List(Element),
) -> Result(#(List(Element), String), Nil) { ) -> Result(#(List(Element), String), Nil) {
use #(chardata, doc) <- result.try(parse_chardata(doc, doctype, "")) use #(chardata, doc) <- result.try(parse_chardata(doc, doctype, ""))
let new_content = case chardata { let content = case chardata {
"" -> content "" -> content
_ -> [Text(chardata), ..content] _ -> [Text(chardata), ..content]
} }
@@ -163,10 +170,10 @@ fn parse_content(
) )
{ {
Ok(#(element, doc)) -> parse_content(doc, doctype, [element, ..content]) Ok(#(element, doc)) -> parse_content(doc, doctype, [element, ..content])
Error(_) -> Ok(#(list.reverse(new_content), doc)) Error(_) -> Ok(#(list.reverse(content), doc))
} }
} }
_ -> Ok(#(list.reverse(new_content), doc)) _ -> Ok(#(list.reverse(content), doc))
} }
} }
@@ -592,7 +599,7 @@ fn parse_digit(doc: String) -> Result(#(String, String), Nil) {
} }
} }
pub fn parse_hex_digit(str: String) -> Result(#(String, String), Nil) { fn parse_hex_digit(str: String) -> Result(#(String, String), Nil) {
case str { case str {
"0" as digit <> tail "0" as digit <> tail
| "1" as digit <> tail | "1" as digit <> tail
@@ -831,7 +838,7 @@ fn try_parsers(
} }
} }
pub fn parse_multiple( fn parse_multiple(
to_parse str: String, to_parse str: String,
with to_run: fn(String) -> Result(#(String, String), Nil), with to_run: fn(String) -> Result(#(String, String), Nil),
) -> Result(#(String, String), Nil) { ) -> Result(#(String, String), Nil) {