]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
PageList enhanced and improved.
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.32 2004-02-15 21:34:37 rurban 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 getTagProperties($tag) {
83         $props = &$GLOBALS['HTML_TagProperties'];
84         return isset($props[$tag]) ? $props[$tag] : 0;
85     }
86
87     function _setTagProperty($prop_flag, $tags) {
88         $props = &$GLOBALS['HTML_TagProperties'];
89         if (is_string($tags))
90             $tags = preg_split('/\s+/', $tags);
91         foreach ($tags as $tag) {
92             if (isset($props[$tag]))
93                 $props[$tag] |= $prop_flag;
94             else
95                 $props[$tag] = $prop_flag;
96         }
97     }
98
99     //
100     // Shell script to generate the following static methods:
101     //
102     // #!/bin/sh
103     // function mkfuncs () {
104     //     for tag in "$@"
105     //     do
106     //         echo "    function $tag (/*...*/) {"
107     //         echo "        \$el = new HtmlElement('$tag');"
108     //         echo "        return \$el->_init2(func_get_args());"
109     //         echo "    }"
110     //     done
111     // }
112     // d='
113     //     /****************************************/'
114     // mkfuncs link meta style script noscript
115     // echo "$d"
116     // mkfuncs a img br span
117     // echo "$d"
118     // mkfuncs h1 h2 h3 h4 h5 h6
119     // echo "$d"
120     // mkfuncs hr div p pre blockquote
121     // echo "$d"
122     // mkfuncs em strong small
123     // echo "$d"
124     // mkfuncs tt u sup sub
125     // echo "$d"
126     // mkfuncs ul ol dl li dt dd
127     // echo "$d"
128     // mkfuncs table caption thead tbody tfoot tr td th colgroup col
129     // echo "$d"
130     // mkfuncs form input option select textarea
131     // echo "$d"
132     // mkfuncs area map frame frameset iframe nobody
133
134     function link (/*...*/) {
135         $el = new HtmlElement('link');
136         return $el->_init2(func_get_args());
137     }
138     function meta (/*...*/) {
139         $el = new HtmlElement('meta');
140         return $el->_init2(func_get_args());
141     }
142     function style (/*...*/) {
143         $el = new HtmlElement('style');
144         return $el->_init2(func_get_args());
145     }
146     function script (/*...*/) {
147         $el = new HtmlElement('script');
148         return $el->_init2(func_get_args());
149     }
150     function noscript (/*...*/) {
151         $el = new HtmlElement('noscript');
152         return $el->_init2(func_get_args());
153     }
154
155     /****************************************/
156     function a (/*...*/) {
157         $el = new HtmlElement('a');
158         return $el->_init2(func_get_args());
159     }
160     function img (/*...*/) {
161         $el = new HtmlElement('img');
162         return $el->_init2(func_get_args());
163     }
164     function br (/*...*/) {
165         $el = new HtmlElement('br');
166         return $el->_init2(func_get_args());
167     }
168     function span (/*...*/) {
169         $el = new HtmlElement('span');
170         return $el->_init2(func_get_args());
171     }
172
173     /****************************************/
174     function h1 (/*...*/) {
175         $el = new HtmlElement('h1');
176         return $el->_init2(func_get_args());
177     }
178     function h2 (/*...*/) {
179         $el = new HtmlElement('h2');
180         return $el->_init2(func_get_args());
181     }
182     function h3 (/*...*/) {
183         $el = new HtmlElement('h3');
184         return $el->_init2(func_get_args());
185     }
186     function h4 (/*...*/) {
187         $el = new HtmlElement('h4');
188         return $el->_init2(func_get_args());
189     }
190     function h5 (/*...*/) {
191         $el = new HtmlElement('h5');
192         return $el->_init2(func_get_args());
193     }
194     function h6 (/*...*/) {
195         $el = new HtmlElement('h6');
196         return $el->_init2(func_get_args());
197     }
198
199     /****************************************/
200     function hr (/*...*/) {
201         $el = new HtmlElement('hr');
202         return $el->_init2(func_get_args());
203     }
204     function div (/*...*/) {
205         $el = new HtmlElement('div');
206         return $el->_init2(func_get_args());
207     }
208     function p (/*...*/) {
209         $el = new HtmlElement('p');
210         return $el->_init2(func_get_args());
211     }
212     function pre (/*...*/) {
213         $el = new HtmlElement('pre');
214         return $el->_init2(func_get_args());
215     }
216     function blockquote (/*...*/) {
217         $el = new HtmlElement('blockquote');
218         return $el->_init2(func_get_args());
219     }
220
221     /****************************************/
222     function em (/*...*/) {
223         $el = new HtmlElement('em');
224         return $el->_init2(func_get_args());
225     }
226     function strong (/*...*/) {
227         $el = new HtmlElement('strong');
228         return $el->_init2(func_get_args());
229     }
230     function small (/*...*/) {
231         $el = new HtmlElement('small');
232         return $el->_init2(func_get_args());
233     }
234
235     /****************************************/
236     function tt (/*...*/) {
237         $el = new HtmlElement('tt');
238         return $el->_init2(func_get_args());
239     }
240     function u (/*...*/) {
241         $el = new HtmlElement('u');
242         return $el->_init2(func_get_args());
243     }
244     function sup (/*...*/) {
245         $el = new HtmlElement('sup');
246         return $el->_init2(func_get_args());
247     }
248     function sub (/*...*/) {
249         $el = new HtmlElement('sub');
250         return $el->_init2(func_get_args());
251     }
252
253     /****************************************/
254     function ul (/*...*/) {
255         $el = new HtmlElement('ul');
256         return $el->_init2(func_get_args());
257     }
258     function ol (/*...*/) {
259         $el = new HtmlElement('ol');
260         return $el->_init2(func_get_args());
261     }
262     function dl (/*...*/) {
263         $el = new HtmlElement('dl');
264         return $el->_init2(func_get_args());
265     }
266     function li (/*...*/) {
267         $el = new HtmlElement('li');
268         return $el->_init2(func_get_args());
269     }
270     function dt (/*...*/) {
271         $el = new HtmlElement('dt');
272         return $el->_init2(func_get_args());
273     }
274     function dd (/*...*/) {
275         $el = new HtmlElement('dd');
276         return $el->_init2(func_get_args());
277     }
278
279     /****************************************/
280     function table (/*...*/) {
281         $el = new HtmlElement('table');
282         return $el->_init2(func_get_args());
283     }
284     function caption (/*...*/) {
285         $el = new HtmlElement('caption');
286         return $el->_init2(func_get_args());
287     }
288     function thead (/*...*/) {
289         $el = new HtmlElement('thead');
290         return $el->_init2(func_get_args());
291     }
292     function tbody (/*...*/) {
293         $el = new HtmlElement('tbody');
294         return $el->_init2(func_get_args());
295     }
296     function tfoot (/*...*/) {
297         $el = new HtmlElement('tfoot');
298         return $el->_init2(func_get_args());
299     }
300     function tr (/*...*/) {
301         $el = new HtmlElement('tr');
302         return $el->_init2(func_get_args());
303     }
304     function td (/*...*/) {
305         $el = new HtmlElement('td');
306         return $el->_init2(func_get_args());
307     }
308     function th (/*...*/) {
309         $el = new HtmlElement('th');
310         return $el->_init2(func_get_args());
311     }
312     function colgroup (/*...*/) {
313         $el = new HtmlElement('colgroup');
314         return $el->_init2(func_get_args());
315     }
316     function col (/*...*/) {
317         $el = new HtmlElement('col');
318         return $el->_init2(func_get_args());
319     }
320
321     /****************************************/
322     function form (/*...*/) {
323         $el = new HtmlElement('form');
324         return $el->_init2(func_get_args());
325     }
326     function input (/*...*/) {
327         $el = new HtmlElement('input');
328         return $el->_init2(func_get_args());
329     }
330     function option (/*...*/) {
331         $el = new HtmlElement('option');
332         return $el->_init2(func_get_args());
333     }
334     function select (/*...*/) {
335         $el = new HtmlElement('select');
336         return $el->_init2(func_get_args());
337     }
338     function textarea (/*...*/) {
339         $el = new HtmlElement('textarea');
340         return $el->_init2(func_get_args());
341     }
342
343     /****************************************/
344     function area (/*...*/) {
345         $el = new HtmlElement('area');
346         return $el->_init2(func_get_args());
347     }
348     function map (/*...*/) {
349         $el = new HtmlElement('map');
350         return $el->_init2(func_get_args());
351     }
352     function frame (/*...*/) {
353         $el = new HtmlElement('frame');
354         return $el->_init2(func_get_args());
355     }
356     function frameset (/*...*/) {
357         $el = new HtmlElement('frameset');
358         return $el->_init2(func_get_args());
359     }
360     function iframe (/*...*/) {
361         $el = new HtmlElement('iframe');
362         return $el->_init2(func_get_args());
363     }
364     function nobody (/*...*/) {
365         $el = new HtmlElement('nobody');
366         return $el->_init2(func_get_args());
367     }
368 }
369
370 define('HTMLTAG_EMPTY', 1);
371 define('HTMLTAG_INLINE', 2);
372 define('HTMLTAG_ACCEPTS_INLINE', 4);
373
374
375 HTML::_setTagProperty(HTMLTAG_EMPTY,
376                       'area base basefont br col frame hr img input isindex link meta param');
377 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
378                       // %inline elements:
379                       'b big i small tt ' // %fontstyle
380                       . 's strike u ' // (deprecated)
381                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
382                       . 'a img object br script map q sub sup span bdo '//%special
383                       . 'button input label option select textarea ' //%formctl
384
385                       // %block elements which contain inline content
386                       . 'address h1 h2 h3 h4 h5 h6 p pre '
387                       // %block elements which contain either block or inline content
388                       . 'div fieldset frameset'
389
390                       // other with inline content
391                       . 'caption dt label legend '
392                       // other with either inline or block
393                       . 'dd del ins li td th colgroup ');
394
395 HTML::_setTagProperty(HTMLTAG_INLINE,
396                       // %inline elements:
397                       'b big i small tt ' // %fontstyle
398                       . 's strike u ' // (deprecated)
399                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
400                       . 'a img object br script map q sub sup span bdo '//%special
401                       . 'button input label option select textarea ' //%formctl
402                       . 'nobody iframe'
403                       );
404
405 /**
406  * Generate hidden form input fields.
407  *
408  * @param $query_args hash  A hash mapping names to values for the hidden inputs.
409  * Values in the hash can themselves be hashes.  The will result in hidden inputs
410  * which will reconstruct the nested structure in the resulting query args as
411  * processed by PHP.
412  *
413  * Example:
414  *
415  * $args = array('x' => '2',
416  *               'y' => array('a' => 'aval', 'b' => 'bval'));
417  * $inputs = HiddenInputs($args);
418  *
419  * Will result in:
420  *
421  *  <input type="hidden" name="x" value = "2" />
422  *  <input type="hidden" name="y[a]" value = "aval" />
423  *  <input type="hidden" name="y[b]" value = "bval" />
424  *
425  * @return object An XmlContent object containing the inputs.
426  */
427 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
428     $inputs = HTML();
429
430     foreach ($query_args as $key => $val) {
431         if (in_array($key,$exclude)) continue;
432         $name = $pfx ? $pfx . "[$key]" : $key;
433         if (is_array($val))
434             $inputs->pushContent(HiddenInputs($val, $name));
435         else
436             $inputs->pushContent(HTML::input(array('type' => 'hidden',
437                                                    'name' => $name,
438                                                    'value' => $val)));
439     }
440     return $inputs;
441 }
442
443
444 /** Generate a <script> tag containing javascript.
445  *
446  * @param string $js  The javascript.
447  * @param string $script_args  (optional) hash of script tags options
448  *                             e.g. to provide another version or the defer attr
449  * @return HtmlElement A <script> element.
450  */
451 function JavaScript ($js, $script_args = false) {
452     $default_script_args = array('version' => 'JavaScript',
453                                  'type' => 'text/javascript');
454     $script_args = $script_args ? array_merge($default_script_args,$script_args)
455                                 : $default_script_args;
456     if (empty($js))
457         return HTML::script($script_args);
458     else
459         return HTML::script($script_args,
460                             new RawXml("<!-- //\n${js}\n// -->"));
461 }
462
463 /** Conditionally display content based of whether javascript is supported.
464  *
465  * This conditionally (on the client side) displays one of two alternate
466  * contents depending on whether the client supports javascript.
467  *
468  * NOTE:
469  * The content you pass as arguments to this function must be block-level.
470  * (This is because the <noscript> tag is block-level.)
471  *
472  * @param mixed $if_content Content to display if the browser supports
473  * javascript.
474  *
475  * @param mixed $else_content Content to display if the browser does
476  * not support javascript.
477  *
478  * @return XmlContent
479  */
480 function IfJavaScript($if_content = false, $else_content = false) {
481     $html = array();
482     if ($if_content) {
483         $xml = AsXML($if_content);
484         $js = sprintf('document.write("%s");',
485                       addcslashes($xml, "\0..\37!@\\\177..\377"));
486         $html[] = JavaScript($js);
487     }
488     if ($else_content) {
489         $html[] = HTML::noscript(false, $else_content);
490     }
491     return HTML($html);
492 }
493     
494 /**
495  $Log: not supported by cvs2svn $
496  Revision 1.31  2003/02/27 22:47:26  dairiki
497  New functions in HtmlElement:
498
499  JavaScript($js)
500     Helper for generating javascript.
501
502  IfJavaScript($if_content, $else_content)
503     Helper for generating
504        <script>document.write('...')</script><noscript>...</noscript>
505     constructs.
506
507  Revision 1.30  2003/02/17 06:02:25  dairiki
508  Remove functions HiddenGets() and HiddenPosts().
509
510  These functions were evil.  They didn't check the request method,
511  so they often resulted in GET args being converted to POST args,
512  etc...
513
514  One of these is still used in lib/plugin/WikiAdminSelect.php,
515  but, so far as I can tell, that code is both broken _and_ it
516  doesn't do anything.
517
518  Revision 1.29  2003/02/15 01:54:19  dairiki
519  Added HTML::meta() for <meta> tag.
520
521  Revision 1.28  2003/01/04 02:32:30  carstenklapp
522  Added 'col' and 'colgroup' table elements used by PluginManager.
523
524  */
525
526 // (c-file-style: "gnu")
527 // Local Variables:
528 // mode: php
529 // tab-width: 8
530 // c-basic-offset: 4
531 // c-hanging-comment-ender-p: nil
532 // indent-tabs-mode: nil
533 // End:
534 ?>