/
Element.m
174 lines (134 loc) · 4.74 KB
/
Element.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
#import "Element.h"
#import "NamedNodeMap.h"
#import "DOMHelperUtilities.h"
@interface Element()
@property(nonatomic,retain,readwrite) NSString* tagName;
@end
@implementation Element
@synthesize tagName;
- (void)dealloc {
self.tagName = nil;
[super dealloc];
}
- (id)initWithLocalName:(NSString*) n attributes:(NSMutableDictionary*) attributes {
self = [super initType:DOMNodeType_ELEMENT_NODE name:n];
if (self) {
self.tagName = n;
for( NSString* attributeName in attributes.allKeys )
{
[self setAttribute:attributeName value:[attributes objectForKey:attributeName]];
}
}
return self;
}
- (id)initWithQualifiedName:(NSString*) n inNameSpaceURI:(NSString*) nsURI attributes:(NSMutableDictionary *)attributes
{
self = [super initType:DOMNodeType_ELEMENT_NODE name:n inNamespace:nsURI];
if (self) {
self.tagName = n;
for( Attr* attribute in attributes.allValues )
{
[self.attributes setNamedItemNS:attribute inNodeNamespace:nsURI];
}
}
return self;
}
-(NSString*) getAttribute:(NSString*) name
{
/**
WARNING: the definition in the spec WILL CORRUPT unsuspecting Objective-C code (including a lot of the original SVGKit code!).
The spec - instead of defining 'nil' - defines "" (empty string) as the
correct response.
But in most of the modern, C-based, (non-scripting) languages, "" means 0.
Very dangerous!
*/
Attr* result = (Attr*) [self.attributes getNamedItem:name];
if( result == nil || result.value == nil )
return @""; // according to spec
else
return result.value;
}
-(void) setAttribute:(NSString*) name value:(NSString*) value
{
Attr* att = [[[Attr alloc] initWithName:name value:value] autorelease];
[self.attributes setNamedItem:att];
}
-(void) removeAttribute:(NSString*) name
{
[self.attributes removeNamedItem:name];
NSAssert( FALSE, @"Not fully implemented. Spec says: If the removed attribute is known to have a default value, an attribute immediately appears containing the default value as well as the corresponding namespace URI, local name, and prefix when applicable." );
}
-(Attr*) getAttributeNode:(NSString*) name
{
return (Attr*) [self.attributes getNamedItem:name];
}
-(Attr*) setAttributeNode:(Attr*) newAttr
{
Attr* oldAtt = (Attr*) [self.attributes getNamedItem:newAttr.nodeName];
[self.attributes setNamedItem:newAttr];
return oldAtt;
}
-(Attr*) removeAttributeNode:(Attr*) oldAttr
{
[self.attributes removeNamedItem:oldAttr.nodeName];
NSAssert( FALSE, @"Not fully implemented. Spec: If the removed Attr has a default value it is immediately replaced. The replacing attribute has the same namespace URI and local name, as well as the original prefix, when applicable. " );
return oldAttr;
}
-(NodeList*) getElementsByTagName:(NSString*) name
{
NodeList* accumulator = [[[NodeList alloc] init] autorelease];
[DOMHelperUtilities privateGetElementsByName:name inNamespace:nil childrenOfElement:self addToList:accumulator];
return accumulator;
}
// Introduced in DOM Level 2:
-(NSString*) getAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
{
Attr* result = (Attr*) [self.attributes getNamedItemNS:namespaceURI localName:localName];
if( result == nil || result.value == nil )
return @""; // according to spec
else
return result.value;
}
// Introduced in DOM Level 2:
-(void) setAttributeNS:(NSString*) namespaceURI qualifiedName:(NSString*) qualifiedName value:(NSString*) value
{
Attr* att = [[[Attr alloc] initWithNamespace:namespaceURI qualifiedName:qualifiedName value:value] autorelease];
[self.attributes setNamedItemNS:att];
}
// Introduced in DOM Level 2:
-(void) removeAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
{
NSAssert( FALSE, @"Not implemented yet" );
}
// Introduced in DOM Level 2:
-(Attr*) getAttributeNodeNS:(NSString*) namespaceURI localName:(NSString*) localName
{
Attr* result = (Attr*) [self.attributes getNamedItemNS:namespaceURI localName:localName];
return result;
}
// Introduced in DOM Level 2:
-(Attr*) setAttributeNodeNS:(Attr*) newAttr
{
NSAssert( FALSE, @"Not implemented yet" );
return nil;
}
// Introduced in DOM Level 2:
-(NodeList*) getElementsByTagNameNS:(NSString*) namespaceURI localName:(NSString*) localName
{
NodeList* accumulator = [[[NodeList alloc] init] autorelease];
[DOMHelperUtilities privateGetElementsByName:localName inNamespace:namespaceURI childrenOfElement:self addToList:accumulator];
return accumulator;
}
// Introduced in DOM Level 2:
-(BOOL) hasAttribute:(NSString*) name
{
Attr* result = (Attr*) [self.attributes getNamedItem:name];
return result != nil;
}
// Introduced in DOM Level 2:
-(BOOL) hasAttributeNS:(NSString*) namespaceURI localName:(NSString*) localName
{
NSAssert( FALSE, @"Not implemented yet" );
return FALSE;
}
@end