forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLNode.m
317 lines (261 loc) · 8.79 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
// HTMLNode.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import "HTMLNode.h"
#import "HTMLDocument.h"
#import "HTMLTextNode.h"
#import "HTMLTreeEnumerator.h"
NS_ASSUME_NONNULL_BEGIN
@interface HTMLChildrenRelationshipProxy : HTMLGenericOf(NSMutableOrderedSet, HTMLNode *)
- (instancetype)initWithNode:(HTMLNode *)node children:(HTMLMutableOrderedSetOf(HTMLNode *) *)children;
@property (readonly, strong, nonatomic) HTMLNode *node;
@property (readonly, strong, nonatomic) HTMLMutableOrderedSetOf(HTMLNode *) *children;
@end
@implementation HTMLNode
{
HTMLMutableOrderedSetOf(HTMLNode *) *_children;
}
- (instancetype)init
{
if ((self = [super init])) {
_children = [NSMutableOrderedSet new];
}
return self;
}
- (HTMLDocument * __nullable)document
{
HTMLNode *currentNode = self.parentNode;
while (currentNode && ![currentNode isKindOfClass:[HTMLDocument class]]) {
currentNode = currentNode.parentNode;
}
return (HTMLDocument *)currentNode;
}
- (void)setParentNode:(HTMLNode * __nullable)parentNode
{
[self setParentNode:parentNode updateChildren:YES];
}
- (void)setParentNode:(HTMLNode * __nullable)parentNode updateChildren:(BOOL)updateChildren
{
[_parentNode removeChild:self updateParentNode:NO];
_parentNode = parentNode;
if (updateChildren) {
[parentNode addChild:self updateParentNode:NO];
}
}
- (HTMLElement * __nullable)parentElement
{
HTMLNode *parent = self.parentNode;
return [parent isKindOfClass:[HTMLElement class]] ? (HTMLElement *)parent : nil;
}
- (void)setParentElement:(HTMLElement * __nullable)parentElement
{
self.parentNode = parentElement;
}
- (void)removeFromParentNode
{
[self.parentNode.mutableChildren removeObject:self];
}
- (HTMLOrderedSetOf(HTMLNode *) *)children
{
return [_children copy];
}
// In order to quickly mutate the children set, we need to pull some shenanigans. From the Key-Value Coding Programming Guide:
//
// > When the default implementation of valueForKey: is invoked on a receiver, the following search pattern is used:
// >
// > 1. Searches the class of the receiver for an accessor method whose name matches the pattern get<Key>, <key>, or is<Key>, in that order. If such a method is found it is invoked.…
// > 2. Otherwise (no simple accessor method is found), searches the class of the receiver for methods whose names match the patterns countOf<Key> and objectIn<Key>AtIndex: … and <key>AtIndexes:….
// > If the countOf<Key> method and at least one of the other two possible methods are found, a collection proxy object that responds to all NSArray [sic] methods is returned. Each NSArray [sic] message sent to the collection proxy object will result in some combination of countOf<Key>, objectIn<Key>AtIndex:, and <key>AtIndexes: messages being sent to the original receiver of valueForKey:.
//
// From this, we can see that implementing -children stops us at step 1, and our implementation involves copying the set so it is slow. To work around this, we become KVC-compliant for the key "HTMLMutableChildren" and implement the accessors for that key. Since we don't implement -HTMLMutableChildren et al (step 1), our accessors are used instead (step 2), and all is well.
//
// Note that -mutableOrderedSetValueForKey: will still work for the key "children", it'll just be slow.
- (HTMLMutableOrderedSetOf(HTMLNode *) *)mutableChildren
{
return [[HTMLChildrenRelationshipProxy alloc] initWithNode:self children:_children];
}
- (void)addChild:(HTMLNode *)child
{
NSParameterAssert(child);
[self.mutableChildren addObject:child];
}
- (void)removeChild:(HTMLNode *)child
{
NSParameterAssert(child);
[self.mutableChildren removeObject:child];
}
- (NSUInteger)numberOfChildren
{
return _children.count;
}
- (HTMLNode *)childAtIndex:(NSUInteger)index
{
return _children[index];
}
- (NSUInteger)indexOfChild:(HTMLNode *)child
{
return [_children indexOfObject:child];
}
- (void)insertObject:(HTMLNode *)node inChildrenAtIndex:(NSUInteger)index
{
if ([_children containsObject:node]) {
return;
}
[_children insertObject:node atIndex:index];
[node setParentNode:self updateChildren:NO];
}
- (void)insertChildren:(NSArray *)array atIndexes:(NSIndexSet *)indexes
{
NSUInteger nextIndex = indexes.firstIndex;
for (HTMLNode *child in array) {
[self insertObject:child inChildrenAtIndex:nextIndex];
nextIndex = [indexes indexGreaterThanIndex:nextIndex];
}
}
- (void)removeObjectFromChildrenAtIndex:(NSUInteger)index
{
HTMLNode *node = _children[index];
[_children removeObjectAtIndex:index];
[node setParentNode:nil updateChildren:NO];
}
- (void)removeChildrenAtIndexes:(NSIndexSet *)indexes
{
NSArray *nodes = [_children objectsAtIndexes:indexes];
[_children removeObjectsAtIndexes:indexes];
for (HTMLNode *node in nodes) {
[node setParentNode:nil updateChildren:NO];
}
}
- (void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(HTMLNode *)node
{
HTMLNode *old = _children[index];
[_children replaceObjectAtIndex:index withObject:node];
[old setParentNode:nil updateChildren:NO];
[node setParentNode:self updateChildren:NO];
}
- (void)addChild:(HTMLNode *)node updateParentNode:(BOOL)updateParentNode
{
[_children addObject:node];
if (updateParentNode) {
[node setParentNode:self updateChildren:NO];
}
}
- (void)removeChild:(HTMLNode *)node updateParentNode:(BOOL)updateParentNode
{
[_children removeObject:node];
if (updateParentNode) {
[node setParentNode:nil updateChildren:NO];
}
}
- (void)insertString:(NSString *)string atChildNodeIndex:(NSUInteger)index
{
NSParameterAssert(string);
id candidate = index > 0 ? _children[index - 1] : nil;
HTMLTextNode *textNode;
if ([candidate isKindOfClass:[HTMLTextNode class]]) {
textNode = candidate;
} else {
textNode = [HTMLTextNode new];
[[self mutableChildren] insertObject:textNode atIndex:index];
}
[textNode appendString:string];
}
- (HTMLArrayOf(HTMLElement *) *)childElementNodes
{
NSMutableArray *childElements = [NSMutableArray arrayWithCapacity:self.numberOfChildren];
for (id node in _children) {
if ([node isKindOfClass:[HTMLElement class]]) {
[childElements addObject:node];
}
}
return childElements;
}
- (HTMLEnumeratorOf(HTMLNode *) *)treeEnumerator
{
return [[HTMLTreeEnumerator alloc] initWithNode:self reversed:NO];
}
- (HTMLEnumeratorOf(HTMLNode *) *)reversedTreeEnumerator
{
return [[HTMLTreeEnumerator alloc] initWithNode:self reversed:YES];
}
- (NSString *)textContent
{
NSMutableArray *parts = [NSMutableArray new];
for (HTMLTextNode *node in self.treeEnumerator) {
if ([node isKindOfClass:[HTMLTextNode class]]) {
[parts addObject:node.data];
}
}
return [parts componentsJoinedByString:@""];
}
- (void)setTextContent:(NSString *)textContent
{
NSParameterAssert(textContent);
[[self mutableChildren] removeAllObjects];
if (textContent.length > 0) {
HTMLTextNode *textNode = [[HTMLTextNode alloc] initWithData:textContent];
[[self mutableChildren] addObject:textNode];
}
}
- (NSArray *)textComponents
{
NSMutableArray *textComponents = [NSMutableArray new];
for (HTMLTextNode *textNode in _children) {
if ([textNode isKindOfClass:[HTMLTextNode class]]) {
[textComponents addObject:textNode.data];
}
}
return textComponents;
}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone * __nullable)zone
{
return [[self.class allocWithZone:zone] init];
}
@end
/**
* The proxy returned by -mutableOrderedSetValueForKey: is quite useless, crashing in -removeObject: and -indexOfObject:. Here's an alternate.
*/
@implementation HTMLChildrenRelationshipProxy : NSMutableOrderedSet
- (instancetype)initWithNode:(HTMLNode *)node children:(NSMutableOrderedSet *)children
{
if ((self = [super init])) {
_node = node;
_children = children;
}
return self;
}
- (NSUInteger)count
{
return _children.count;
}
- (id)objectAtIndex:(NSUInteger)index
{
return [_children objectAtIndex:index];
}
- (NSUInteger)indexOfObject:(id)object
{
return [_children indexOfObject:object];
}
- (void)insertObject:(id)object atIndex:(NSUInteger)index
{
[_node insertObject:object inChildrenAtIndex:index];
}
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
{
[_node insertChildren:objects atIndexes:indexes];
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)object
{
[_node replaceObjectInChildrenAtIndex:index withObject:object];
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
[_node removeObjectFromChildrenAtIndex:index];
}
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
{
[_node removeChildrenAtIndexes:indexes];
}
@end
NS_ASSUME_NONNULL_END