Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 518 lines (418 sloc) 15.656 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
06fa83e @mikeabdullah On reflection, it makes more sense to represent only the attributes o…
mikeabdullah authored
30 #import "KSXMLAttributes.h"
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
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 {
98928dc @mikeabdullah Log if trying to add a CSS class twice to an element during debug bui…
mikeabdullah authored
122 #ifdef DEBUG
123 if ([_classNames containsObject:className])
124 {
125 NSLog(@"Adding class \"%@\" to an element twice", className);
126 }
127 #endif
128
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
129 [_classNames addObject:className];
130 }
131
30bbf6a Allow values for attributes to be type id, not
Dan Wood authored
132 - (void)pushAttribute:(NSString *)attribute value:(id)value;
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
133 {
134 if ([attribute isEqualToString:@"class"])
135 {
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
136 return [self pushClassName:value];
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
137 }
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
138
139 // Keep track of IDs in use
140 if ([attribute isEqualToString:@"id"]) [_IDs addObject:value];
141 [super pushAttribute:attribute value:value];
e96c6c8 @mikeabdullah Calling [htmlWriter pushElementAttribute:@"class" value:@"foo"] autom…
mikeabdullah authored
142 }
143
06fa83e @mikeabdullah On reflection, it makes more sense to represent only the attributes o…
mikeabdullah authored
144 - (KSXMLAttributes *)currentAttributes;
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
145 {
06fa83e @mikeabdullah On reflection, it makes more sense to represent only the attributes o…
mikeabdullah authored
146 KSXMLAttributes *result = [super currentAttributes];
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
147
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
148 // Add in buffered class info
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
149 NSString *class = [self currentElementClassName];
3aeccf6 @mikeabdullah KSXMLWriter uses a KSElementInfo object internally.
mikeabdullah authored
150 if (class) [result addAttribute:@"class" value:class];
589179c @mikeabdullah Include -className in -elementAttributes.
mikeabdullah authored
151
152 return result;
153 }
154
0e37440 @mikeabdullah Merge branch 'release-1.2'
mikeabdullah authored
155 - (BOOL)hasCurrentAttributes;
d7376e3 @mikeabdullah Sister method: -[KSXMLWriter currentElementHasAttributes]
mikeabdullah authored
156 {
0e37440 @mikeabdullah Merge branch 'release-1.2'
mikeabdullah authored
157 return ([super hasCurrentAttributes] || [_classNames count]);
d7376e3 @mikeabdullah Sister method: -[KSXMLWriter currentElementHasAttributes]
mikeabdullah authored
158 }
159
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
160 #pragma mark HTML Fragments
161
162 - (void)writeHTMLString:(NSString *)html;
163 {
164 [self writeString:html];
165 }
166
167 - (void)writeHTMLFormat:(NSString *)format , ...
168 {
169 va_list argList;
170 va_start(argList, format);
171 NSString *aString = [[[NSString alloc] initWithFormat:format arguments:argList] autorelease];
172 va_end(argList);
173
174 [self writeHTMLString:aString];
175 }
176
177 #pragma mark General
178
179 - (void)startElement:(NSString *)tagName className:(NSString *)className;
180 {
181 [self startElement:tagName idName:nil className:className];
182 }
183
184 - (void)startElement:(NSString *)tagName idName:(NSString *)idName className:(NSString *)className;
185 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
186 if (idName) [self pushAttribute:@"id" value:idName];
187 if (className) [self pushAttribute:@"class" value:className];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
188
189 [self startElement:tagName];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
190 }
191
fba7c04 HTML Writer tracks all element IDs written, so can tell you if an ID …
Mike authored
192 - (BOOL)isIDValid:(NSString *)anID; // NO if the ID has already been used
193 {
194 BOOL result = ![_IDs containsObject:anID];
195 return result;
196 }
197
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
198 #pragma mark Line Break
199
200 - (void)writeLineBreak;
201 {
202 [self startElement:@"br"];
203 [self endElement];
204 }
205
206 #pragma mark Higher-level Tag Writing
207
208 - (void)startAnchorElementWithHref:(NSString *)href title:(NSString *)titleString target:(NSString *)targetString rel:(NSString *)relString;
209 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
210 if (href) [self pushAttribute:@"href" value:href];
211 if (targetString) [self pushAttribute:@"target" value:targetString];
212 if (titleString) [self pushAttribute:@"title" value:titleString];
213 if (relString) [self pushAttribute:@"rel" value:relString];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
214
215 [self startElement:@"a"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
216 }
217
ac684bc @mikeabdullah Don't need to pass id and class directly when writing an image.
mikeabdullah authored
218 - (void)writeImageWithSrc:(NSString *)src
219 alt:(NSString *)alt
30bbf6a Allow values for attributes to be type id, not
Dan Wood authored
220 width:(id)width
221 height:(id)height;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
222 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
223 [self pushAttribute:@"src" value:src];
224 [self pushAttribute:@"alt" value:alt];
225 if (width) [self pushAttribute:@"width" value:width];
226 if (height) [self pushAttribute:@"height" value:height];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
227
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
228 [self startElement:@"img"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
229 [self endElement];
230 }
231
232 #pragma mark Link
233
234 - (void)writeLinkWithHref:(NSString *)href
235 type:(NSString *)type
236 rel:(NSString *)rel
237 title:(NSString *)title
238 media:(NSString *)media;
239 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
240 if (rel) [self pushAttribute:@"rel" value:rel];
15c3119 @mikeabdullah Merge (I think).
mikeabdullah authored
241 if (!type) type = @"text/css"; [self pushAttribute:@"type" value:type];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
242 [self pushAttribute:@"href" value:href];
243 if (title) [self pushAttribute:@"title" value:title];
244 if (media) [self pushAttribute:@"media" value:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
245
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
246 [self startElement:@"link"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
247 [self endElement];
248 }
249
250 - (void)writeLinkToStylesheet:(NSString *)href
251 title:(NSString *)title
252 media:(NSString *)media;
253 {
42af387 @mikeabdullah Writing a <LINK> with nil type will automatically fill in default typ…
mikeabdullah authored
254 [self writeLinkWithHref:href type:nil rel:@"stylesheet" title:title media:media];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
255 }
256
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
257 #pragma mark Scripts
258
3a9930c @mikeabdullah -writeJavascriptWithSrc:encoding: method that tries to do the right w…
mikeabdullah authored
259 - (void)writeJavascriptWithSrc:(NSString *)src encoding:(NSStringEncoding)encoding;
260 {
261 // According to the HTML spec, charset only needs to be specified if the script is a different encoding to the document
262 NSString *charset = nil;
263 if (encoding != [self encoding])
264 {
265 charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(encoding));
266 }
267
268 [self writeJavascriptWithSrc:src charset:charset];
269 }
270
b695665 Src may be nil; it's handled in support classes
Dan Wood authored
271 - (void)writeJavascriptWithSrc:(NSString *)src charset:(NSString *)charset; // src may be nil
272 {
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
273 if (charset) [self pushAttribute:@"charset" value:charset];
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
274 [self startJavascriptElementWithSrc:src];
60c7e89 @mikeabdullah Simpler method for pulling in an external script.
mikeabdullah authored
275 [self endElement];
276 }
de567f7 added method to output JavaScript (via Dan)
Terrence Talbot authored
277
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
278 - (void)writeJavascript:(NSString *)script useCDATA:(BOOL)useCDATA;
279 {
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
280 [self startJavascriptElementWithSrc:nil];
4d9ca24 @mikeabdullah -writeJavascript:useCDATA: convenience method.
mikeabdullah authored
281
282 if (useCDATA) [self startJavascriptCDATA];
283 [self writeString:script];
284 if (useCDATA) [self endJavascriptCDATA];
285
286 [self endElement];
287 }
288
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
289 - (void)startJavascriptElementWithSrc:(NSString *)src; // src may be nil
290 {
4af9cd9 @mikeabdullah HTML5 doesn't bother specifying the type of <SCRIPT>s
mikeabdullah authored
291 // HTML5 doesn't need the script type specified, but older doc types do for standards-compliance
292 if (![[self docType] isEqualToString:KSHTMLWriterDocTypeHTML_5])
293 {
294 [self pushAttribute:@"type" value:@"text/javascript"];
295 }
296
297 // Script
3530727 Don't specify charset for inline javascript, only
Dan Wood authored
298 if (src)
299 {
300 [self pushAttribute:@"src" value:src];
301 }
1d5ee5f @mikeabdullah If linking to a script, generally should specify charset
mikeabdullah authored
302
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
303 [self startElement:@"script"];
8a9783e @mikeabdullah Embedded scripts should start on a newline.
mikeabdullah authored
304
305 // 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
306 if (!src)
307 {
308 [self writeString:@"\n"];
309 [self stopWritingInline];
310 }
1ca811e @mikeabdullah Split out -startJavascriptElementWithSrc:
mikeabdullah authored
311 }
312
f241869 @mikeabdullah Split out -startJavascriptCDATA and -endJavascriptCDATA.
mikeabdullah authored
313 - (void)startJavascriptCDATA;
314 {
315 [self writeString:@"\n/* "];
316 [self startCDATA];
317 [self writeString:@" */"];
318 }
319
320 - (void)endJavascriptCDATA;
321 {
322 [self writeString:@"\n/* "];
323 [self endCDATA];
324 [self writeString:@" */\n"];
325 }
326
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
327 #pragma mark Param
328
329 - (void)writeParamElementWithName:(NSString *)name value:(NSString *)value;
330 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
331 if (name) [self pushAttribute:@"name" value:name];
332 if (value) [self pushAttribute:@"value" value:value];
0bc7e19 Add new convenience method for writing <param name="foo" value="bar" />
Dan Wood authored
333 [self startElement:@"param"];
334 [self endElement];
335 }
336
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
337 #pragma mark Style
338
6dd07ee @mikeabdullah Convenience method for writing inline CSS.
mikeabdullah authored
339 - (void)writeStyleElementWithCSSString:(NSString *)css;
340 {
341 [self startStyleElementWithType:@"text/css"];
342 [self writeString:css]; // browsers don't expect styles to be XML escaped
343 [self endElement];
344 }
345
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
346 - (void)startStyleElementWithType:(NSString *)type;
347 {
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
348 if (type) [self pushAttribute:@"type" value:type];
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
349 [self startElement:@"style"];
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
350 }
351
352 #pragma mark Elements Stack
353
1b3d197 @mikeabdullah KSHTMLWriter gains:
mikeabdullah authored
354 - (BOOL)hasListOpen;
355 {
356 return ([self hasOpenElement:@"ul"] || [self hasOpenElement:@"ol"]);
357 }
358
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
359 - (BOOL)topElementIsList;
360 {
1b3d197 @mikeabdullah KSHTMLWriter gains:
mikeabdullah authored
361 return [[self class] elementIsList:[self topElement]];
362 }
363
364 + (BOOL)elementIsList:(NSString *)element;
365 {
366 BOOL result = ([element isEqualToString:@"ul"] ||
367 [element isEqualToString:@"ol"]);
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
368 return result;
369 }
370
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
371 #pragma mark (X)HTML
372
373 - (BOOL)elementCanBeEmpty:(NSString *)tagName;
374 {
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
375 if ([tagName isEqualToString:@"br"] ||
376 [tagName isEqualToString:@"img"] ||
377 [tagName isEqualToString:@"hr"] ||
378 [tagName isEqualToString:@"meta"] ||
379 [tagName isEqualToString:@"link"] ||
380 [tagName isEqualToString:@"input"] ||
381 [tagName isEqualToString:@"base"] ||
382 [tagName isEqualToString:@"basefont"] ||
383 [tagName isEqualToString:@"param"] ||
384 [tagName isEqualToString:@"area"] ||
385 [tagName isEqualToString:@"source"]) return YES;
c35c721 @mikeabdullah Only a handful of HTML elements are allowed to be empty.
mikeabdullah authored
386
387 return NO;
388 }
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
389
390 - (BOOL)canWriteElementInline:(NSString *)tagName;
391 {
392 switch ([tagName length])
393 {
394 case 1:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
395 if ([tagName isEqualToString:@"a"] ||
396 [tagName isEqualToString:@"b"] ||
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
397 [tagName isEqualToString:@"i"] ||
58a8fbc @mikeabdullah Somehow <U> got left off the list of inline elements.
mikeabdullah authored
398 [tagName isEqualToString:@"u"] ||
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
399 [tagName isEqualToString:@"q"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
400 break;
401
402 case 2:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
403 if ([tagName isEqualToString:@"br"] ||
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
404 [tagName isEqualToString:@"em"] ||
405 [tagName isEqualToString:@"tt"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
406 break;
407
408 case 3:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
409 if ([tagName isEqualToString:@"img"] ||
410 [tagName isEqualToString:@"sup"] ||
411 [tagName isEqualToString:@"sub"] ||
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
412 [tagName isEqualToString:@"big"] ||
413 [tagName isEqualToString:@"del"] ||
414 [tagName isEqualToString:@"ins"] ||
415 [tagName isEqualToString:@"dfn"] ||
416 [tagName isEqualToString:@"map"] ||
417 [tagName isEqualToString:@"var"] ||
418 [tagName isEqualToString:@"bdo"] ||
419 [tagName isEqualToString:@"kbd"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
420 break;
421
422 case 4:
f23f8c7 @mikeabdullah XML Writing should be case-sensitive.
mikeabdullah authored
423 if ([tagName isEqualToString:@"span"] ||
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
424 [tagName isEqualToString:@"font"] ||
425 [tagName isEqualToString:@"abbr"] ||
426 [tagName isEqualToString:@"cite"] ||
427 [tagName isEqualToString:@"code"] ||
428 [tagName isEqualToString:@"samp"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
429 break;
430
431 case 5:
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
432 if ([tagName isEqualToString:@"small"] ||
433 [tagName isEqualToString:@"input"] ||
434 [tagName isEqualToString:@"label"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
435 break;
436
437 case 6:
782c637 @mikeabdullah Expand the list of known inline elements
mikeabdullah authored
438 if ([tagName isEqualToString:@"strong"] ||
439 [tagName isEqualToString:@"select"] ||
440 [tagName isEqualToString:@"button"] ||
441 [tagName isEqualToString:@"object"] ||
442 [tagName isEqualToString:@"applet"] ||
443 [tagName isEqualToString:@"script"]) return YES;
444 break;
445
446 case 7:
447 if ([tagName isEqualToString:@"acronym"]) return YES;
448 break;
449
450 case 8:
451 if ([tagName isEqualToString:@"textarea"]) return YES;
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
452 break;
453 }
454
455 return [super canWriteElementInline:tagName];
456 }
457
05b69fd @mikeabdullah HTML Writer reports that lists may only contain list items.
mikeabdullah authored
458 - (BOOL)validateElement:(NSString *)element;
459 {
460 if (![super validateElement:element]) return NO;
461
462 // Lists can only contain list items
463 if ([self topElementIsList])
464 {
465 return [element isEqualToString:@"li"];
466 }
467 else
468 {
469 return YES;
470 }
471 }
472
a85616d @mikeabdullah Only ordered list items can have a value.
mikeabdullah authored
473 - (NSString *)validateAttribute:(NSString *)name value:(NSString *)value ofElement:(NSString *)element;
474 {
475 NSString *result = [super validateAttribute:name value:value ofElement:element];
476 if (!result) return nil;
477
478 // value is only allowed as a list item attribute when in an ordered list
479 if ([element isEqualToString:@"li"] && [name isEqualToString:@"value"])
480 {
481 if (![[self topElement] isEqualToString:@"ol"]) result = nil;
482 }
483
484 return result;
485 }
486
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
487 #pragma mark Element Primitives
488
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
489 - (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
490 {
566d24c @mikeabdullah non-lowercase tags shouldn't be used for HTML.
mikeabdullah authored
491 NSAssert1([elementName isEqualToString:[elementName lowercaseString]], @"Attempt to start non-lowercase element: %@", elementName);
492
493
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
494 // Add in any pre-written classes
5df9cd7 @mikeabdullah Don't bother exposing -[KSHTMLWriter elementClassName]. Can use [[[wr…
mikeabdullah authored
495 NSString *class = [self currentElementClassName];
ecfabe0 @mikeabdullah Expose -className.
mikeabdullah authored
496 if (class)
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
497 {
498 [_classNames removeAllObjects];
b12f4c9 Sorry to keep changing this, but -pushElementAttribute: really is too…
Mike authored
499 [super pushAttribute:@"class" value:class];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
500 }
9beb73b @mikeabdullah Take a bunch of primitive element APIs private. Just need to use -add…
mikeabdullah authored
501
c8bc317 @mikeabdullah Can now ditch -openTag:writeInline:
mikeabdullah authored
502 [super startElement:elementName writeInline:writeInline];
75c4ca6 @mikeabdullah Experimental support for building up CSS class names before writing a…
mikeabdullah authored
503 }
504
994c91e @mikeabdullah And the real star of our show arrives: KSHTMLWriter!
mikeabdullah authored
505 - (void)closeEmptyElementTag; // /> OR > depending on -isXHTML
506 {
507 if ([self isXHTML])
508 {
509 [super closeEmptyElementTag];
510 }
511 else
512 {
513 [self writeString:@">"];
514 }
515 }
516
517 @end
Something went wrong with that request. Please try again.