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