-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocHTMLElement.m
250 lines (207 loc) · 5.25 KB
/
DocHTMLElement.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
/*
Copyright (C) 2008 Nicolas Roard
Author: Nicolas Roard
Date: June 2008
License: Modified BSD (see COPYING)
*/
#import "DocHTMLElement.h"
@interface DocHTMLElement (Private)
- (void) collectChildContentWithString: (NSMutableString *)buf;
@end
@interface BlankHTMLElement : DocHTMLElement
@end
@implementation BlankHTMLElement
- (NSString *) content
{
NSMutableString *buf = [NSMutableString new];
[self collectChildContentWithString: buf];
return buf;
}
@end
@implementation DocHTMLElement
+ (DocHTMLElement *) blankElement
{
return [[BlankHTMLElement alloc] initWithName: @"Blank"];
}
+ (DocHTMLElement *) elementWithName: (NSString *) aName
{
return [[DocHTMLElement alloc] initWithName: aName];
}
- (DocHTMLElement *) initWithName: (NSString *) aName
{
SUPERINIT;
children = [NSMutableArray new];
attributes = [NSMutableDictionary new];
elementName = [[NSString alloc] initWithString: aName];
blockElementNames = [[NSSet alloc] initWithObjects: @"div", @"dt", @"dl", nil];
return self;
}
- (BOOL) isEqual: (id)anObject
{
return [[self description] isEqualToString: [anObject description]];
}
- (DocHTMLElement *) addText: (NSString *) aText
{
if (aText)
{
[children addObject: aText];
}
return self;
}
- (DocHTMLElement *) add: (DocHTMLElement *) anElem
{
if (anElem)
{
[children addObject: anElem];
}
return self;
}
- (DocHTMLElement *) id: (NSString *) anID
{
if (anID)
{
[attributes setObject: anID forKey: @"id"];
}
return self;
}
- (DocHTMLElement *) class: (NSString *) aClass
{
if (aClass)
{
[attributes setObject: aClass forKey: @"class"];
}
return self;
}
- (DocHTMLElement *) name: (NSString *) aName
{
if (aName)
{
[attributes setObject: aName forKey: @"name"];
}
return self;
}
/*
forwarding...
*/
- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector
{
NSString *sig = NSStringFromSelector(aSelector);
//NSLog (@"methodSignature for selector <%@>", sig);
NSArray *components = [sig componentsSeparatedByString: @":"];
NSMutableString *signature = [NSMutableString stringWithString: @"@@:"];
NSUInteger nbOfComponents = [components count];
for (int i=0; i < nbOfComponents; i++)
{
NSString *component = [components objectAtIndex: i];
if ([component length] == 0)
continue;
//NSLog (@"component <%@>", component);
[signature appendString: @"@"];
}
// NSLog (@"generated sig <%@> for sel<%@>", signature, sig);
return [NSMethodSignature signatureWithObjCTypes: [signature UTF8String]];
}
#define CALLM(assig,asig)\
if ([component isEqualToString: assig])\
{\
id arg;\
[invocation getArgument: &arg atIndex: i + 2];\
[self asig: arg];\
}
- (void) forwardInvocation: (NSInvocation *) invocation
{
//NSLog (@"invocation called <%@>", NSStringFromSelector([invocation selector]));
NSString *sig = NSStringFromSelector([invocation selector]);
NSArray *components = [sig componentsSeparatedByString: @":"];
NSUInteger nbOfComponents = [components count];
for (int i = 0; i < nbOfComponents; i++)
{
NSString *component = [components objectAtIndex: i];
if ([component length] == 0)
continue;
//NSLog (@"component <%@>", component);
if ([component isEqualToString: @"id"])
{
__unsafe_unretained id arg;
[invocation getArgument: &arg atIndex: i + 2];
[self id: arg];
}
if ([component isEqualToString: @"class"])
{
__unsafe_unretained id arg;
[invocation getArgument: &arg atIndex: i + 2];
[self class: arg];
}
if ([component isEqualToString: @"with"])
{
__unsafe_unretained id arg;
[invocation getArgument: &arg atIndex: i + 2];
[self with: arg];
}
if ([component isEqualToString: @"and"])
{
__unsafe_unretained id arg;
[invocation getArgument: &arg atIndex: i + 2];
[self and: arg];
}
}
//NSLog (@"after invocation of <%@>:\n%@", sig, [self content]);
//__unsafe_unretained id newSelf = self;
[invocation setReturnValue: (void *)&self];
}
- (DocHTMLElement *) with: (id) something
{
if ([something isKindOfClass: [NSString class]])
{
return [self addText: something];
}
else
{
return [self add: something];
}
}
- (DocHTMLElement *) and: (id) something
{
return [self with: something];
}
- (void) collectChildContentWithString: (NSMutableString *)buf
{
for (id elem in children)
{
if ([elem isKindOfClass: [DocHTMLElement class]])
{
[buf appendString: [elem content]];
}
else if ([elem isKindOfClass: [NSString class]])
{
[buf appendString: elem];
}
else
{
[buf appendString: [elem description]];
}
}
}
- (NSString *) content
{
NSMutableString* buf = [NSMutableString new];
BOOL insertNewLine = [blockElementNames containsObject: elementName];
[buf appendFormat: @"<%@", elementName];
for (NSString *key in attributes)
{
[buf appendFormat: @" %@=\"%@\"", key, [attributes objectForKey: key]];
}
[buf appendString: @">"];
if (insertNewLine)
[buf appendString: @"\n"];
[self collectChildContentWithString: buf];
[buf appendFormat: @"</%@>", elementName];
if (insertNewLine)
[buf appendString: @"\n"];
return buf;
}
- (NSString *) description
{
return [self content];
}
@end