Add Character Token methods for splitting leading whitespace
Since all successive characters are accumulated into the character token in the tokenization stage, leading whitespace must be explicitly ignored during the tree construction, hence these methods.
This commit is contained in:
@@ -18,5 +18,7 @@
|
||||
- (void)appendString:(NSString *)string;
|
||||
|
||||
- (BOOL)isWhitespaceToken;
|
||||
- (HTMLCharacterToken *)tokenByRetainingLeadingWhitespace;
|
||||
- (HTMLCharacterToken *)tokenByTrimmingLeadingWhitespace;
|
||||
|
||||
@end
|
||||
|
||||
@@ -8,6 +8,19 @@
|
||||
|
||||
#import "HTMLCharacterToken.h"
|
||||
|
||||
NS_INLINE BOOL isHtmlWhitespace(char c)
|
||||
{
|
||||
return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
|
||||
}
|
||||
|
||||
NS_INLINE size_t LeadingWhitespaceLength(NSString *string)
|
||||
{
|
||||
const char *str = string.UTF8String;
|
||||
size_t idx = 0;
|
||||
while (isHtmlWhitespace(*str)) { str++; idx++; }
|
||||
return idx;
|
||||
}
|
||||
|
||||
@interface HTMLCharacterToken ()
|
||||
{
|
||||
NSMutableString *_characters;
|
||||
@@ -40,6 +53,26 @@
|
||||
return [_characters rangeOfCharacterFromSet:set].location == NSNotFound;
|
||||
}
|
||||
|
||||
- (HTMLCharacterToken *)tokenByRetainingLeadingWhitespace
|
||||
{
|
||||
size_t index = LeadingWhitespaceLength(_characters);
|
||||
if (index > 0) {
|
||||
NSString *leading = [_characters substringToIndex:index];
|
||||
return [[HTMLCharacterToken alloc] initWithString:leading];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (HTMLCharacterToken *)tokenByTrimmingLeadingWhitespace
|
||||
{
|
||||
size_t index = LeadingWhitespaceLength(_characters);
|
||||
if (index < _characters.length) {
|
||||
NSString *remaining = [_characters substringFromIndex:index];
|
||||
return [[HTMLCharacterToken alloc] initWithString:remaining];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
#pragma mark - NSObject
|
||||
|
||||
- (BOOL)isEqual:(id)other
|
||||
|
||||
Reference in New Issue
Block a user