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