-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWAHtmlRenderer.m
145 lines (115 loc) · 3.23 KB
/
WAHtmlRenderer.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
//
// WAHtmlRenderer.m
// ObjectiveHTTPD
//
// Created by Marcel Weiher on 6/23/07.
// Copyright 2007 Marcel Weiher. All rights reserved.
//
#import "WAHtmlRenderer.h"
#import <MPWFoundation/MPWXmlGeneratorStream.h>
@implementation WAHtmlRenderer
-(SEL)streamWriterMessage
{
return @selector(renderOn:);
}
+defaultTarget
{
return [MPWXmlGeneratorStream stream];
}
-(void)bold:textContent
{
[self.target writeElementName:"b" attributes:nil contents:textContent];
}
-(void)element:(char*)name attributes:attributes content:content selector:(SEL)selector
{
// NSLog(@"element with contents and selector");
[self startTag:name attributes:attributes];
// NSLog(@"did start, will do content");
[content performSelector:selector withObject:self];
// NSLog(@"did content");
// objc_msgSend( content, selector, self );
[self closeTag:name];
}
-(void)element:(char*)name content:content selector:(SEL)selector
{
[self element:name attributes:nil content:content selector:selector];
}
-(void)element:(char*)name attributes:attrs content:content
{
// SEL msg = [self streamWriterMessage];
// NSLog(@"will forward");
[self element:name attributes:attrs content:content selector:streamWriterMessage];
}
-(void)element:(char*)name content:content
{
[self element:name content:content selector:[self streamWriterMessage]];
}
-(void)anchor:textContent href:hrefUrl
{
[self.target beginStartTag:"a"];
[self.target writeAttribute:@"href" value:hrefUrl];
[self.target endStartTag:"a" single:NO];
[self.target writeString:textContent];
[self.target writeCloseTag:"a"];
// [self element:"a" attributes:[NSString stringWithFormat:@"href='%@'",hrefUrl] content:textContent];
}
-(void)startTag:(char*)name attributes:attrs
{
[self.target writeStartTag:name attributes:attrs single:NO];
}
-(void)startTag:(char*)name
{
[self.target writeStartTag:name attributes:nil single:NO];
}
-(void)closeTag:(char*)name
{
[self.target writeCloseTag:name];
}
-result { return [self finalTarget]; }
-resultString { return [[self result] stringValue]; }
@end
@implementation NSObject(renderOn)
-(void)renderOn:(WAHtmlRenderer*)renderer
{
[renderer writeNSObject:self];
}
@end
@implementation WAHtmlRenderer(testing)
+(void)testBoldNews
{
id stream=[self stream];
[stream bold:@"News"];
id result=[stream resultString];
IDEXPECT( result, @"<b>News</b>", @"Result of doing bold:'news'");
}
+(void)testAnchor
{
id stream=[self stream];
[stream anchor:@"Products" href:@"Products.html"];
id result=[stream resultString];
IDEXPECT( result, @"<a href=\"Products.html\">Products</a>", @"Result of doing anchor:'Products' :'Products.html'");
}
+(void)testTagConstruction
{
id stream=[self stream];
[stream startTag:"html" attributes:nil];
[stream closeTag:"html"];
id result=[stream resultString];
IDEXPECT( result, @"<html></html>", @"Result of empty html element");
}
+(void)testContent
{
WAHtmlRenderer *stream=[self stream];
[stream element:"b" attributes:@"attr='value'" content:@"some content"];
IDEXPECT( [stream resultString] , @"<b attr='value'>some content</b>", @"generic content" );
}
+(NSArray*)testSelectors
{
return [NSArray arrayWithObjects:
@"testBoldNews",
@"testAnchor",
@"testTagConstruction",
@"testContent",
nil];
}
@end