forked from unixpickle/ANHTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANHTMLParser.m
More file actions
273 lines (229 loc) · 7.42 KB
/
ANHTMLParser.m
File metadata and controls
273 lines (229 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// ANHTMLParser.m
// ANHTML
//
// Created by Alex Nichol on 11/17/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "ANHTMLParser.h"
@interface ANHTMLParser (Private)
- (BOOL)encounteredTagStart;
- (BOOL)readElementAndCallback:(NSString *)elementDeclaration;
- (BOOL)encounteredEscapeStart;
- (NSString *)unescapeAttributeString:(NSString *)escapedString;
- (unichar)readAndUnescapeNext:(StringFeed *)aFeed;
- (void)delegateInformText:(NSString *)theText;
- (void)delegateInformElementStart:(NSString *)tag attributes:(NSDictionary *)attributes;
- (void)delegateInformElementEnd:(NSString *)tag;
- (unichar)delegateGetEscapeValue:(NSString *)eCode;
@end
@implementation ANHTMLParser
@synthesize delegate;
- (id)initWithDocumentData:(NSData *)data {
if ((self = [super init])) {
feed = [[StringFeed alloc] initWithStringData:data];
if (!feed) {
return nil;
}
}
return self;
}
- (id)initWithDocumentString:(NSString *)string {
if ((self = [super init])) {
feed = [[StringFeed alloc] initWithString:string];
if (!feed) {
return nil;
}
}
return self;
}
- (void)parse {
// reset parsing stats
[feed setOffset:0];
plainBuffer = [[NSMutableString alloc] init];
while (![feed isFinished]) {
unichar c = [feed getCharacter];
if (c == '<') {
[feed pushCurrentOffset];
if (![self encounteredTagStart]) {
[feed restoreOffset];
[plainBuffer appendFormat:@"%C", c];
} else {
[feed skipOffset];
}
} else if (c == '&') {
[feed pushCurrentOffset];
if (![self encounteredEscapeStart]) {
[feed restoreOffset];
[plainBuffer appendFormat:@"%C", c];
} else {
[feed skipOffset];
}
} else {
[plainBuffer appendFormat:@"%C", c];
}
}
if ([plainBuffer length] > 0) {
[self delegateInformText:plainBuffer];
}
}
#pragma mark - Private -
- (BOOL)encounteredTagStart {
if ([feed isFinished]) {
return NO;
}
// different reasons that a '<' might be in our document
if ([feed stringFollows:@"/"]) {
// end of a tag
[feed skipUntil:@"/"]; // skip the / character
NSString * tagContent = [feed readUntil:@">" notEncountering:@"<"];
if (!tagContent) {
return NO;
}
// remove trailing whitespace
NSCharacterSet * whitespace = [NSCharacterSet whitespaceCharacterSet];
NSString * trimmed = [tagContent stringByTrimmingCharactersInSet:whitespace];
if ([trimmed length] == 0) return NO;
// remove stuff after first token (e.g. after element name)
NSRange spaceRange = [trimmed rangeOfCharacterFromSet:whitespace];
if (spaceRange.location != NSNotFound) {
trimmed = [trimmed substringWithRange:NSMakeRange(0, spaceRange.location)];
}
if ([trimmed length] == 0) return NO;
[self delegateInformElementEnd:trimmed];
return YES;
} else if ([feed stringFollows:@"!"]) {
// comment of some sort
if ([feed stringFollows:@"!--"]) {
// long comment
return [feed skipUntil:@"-->"];
} else {
// shorthand comment
return [feed skipUntil:@">"];
}
} else if ([feed stringFollows:@"?"]) {
// PHP, XML info
return [feed skipUntil:@"?>"];
} else {
// beginning of an element
NSString * elementDec = [feed readUntil:@">" notEncountering:@"<"];
if (!elementDec) {
return NO;
}
return [self readElementAndCallback:elementDec];
}
}
- (BOOL)readElementAndCallback:(NSString *)elementDeclaration {
NSCharacterSet * whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
StringFeed * elemFeed = [[StringFeed alloc] initWithString:elementDeclaration];
if ([elemFeed isFinished]) return NO;
// read the element name token
NSString * elementName = [elemFeed readUntilCharacterInSet:whitespace];
if ([elementName length] == 0) {
return NO;
}
NSMutableDictionary * attributes = [NSMutableDictionary dictionary];
// read the attribute tokens
while (![elemFeed isFinished]) {
[elemFeed consumeCharactersInSet:whitespace];
if ([elemFeed isFinished]) break;
NSString * attributeName = [elemFeed readUntil:@"="];
if (!attributeName) break;
if (![elemFeed skipUntil:@"\""]) break;
NSString * attributeValue = [self unescapeAttributeString:[elemFeed readUntil:@"\""]];
if (!attributeValue) break;
[attributes setObject:attributeValue forKey:attributeName];
}
[self delegateInformElementStart:elementName attributes:attributes];
// the <tag ... /> syntax
if ([[elementDeclaration stringByTrimmingCharactersInSet:whitespace] hasSuffix:@"/"]) {
[self delegateInformElementEnd:elementName];
}
return YES;
}
#pragma mark Unescaping
- (BOOL)encounteredEscapeStart {
unichar escapeChar = [self readAndUnescapeNext:feed];
if (escapeChar == 0) {
return NO;
}
[plainBuffer appendFormat:@"%C", escapeChar];
return YES;
}
- (NSString *)unescapeAttributeString:(NSString *)escapedString {
if (!escapedString) return nil;
StringFeed * subFeed = [[StringFeed alloc] initWithString:escapedString];
NSMutableString * unescaped = [[NSMutableString alloc] init];
while (![subFeed isFinished]) {
unichar token = [subFeed getCharacter];
if (token == '&') {
[subFeed pushCurrentOffset];
unichar original = [self readAndUnescapeNext:subFeed];
if (original == 0) {
[subFeed restoreOffset];
[unescaped appendFormat:@"%C", token];
} else {
[subFeed skipOffset];
[unescaped appendFormat:@"%C", original];
}
} else {
[unescaped appendFormat:@"%C", token];
}
}
return [unescaped copy]; // immutable copy
}
- (unichar)readAndUnescapeNext:(StringFeed *)aFeed {
NSString * escBody = [aFeed readUntil:@";" notEncountering:@"&"];
if (!escBody) return 0;
unichar escapeChar = [self delegateGetEscapeValue:escBody];
return escapeChar;
}
#pragma mark Delegate Callbacks
- (void)delegateInformText:(NSString *)theText {
NSCharacterSet * whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSCharacterSet * newLines = [NSCharacterSet newlineCharacterSet];
BOOL isJustSpace = YES;
// remove instances where there are two spaces in a row,
// replace with the first space only, deleting the second.
NSMutableString * noRepeats = [[theText stringByTrimmingCharactersInSet:newLines] mutableCopy];
for (NSInteger i = 0; i < (NSInteger)[noRepeats length] - 1; i++) {
unichar current = [noRepeats characterAtIndex:i];
unichar next = [noRepeats characterAtIndex:i + 1];
if ([whitespace characterIsMember:current]) {
if ([whitespace characterIsMember:next]) {
// we have repeating whitespace, remove next character,
// then set i so that we will check this character again
// with the next character
[noRepeats deleteCharactersInRange:NSMakeRange(i + 1, 1)];
i--;
} else {
isJustSpace = NO;
}
} else {
isJustSpace = NO;
}
}
if (isJustSpace) return;
[delegate htmlParser:self didEncounterPlainText:[noRepeats copy]]; // immutable copy
}
- (void)delegateInformElementStart:(NSString *)tag attributes:(NSDictionary *)attributes {
if ([plainBuffer length] > 0) {
[self delegateInformText:plainBuffer];
plainBuffer = [[NSMutableString alloc] init];
}
[delegate htmlParser:self didStartElement:tag attributes:attributes];
}
- (void)delegateInformElementEnd:(NSString *)tag {
if ([plainBuffer length] > 0) {
[self delegateInformText:plainBuffer];
plainBuffer = [[NSMutableString alloc] init];
}
[delegate htmlParser:self didEndElement:tag];
}
- (unichar)delegateGetEscapeValue:(NSString *)eCode {
if (![delegate respondsToSelector:@selector(htmlParser:characterForEscapeCode:)]) {
return 0;
}
return [delegate htmlParser:self characterForEscapeCode:eCode];
}
@end