forked from SVGKit/SVGKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.m
More file actions
382 lines (310 loc) · 9.54 KB
/
Copy pathNode.m
File metadata and controls
382 lines (310 loc) · 9.54 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
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
//
// Node.m
// SVGKit
//
// Created by adam on 22/05/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Node.h"
#import "Node+Mutable.h"
#import "NodeList+Mutable.h"
#import "NamedNodeMap.h"
@implementation Node
@synthesize nodeName;
@synthesize nodeValue;
@synthesize nodeType;
@synthesize parentNode;
@synthesize childNodes;
@synthesize firstChild;
@synthesize lastChild;
@synthesize previousSibling;
@synthesize nextSibling;
@synthesize attributes;
// Modified in DOM Level 2:
@synthesize ownerDocument;
@synthesize hasAttributes, hasChildNodes;
@synthesize localName;
- (id)init
{
NSAssert( FALSE, @"This class has no init method - it MUST NOT be init'd via init - you MUST use one of the multi-argument constructors instead" );
return nil;
}
- (id)initType:(DOMNodeType) nt name:(NSString*) n value:(NSString*) v
{
if( [v isKindOfClass:[NSMutableString class]])
{
/** Apple allows this, but it breaks the whole of Obj-C / cocoa, which is damn stupid
So we have to fix it.*/
v = [NSString stringWithString:v];
}
self = [super init];
if (self) {
self.nodeType = nt;
switch( nt )
{
case DOMNodeType_ATTRIBUTE_NODE:
case DOMNodeType_CDATA_SECTION_NODE:
case DOMNodeType_COMMENT_NODE:
case DOMNodeType_PROCESSING_INSTRUCTION_NODE:
case DOMNodeType_TEXT_NODE:
{
self.nodeName = n;
self.nodeValue = v;
}break;
case DOMNodeType_DOCUMENT_NODE:
case DOMNodeType_DOCUMENT_TYPE_NODE:
case DOMNodeType_DOCUMENT_FRAGMENT_NODE:
case DOMNodeType_ENTITY_REFERENCE_NODE:
case DOMNodeType_ENTITY_NODE:
case DOMNodeType_NOTATION_NODE:
case DOMNodeType_ELEMENT_NODE:
{
NSAssert( FALSE, @"NodeType = %i cannot be init'd with a value; nodes of that type have no value in the DOM spec", nt);
self = nil;
}break;
}
self.childNodes = [[NodeList alloc] init];
}
return self;
}
- (id)initType:(DOMNodeType) nt name:(NSString*) n
{
self = [super init];
if (self) {
self.nodeType = nt;
switch( nt )
{
case DOMNodeType_ATTRIBUTE_NODE:
case DOMNodeType_CDATA_SECTION_NODE:
case DOMNodeType_COMMENT_NODE:
case DOMNodeType_PROCESSING_INSTRUCTION_NODE:
case DOMNodeType_TEXT_NODE:
{
NSAssert( FALSE, @"NodeType = %i cannot be init'd without a value; nodes of that type MUST have a value in the DOM spec", nt);
self = nil;
}break;
case DOMNodeType_DOCUMENT_NODE:
case DOMNodeType_DOCUMENT_TYPE_NODE:
case DOMNodeType_DOCUMENT_FRAGMENT_NODE:
case DOMNodeType_ENTITY_REFERENCE_NODE:
case DOMNodeType_ENTITY_NODE:
case DOMNodeType_NOTATION_NODE:
{
self.nodeName = n;
}break;
case DOMNodeType_ELEMENT_NODE:
{
self.nodeName = n;
self.attributes = [[NamedNodeMap alloc] init];
}break;
}
self.childNodes = [[NodeList alloc] init];
}
return self;
}
#pragma mark - Objective-C init methods DOM LEVEL 2 (preferred init - safer/better!)
-(void) postInitNamespaceHandling:(NSString*) nsURI
{
NSArray* nameSpaceParts = [self.nodeName componentsSeparatedByString:@":"];
self.localName = [nameSpaceParts lastObject];
if( [nameSpaceParts count] > 1 )
self.prefix = [nameSpaceParts objectAtIndex:0];
self.namespaceURI = nsURI;
}
- (id)initType:(DOMNodeType) nt name:(NSString*) n inNamespace:(NSString*) nsURI
{
self = [self initType:nt name:n];
if( self )
{
[self postInitNamespaceHandling:nsURI];
}
return self;
}
- (id)initType:(DOMNodeType) nt name:(NSString*) n value:(NSString*) v inNamespace:(NSString*) nsURI
{
if( [v isKindOfClass:[NSMutableString class]])
{
/** Apple allows this, but it breaks the whole of Obj-C / cocoa, which is damn stupid
So we have to fix it.*/
v = [NSString stringWithString:v];
}
self = [self initType:nt name:n value:v];
if( self )
{
[self postInitNamespaceHandling:nsURI];
}
return self;
}
#pragma mark - Official DOM method implementations
-(Node*) insertBefore:(Node*) newChild refChild:(Node*) refChild
{
if( refChild == nil )
{
[self.childNodes.internalArray addObject:newChild];
newChild.parentNode = self;
}
else
{
[self.childNodes.internalArray insertObject:newChild atIndex:[self.childNodes.internalArray indexOfObject:refChild]];
}
return newChild;
}
-(Node*) replaceChild:(Node*) newChild oldChild:(Node*) oldChild
{
if( newChild.nodeType == DOMNodeType_DOCUMENT_FRAGMENT_NODE )
{
/** Spec:
"If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, which are inserted in the same order. If the newChild is already in the tree, it is first removed."
*/
int oldIndex = [self.childNodes.internalArray indexOfObject:oldChild];
NSAssert( FALSE, @"We should be recursing down the tree to find 'newChild' at any location, and removing it - required by spec - but we have no convenience method for that search, yet" );
for( Node* child in newChild.childNodes.internalArray )
{
[self.childNodes.internalArray insertObject:child atIndex:oldIndex++];
}
newChild.parentNode = self;
oldChild.parentNode = nil;
return oldChild;
}
else
{
[self.childNodes.internalArray replaceObjectAtIndex:[self.childNodes.internalArray indexOfObject:oldChild] withObject:newChild];
newChild.parentNode = self;
oldChild.parentNode = nil;
return oldChild;
}
}
-(Node*) removeChild:(Node*) oldChild
{
[self.childNodes.internalArray removeObject:oldChild];
oldChild.parentNode = nil;
return oldChild;
}
-(Node*) appendChild:(Node*) newChild
{
[self.childNodes.internalArray removeObject:newChild]; // required by spec
[self.childNodes.internalArray addObject:newChild];
newChild.parentNode = self;
return newChild;
}
-(BOOL)hasChildNodes
{
return (self.childNodes.length > 0);
}
-(Node*) cloneNode:(BOOL) deep
{
NSAssert( FALSE, @"Not implemented yet - read the spec. Sounds tricky. I'm too tired, and would probably screw it up right now" );
return nil;
}
// Modified in DOM Level 2:
-(void) normalize
{
NSAssert( FALSE, @"Not implemented yet - read the spec. Sounds tricky. I'm too tired, and would probably screw it up right now" );
}
// Introduced in DOM Level 2:
-(BOOL) isSupportedFeature:(NSString*) feature version:(NSString*) version
{
NSAssert( FALSE, @"Not implemented yet - read the spec. I have literally no idea what this is supposed to do." );
return FALSE;
}
// Introduced in DOM Level 2:
@synthesize namespaceURI;
// Introduced in DOM Level 2:
@synthesize prefix;
// Introduced in DOM Level 2:
-(BOOL)hasAttributes
{
if( self.attributes == nil )
return FALSE;
return (self.attributes.length > 0 );
}
#pragma mark - SPECIAL CASE: DOM level 3 method
/**
Note that the DOM 3 spec defines this as RECURSIVE:
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent
*/
-(NSString *)textContent
{
switch( self.nodeType )
{
case DOMNodeType_ELEMENT_NODE:
case DOMNodeType_ATTRIBUTE_NODE:
case DOMNodeType_ENTITY_NODE:
case DOMNodeType_ENTITY_REFERENCE_NODE:
case DOMNodeType_DOCUMENT_FRAGMENT_NODE:
{
/** DOM 3 Spec:
"concatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children."
*/
NSMutableString* stringAccumulator = [[NSMutableString alloc] init];
for( Node* subNode in self.childNodes.internalArray )
{
NSString* subText = subNode.textContent; // don't call this method twice; it's expensive to calculate!
if( subText != nil ) // Yes, really: Apple docs require that you never append a nil substring. Sigh
[stringAccumulator appendString:subText];
}
return [NSString stringWithString:stringAccumulator];
}
case DOMNodeType_TEXT_NODE:
case DOMNodeType_CDATA_SECTION_NODE:
case DOMNodeType_COMMENT_NODE:
case DOMNodeType_PROCESSING_INSTRUCTION_NODE:
{
return self.nodeValue; // should never be nil; anything with a valid value will be at least an empty string i.e. ""
}
case DOMNodeType_DOCUMENT_NODE:
case DOMNodeType_NOTATION_NODE:
case DOMNodeType_DOCUMENT_TYPE_NODE:
{
return nil;
}
}
}
#pragma mark - ADDITIONAL to SVG Spec: useful debug / output / description methods
-(NSString *)description
{
NSString* nodeTypeName;
switch( self.nodeType )
{
case DOMNodeType_ELEMENT_NODE:
nodeTypeName = @"ELEMENT";
break;
case DOMNodeType_TEXT_NODE:
nodeTypeName = @"TEXT";
break;
case DOMNodeType_ENTITY_NODE:
nodeTypeName = @"ENTITY";
break;
case DOMNodeType_COMMENT_NODE:
nodeTypeName = @"COMMENT";
break;
case DOMNodeType_DOCUMENT_NODE:
nodeTypeName = @"DOCUMENT";
break;
case DOMNodeType_NOTATION_NODE:
nodeTypeName = @"NOTATION";
break;
case DOMNodeType_ATTRIBUTE_NODE:
nodeTypeName = @"ATTRIBUTE";
break;
case DOMNodeType_CDATA_SECTION_NODE:
nodeTypeName = @"CDATA";
break;
case DOMNodeType_DOCUMENT_TYPE_NODE:
nodeTypeName = @"DOC TYPE";
break;
case DOMNodeType_ENTITY_REFERENCE_NODE:
nodeTypeName = @"ENTITY REF";
break;
case DOMNodeType_DOCUMENT_FRAGMENT_NODE:
nodeTypeName = @"DOC FRAGMENT";
break;
case DOMNodeType_PROCESSING_INSTRUCTION_NODE:
nodeTypeName = @"PROCESSING INSTRUCTION";
break;
default:
nodeTypeName = @"N/A (DATA IS MISSING FROM NODE INSTANCE)";
}
return [NSString stringWithFormat:@"Node: %@ (%@) value:[%@] @@%ld attributes + %ld x children", self.nodeName, nodeTypeName, [self.nodeValue length]<11 ? self.nodeValue : [NSString stringWithFormat:@"%@...",[self.nodeValue substringToIndex:10]], self.attributes.length, self.childNodes.length];
}
@end