Skip to content

Commit

Permalink
Merge pull request #17 from typeable/cdata
Browse files Browse the repository at this point in the history
Skip over CDATA
  • Loading branch information
ndmitchell authored Aug 25, 2024
2 parents 9aae52a + be5b46e commit f4c36c1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
46 changes: 32 additions & 14 deletions cbits/hexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,26 +466,44 @@ static str parse_content(document* d)
// have found a </
break;
}
else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
else if (peek_at(d, 1) == '!')
{
skip(d, 3);
// you can't reuse the two '-' characters for the closing as well
if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
if (peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
{
set_error(d, "Didn't get a closing comment");
return start_end(0, 0);
}
skip(d, 2);
for (;;)
{
if (!find(d, '>'))
skip(d, 3);
// you can't reuse the two '-' characters for the closing as well
if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
{
set_error(d, "Didn't get a closing comment");
return start_end(0, 0);
}
skip(d, 1);
if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
break;
skip(d, 2);
for (;;)
{
if (!find(d, '>'))
{
set_error(d, "Didn't get a closing comment");
return start_end(0, 0);
}
skip(d, 1);
if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
break;
}
} else if (d->end - d->cursor >= 9 && memcmp(d->cursor + 2, "[CDATA[", 7) == 0) {
skip(d, 9);
for (;;)
{
if (!find(d, '>'))
{
set_error(d, "Didn't close CDATA");
return start_end(0, 0);
}
skip(d, 1);
if (peek_at(d, -3) == ']' && peek_at(d, -2) == ']')
break;
}
} else {
parse_tag(d);
}
}
else
Expand Down
3 changes: 2 additions & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ examples =
,(False, "<test")
,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
,(True, "<foo bar.baz=\"qux\"></foo>")
,(True, "<test><![CDATA[foo<x>]]</y>]><z>]baz]]></test>")
]

main :: IO ()
Expand Down Expand Up @@ -80,5 +81,5 @@ rerender = inside
| otherwise = error "Invalid name"
validAttr x | BS.notElem '\"' x = x
| otherwise = error "Invalid attribute"
validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x = x
validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x || BS.isInfixOf "<![CDATA[" x = x
| otherwise = error $ show ("Invalid string", x)

0 comments on commit f4c36c1

Please sign in to comment.