]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
Finish conversion to new OO HTML generation scheme.
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.6 2002-01-22 03:17:47 dairiki Exp $');
2 /*
3  * Code for writing XML.
4  */
5 require_once("lib/XmlElement.php");
6 /**
7  * An XML element.
8  */
9
10 class HtmlElement extends XmlElement
11 {
12     function HtmlElement ($tagname /* , $attr_or_content , ...*/) {
13         $this->_tag = $tagname;
14         $this->_content = array();
15         $this->_properties = HTML::getTagProperties($tagname);
16         
17         if (func_num_args() > 1)
18             $this->_init(array_slice(func_get_args(), 1));
19         else 
20             $this->_attr = array();
21     }
22
23     function _emptyTag () {
24         return substr($this->_startTag(), 0, -1) . " />";
25     }
26
27     function isEmpty () {
28         return ($this->_properties & HTMLTAG_EMPTY) != 0;
29     }
30
31     function hasInlineContent () {
32         return ($this->_properties & HTMLTAG_ACCEPTS_INLINE) != 0;
33     }
34
35     function isInlineElement () {
36         return ($this->_properties & HTMLTAG_INLINE) != 0;
37     }
38 };
39
40 function HTML ($tag /* , ... */) {
41     $el = new HtmlElement($tag);
42     if (func_num_args() > 1)
43         $el->_init(array_slice(func_get_args(), 1));
44     return $el;
45 }
46
47 define('NBSP', "\xA0");         // iso-8859-x non-breaking space.
48
49 class HTML {
50     function raw ($html_text) {
51         return new RawXML($html_text);
52     }
53
54
55     function link (/*...*/) {
56         $el = new HtmlElement('link');
57         $el->_init(func_get_args());
58         return $el;
59     }
60
61     function style (/*...*/) {
62         $el = new HtmlElement('style');
63         $el->_init(func_get_args());
64         return $el;
65     }
66
67     function script (/*...*/) {
68         $el = new HtmlElement('script');
69         $el->_init(func_get_args());
70         return $el;
71     }
72
73     function noscript (/*...*/) {
74         $el = new HtmlElement('noscript');
75         $el->_init(func_get_args());
76         return $el;
77     }
78
79     
80     function a (/*...*/) {
81         $el = new HtmlElement('a');
82         $el->_init(func_get_args());
83         return $el;
84     }
85
86     function img (/*...*/) {
87         $el = new HtmlElement('img');
88         $el->_init(func_get_args());
89         return $el;
90     }
91
92     function br (/*...*/) {
93         $el = new HtmlElement('br');
94         $el->_init(func_get_args());
95         return $el;
96     }
97
98     function h1 (/*...*/) {
99         $el = new HtmlElement('h1');
100         $el->_init(func_get_args());
101         return $el;
102     }
103
104     function h1 (/*...*/) {
105         $el = new HtmlElement('h1');
106         $el->_init(func_get_args());
107         return $el;
108     }
109
110     function h2 (/*...*/) {
111         $el = new HtmlElement('h2');
112         $el->_init(func_get_args());
113         return $el;
114     }
115
116     function h3 (/*...*/) {
117         $el = new HtmlElement('h3');
118         $el->_init(func_get_args());
119         return $el;
120     }
121
122     function h4 (/*...*/) {
123         $el = new HtmlElement('h4');
124         $el->_init(func_get_args());
125         return $el;
126     }
127
128     function h5 (/*...*/) {
129         $el = new HtmlElement('h5');
130         $el->_init(func_get_args());
131         return $el;
132     }
133
134     function h6 (/*...*/) {
135         $el = new HtmlElement('h6');
136         $el->_init(func_get_args());
137         return $el;
138     }
139
140     function div (/*...*/) {
141         $el = new HtmlElement('div');
142         $el->_init(func_get_args());
143         return $el;
144     }
145
146     function p (/*...*/) {
147         $el = new HtmlElement('p');
148         $el->_init(func_get_args());
149         return $el;
150     }
151
152     function blockquote (/*...*/) {
153         $el = new HtmlElement('blockquote');
154         $el->_init(func_get_args());
155         return $el;
156     }
157
158     function span (/*...*/) {
159         $el = new HtmlElement('span');
160         $el->_init(func_get_args());
161         return $el;
162     }
163
164     function em (/*...*/) {
165         $el = new HtmlElement('em');
166         $el->_init(func_get_args());
167         return $el;
168     }
169
170     function strong (/*...*/) {
171         $el = new HtmlElement('strong');
172         $el->_init(func_get_args());
173         return $el;
174     }
175     
176     function small (/*...*/) {
177         $el = new HtmlElement('small');
178         $el->_init(func_get_args());
179         return $el;
180     }
181     
182     function tt (/*...*/) {
183         $el = new HtmlElement('tt');
184         $el->_init(func_get_args());
185         return $el;
186     }
187
188     function u (/*...*/) {
189         $el = new HtmlElement('u');
190         $el->_init(func_get_args());
191         return $el;
192     }
193
194     function sup (/*...*/) {
195         $el = new HtmlElement('sup');
196         $el->_init(func_get_args());
197         return $el;
198     }
199
200     function sub (/*...*/) {
201         $el = new HtmlElement('sub');
202         $el->_init(func_get_args());
203         return $el;
204     }
205
206     function ul (/*...*/) {
207         $el = new HtmlElement('ul');
208         $el->_init(func_get_args());
209         return $el;
210     }
211
212     function ol (/*...*/) {
213         $el = new HtmlElement('ol');
214         $el->_init(func_get_args());
215         return $el;
216     }
217
218     function dl (/*...*/) {
219         $el = new HtmlElement('dl');
220         $el->_init(func_get_args());
221         return $el;
222     }
223
224     function li (/*...*/) {
225         $el = new HtmlElement('li');
226         $el->_init(func_get_args());
227         return $el;
228     }
229
230     function dt (/*...*/) {
231         $el = new HtmlElement('dt');
232         $el->_init(func_get_args());
233         return $el;
234     }
235
236     function dd (/*...*/) {
237         $el = new HtmlElement('dd');
238         $el->_init(func_get_args());
239         return $el;
240     }
241
242     function table (/*...*/) {
243         $el = new HtmlElement('table');
244         $el->_init(func_get_args());
245         return $el;
246     }
247
248     function caption (/*...*/) {
249         $el = new HtmlElement('caption');
250         $el->_init(func_get_args());
251         return $el;
252     }
253
254     function thead (/*...*/) {
255         $el = new HtmlElement('thead');
256         $el->_init(func_get_args());
257         return $el;
258     }
259
260     function tbody (/*...*/) {
261         $el = new HtmlElement('tbody');
262         $el->_init(func_get_args());
263         return $el;
264     }
265
266     function tfoot (/*...*/) {
267         $el = new HtmlElement('tfoot');
268         $el->_init(func_get_args());
269         return $el;
270     }
271
272     function tr (/*...*/) {
273         $el = new HtmlElement('tr');
274         $el->_init(func_get_args());
275         return $el;
276     }
277
278     function td (/*...*/) {
279         $el = new HtmlElement('td');
280         $el->_init(func_get_args());
281         return $el;
282     }
283
284     function th (/*...*/) {
285         $el = new HtmlElement('th');
286         $el->_init(func_get_args());
287         return $el;
288     }
289
290     function form (/*...*/) {
291         $el = new HtmlElement('form');
292         $el->_init(func_get_args());
293         return $el;
294     }
295
296     function input (/*...*/) {
297         $el = new HtmlElement('input');
298         $el->_init(func_get_args());
299         return $el;
300     }
301
302     function getTagProperties($tag) {
303         $props = &$GLOBALS['HTML_TagProperties'];
304         return isset($props[$tag]) ? $props[$tag] : 0;
305     }
306     
307     function _setTagProperty($prop_flag, $tags) {
308         $props = &$GLOBALS['HTML_TagProperties'];
309         if (is_string($tags))
310             $tags = preg_split('/\s+/', $tags);
311         foreach ($tags as $tag) {
312             if (isset($props[$tag]))
313                 $props[$tag] |= $prop_flag;
314             else
315                 $props[$tag] = $prop_flag;
316         }
317     }
318 }
319
320 define('HTMLTAG_EMPTY', 1);
321 define('HTMLTAG_INLINE', 2);
322 define('HTMLTAG_ACCEPTS_INLINE', 4);
323
324
325 HTML::_setTagProperty(HTMLTAG_EMPTY,
326                       'area base basefont br col frame hr img input isindex link meta param');
327 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
328                       // %inline elements:
329                       'b big i small tt ' // %fontstyle
330                       . 's strike u ' // (deprecated)
331                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
332                       . 'a img object br script map q sub sup span bdo '//%special
333                       . 'button input label select textarea ' //%formctl
334
335                       // %block elements which contain inline content
336                       . 'address h1 h2 h3 h4 h5 h6 p pre '
337                       // %block elements which contain either block or inline content
338                       . 'div fieldset '
339
340                       // other with inline content
341                       . 'caption dt label legend '
342                       // other with either inline or block
343                       . 'dd del ins li td th ');
344
345 HTML::_setTagProperty(HTMLTAG_INLINE,
346                       // %inline elements:
347                       'b big i small tt ' // %fontstyle
348                       . 's strike u ' // (deprecated)
349                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
350                       . 'a img object br script map q sub sup span bdo '//%special
351                       . 'button input label select textarea ' //%formctl
352                       );
353
354       
355 // (c-file-style: "gnu")
356 // Local Variables:
357 // mode: php
358 // tab-width: 8
359 // c-basic-offset: 4
360 // c-hanging-comment-ender-p: nil
361 // indent-tabs-mode: nil
362 // End:   
363 ?>