]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
new PrevNext plugin with two buttons
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.23 2002-08-24 13:18:56 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 define('NBSP', "\xA0");         // iso-8859-x non-breaking space.
78
79 class HTML extends HtmlElement {
80     function raw ($html_text) {
81         return new RawXML($html_text);
82     }
83
84     function getTagProperties($tag) {
85         $props = &$GLOBALS['HTML_TagProperties'];
86         return isset($props[$tag]) ? $props[$tag] : 0;
87     }
88
89     function _setTagProperty($prop_flag, $tags) {
90         $props = &$GLOBALS['HTML_TagProperties'];
91         if (is_string($tags))
92             $tags = preg_split('/\s+/', $tags);
93         foreach ($tags as $tag) {
94             if (isset($props[$tag]))
95                 $props[$tag] |= $prop_flag;
96             else
97                 $props[$tag] = $prop_flag;
98         }
99     }
100
101     //
102     // Shell script to generate the following static methods:
103     //
104     // #!/bin/sh
105     // function mkfuncs () {
106     //     for tag in "$@"
107     //     do
108     //         echo "    function $tag (/*...*/) {"
109     //         echo "        \$el = new HtmlElement('$tag');"
110     //         echo "        return \$el->_init2(func_get_args());"
111     //         echo "    }"
112     //     done
113     // }
114     // d='
115     //     /****************************************/'
116     // mkfuncs link style script noscript
117     // echo "$d"
118     // mkfuncs a img br span
119     // echo "$d"
120     // mkfuncs h1 h2 h3 h4 h5 h6
121     // echo "$d"
122     // mkfuncs hr div p pre blockquote
123     // echo "$d"
124     // mkfuncs em strong small
125     // echo "$d"
126     // mkfuncs tt u sup sub
127     // echo "$d"
128     // mkfuncs ul ol dl li dt dd
129     // echo "$d"
130     // mkfuncs table caption thead tbody tfoot tr td th
131     // echo "$d"
132     // mkfuncs form input option select textarea area map
133
134     function link (/*...*/) {
135         $el = new HtmlElement('link');
136         return $el->_init2(func_get_args());
137     }
138     function style (/*...*/) {
139         $el = new HtmlElement('style');
140         return $el->_init2(func_get_args());
141     }
142     function script (/*...*/) {
143         $el = new HtmlElement('script');
144         return $el->_init2(func_get_args());
145     }
146     function noscript (/*...*/) {
147         $el = new HtmlElement('noscript');
148         return $el->_init2(func_get_args());
149     }
150
151     /****************************************/
152     function a (/*...*/) {
153         $el = new HtmlElement('a');
154         return $el->_init2(func_get_args());
155     }
156     function img (/*...*/) {
157         $el = new HtmlElement('img');
158         return $el->_init2(func_get_args());
159     }
160     function br (/*...*/) {
161         $el = new HtmlElement('br');
162         return $el->_init2(func_get_args());
163     }
164     function span (/*...*/) {
165         $el = new HtmlElement('span');
166         return $el->_init2(func_get_args());
167     }
168
169     /****************************************/
170     function h1 (/*...*/) {
171         $el = new HtmlElement('h1');
172         return $el->_init2(func_get_args());
173     }
174     function h2 (/*...*/) {
175         $el = new HtmlElement('h2');
176         return $el->_init2(func_get_args());
177     }
178     function h3 (/*...*/) {
179         $el = new HtmlElement('h3');
180         return $el->_init2(func_get_args());
181     }
182     function h4 (/*...*/) {
183         $el = new HtmlElement('h4');
184         return $el->_init2(func_get_args());
185     }
186     function h5 (/*...*/) {
187         $el = new HtmlElement('h5');
188         return $el->_init2(func_get_args());
189     }
190     function h6 (/*...*/) {
191         $el = new HtmlElement('h6');
192         return $el->_init2(func_get_args());
193     }
194
195     /****************************************/
196     function hr (/*...*/) {
197         $el = new HtmlElement('hr');
198         return $el->_init2(func_get_args());
199     }
200     function div (/*...*/) {
201         $el = new HtmlElement('div');
202         return $el->_init2(func_get_args());
203     }
204     function p (/*...*/) {
205         $el = new HtmlElement('p');
206         return $el->_init2(func_get_args());
207     }
208     function pre (/*...*/) {
209         $el = new HtmlElement('pre');
210         return $el->_init2(func_get_args());
211     }
212     function blockquote (/*...*/) {
213         $el = new HtmlElement('blockquote');
214         return $el->_init2(func_get_args());
215     }
216
217     /****************************************/
218     function em (/*...*/) {
219         $el = new HtmlElement('em');
220         return $el->_init2(func_get_args());
221     }
222     function strong (/*...*/) {
223         $el = new HtmlElement('strong');
224         return $el->_init2(func_get_args());
225     }
226     function small (/*...*/) {
227         $el = new HtmlElement('small');
228         return $el->_init2(func_get_args());
229     }
230
231     /****************************************/
232     function tt (/*...*/) {
233         $el = new HtmlElement('tt');
234         return $el->_init2(func_get_args());
235     }
236     function u (/*...*/) {
237         $el = new HtmlElement('u');
238         return $el->_init2(func_get_args());
239     }
240     function sup (/*...*/) {
241         $el = new HtmlElement('sup');
242         return $el->_init2(func_get_args());
243     }
244     function sub (/*...*/) {
245         $el = new HtmlElement('sub');
246         return $el->_init2(func_get_args());
247     }
248
249     /****************************************/
250     function ul (/*...*/) {
251         $el = new HtmlElement('ul');
252         return $el->_init2(func_get_args());
253     }
254     function ol (/*...*/) {
255         $el = new HtmlElement('ol');
256         return $el->_init2(func_get_args());
257     }
258     function dl (/*...*/) {
259         $el = new HtmlElement('dl');
260         return $el->_init2(func_get_args());
261     }
262     function li (/*...*/) {
263         $el = new HtmlElement('li');
264         return $el->_init2(func_get_args());
265     }
266     function dt (/*...*/) {
267         $el = new HtmlElement('dt');
268         return $el->_init2(func_get_args());
269     }
270     function dd (/*...*/) {
271         $el = new HtmlElement('dd');
272         return $el->_init2(func_get_args());
273     }
274
275     /****************************************/
276     function table (/*...*/) {
277         $el = new HtmlElement('table');
278         return $el->_init2(func_get_args());
279     }
280     function caption (/*...*/) {
281         $el = new HtmlElement('caption');
282         return $el->_init2(func_get_args());
283     }
284     function thead (/*...*/) {
285         $el = new HtmlElement('thead');
286         return $el->_init2(func_get_args());
287     }
288     function tbody (/*...*/) {
289         $el = new HtmlElement('tbody');
290         return $el->_init2(func_get_args());
291     }
292     function tfoot (/*...*/) {
293         $el = new HtmlElement('tfoot');
294         return $el->_init2(func_get_args());
295     }
296     function tr (/*...*/) {
297         $el = new HtmlElement('tr');
298         return $el->_init2(func_get_args());
299     }
300     function td (/*...*/) {
301         $el = new HtmlElement('td');
302         return $el->_init2(func_get_args());
303     }
304     function th (/*...*/) {
305         $el = new HtmlElement('th');
306         return $el->_init2(func_get_args());
307     }
308
309     /****************************************/
310     function form (/*...*/) {
311         $el = new HtmlElement('form');
312         return $el->_init2(func_get_args());
313     }
314     function input (/*...*/) {
315         $el = new HtmlElement('input');
316         return $el->_init2(func_get_args());
317     }
318     function option (/*...*/) {
319         $el = new HtmlElement('option');
320         return $el->_init2(func_get_args());
321     }
322     function select (/*...*/) {
323         $el = new HtmlElement('select');
324         return $el->_init2(func_get_args());
325     }
326     function textarea (/*...*/) {
327         $el = new HtmlElement('textarea');
328         return $el->_init2(func_get_args());
329     }
330     function area (/*...*/) {
331         $el = new HtmlElement('area');
332         return $el->_init2(func_get_args());
333     }
334     function map (/*...*/) {
335         $el = new HtmlElement('map');
336         return $el->_init2(func_get_args());
337     }
338 }
339
340 define('HTMLTAG_EMPTY', 1);
341 define('HTMLTAG_INLINE', 2);
342 define('HTMLTAG_ACCEPTS_INLINE', 4);
343
344
345 HTML::_setTagProperty(HTMLTAG_EMPTY,
346                       'area base basefont br col frame hr img input isindex link meta param');
347 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
348                       // %inline elements:
349                       'b big i small tt ' // %fontstyle
350                       . 's strike u ' // (deprecated)
351                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
352                       . 'a img object br script map q sub sup span bdo '//%special
353                       . 'button input label option select textarea ' //%formctl
354
355                       // %block elements which contain inline content
356                       . 'address h1 h2 h3 h4 h5 h6 p pre '
357                       // %block elements which contain either block or inline content
358                       . 'div fieldset '
359
360                       // other with inline content
361                       . 'caption dt label legend '
362                       // other with either inline or block
363                       . 'dd del ins li td th ');
364
365 HTML::_setTagProperty(HTMLTAG_INLINE,
366                       // %inline elements:
367                       'b big i small tt ' // %fontstyle
368                       . 's strike u ' // (deprecated)
369                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
370                       . 'a img object br script map q sub sup span bdo '//%special
371                       . 'button input label option select textarea ' //%formctl
372                       );
373
374 /**
375  * Generate hidden form input fields.
376  *
377  * @param $query_args hash  A hash mapping names to values for the hidden inputs.
378  * Values in the hash can themselves be hashes.  The will result in hidden inputs
379  * which will reconstruct the nested structure in the resulting query args as
380  * processed by PHP.
381  *
382  * Example:
383  *
384  * $args = array('x' => '2',
385  *               'y' => array('a' => 'aval', 'b' => 'bval'));
386  * $inputs = HiddenInputs($args);
387  *
388  * Will result in:
389  *
390  *  <input type="hidden" name="x" value = "2" />
391  *  <input type="hidden" name="y[a]" value = "aval" />
392  *  <input type="hidden" name="y[b]" value = "bval" />
393  *
394  * @return object An XmlContent object containing the inputs.
395  */
396 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
397     $inputs = HTML();
398
399     foreach ($query_args as $key => $val) {
400         if (in_array($key,$exclude)) continue;
401         $name = $pfx ? $pfx . "[$key]" : $key;
402         if (is_array($val))
403             $inputs->pushContent(HiddenInputs($val, $name));
404         else
405             $inputs->pushContent(HTML::input(array('type' => 'hidden',
406                                                    'name' => $name,
407                                                    'value' => $val)));
408     }
409     return $inputs;
410 }
411
412 function HiddenGets ($exclude = array()) {
413     global $HTTP_GET_VARS;
414     HiddenInputs($HTTP_GET_VARS, false, $exclude);
415 }
416
417 function HiddenPosts ($exclude = array()) {
418     global $HTTP_POST_VARS;
419     HiddenInputs($HTTP_POST_VARS, false, $exclude);
420 }
421
422 // (c-file-style: "gnu")
423 // Local Variables:
424 // mode: php
425 // tab-width: 8
426 // c-basic-offset: 4
427 // c-hanging-comment-ender-p: nil
428 // indent-tabs-mode: nil
429 // End:
430 ?>