Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 431 lines (342 sloc) 12.708 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
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
148 #pragma mark HTML Fragments
149
150 - (void)writeHTMLString:(NSString *)html;
151 {
152 [self writeString:html];
153 }
154
155 - (void)writeHTMLFormat:(NSString *)format , ...
156 {
157 va_list argList;
158 va_start(argList, format);
159 NSString *aString = [[[NSString alloc] initWithFormat:format arguments:argList] autorelease];
160 va_end(argList);
161
162 [self writeHTMLString:aString];
163 }
164
165 #pragma mark General
166
167 - (void)startElement:(NSString *)tagName className:(NSString *)className;
168 {
169 [self startElement:tagName idName:nil className:className];
170 }
171
172 - (void)startElement:(NSString *)tagName idName:(NSString *)idName className:(NSString *)className;
173 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
174 if (idName) [self pushAttribute:@"id" value:idName];
175 if (className) [self pushAttribute:@"class" value:className];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
176
177 [self startElement:tagName];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
178 }
179
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
180 - (BOOL)isIDValid:(NSString *)anID; // NO if the ID has already been used
181 {
182 BOOL result = ![_IDs containsObject:anID];
183 return result;
184 }
185
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
186 #pragma mark Line Break
187
188 - (void)writeLineBreak;
189 {
190 [self startElement:@"br"];
191 [self endElement];
192 }
193
194 #pragma mark Higher-level Tag Writing
195
196 - (void)startAnchorElementWithHref:(NSString *)href title:(NSString *)titleString target:(NSString *)targetString rel:(NSString *)relString;
197 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
198 if (href) [self pushAttribute:@"href" value:href];
199 if (targetString) [self pushAttribute:@"target" value:targetString];
200 if (titleString) [self pushAttribute:@"title" value:titleString];
201 if (relString) [self pushAttribute:@"rel" value:relString];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
202
203 [self startElement:@"a"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
204 }
205
ac684bc @mikeabdullah Don't need to pass id and class directly when writing an image.
mikeabdullah authored
206 - (void)writeImageWithSrc:(NSString *)src
207 alt:(NSString *)alt
30bbf6a Allow values for attributes to be type id, not
Dan Wood authored
208 width:(id)width
209 height:(id)height;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
210 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
211 [self pushAttribute:@"src" value:src];
212 [self pushAttribute:@"alt" value:alt];
213 if (width) [self pushAttribute:@"width" value:width];
214 if (height) [self pushAttribute:@"height" value:height];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
215
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
216 [self startElement:@"img"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
217 [self endElement];
218 }
219
220 #pragma mark Link
221
222 - (void)writeLinkWithHref:(NSString *)href
223 type:(NSString *)type
224 rel:(NSString *)rel
225 title:(NSString *)title
226 media:(NSString *)media;
227 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
228 if (rel) [self pushAttribute:@"rel" value:rel];
15c3119 @mikeabdullah Merge (I think).
mikeabdullah authored
229 if (!type) type = @"text/css"; [self pushAttribute:@"type" value:type];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
230 [self pushAttribute:@"href" value:href];
231 if (title) [self pushAttribute:@"title" value:title];
232 if (media) [self pushAttribute:@"media" value:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
233
faa3c34 Whoops, the attribute and the value were backwards.
Dan Wood authored
234 [self pushAttribute:@"charset" value:@"UTF-8"];
3e48d47 Explicitly write out links (link and style tags) with a charset, so t…
Dan Wood authored
235
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
236 [self startElement:@"link"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
237 [self endElement];
238 }
239
240 - (void)writeLinkToStylesheet:(NSString *)href
241 title:(NSString *)title
242 media:(NSString *)media;
243 {
42af387 @mikeabdullah Writing a <LINK> with nil type will automatically fill in default typ…
mikeabdullah authored
244 [self writeLinkWithHref:href type:nil rel:@"stylesheet" title:title media:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
245 }
246
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
247 #pragma mark Scripts
248
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
249 - (void)writeJavascriptWithSrc:(NSString *)src charset:(NSString *)charset;
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
250 {
27cf398 @mikeabdullah Assertion.
mikeabdullah authored
251 NSParameterAssert(src);
252
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
253 if (charset) [self pushAttribute:@"charset" value:charset];
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
254 [self startJavascriptElementWithSrc:src];
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
255 [self endElement];
256 }
de567f7 added method to output JavaScript (via Dan)
Terrence Talbot authored
257
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
258 - (void)writeJavascript:(NSString *)script useCDATA:(BOOL)useCDATA;
259 {
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
260 [self startJavascriptElementWithSrc:nil];
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
261
262 if (useCDATA) [self startJavascriptCDATA];
263 [self writeString:script];
264 if (useCDATA) [self endJavascriptCDATA];
265
266 [self endElement];
267 }
268
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
269 - (void)startJavascriptElementWithSrc:(NSString *)src; // src may be nil
270 {
4af9cd9 @mikeabdullah HTML5 doesn't bother specifying the type of <SCRIPT>s
mikeabdullah authored
271 // HTML5 doesn't need the script type specified, but older doc types do for standards-compliance
272 if (![[self docType] isEqualToString:KSHTMLWriterDocTypeHTML_5])
273 {
274 [self pushAttribute:@"type" value:@"text/javascript"];
275 }
276
277 // Script
3530727 Don't specify charset for inline javascript, only
Dan Wood authored
278 if (src)
279 {
280 [self pushAttribute:@"src" value:src];
281 }
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
282
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
283 [self startElement:@"script"];
8a9783e @mikeabdullah Embedded scripts should start on a newline.
mikeabdullah authored
284
285 // 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
286 if (!src)
287 {
288 [self writeString:@"\n"];
289 [self stopWritingInline];
290 }
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
291 }
292
f241869 @mikeabdullah Split out -startJavascriptCDATA and -endJavascriptCDATA.
mikeabdullah authored
293 - (void)startJavascriptCDATA;
294 {
295 [self writeString:@"\n/* "];
296 [self startCDATA];
297 [self writeString:@" */"];
298 }
299
300 - (void)endJavascriptCDATA;
301 {
302 [self writeString:@"\n/* "];
303 [self endCDATA];
304 [self writeString:@" */\n"];
305 }
306
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
307 #pragma mark Param
308
309 - (void)writeParamElementWithName:(NSString *)name value:(NSString *)value;
310 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
311 if (name) [self pushAttribute:@"name" value:name];
312 if (value) [self pushAttribute:@"value" value:value];
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
313 [self startElement:@"param"];
314 [self endElement];
315 }
316
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
317 #pragma mark Style
318
6dd07ee @mikeabdullah Convenience method for writing inline CSS.
mikeabdullah authored
319 - (void)writeStyleElementWithCSSString:(NSString *)css;
320 {
321 [self startStyleElementWithType:@"text/css"];
322 [self writeString:css]; // browsers don't expect styles to be XML escaped
323 [self endElement];
324 }
325
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
326 - (void)startStyleElementWithType:(NSString *)type;
327 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
328 if (type) [self pushAttribute:@"type" value:type];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
329 [self startElement:@"style"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
330 }
331
332 #pragma mark Elements Stack
333
334 - (BOOL)topElementIsList;
335 {
336 NSString *tagName = [self topElement];
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
337 BOOL result = ([tagName isEqualToString:@"ul"] ||
338 [tagName isEqualToString:@"ol"]);
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
339 return result;
340 }
341
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
342 #pragma mark (X)HTML
343
344 - (BOOL)elementCanBeEmpty:(NSString *)tagName;
345 {
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
346 if ([tagName isEqualToString:@"br"] ||
347 [tagName isEqualToString:@"img"] ||
348 [tagName isEqualToString:@"hr"] ||
349 [tagName isEqualToString:@"meta"] ||
350 [tagName isEqualToString:@"link"] ||
351 [tagName isEqualToString:@"input"] ||
352 [tagName isEqualToString:@"base"] ||
353 [tagName isEqualToString:@"basefont"] ||
354 [tagName isEqualToString:@"param"] ||
355 [tagName isEqualToString:@"area"] ||
356 [tagName isEqualToString:@"source"]) return YES;
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
357
358 return NO;
359 }
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
360
361 - (BOOL)canWriteElementInline:(NSString *)tagName;
362 {
363 switch ([tagName length])
364 {
365 case 1:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
366 if ([tagName isEqualToString:@"a"] ||
367 [tagName isEqualToString:@"b"] ||
368 [tagName isEqualToString:@"i"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
369 break;
370
371 case 2:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
372 if ([tagName isEqualToString:@"br"] ||
373 [tagName isEqualToString:@"em"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
374 break;
375
376 case 3:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
377 if ([tagName isEqualToString:@"img"] ||
378 [tagName isEqualToString:@"sup"] ||
379 [tagName isEqualToString:@"sub"] ||
380 [tagName isEqualToString:@"big"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
381 break;
382
383 case 4:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
384 if ([tagName isEqualToString:@"span"] ||
385 [tagName isEqualToString:@"font"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
386 break;
387
388 case 5:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
389 if ([tagName isEqualToString:@"small"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
390 break;
391
392 case 6:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
393 if ([tagName isEqualToString:@"strong"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
394 break;
395 }
396
397 return [super canWriteElementInline:tagName];
398 }
399
400 #pragma mark Element Primitives
401
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
402 - (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
403 {
566d24c @mikeabdullah non-lowercase tags shouldn't be used for HTML.
mikeabdullah authored
404 NSAssert1([elementName isEqualToString:[elementName lowercaseString]], @"Attempt to start non-lowercase element: %@", elementName);
405
406
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
407 // Add in any pre-written classes
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
408 NSString *class = [self currentElementClassName];
ecfabe0 @mikeabdullah Expose -className.
mikeabdullah authored
409 if (class)
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
410 {
411 [_classNames removeAllObjects];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
412 [super pushAttribute:@"class" value:class];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
413 }
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
414
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
415 [super startElement:elementName writeInline:writeInline];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
416 }
417
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
418 - (void)closeEmptyElementTag; // /> OR > depending on -isXHTML
419 {
420 if ([self isXHTML])
421 {
422 [super closeEmptyElementTag];
423 }
424 else
425 {
426 [self writeString:@">"];
427 }
428 }
429
430 @end
Something went wrong with that request. Please try again.