OcXmlLib: Add support for comments
closes cidanthera/bugtracker#1053
@@ -652,6 +652,8 @@ XmlParseTagOpen (
)
{
CHAR8 Current;
+ CHAR8 Next;
+ BOOLEAN IsComment;
XML_PARSER_INFO (Parser, "tag_open");
@@ -684,13 +686,63 @@ XmlParseTagOpen (
}
//
- // Skip the control sequence.
+ // A crazy XML comment may look like this:
+ // <!-- som>>><<<<ething -->
+ //
+ // '<' has already been consumed a bit earlier, now continue to check '!',
+ // and then the two '-'.
+ //
+ IsComment = FALSE;
+ if (Current == '!') {
+ //
+ // Consume one more byte to check the two '-'.
+ // Now "<!--" is guaranteed.
//
- do {
XmlParserConsume (Parser, 1);
- } while (XmlParserPeek (Parser, CURRENT_CHARACTER) != '>' && Parser->Position < Parser->Length);
+ Current = XmlParserPeek (Parser, CURRENT_CHARACTER);
+ Next = XmlParserPeek (Parser, NEXT_CHARACTER);
+ if (Current == '-' && Next == '-') {
+ //
+ // Now consume Current and Next which take up 2 bytes.
+ //
+ XmlParserConsume (Parser, 2);
+ IsComment = TRUE;
+ }
+ }
+
+ //
+ // Skip the control sequence.
+ // NOTE: This inner loop is created mainly for code simplification.
+ //
+ while (Parser->Position < Parser->Length) {
+ if (IsComment) {
+ //
+ // Scan "-->" for comments and break if matched.
+ //
+ if (XmlParserPeek (Parser, CURRENT_CHARACTER) == '-'
+ && XmlParserPeek (Parser, NEXT_CHARACTER) == '-'
+ && XmlParserPeek (Parser, 2) == '>') {
+ //
+ // "-->" should all be consumed, which takes 3 bytes.
+ //
+ XmlParserConsume (Parser, 3);
+ break;
+ }
+ } else {
+ //
+ // For non-comments, simply match '>'.
+ //
+ if (XmlParserPeek (Parser, CURRENT_CHARACTER) == '>') {
XmlParserConsume (Parser, 1);
+ break;
+ }
+ }
+ //
+ // Consume each byte normally.
+ //
+ XmlParserConsume (Parser, 1);
+ }
} while (Parser->Position < Parser->Length);
//
