Skip to content

Commit f4c36c1

Browse files
authored
Merge pull request #17 from typeable/cdata
Skip over CDATA
2 parents 9aae52a + be5b46e commit f4c36c1

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

cbits/hexml.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -466,26 +466,44 @@ static str parse_content(document* d)
466466
// have found a </
467467
break;
468468
}
469-
else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
469+
else if (peek_at(d, 1) == '!')
470470
{
471-
skip(d, 3);
472-
// you can't reuse the two '-' characters for the closing as well
473-
if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
471+
if (peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
474472
{
475-
set_error(d, "Didn't get a closing comment");
476-
return start_end(0, 0);
477-
}
478-
skip(d, 2);
479-
for (;;)
480-
{
481-
if (!find(d, '>'))
473+
skip(d, 3);
474+
// you can't reuse the two '-' characters for the closing as well
475+
if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
482476
{
483477
set_error(d, "Didn't get a closing comment");
484478
return start_end(0, 0);
485479
}
486-
skip(d, 1);
487-
if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
488-
break;
480+
skip(d, 2);
481+
for (;;)
482+
{
483+
if (!find(d, '>'))
484+
{
485+
set_error(d, "Didn't get a closing comment");
486+
return start_end(0, 0);
487+
}
488+
skip(d, 1);
489+
if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
490+
break;
491+
}
492+
} else if (d->end - d->cursor >= 9 && memcmp(d->cursor + 2, "[CDATA[", 7) == 0) {
493+
skip(d, 9);
494+
for (;;)
495+
{
496+
if (!find(d, '>'))
497+
{
498+
set_error(d, "Didn't close CDATA");
499+
return start_end(0, 0);
500+
}
501+
skip(d, 1);
502+
if (peek_at(d, -3) == ']' && peek_at(d, -2) == ']')
503+
break;
504+
}
505+
} else {
506+
parse_tag(d);
489507
}
490508
}
491509
else

src/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ examples =
2020
,(False, "<test")
2121
,(True, "<?xml version=\"1.1\"?>\n<greeting>Hello, world!</greeting>")
2222
,(True, "<foo bar.baz=\"qux\"></foo>")
23+
,(True, "<test><![CDATA[foo<x>]]</y>]><z>]baz]]></test>")
2324
]
2425

2526
main :: IO ()
@@ -80,5 +81,5 @@ rerender = inside
8081
| otherwise = error "Invalid name"
8182
validAttr x | BS.notElem '\"' x = x
8283
| otherwise = error "Invalid attribute"
83-
validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x = x
84+
validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x || BS.isInfixOf "<![CDATA[" x = x
8485
| otherwise = error $ show ("Invalid string", x)

0 commit comments

Comments
 (0)