on master help sign in
Add support for comments
0486a23Neil Mitchell
cbits/hexml.c+23−1
@@ -444,11 +444,33 @@ str parse_content(document* d)
444 {
444 {
445 break;
445 break;
446 }
446 }
447− if (peekAt(d, 1) == '/')
447+ else if (peekAt(d, 1) == '/')
448 {
448 {
449 // have found a </
449 // have found a </
450 break;
450 break;
451 }
451 }
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+ }
452 else
474 else
453 {
475 {
454 parse_tag(d);
476 parse_tag(d);
src/Main.hs+2−1
@@ -13,6 +13,7 @@ examples :: [(Bool, BS.ByteString)]
13examples =
13examples =
14 [(True, "<test id='bob'>here<extra/>there</test>")
14 [(True, "<test id='bob'>here<extra/>there</test>")
15 ,(True, "<test /><close />")
15 ,(True, "<test /><close />")
16+ ,(True, "<test /><!-- comment > --><close />")
16 ,(True, "<test id=\"bob value\" another-attr=\"test with <\">here </test> more text at the end<close />")
17 ,(True, "<test id=\"bob value\" another-attr=\"test with <\">here </test> more text at the end<close />")
17 ,(False, "<test></more>")
18 ,(False, "<test></more>")
18 ,(False, "<test")
19 ,(False, "<test")
@@ -58,5 +59,5 @@ rerender = inside
58 | otherwise = error "Invalid name"
59 | otherwise = error "Invalid name"
59 validAttr x | BS.notElem '\"' x = x
60 validAttr x | BS.notElem '\"' x = x
60 | otherwise = error "Invalid attribute"
61 | otherwise = error "Invalid attribute"
61− validStr x | BS.notElem '<' x = x
62+ validStr x | BS.notElem '<' x || BS.isInfixOf "<!--" x = x
62 | otherwise = error $ show ("Invalid string", x)
63 | otherwise = error $ show ("Invalid string", x)