Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 444 lines (354 sloc) 13.238 kB
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
1 //
2 // KSHTMLWriter.m
3 //
efecaa1 @mikeabdullah Settle on BSD license.
mikeabdullah authored
4 // Copyright (c) 2010, Mike Abdullah and Karelia Software
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL MIKE ABDULLAH OR KARELIA SOFTWARE BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
25 //
26
efecaa1 @mikeabdullah Settle on BSD license.
mikeabdullah authored
27
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
28 #import "KSHTMLWriter.h"
29
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
30 #import "KSElementInfo.h"
31
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
32
0225a1a @mikeabdullah Lots of lovely doctype constants
mikeabdullah authored
33 NSString *KSHTMLWriterDocTypeHTML_4_01_Strict = @"HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"";
34 NSString *KSHTMLWriterDocTypeHTML_4_01_Transitional = @"HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"";
35 NSString *KSHTMLWriterDocTypeHTML_4_01_Frameset = @"HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\"";
36 NSString *KSHTMLWriterDocTypeXHTML_1_0_Strict = @"html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"";
37 NSString *KSHTMLWriterDocTypeXHTML_1_0_Transitional = @"html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"";
38 NSString *KSHTMLWriterDocTypeXHTML_1_0_Frameset = @"html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\"";
39 NSString *KSHTMLWriterDocTypeXHTML_1_1 = @"html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"";
40 NSString *KSHTMLWriterDocTypeHTML_5 = @"html";
41
42
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
43 @interface KSHTMLWriter ()
44 @property(nonatomic, copy, readwrite) NSString *docType;
45 @end
46
47
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
48 @implementation KSHTMLWriter
49
72adfa1 @mikeabdullah Documenting .isXHTML generation better.
mikeabdullah authored
50 #pragma mark Creating an HTML Writer
51
5f5a663 @mikeabdullah Consistently use "writer" or "output" terminology.
mikeabdullah authored
52 - (id)initWithOutputWriter:(id <KSWriter>)output;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
53 {
5f5a663 @mikeabdullah Consistently use "writer" or "output" terminology.
mikeabdullah authored
54 [super initWithOutputWriter:output];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
55
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
56 [self setDocType:KSHTMLWriterDocTypeHTML_5];
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
57 _IDs = [[NSMutableSet alloc] init];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
58 _classNames = [[NSMutableArray alloc] init];
59
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
60 return self;
61 }
62
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
63 - (id)initWithOutputWriter:(id <KSWriter>)output docType:(NSString *)docType encoding:(NSStringEncoding)encoding;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
64 {
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
65 if (self = [self initWithOutputWriter:output encoding:encoding])
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
66 {
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
67 [self setDocType:docType];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
68 }
69
70 return self;
71 }
72
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
73 - (void)dealloc
74 {
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
75 [_IDs release];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
76 [_classNames release];
77
78 [super dealloc];
79 }
80
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
81 #pragma mark DTD
72adfa1 @mikeabdullah Documenting .isXHTML generation better.
mikeabdullah authored
82
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
83 - (void)startDocumentWithDocType:(NSString *)docType encoding:(NSStringEncoding)encoding;
84 {
85 [self setDocType:docType];
86 [super startDocumentWithDocType:docType encoding:encoding];
87 }
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
88
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
89 @synthesize docType = _docType;
90 - (void)setDocType:(NSString *)docType;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
91 {
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
92 docType = [docType copy];
93 [_docType release]; _docType = docType;
94
3e35588 @mikeabdullah Convenience class method to know if a doctype will produce XHTML
mikeabdullah authored
95 _isXHTML = [[self class] isDocTypeXHTML:docType];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
96 }
97
265ea17 @mikeabdullah Make use of the new docType support
mikeabdullah authored
98 - (BOOL)isXHTML; { return _isXHTML; }
99
3e35588 @mikeabdullah Convenience class method to know if a doctype will produce XHTML
mikeabdullah authored
100 + (BOOL)isDocTypeXHTML:(NSString *)docType;
101 {
102 BOOL result = !([docType isEqualToString:KSHTMLWriterDocTypeHTML_4_01_Strict] ||
103 [docType isEqualToString:KSHTMLWriterDocTypeHTML_4_01_Transitional] ||
104 [docType isEqualToString:KSHTMLWriterDocTypeHTML_4_01_Frameset]);
105 return result;
106 }
107
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
108 #pragma mark CSS Class Name
109
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
110 - (NSString *)currentElementClassName;
ecfabe0 @mikeabdullah Expose -className.
mikeabdullah authored
111 {
112 NSString *result = nil;
113 if ([_classNames count])
114 {
115 result = [_classNames componentsJoinedByString:@" "];
116 }
117 return result;
118 }
119
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
120 - (void)pushClassName:(NSString *)className;
121 {
122 [_classNames addObject:className];
123 }
124
30bbf6a Allow values for attributes to be type id, not
Dan Wood authored
125 - (void)pushAttribute:(NSString *)attribute value:(id)value;
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
126 {
127 if ([attribute isEqualToString:@"class"])
128 {
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
129 return [self pushClassName:value];
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
130 }
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
131
132 // Keep track of IDs in use
133 if ([attribute isEqualToString:@"id"]) [_IDs addObject:value];
134 [super pushAttribute:attribute value:value];
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
135 }
136
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
137 - (KSElementInfo *)currentElementInfo;
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
138 {
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
139 KSElementInfo *result = [super currentElementInfo];
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
140
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
141 // Add in buffered class info
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
142 NSString *class = [self currentElementClassName];
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
143 if (class) [result addAttribute:@"class" value:class];
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
144
145 return result;
146 }
147
d7376e3 @mikeabdullah Sister method: -[KSXMLWriter currentElementHasAttributes]
mikeabdullah authored
148 - (BOOL)currentElementHasAttributes;
149 {
150 return ([super currentElementHasAttributes] || [_classNames count]);
151 }
152
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
153 #pragma mark HTML Fragments
154
155 - (void)writeHTMLString:(NSString *)html;
156 {
157 [self writeString:html];
158 }
159
160 - (void)writeHTMLFormat:(NSString *)format , ...
161 {
162 va_list argList;
163 va_start(argList, format);
164 NSString *aString = [[[NSString alloc] initWithFormat:format arguments:argList] autorelease];
165 va_end(argList);
166
167 [self writeHTMLString:aString];
168 }
169
170 #pragma mark General
171
172 - (void)startElement:(NSString *)tagName className:(NSString *)className;
173 {
174 [self startElement:tagName idName:nil className:className];
175 }
176
177 - (void)startElement:(NSString *)tagName idName:(NSString *)idName className:(NSString *)className;
178 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
179 if (idName) [self pushAttribute:@"id" value:idName];
180 if (className) [self pushAttribute:@"class" value:className];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
181
182 [self startElement:tagName];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
183 }
184
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
185 - (BOOL)isIDValid:(NSString *)anID; // NO if the ID has already been used
186 {
187 BOOL result = ![_IDs containsObject:anID];
188 return result;
189 }
190
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
191 #pragma mark Line Break
192
193 - (void)writeLineBreak;
194 {
195 [self startElement:@"br"];
196 [self endElement];
197 }
198
199 #pragma mark Higher-level Tag Writing
200
201 - (void)startAnchorElementWithHref:(NSString *)href title:(NSString *)titleString target:(NSString *)targetString rel:(NSString *)relString;
202 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
203 if (href) [self pushAttribute:@"href" value:href];
204 if (targetString) [self pushAttribute:@"target" value:targetString];
205 if (titleString) [self pushAttribute:@"title" value:titleString];
206 if (relString) [self pushAttribute:@"rel" value:relString];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
207
208 [self startElement:@"a"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
209 }
210
ac684bc @mikeabdullah Don't need to pass id and class directly when writing an image.
mikeabdullah authored
211 - (void)writeImageWithSrc:(NSString *)src
212 alt:(NSString *)alt
30bbf6a Allow values for attributes to be type id, not
Dan Wood authored
213 width:(id)width
214 height:(id)height;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
215 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
216 [self pushAttribute:@"src" value:src];
217 [self pushAttribute:@"alt" value:alt];
218 if (width) [self pushAttribute:@"width" value:width];
219 if (height) [self pushAttribute:@"height" value:height];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
220
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
221 [self startElement:@"img"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
222 [self endElement];
223 }
224
225 #pragma mark Link
226
227 - (void)writeLinkWithHref:(NSString *)href
228 type:(NSString *)type
229 rel:(NSString *)rel
230 title:(NSString *)title
231 media:(NSString *)media;
232 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
233 if (rel) [self pushAttribute:@"rel" value:rel];
15c3119 @mikeabdullah Merge (I think).
mikeabdullah authored
234 if (!type) type = @"text/css"; [self pushAttribute:@"type" value:type];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
235 [self pushAttribute:@"href" value:href];
236 if (title) [self pushAttribute:@"title" value:title];
237 if (media) [self pushAttribute:@"media" value:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
238
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
239 [self startElement:@"link"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
240 [self endElement];
241 }
242
243 - (void)writeLinkToStylesheet:(NSString *)href
244 title:(NSString *)title
245 media:(NSString *)media;
246 {
42af387 @mikeabdullah Writing a <LINK> with nil type will automatically fill in default typ…
mikeabdullah authored
247 [self writeLinkWithHref:href type:nil rel:@"stylesheet" title:title media:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
248 }
249
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
250 #pragma mark Scripts
251
3a9930c @mikeabdullah -writeJavascriptWithSrc:encoding: method that tries to do the right w…
mikeabdullah authored
252 - (void)writeJavascriptWithSrc:(NSString *)src encoding:(NSStringEncoding)encoding;
253 {
254 // According to the HTML spec, charset only needs to be specified if the script is a different encoding to the document
255 NSString *charset = nil;
256 if (encoding != [self encoding])
257 {
258 charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(encoding));
259 }
260
261 [self writeJavascriptWithSrc:src charset:charset];
262 }
263
b695665 Src may be nil; it's handled in support classes
Dan Wood authored
264 - (void)writeJavascriptWithSrc:(NSString *)src charset:(NSString *)charset; // src may be nil
265 {
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
266 if (charset) [self pushAttribute:@"charset" value:charset];
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
267 [self startJavascriptElementWithSrc:src];
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
268 [self endElement];
269 }
de567f7 added method to output JavaScript (via Dan)
Terrence Talbot authored
270
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
271 - (void)writeJavascript:(NSString *)script useCDATA:(BOOL)useCDATA;
272 {
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
273 [self startJavascriptElementWithSrc:nil];
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
274
275 if (useCDATA) [self startJavascriptCDATA];
276 [self writeString:script];
277 if (useCDATA) [self endJavascriptCDATA];
278
279 [self endElement];
280 }
281
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
282 - (void)startJavascriptElementWithSrc:(NSString *)src; // src may be nil
283 {
4af9cd9 @mikeabdullah HTML5 doesn't bother specifying the type of <SCRIPT>s
mikeabdullah authored
284 // HTML5 doesn't need the script type specified, but older doc types do for standards-compliance
285 if (![[self docType] isEqualToString:KSHTMLWriterDocTypeHTML_5])
286 {
287 [self pushAttribute:@"type" value:@"text/javascript"];
288 }
289
290 // Script
3530727 Don't specify charset for inline javascript, only
Dan Wood authored
291 if (src)
292 {
293 [self pushAttribute:@"src" value:src];
294 }
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
295
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
296 [self startElement:@"script"];
8a9783e @mikeabdullah Embedded scripts should start on a newline.
mikeabdullah authored
297
298 // Embedded scripts should start on their own line for clarity
2d68556 @mikeabdullah Embedded scripts should also write their end tag on a new line.
mikeabdullah authored
299 if (!src)
300 {
301 [self writeString:@"\n"];
302 [self stopWritingInline];
303 }
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
304 }
305
f241869 @mikeabdullah Split out -startJavascriptCDATA and -endJavascriptCDATA.
mikeabdullah authored
306 - (void)startJavascriptCDATA;
307 {
308 [self writeString:@"\n/* "];
309 [self startCDATA];
310 [self writeString:@" */"];
311 }
312
313 - (void)endJavascriptCDATA;
314 {
315 [self writeString:@"\n/* "];
316 [self endCDATA];
317 [self writeString:@" */\n"];
318 }
319
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
320 #pragma mark Param
321
322 - (void)writeParamElementWithName:(NSString *)name value:(NSString *)value;
323 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
324 if (name) [self pushAttribute:@"name" value:name];
325 if (value) [self pushAttribute:@"value" value:value];
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
326 [self startElement:@"param"];
327 [self endElement];
328 }
329
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
330 #pragma mark Style
331
6dd07ee @mikeabdullah Convenience method for writing inline CSS.
mikeabdullah authored
332 - (void)writeStyleElementWithCSSString:(NSString *)css;
333 {
334 [self startStyleElementWithType:@"text/css"];
335 [self writeString:css]; // browsers don't expect styles to be XML escaped
336 [self endElement];
337 }
338
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
339 - (void)startStyleElementWithType:(NSString *)type;
340 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
341 if (type) [self pushAttribute:@"type" value:type];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
342 [self startElement:@"style"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
343 }
344
345 #pragma mark Elements Stack
346
347 - (BOOL)topElementIsList;
348 {
349 NSString *tagName = [self topElement];
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
350 BOOL result = ([tagName isEqualToString:@"ul"] ||
351 [tagName isEqualToString:@"ol"]);
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
352 return result;
353 }
354
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
355 #pragma mark (X)HTML
356
357 - (BOOL)elementCanBeEmpty:(NSString *)tagName;
358 {
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
359 if ([tagName isEqualToString:@"br"] ||
360 [tagName isEqualToString:@"img"] ||
361 [tagName isEqualToString:@"hr"] ||
362 [tagName isEqualToString:@"meta"] ||
363 [tagName isEqualToString:@"link"] ||
364 [tagName isEqualToString:@"input"] ||
365 [tagName isEqualToString:@"base"] ||
366 [tagName isEqualToString:@"basefont"] ||
367 [tagName isEqualToString:@"param"] ||
368 [tagName isEqualToString:@"area"] ||
369 [tagName isEqualToString:@"source"]) return YES;
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
370
371 return NO;
372 }
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
373
374 - (BOOL)canWriteElementInline:(NSString *)tagName;
375 {
376 switch ([tagName length])
377 {
378 case 1:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
379 if ([tagName isEqualToString:@"a"] ||
380 [tagName isEqualToString:@"b"] ||
381 [tagName isEqualToString:@"i"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
382 break;
383
384 case 2:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
385 if ([tagName isEqualToString:@"br"] ||
386 [tagName isEqualToString:@"em"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
387 break;
388
389 case 3:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
390 if ([tagName isEqualToString:@"img"] ||
391 [tagName isEqualToString:@"sup"] ||
392 [tagName isEqualToString:@"sub"] ||
393 [tagName isEqualToString:@"big"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
394 break;
395
396 case 4:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
397 if ([tagName isEqualToString:@"span"] ||
398 [tagName isEqualToString:@"font"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
399 break;
400
401 case 5:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
402 if ([tagName isEqualToString:@"small"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
403 break;
404
405 case 6:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
406 if ([tagName isEqualToString:@"strong"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
407 break;
408 }
409
410 return [super canWriteElementInline:tagName];
411 }
412
413 #pragma mark Element Primitives
414
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
415 - (void)startElement:(NSString *)elementName writeInline:(BOOL)writeInline; // for more control
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
416 {
566d24c @mikeabdullah non-lowercase tags shouldn't be used for HTML.
mikeabdullah authored
417 NSAssert1([elementName isEqualToString:[elementName lowercaseString]], @"Attempt to start non-lowercase element: %@", elementName);
418
419
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
420 // Add in any pre-written classes
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
421 NSString *class = [self currentElementClassName];
ecfabe0 @mikeabdullah Expose -className.
mikeabdullah authored
422 if (class)
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
423 {
424 [_classNames removeAllObjects];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
425 [super pushAttribute:@"class" value:class];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
426 }
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
427
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
428 [super startElement:elementName writeInline:writeInline];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
429 }
430
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
431 - (void)closeEmptyElementTag; // /> OR > depending on -isXHTML
432 {
433 if ([self isXHTML])
434 {
435 [super closeEmptyElementTag];
436 }
437 else
438 {
439 [self writeString:@">"];
440 }
441 }
442
443 @end
Something went wrong with that request. Please try again.