Index: src/OFXMLParser.h ================================================================== --- src/OFXMLParser.h +++ src/OFXMLParser.h @@ -26,10 +26,12 @@ didEndTagWithName: (OFString*)name prefix: (OFString*)prefix namespace: (OFString*)ns; - (void)xmlParser: (OFXMLParser*)parser foundString: (OFString*)string; +- (void)xmlParser: (OFXMLParser*)parser + foundComment: (OFString*)comment; - (OFString*)xmlParser: (OFXMLParser*)parser foundUnknownEntityNamed: (OFString*)entity; @end @protocol OFXMLUnescapingDelegate @@ -47,11 +49,15 @@ OF_XMLPARSER_IN_TAG, OF_XMLPARSER_IN_ATTR_NAME, OF_XMLPARSER_EXPECT_DELIM, OF_XMLPARSER_IN_ATTR_VALUE, OF_XMLPARSER_EXPECT_CLOSE, - OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE + OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE, + OF_XMLPARSER_IN_COMMENT_1, + OF_XMLPARSER_IN_COMMENT_2, + OF_XMLPARSER_IN_COMMENT_3, + OF_XMLPARSER_IN_COMMENT_4 } state; OFString *cache; OFString *name; OFString *prefix; OFString *ns; Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -150,13 +150,13 @@ if ([cache length] > 0) { OFString *str; pool = [[OFAutoreleasePool alloc] init]; str = transform_string(cache, self); + str = [[str copy] autorelease]; [delegate xmlParser: self foundString: str]; - [pool release]; } [cache setToCString: ""]; @@ -168,10 +168,13 @@ /* Tag was just opened */ case OF_XMLPARSER_TAG_OPENED: if (buf[i] == '/') { last = i + 1; state = OF_XMLPARSER_IN_CLOSE_TAG_NAME; + } else if(buf[i] == '!') { + last = i + 1; + state = OF_XMLPARSER_IN_COMMENT_1; } else { state = OF_XMLPARSER_IN_TAG_NAME; i--; } break; @@ -433,10 +436,48 @@ last = i + 1; state = OF_XMLPARSER_OUTSIDE_TAG; } else if (buf[i] != ' ') @throw [OFMalformedXMLException newWithClass: isa]; + break; + + /* Comment */ + case OF_XMLPARSER_IN_COMMENT_1: + case OF_XMLPARSER_IN_COMMENT_2: + if (buf[i] != '-') + @throw [OFMalformedXMLException + newWithClass: isa]; + last = i + 1; + state++; + break; + case OF_XMLPARSER_IN_COMMENT_3: + if (buf[i] == '-') + state = OF_XMLPARSER_IN_COMMENT_4; + break; + case OF_XMLPARSER_IN_COMMENT_4: + if (buf[i] == '-') { + OFString *str; + + [cache appendCString: buf + last + withLength: i - last]; + + pool = [[OFAutoreleasePool alloc] init]; + str = [OFMutableString + stringWithCString: [cache cString] + length: [cache length] - 1]; + [str removeLeadingAndTrailingWhitespaces]; + [delegate xmlParser: self + foundComment: str]; + [pool release]; + + [cache setToCString: ""]; + + last = i + 1; + state = OF_XMLPARSER_EXPECT_CLOSE; + } else + state = OF_XMLPARSER_IN_COMMENT_3; + break; } } len = size - last; @@ -558,12 +599,17 @@ - (void)xmlParser: (OFXMLParser*)parser foundString: (OFString*)string { } + +- (void)xmlParser: (OFXMLParser*)parser + foundComment: (OFString*)comment +{ +} - (OFString*)xmlParser: (OFXMLParser*)parser foundUnknownEntityNamed: (OFString*)entity { return nil; } @end Index: tests/OFXMLParser/OFXMLParser.m ================================================================== --- tests/OFXMLParser/OFXMLParser.m +++ tests/OFXMLParser/OFXMLParser.m @@ -14,11 +14,11 @@ #include #include #import "OFXMLParser.h" -@interface ParserDelegate: OFObject +@interface ParserDelegate: OFObject @end @implementation ParserDelegate - (void)xmlParser: (OFXMLParser*)parser didStartTagWithName: (OFString*)name @@ -64,10 +64,16 @@ - (void)xmlParser: (OFXMLParser*)parser foundString: (OFString*)string { printf("STRING\n\"%s\"\n\n", [string cString]); } + +- (void)xmlParser: (OFXMLParser*)parser + foundComment: (OFString*)comment +{ + printf("COMMENT\n\"%s\"\n\n", [comment cString]); +} - (OFString*)xmlParser: (OFXMLParser*)parser foundUnknownEntityNamed: (OFString*)entity { if ([entity isEqual: @"foo"]) @@ -80,11 +86,11 @@ int main() { const char *foo = "bar\r\n" "foo<barbar quxbar\r\n" - ""; + ""; size_t len = strlen(foo); size_t i; OFXMLParser *parser = [OFXMLParser xmlParser]; [parser setDelegate: [[ParserDelegate alloc] init]];