on master help sign in
Add support for comments
commit0486a23188552f4f6f0dbcbf57c9192b449b3f26 authorNeil Mitchell <ndmitchell@gmail.com> dateWed 30 Nov 2016, 19:08 (10 years ago) parent7b6ff3d
2 files changed+25−2
cbits/hexml.c+23−1 src/Main.hs+2−1
cbits/hexml.c+23−1
@@ -444,11 +444,33 @@ str parse_content(document* d)
444444 {
445445 break;
446446 }
447− if (peekAt(d, 1) == '/')
447+ else if (peekAt(d, 1) == '/')
448448 {
449449 // have found a </
450450 break;
451451 }
452+ else if (peekAt(d, 1) == '!' && peekAt(d, 2) == '-' && peekAt(d, 3) == '-')
453+ {
454+ skip(d, 3);
455+ // you can't reuse the two '-' characters for the closing as well
456+ if (peekAt(d, 0) == '\0' || peekAt(d, 1) == '\0')
457+ {
458+ set_error(d, "Didn't get a closing comment");
459+ return start_end(0, 0);
460+ }
461+ skip(d, 2);
462+ while (1)
463+ {
464+ if (!find(d, '>'))
465+ {
466+ set_error(d, "Didn't get a closing comment");
467+ return start_end(0, 0);
468+ }
469+ skip(d, 1);
470+ if (peekAt(d, -3) == '-' && peekAt(d, -2))
471+ break;
472+ }
473+ }
452474 else
453475 {
454476 parse_tag(d);
src/Main.hs+2−1
@@ -13,6 +13,7 @@ examples :: [(Bool, BS.ByteString)]
1313examples =
1414 [(True, "<test id='bob'>here<extra/>there</test>")
1515 ,(True, "<test /><close />")
16+ ,(True, "<test /><!-- comment > --><close />")
1617 ,(True, "<test id=\"bob value\" another-attr=\"test with <\">here </test> more text at the end<close />")
1718 ,(False, "<test></more>")
1819 ,(False, "<test")
@@ -58,5 +59,5 @@ rerender = inside
5859 | otherwise = error "Invalid name"
5960 validAttr x | BS.notElem '\"' x = x
6061 | otherwise = error "Invalid attribute"
61− validStr x | BS.notElem '<' x = x
62+ validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x = x
6263 | otherwise = error $ show ("Invalid string", x)