Browse files

Fixed crash on encountering a processing instruction.

  • Loading branch information...
1 parent ba0cac3 commit d8f8cce29016711f9d2e6b6fb985e9b58eb906c7 @odrobnik odrobnik committed Aug 9, 2012
Showing with 36 additions and 7 deletions.
  1. +9 −0 Core/Source/DTHTMLParser.h
  2. +27 −7 Core/Source/DTHTMLParser.m
View
9 Core/Source/DTHTMLParser.h
@@ -76,6 +76,15 @@
- (void)parser:(DTHTMLParser *)parser foundCDATA:(NSData *)CDATABlock;
/**
+ Sent by a parser object to its delegate when it encounters a processing instruction.
+
+ @param parser A DTHTMLParser object parsing HTML.
+ @param target A string representing the target of a processing instruction.
+ @param data A string representing the data for a processing instruction.
+ */
+- (void)parser:(DTHTMLParser *)parser foundProcessingInstructionWithTarget:(NSString *)target data:(NSString *)data;
+
+/**
Sent by a parser object to its delegate when it encounters a fatal error.
When this method is invoked, parsing is stopped. For further information about the error, you can query parseError or you can send the parser a parserError message. You can also send the parser lineNumber and columnNumber messages to further isolate where the error occurred. Typically you implement this method to display information about the error to the user.
View
34 Core/Source/DTHTMLParser.m
@@ -13,6 +13,7 @@
@interface DTHTMLParser()
@property (nonatomic, strong) NSError *parserError;
+@property (nonatomic, assign) NSStringEncoding encoding;
@end
@@ -26,10 +27,8 @@ @interface DTHTMLParser()
void _characters(void *context, const xmlChar *ch, int len);
void _comment(void *context, const xmlChar *value);
void _dterror(void *context, const char *msg, ...);
-
void _cdataBlock(void *context, const xmlChar *value, int len);
-
-void _ignorableWhitespace (void *context, const xmlChar *ch, int len);
+void _processingInstruction (void *context, const xmlChar *target, const xmlChar *data);
#pragma mark Event functions
void _startDocument(void *context)
@@ -114,7 +113,7 @@ void _characters(void *context, const xmlChar *chars, int len)
{
DTHTMLParser *myself = (__bridge DTHTMLParser *)context;
- NSString *string = [[NSString alloc] initWithBytes:chars length:len encoding:NSUTF8StringEncoding];
+ NSString *string = [[NSString alloc] initWithBytes:chars length:len encoding:myself.encoding];
[myself.delegate parser:myself foundCharacters:string];
}
@@ -123,7 +122,7 @@ void _comment(void *context, const xmlChar *chars)
{
DTHTMLParser *myself = (__bridge DTHTMLParser *)context;
- NSString *string = [NSString stringWithUTF8String:(char *)chars];
+ NSString *string = [NSString stringWithCString:(const char *)chars encoding:myself.encoding];
[myself.delegate parser:myself foundComment:string];
}
@@ -156,11 +155,22 @@ void _cdataBlock(void *context, const xmlChar *value, int len)
[myself.delegate parser:myself foundCDATA:data];
}
+void _processingInstruction (void *context, const xmlChar *target, const xmlChar *data)
+{
+ DTHTMLParser *myself = (__bridge DTHTMLParser *)context;
+
+ NSStringEncoding encoding = myself.encoding;
+
+ NSString *targetStr = [NSString stringWithCString:(const char *)target encoding:encoding];
+ NSString *dataStr = [NSString stringWithCString:(const char *)data encoding:encoding];
+
+ [myself.delegate parser:myself foundProcessingInstructionWithTarget:targetStr data:dataStr];
+}
+
@implementation DTHTMLParser
{
htmlSAXHandler _handler;
- NSStringEncoding _encoding;
NSData *_data;
__unsafe_unretained id <DTHTMLParserDelegate> _delegate;
@@ -255,6 +265,7 @@ - (void)abortParsing
_handler.characters = NULL;
_handler.comment = NULL;
_handler.error = NULL;
+ _handler.processingInstruction = NULL;
// inform delegate
if ([_delegate respondsToSelector:@selector(parser:parseErrorOccurred:)])
@@ -344,7 +355,16 @@ - (void)setDelegate:(__unsafe_unretained id<DTHTMLParserDelegate>)delegate;
else
{
_handler.cdataBlock = NULL;
- }
+ }
+
+ if ([delegate respondsToSelector:@selector(parser:foundProcessingInstructionWithTarget:data:)])
+ {
+ _handler.processingInstruction = _processingInstruction;
+ }
+ else
+ {
+ _handler.processingInstruction = NULL;
+ }
}
- (NSInteger)lineNumber

0 comments on commit d8f8cce

Please sign in to comment.