]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
Replaced NBSP constant with a new nbsp() function. The return value of HTML::nbsp...
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.26 2002-10-29 01:12:23 carstenklapp 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->_init(func_get_args());
14     //    $this->_properties = HTML::getTagProperties($tagname);
15     //}
16
17
18     function _init ($args) {
19         XmlElement::_init($args);
20         $this->_properties = HTML::getTagProperties($this->_tag);
21     }
22
23     /**
24      * @access protected
25      * This is used by the static factory methods is class HTML.
26      */
27     function _init2 ($args) {
28         if ($args) {
29             if (is_array($args[0]))
30                 $this->_attr = array_shift($args);
31             elseif ($args[0] === false)
32                 array_shift($args);
33         }
34         
35         if (count($args) == 1 && is_array($args[0]))
36             $args = $args[0];
37         $this->_content = $args;
38         return $this;
39     }
40
41     /** Add a "tooltip" to an element.
42      *
43      * @param $tooltip_text string The tooltip text.
44      */
45     function addTooltip ($tooltip_text) {
46         $this->setAttr('title', $tooltip_text);
47
48         // FIXME: this should be initialized from title by an onLoad() function.
49         //        (though, that may not be possible.)
50         $qtooltip = str_replace("'", "\\'", $tooltip_text);
51         $this->setAttr('onmouseover',
52                        sprintf('window.status="%s"; return true;',
53                                addslashes($tooltip_text)));
54         $this->setAttr('onmouseout', "window.status='';return true;");
55     }
56
57     function emptyTag () {
58         if (($this->_properties & HTMLTAG_EMPTY) == 0)
59             return $this->startTag() . "</$this->_tag>";
60
61         return substr($this->startTag(), 0, -1) . " />";
62     }
63
64     function hasInlineContent () {
65         return ($this->_properties & HTMLTAG_ACCEPTS_INLINE) != 0;
66     }
67
68     function isInlineElement () {
69         return ($this->_properties & HTMLTAG_INLINE) != 0;
70     }
71 };
72
73 function HTML (/* $content, ... */) {
74     return new XmlContent(func_get_args());
75 }
76
77 class HTML extends HtmlElement {
78     function raw ($html_text) {
79         return new RawXML($html_text);
80     }
81     
82     function nbsp() {
83         if (CHARSET == 'utf-8')
84             return new RawXML("\xC2\xA0");         // utf-8 non-breaking space.
85         elseif (CHARSET == 'iso-8859-1')
86             return new RawXML("\xA0");        // iso-8859-x non-breaking space.
87         else
88             return new RawXML("&nbsp;");     // html-entity non-breaking space.
89     }
90
91     function getTagProperties($tag) {
92         $props = &$GLOBALS['HTML_TagProperties'];
93         return isset($props[$tag]) ? $props[$tag] : 0;
94     }
95
96     function _setTagProperty($prop_flag, $tags) {
97         $props = &$GLOBALS['HTML_TagProperties'];
98         if (is_string($tags))
99             $tags = preg_split('/\s+/', $tags);
100         foreach ($tags as $tag) {
101             if (isset($props[$tag]))
102                 $props[$tag] |= $prop_flag;
103             else
104                 $props[$tag] = $prop_flag;
105         }
106     }
107
108     //
109     // Shell script to generate the following static methods:
110     //
111     // #!/bin/sh
112     // function mkfuncs () {
113     //     for tag in "$@"
114     //     do
115     //         echo "    function $tag (/*...*/) {"
116     //         echo "        \$el = new HtmlElement('$tag');"
117     //         echo "        return \$el->_init2(func_get_args());"
118     //         echo "    }"
119     //     done
120     // }
121     // d='
122     //     /****************************************/'
123     // mkfuncs link style script noscript
124     // echo "$d"
125     // mkfuncs a img br span
126     // echo "$d"
127     // mkfuncs h1 h2 h3 h4 h5 h6
128     // echo "$d"
129     // mkfuncs hr div p pre blockquote
130     // echo "$d"
131     // mkfuncs em strong small
132     // echo "$d"
133     // mkfuncs tt u sup sub
134     // echo "$d"
135     // mkfuncs ul ol dl li dt dd
136     // echo "$d"
137     // mkfuncs table caption thead tbody tfoot tr td th
138     // echo "$d"
139     // mkfuncs form input option select textarea
140     // echo "$d"
141     // mkfuncs area map frame frameset iframe nobody
142
143     function link (/*...*/) {
144         $el = new HtmlElement('link');
145         return $el->_init2(func_get_args());
146     }
147     function style (/*...*/) {
148         $el = new HtmlElement('style');
149         return $el->_init2(func_get_args());
150     }
151     function script (/*...*/) {
152         $el = new HtmlElement('script');
153         return $el->_init2(func_get_args());
154     }
155     function noscript (/*...*/) {
156         $el = new HtmlElement('noscript');
157         return $el->_init2(func_get_args());
158     }
159
160     /****************************************/
161     function a (/*...*/) {
162         $el = new HtmlElement('a');
163         return $el->_init2(func_get_args());
164     }
165     function img (/*...*/) {
166         $el = new HtmlElement('img');
167         return $el->_init2(func_get_args());
168     }
169     function br (/*...*/) {
170         $el = new HtmlElement('br');
171         return $el->_init2(func_get_args());
172     }
173     function span (/*...*/) {
174         $el = new HtmlElement('span');
175         return $el->_init2(func_get_args());
176     }
177
178     /****************************************/
179     function h1 (/*...*/) {
180         $el = new HtmlElement('h1');
181         return $el->_init2(func_get_args());
182     }
183     function h2 (/*...*/) {
184         $el = new HtmlElement('h2');
185         return $el->_init2(func_get_args());
186     }
187     function h3 (/*...*/) {
188         $el = new HtmlElement('h3');
189         return $el->_init2(func_get_args());
190     }
191     function h4 (/*...*/) {
192         $el = new HtmlElement('h4');
193         return $el->_init2(func_get_args());
194     }
195     function h5 (/*...*/) {
196         $el = new HtmlElement('h5');
197         return $el->_init2(func_get_args());
198     }
199     function h6 (/*...*/) {
200         $el = new HtmlElement('h6');
201         return $el->_init2(func_get_args());
202     }
203
204     /****************************************/
205     function hr (/*...*/) {
206         $el = new HtmlElement('hr');
207         return $el->_init2(func_get_args());
208     }
209     function div (/*...*/) {
210         $el = new HtmlElement('div');
211         return $el->_init2(func_get_args());
212     }
213     function p (/*...*/) {
214         $el = new HtmlElement('p');
215         return $el->_init2(func_get_args());
216     }
217     function pre (/*...*/) {
218         $el = new HtmlElement('pre');
219         return $el->_init2(func_get_args());
220     }
221     function blockquote (/*...*/) {
222         $el = new HtmlElement('blockquote');
223         return $el->_init2(func_get_args());
224     }
225
226     /****************************************/
227     function em (/*...*/) {
228         $el = new HtmlElement('em');
229         return $el->_init2(func_get_args());
230     }
231     function strong (/*...*/) {
232         $el = new HtmlElement('strong');
233         return $el->_init2(func_get_args());
234     }
235     function small (/*...*/) {
236         $el = new HtmlElement('small');
237         return $el->_init2(func_get_args());
238     }
239
240     /****************************************/
241     function tt (/*...*/) {
242         $el = new HtmlElement('tt');
243         return $el->_init2(func_get_args());
244     }
245     function u (/*...*/) {
246         $el = new HtmlElement('u');
247         return $el->_init2(func_get_args());
248     }
249     function sup (/*...*/) {
250         $el = new HtmlElement('sup');
251         return $el->_init2(func_get_args());
252     }
253     function sub (/*...*/) {
254         $el = new HtmlElement('sub');
255         return $el->_init2(func_get_args());
256     }
257
258     /****************************************/
259     function ul (/*...*/) {
260         $el = new HtmlElement('ul');
261         return $el->_init2(func_get_args());
262     }
263     function ol (/*...*/) {
264         $el = new HtmlElement('ol');
265         return $el->_init2(func_get_args());
266     }
267     function dl (/*...*/) {
268         $el = new HtmlElement('dl');
269         return $el->_init2(func_get_args());
270     }
271     function li (/*...*/) {
272         $el = new HtmlElement('li');
273         return $el->_init2(func_get_args());
274     }
275     function dt (/*...*/) {
276         $el = new HtmlElement('dt');
277         return $el->_init2(func_get_args());
278     }
279     function dd (/*...*/) {
280         $el = new HtmlElement('dd');
281         return $el->_init2(func_get_args());
282     }
283
284     /****************************************/
285     function table (/*...*/) {
286         $el = new HtmlElement('table');
287         return $el->_init2(func_get_args());
288     }
289     function caption (/*...*/) {
290         $el = new HtmlElement('caption');
291         return $el->_init2(func_get_args());
292     }
293     function thead (/*...*/) {
294         $el = new HtmlElement('thead');
295         return $el->_init2(func_get_args());
296     }
297     function tbody (/*...*/) {
298         $el = new HtmlElement('tbody');
299         return $el->_init2(func_get_args());
300     }
301     function tfoot (/*...*/) {
302         $el = new HtmlElement('tfoot');
303         return $el->_init2(func_get_args());
304     }
305     function tr (/*...*/) {
306         $el = new HtmlElement('tr');
307         return $el->_init2(func_get_args());
308     }
309     function td (/*...*/) {
310         $el = new HtmlElement('td');
311         return $el->_init2(func_get_args());
312     }
313     function th (/*...*/) {
314         $el = new HtmlElement('th');
315         return $el->_init2(func_get_args());
316     }
317
318     /****************************************/
319     function form (/*...*/) {
320         $el = new HtmlElement('form');
321         return $el->_init2(func_get_args());
322     }
323     function input (/*...*/) {
324         $el = new HtmlElement('input');
325         return $el->_init2(func_get_args());
326     }
327     function option (/*...*/) {
328         $el = new HtmlElement('option');
329         return $el->_init2(func_get_args());
330     }
331     function select (/*...*/) {
332         $el = new HtmlElement('select');
333         return $el->_init2(func_get_args());
334     }
335     function textarea (/*...*/) {
336         $el = new HtmlElement('textarea');
337         return $el->_init2(func_get_args());
338     }
339
340     /****************************************/
341     function area (/*...*/) {
342         $el = new HtmlElement('area');
343         return $el->_init2(func_get_args());
344     }
345     function map (/*...*/) {
346         $el = new HtmlElement('map');
347         return $el->_init2(func_get_args());
348     }
349     function frame (/*...*/) {
350         $el = new HtmlElement('frame');
351         return $el->_init2(func_get_args());
352     }
353     function frameset (/*...*/) {
354         $el = new HtmlElement('frameset');
355         return $el->_init2(func_get_args());
356     }
357     function iframe (/*...*/) {
358         $el = new HtmlElement('iframe');
359         return $el->_init2(func_get_args());
360     }
361     function nobody (/*...*/) {
362         $el = new HtmlElement('nobody');
363         return $el->_init2(func_get_args());
364     }
365 }
366
367 define('HTMLTAG_EMPTY', 1);
368 define('HTMLTAG_INLINE', 2);
369 define('HTMLTAG_ACCEPTS_INLINE', 4);
370
371
372 HTML::_setTagProperty(HTMLTAG_EMPTY,
373                       'area base basefont br col frame hr img input isindex link meta param');
374 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
375                       // %inline elements:
376                       'b big i small tt ' // %fontstyle
377                       . 's strike u ' // (deprecated)
378                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
379                       . 'a img object br script map q sub sup span bdo '//%special
380                       . 'button input label option select textarea ' //%formctl
381
382                       // %block elements which contain inline content
383                       . 'address h1 h2 h3 h4 h5 h6 p pre '
384                       // %block elements which contain either block or inline content
385                       . 'div fieldset frameset'
386
387                       // other with inline content
388                       . 'caption dt label legend '
389                       // other with either inline or block
390                       . 'dd del ins li td th ');
391
392 HTML::_setTagProperty(HTMLTAG_INLINE,
393                       // %inline elements:
394                       'b big i small tt ' // %fontstyle
395                       . 's strike u ' // (deprecated)
396                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
397                       . 'a img object br script map q sub sup span bdo '//%special
398                       . 'button input label option select textarea ' //%formctl
399                       . 'nobody iframe'
400                       );
401
402 /**
403  * Generate hidden form input fields.
404  *
405  * @param $query_args hash  A hash mapping names to values for the hidden inputs.
406  * Values in the hash can themselves be hashes.  The will result in hidden inputs
407  * which will reconstruct the nested structure in the resulting query args as
408  * processed by PHP.
409  *
410  * Example:
411  *
412  * $args = array('x' => '2',
413  *               'y' => array('a' => 'aval', 'b' => 'bval'));
414  * $inputs = HiddenInputs($args);
415  *
416  * Will result in:
417  *
418  *  <input type="hidden" name="x" value = "2" />
419  *  <input type="hidden" name="y[a]" value = "aval" />
420  *  <input type="hidden" name="y[b]" value = "bval" />
421  *
422  * @return object An XmlContent object containing the inputs.
423  */
424 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
425     $inputs = HTML();
426
427     foreach ($query_args as $key => $val) {
428         if (in_array($key,$exclude)) continue;
429         $name = $pfx ? $pfx . "[$key]" : $key;
430         if (is_array($val))
431             $inputs->pushContent(HiddenInputs($val, $name));
432         else
433             $inputs->pushContent(HTML::input(array('type' => 'hidden',
434                                                    'name' => $name,
435                                                    'value' => $val)));
436     }
437     return $inputs;
438 }
439
440 function HiddenGets ($exclude = array()) {
441     global $HTTP_GET_VARS;
442     HiddenInputs($HTTP_GET_VARS, false, $exclude);
443 }
444
445 function HiddenPosts ($exclude = array()) {
446     global $HTTP_POST_VARS;
447     HiddenInputs($HTTP_POST_VARS, false, $exclude);
448 }
449
450 // (c-file-style: "gnu")
451 // Local Variables:
452 // mode: php
453 // tab-width: 8
454 // c-basic-offset: 4
455 // c-hanging-comment-ender-p: nil
456 // indent-tabs-mode: nil
457 // End:
458 ?>