From 6dee6c1e746da48522b96f4c5c9654dd8432a72b Mon Sep 17 00:00:00 2001 From: iska Date: Thu, 7 Sep 2017 22:05:02 +0200 Subject: [PATCH] Update input stream reader error handling Use newly specified parse errors: https://html.spec.whatwg.org/multipage/parsing.html#parse-errors --- Sources/HTMLInputStreamReader.m | 23 ++++++++++++++--------- Sources/include/HTMLInputStreamReader.h | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Sources/HTMLInputStreamReader.m b/Sources/HTMLInputStreamReader.m index a493331..777b81d 100644 --- a/Sources/HTMLInputStreamReader.m +++ b/Sources/HTMLInputStreamReader.m @@ -48,10 +48,10 @@ #pragma mark - Errors -- (void)emitParseError:(NSString *)reason +- (void)emitParseError:(NSString *)code details:(NSString *)details { if (self.errorCallback) { - self.errorCallback(reason); + self.errorCallback(code, details); } } @@ -82,16 +82,16 @@ return LINE_FEED; } if (CFStringIsSurrogateLowCharacter(nextInputCharacter)) { - NSString *reason = [NSString stringWithFormat:@"Non-Unicode character found (an isolated low surrogate: 0x%X)", (unsigned int)nextInputCharacter]; - [self emitParseError:reason]; + NSString *details = [NSString stringWithFormat:@"Non-Unicode character found (an isolated low surrogate: 0x%X)", (unsigned int)nextInputCharacter]; + [self emitParseError:@"surrogate-in-input-stream" details:details]; return nextInputCharacter; } if (CFStringIsSurrogateHighCharacter(nextInputCharacter)) { UniChar surrogateLow = CFStringGetCharacterFromInlineBuffer(&_buffer, _location + 1); if (CFStringIsSurrogateLowCharacter(surrogateLow) == NO) { - NSString *reason = [NSString stringWithFormat:@"Non-Unicode character found (an isolated high surrogate: 0x%X)", (unsigned int)nextInputCharacter]; - [self emitParseError:reason]; + NSString *details = [NSString stringWithFormat:@"Non-Unicode character found (an isolated high surrogate: 0x%X)", (unsigned int)nextInputCharacter]; + [self emitParseError:@"surrogate-in-input-stream" details:details]; return nextInputCharacter; } @@ -99,9 +99,14 @@ nextInputCharacter = CFStringGetLongCharacterForSurrogatePair(nextInputCharacter, surrogateLow); } - if (isControlOrUndefinedCharacter(nextInputCharacter)) { - NSString *reason = [NSString stringWithFormat:@"A control/undefined character found: (0x%X)", (unsigned int)nextInputCharacter]; - [self emitParseError:reason]; + if (isControlCharacter(nextInputCharacter)) { + NSString *details = [NSString stringWithFormat:@"A control character found: (0x%X)", (unsigned int)nextInputCharacter]; + [self emitParseError:@"control-character-in-input-stream" details:details]; + } + + if (isNoncharacter(nextInputCharacter)) { + NSString *details = [NSString stringWithFormat:@"A noncharacter found: (0x%X)", (unsigned int)nextInputCharacter]; + [self emitParseError:@"noncharacter-in-input-stream" details:details]; } return nextInputCharacter; diff --git a/Sources/include/HTMLInputStreamReader.h b/Sources/include/HTMLInputStreamReader.h index eaa0292..01d3a6f 100644 --- a/Sources/include/HTMLInputStreamReader.h +++ b/Sources/include/HTMLInputStreamReader.h @@ -17,7 +17,7 @@ @param reason The string describing the reason of the reported error. */ -typedef void (^ HTMLStreamReaderErrorCallback)(NSString *reason); +typedef void (^ HTMLStreamReaderErrorCallback)(NSString *code, NSString *details); /** * HTML Input Stream Reader processor conforming to the HTML standard