-
Notifications
You must be signed in to change notification settings - Fork 229
/
HTMLNode.m
458 lines (358 loc) · 10.6 KB
/
HTMLNode.m
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
// HTMLNode.m
// StackOverflow
//
// Created by Ben Reeves on 09/03/2010.
// Copyright 2010 Ben Reeves. All rights reserved.
//
#import "HTMLNode.h"
@implementation HTMLNode
-(HTMLNode*)parent
{
return [[[HTMLNode alloc] initWithXMLNode:_node->parent] autorelease];
}
void setAttributeNamed(xmlNode * node, const char * nameStr, const char * value) {
char * newVal = (char *)malloc(strlen(value)+1);
memcpy (newVal, value, strlen(value)+1);
for(xmlAttrPtr attr = node->properties; NULL != attr; attr = attr->next)
{
if (strcmp((char*)attr->name, nameStr) == 0)
{
for(xmlNode * child = attr->children; NULL != child; child = child->next)
{
free(child->content);
child->content = (xmlChar*)newVal;
break;
}
break;
}
}
}
NSString * getAttributeNamed(xmlNode * node, const char * nameStr)
{
for(xmlAttrPtr attr = node->properties; NULL != attr; attr = attr->next)
{
if (strcmp((char*)attr->name, nameStr) == 0)
{
for(xmlNode * child = attr->children; NULL != child; child = child->next)
{
return [NSString stringWithCString:(void*)child->content encoding:NSUTF8StringEncoding];
}
break;
}
}
return NULL;
}
-(NSString*)getAttributeNamed:(NSString*)name
{
const char * nameStr = [name UTF8String];
return getAttributeNamed(_node, nameStr);
}
//Returns the class name
-(NSString*)className
{
return [self getAttributeNamed:@"class"];
}
//Returns the tag name
-(NSString*)tagName
{
return [NSString stringWithCString:(void*)_node->name encoding:NSUTF8StringEncoding];
}
-(HTMLNode*)firstChild
{
return [[[HTMLNode alloc] initWithXMLNode:_node->children] autorelease];
}
-(void)findChildrenWithAttribute:(const char*)attribute matchingName:(const char*)className inXMLNode:(xmlNode *)node inArray:(NSMutableArray*)array allowPartial:(BOOL)partial
{
xmlNode *cur_node = NULL;
const char * classNameStr = className;
//BOOL found = NO;
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
for(xmlAttrPtr attr = cur_node->properties; NULL != attr; attr = attr->next)
{
if (strcmp((char*)attr->name, attribute) == 0)
{
for(xmlNode * child = attr->children; NULL != child; child = child->next)
{
BOOL match = NO;
if (!partial && strcmp((char*)child->content, classNameStr) == 0)
match = YES;
else if (partial && strstr ((char*)child->content, classNameStr) != NULL)
match = YES;
if (match)
{
//Found node
HTMLNode * nNode = [[[HTMLNode alloc] initWithXMLNode:cur_node] autorelease];
[array addObject:nNode];
break;
}
}
break;
}
}
[self findChildrenWithAttribute:attribute matchingName:className inXMLNode:cur_node->children inArray:array allowPartial:partial];
}
}
-(void)findChildTags:(NSString*)tagName inXMLNode:(xmlNode *)node inArray:(NSMutableArray*)array
{
xmlNode *cur_node = NULL;
const char * tagNameStr = [tagName UTF8String];
if (tagNameStr == nil)
return;
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
if (cur_node->name && strcmp((char*)cur_node->name, tagNameStr) == 0)
{
HTMLNode * node = [[[HTMLNode alloc] initWithXMLNode:cur_node] autorelease];
[array addObject:node];
}
[self findChildTags:tagName inXMLNode:cur_node->children inArray:array];
}
}
-(NSArray*)findChildTags:(NSString*)tagName
{
NSMutableArray * array = [NSMutableArray array];
[self findChildTags:tagName inXMLNode:_node->children inArray:array];
return array;
}
-(HTMLNode*)findChildTag:(NSString*)tagName inXMLNode:(xmlNode *)node
{
xmlNode *cur_node = NULL;
const char * tagNameStr = [tagName UTF8String];
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
if (cur_node && cur_node->name && strcmp((char*)cur_node->name, tagNameStr) == 0)
{
return [[[HTMLNode alloc] initWithXMLNode:cur_node] autorelease];
}
HTMLNode * cNode = [self findChildTag:tagName inXMLNode:cur_node->children];
if (cNode != NULL)
{
return cNode;
}
}
return NULL;
}
-(HTMLNode*)findChildTag:(NSString*)tagName
{
return [self findChildTag:tagName inXMLNode:_node->children];
}
-(NSArray*)children
{
xmlNode *cur_node = NULL;
NSMutableArray * array = [NSMutableArray array];
for (cur_node = _node->children; cur_node; cur_node = cur_node->next)
{
HTMLNode * node = [[[HTMLNode alloc] initWithXMLNode:cur_node] autorelease];
[array addObject:node];
}
return array;
}
/*
-(NSString*)description
{
NSString * string = [NSString stringWithFormat:@"<%s>%@\n", _node->name, [self contents]];
for (HTMLNode * child in [self children])
{
string = [string stringByAppendingString:[child description]];
}
string = [string stringByAppendingString:[NSString stringWithFormat:@"<%s>\n", _node->name]];
return string;
}*/
-(HTMLNode*)findChildWithAttribute:(const char*)attribute matchingName:(const char*)name inXMLNode:(xmlNode *)node allowPartial:(BOOL)partial
{
xmlNode *cur_node = NULL;
const char * classNameStr = name;
//BOOL found = NO;
if (node == NULL)
return NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
for(xmlAttrPtr attr = cur_node->properties; NULL != attr; attr = attr->next)
{
if (strcmp((char*)attr->name, attribute) == 0)
{
for(xmlNode * child = attr->children; NULL != child; child = child->next)
{
BOOL match = NO;
if (!partial && strcmp((char*)child->content, classNameStr) == 0)
match = YES;
else if (partial && strstr ((char*)child->content, classNameStr) != NULL)
match = YES;
if (match)
{
return [[[HTMLNode alloc] initWithXMLNode:cur_node] autorelease];
}
}
break;
}
}
HTMLNode * cNode = [self findChildWithAttribute:attribute matchingName:name inXMLNode:cur_node->children allowPartial:partial];
if (cNode != NULL)
{
return cNode;
}
}
return NULL;
}
-(HTMLNode*)findChildWithAttribute:(NSString*)attribute matchingName:(NSString*)className allowPartial:(BOOL)partial
{
return [self findChildWithAttribute:[attribute UTF8String] matchingName:[className UTF8String] inXMLNode:_node->children allowPartial:partial];
}
-(HTMLNode*)findChildOfClass:(NSString*)className
{
HTMLNode * node = [self findChildWithAttribute:"class" matchingName:[className UTF8String] inXMLNode:_node->children allowPartial:NO];
return node;
}
-(NSArray*)findChildrenWithAttribute:(NSString*)attribute matchingName:(NSString*)className allowPartial:(BOOL)partial
{
NSMutableArray * array = [NSMutableArray array];
[self findChildrenWithAttribute:[attribute UTF8String] matchingName:[className UTF8String] inXMLNode:_node->children inArray:array allowPartial:partial];
return array;
}
-(NSArray*)findChildrenOfClass:(NSString*)className
{
return [self findChildrenWithAttribute:@"class" matchingName:className allowPartial:NO];
}
-(id)initWithXMLNode:(xmlNode*)xmlNode
{
if (self = [super init])
{
_node = xmlNode;
}
return self;
}
-(void)appendChildContentsToString:(NSMutableString*)string inNode:(xmlNode*)node
{
if (node == NULL)
return;
xmlNode *cur_node = NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
if (cur_node->content)
{
[string appendString:[NSString stringWithCString:(void*)cur_node->content encoding:NSUTF8StringEncoding]];
}
[self appendChildContentsToString:string inNode:cur_node->children];
}
}
-(NSString*)contents
{
if (_node->children && _node->children->content)
{
return [NSString stringWithCString:(void*)_node->children->content encoding:NSUTF8StringEncoding];
}
return nil;
}
HTMLNodeType nodeType(xmlNode * _node)
{
if (_node == NULL || _node->name == NULL)
return HTMLUnkownNode;
const char * tagName = (const char*)_node->name;
if (strcmp(tagName, "a") == 0)
return HTMLHrefNode;
else if (strcmp(tagName, "text") == 0)
return HTMLTextNode;
else if (strcmp(tagName, "code") == 0)
return HTMLCodeNode;
else if (strcmp(tagName, "span") == 0)
return HTMLSpanNode;
else if (strcmp(tagName, "p") == 0)
return HTMLPNode;
else if (strcmp(tagName, "ul") == 0)
return HTMLUlNode;
else if (strcmp(tagName, "li") == 0)
return HTMLLiNode;
else if (strcmp(tagName, "image") == 0)
return HTMLImageNode;
else if (strcmp(tagName, "ol") == 0)
return HTMLOlNode;
else if (strcmp(tagName, "strong") == 0)
return HTMLStrongNode;
else if (strcmp(tagName, "pre") == 0)
return HTMLPreNode;
else if (strcmp(tagName, "blockquote") == 0)
return HTMLBlockQuoteNode;
else
return HTMLUnkownNode;
}
-(HTMLNodeType)nodetype
{
return nodeType(_node);
}
NSString * allNodeContents(xmlNode*node)
{
if (node == NULL)
return nil;
void * contents = xmlNodeGetContent(node);
if (contents)
{
NSString * string = [NSString stringWithCString:contents encoding:NSUTF8StringEncoding];
xmlFree(contents);
return string;
}
return @"";
}
-(NSString*)allContents
{
return allNodeContents(_node);
}
NSString * rawContentsOfNode(xmlNode * node)
{
xmlBufferPtr buffer = xmlBufferCreateSize(1000);
xmlOutputBufferPtr buf = xmlOutputBufferCreateBuffer(buffer, NULL);
hxmlNodeDumpOutput(buf, node->doc, node, 3, 0, NULL);
xmlOutputBufferFlush(buf);
NSString * string = nil;
if (buffer->content) {
string = [[[NSString alloc] initWithBytes:(const void *)xmlBufferContent(buffer) length:xmlBufferLength(buffer) encoding:NSUTF8StringEncoding] autorelease];
}
xmlOutputBufferClose(buf);
xmlBufferFree(buffer);
return string;
/*
xmlNode *cur_node = NULL;
if (node == NULL)
return;
//For each child
for (cur_node = node; cur_node; cur_node = cur_node->next)
{
//If text where not interested in the actuall tag
if (strcmp((const void*)cur_node->name, "text") == 0)
{
//Just append the contents
if (cur_node->content)
{
[string appendString:[NSString stringWithCString:(void*)cur_node->content encoding:NSUTF8StringEncoding]];
}
continue;
}
//Open the tag
[string appendFormat:@"<%s", cur_node->name];
//Loop through each attribute
for(xmlAttrPtr attr = cur_node->properties; NULL != attr; attr = attr->next)
{
[string appendFormat:@" %s=\"", attr->name];
for(xmlNode * child = attr->children; NULL != child; child = child->next)
{
[string appendFormat:@"%s\"", child->content];
break;
}
}
//Close the tag
[string appendFormat:@">"];
//Inser the contents and then append the end tag
if (cur_node->content)
{
[string appendString:[NSString stringWithCString:(void*)cur_node->content encoding:NSUTF8StringEncoding]];
}
rawContentsAppendToString(string, cur_node->children);
[string appendFormat:@"</%s>\n", cur_node->name];
}
*/
}
-(NSString*)rawContents {
rawContentsOfNode(_node);
}
@end