]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
fixed plugin/FrameInclude: missing are the targets at the top, left and bottom frames
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.24 2002-09-02 12:39:02 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
133     // echo "$d"
134     // mkfuncs area map frame frameset nobody
135
136     function link (/*...*/) {
137         $el = new HtmlElement('link');
138         return $el->_init2(func_get_args());
139     }
140     function style (/*...*/) {
141         $el = new HtmlElement('style');
142         return $el->_init2(func_get_args());
143     }
144     function script (/*...*/) {
145         $el = new HtmlElement('script');
146         return $el->_init2(func_get_args());
147     }
148     function noscript (/*...*/) {
149         $el = new HtmlElement('noscript');
150         return $el->_init2(func_get_args());
151     }
152
153     /****************************************/
154     function a (/*...*/) {
155         $el = new HtmlElement('a');
156         return $el->_init2(func_get_args());
157     }
158     function img (/*...*/) {
159         $el = new HtmlElement('img');
160         return $el->_init2(func_get_args());
161     }
162     function br (/*...*/) {
163         $el = new HtmlElement('br');
164         return $el->_init2(func_get_args());
165     }
166     function span (/*...*/) {
167         $el = new HtmlElement('span');
168         return $el->_init2(func_get_args());
169     }
170
171     /****************************************/
172     function h1 (/*...*/) {
173         $el = new HtmlElement('h1');
174         return $el->_init2(func_get_args());
175     }
176     function h2 (/*...*/) {
177         $el = new HtmlElement('h2');
178         return $el->_init2(func_get_args());
179     }
180     function h3 (/*...*/) {
181         $el = new HtmlElement('h3');
182         return $el->_init2(func_get_args());
183     }
184     function h4 (/*...*/) {
185         $el = new HtmlElement('h4');
186         return $el->_init2(func_get_args());
187     }
188     function h5 (/*...*/) {
189         $el = new HtmlElement('h5');
190         return $el->_init2(func_get_args());
191     }
192     function h6 (/*...*/) {
193         $el = new HtmlElement('h6');
194         return $el->_init2(func_get_args());
195     }
196
197     /****************************************/
198     function hr (/*...*/) {
199         $el = new HtmlElement('hr');
200         return $el->_init2(func_get_args());
201     }
202     function div (/*...*/) {
203         $el = new HtmlElement('div');
204         return $el->_init2(func_get_args());
205     }
206     function p (/*...*/) {
207         $el = new HtmlElement('p');
208         return $el->_init2(func_get_args());
209     }
210     function pre (/*...*/) {
211         $el = new HtmlElement('pre');
212         return $el->_init2(func_get_args());
213     }
214     function blockquote (/*...*/) {
215         $el = new HtmlElement('blockquote');
216         return $el->_init2(func_get_args());
217     }
218
219     /****************************************/
220     function em (/*...*/) {
221         $el = new HtmlElement('em');
222         return $el->_init2(func_get_args());
223     }
224     function strong (/*...*/) {
225         $el = new HtmlElement('strong');
226         return $el->_init2(func_get_args());
227     }
228     function small (/*...*/) {
229         $el = new HtmlElement('small');
230         return $el->_init2(func_get_args());
231     }
232
233     /****************************************/
234     function tt (/*...*/) {
235         $el = new HtmlElement('tt');
236         return $el->_init2(func_get_args());
237     }
238     function u (/*...*/) {
239         $el = new HtmlElement('u');
240         return $el->_init2(func_get_args());
241     }
242     function sup (/*...*/) {
243         $el = new HtmlElement('sup');
244         return $el->_init2(func_get_args());
245     }
246     function sub (/*...*/) {
247         $el = new HtmlElement('sub');
248         return $el->_init2(func_get_args());
249     }
250
251     /****************************************/
252     function ul (/*...*/) {
253         $el = new HtmlElement('ul');
254         return $el->_init2(func_get_args());
255     }
256     function ol (/*...*/) {
257         $el = new HtmlElement('ol');
258         return $el->_init2(func_get_args());
259     }
260     function dl (/*...*/) {
261         $el = new HtmlElement('dl');
262         return $el->_init2(func_get_args());
263     }
264     function li (/*...*/) {
265         $el = new HtmlElement('li');
266         return $el->_init2(func_get_args());
267     }
268     function dt (/*...*/) {
269         $el = new HtmlElement('dt');
270         return $el->_init2(func_get_args());
271     }
272     function dd (/*...*/) {
273         $el = new HtmlElement('dd');
274         return $el->_init2(func_get_args());
275     }
276
277     /****************************************/
278     function table (/*...*/) {
279         $el = new HtmlElement('table');
280         return $el->_init2(func_get_args());
281     }
282     function caption (/*...*/) {
283         $el = new HtmlElement('caption');
284         return $el->_init2(func_get_args());
285     }
286     function thead (/*...*/) {
287         $el = new HtmlElement('thead');
288         return $el->_init2(func_get_args());
289     }
290     function tbody (/*...*/) {
291         $el = new HtmlElement('tbody');
292         return $el->_init2(func_get_args());
293     }
294     function tfoot (/*...*/) {
295         $el = new HtmlElement('tfoot');
296         return $el->_init2(func_get_args());
297     }
298     function tr (/*...*/) {
299         $el = new HtmlElement('tr');
300         return $el->_init2(func_get_args());
301     }
302     function td (/*...*/) {
303         $el = new HtmlElement('td');
304         return $el->_init2(func_get_args());
305     }
306     function th (/*...*/) {
307         $el = new HtmlElement('th');
308         return $el->_init2(func_get_args());
309     }
310
311     /****************************************/
312     function form (/*...*/) {
313         $el = new HtmlElement('form');
314         return $el->_init2(func_get_args());
315     }
316     function input (/*...*/) {
317         $el = new HtmlElement('input');
318         return $el->_init2(func_get_args());
319     }
320     function option (/*...*/) {
321         $el = new HtmlElement('option');
322         return $el->_init2(func_get_args());
323     }
324     function select (/*...*/) {
325         $el = new HtmlElement('select');
326         return $el->_init2(func_get_args());
327     }
328     function textarea (/*...*/) {
329         $el = new HtmlElement('textarea');
330         return $el->_init2(func_get_args());
331     }
332
333     /****************************************/
334     function area (/*...*/) {
335         $el = new HtmlElement('area');
336         return $el->_init2(func_get_args());
337     }
338     function map (/*...*/) {
339         $el = new HtmlElement('map');
340         return $el->_init2(func_get_args());
341     }
342     function frame (/*...*/) {
343         $el = new HtmlElement('frame');
344         return $el->_init2(func_get_args());
345     }
346     function frameset (/*...*/) {
347         $el = new HtmlElement('frameset');
348         return $el->_init2(func_get_args());
349     }
350     function nobody (/*...*/) {
351         $el = new HtmlElement('nobody');
352         return $el->_init2(func_get_args());
353     }
354 }
355
356 define('HTMLTAG_EMPTY', 1);
357 define('HTMLTAG_INLINE', 2);
358 define('HTMLTAG_ACCEPTS_INLINE', 4);
359
360
361 HTML::_setTagProperty(HTMLTAG_EMPTY,
362                       'area base basefont br col frame hr img input isindex link meta param');
363 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
364                       // %inline elements:
365                       'b big i small tt ' // %fontstyle
366                       . 's strike u ' // (deprecated)
367                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
368                       . 'a img object br script map q sub sup span bdo '//%special
369                       . 'button input label option select textarea ' //%formctl
370
371                       // %block elements which contain inline content
372                       . 'address h1 h2 h3 h4 h5 h6 p pre '
373                       // %block elements which contain either block or inline content
374                       . 'div fieldset frameset'
375
376                       // other with inline content
377                       . 'caption dt label legend '
378                       // other with either inline or block
379                       . 'dd del ins li td th ');
380
381 HTML::_setTagProperty(HTMLTAG_INLINE,
382                       // %inline elements:
383                       'b big i small tt ' // %fontstyle
384                       . 's strike u ' // (deprecated)
385                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
386                       . 'a img object br script map q sub sup span bdo '//%special
387                       . 'button input label option select textarea ' //%formctl
388                       . 'nobody'
389                       );
390
391 /**
392  * Generate hidden form input fields.
393  *
394  * @param $query_args hash  A hash mapping names to values for the hidden inputs.
395  * Values in the hash can themselves be hashes.  The will result in hidden inputs
396  * which will reconstruct the nested structure in the resulting query args as
397  * processed by PHP.
398  *
399  * Example:
400  *
401  * $args = array('x' => '2',
402  *               'y' => array('a' => 'aval', 'b' => 'bval'));
403  * $inputs = HiddenInputs($args);
404  *
405  * Will result in:
406  *
407  *  <input type="hidden" name="x" value = "2" />
408  *  <input type="hidden" name="y[a]" value = "aval" />
409  *  <input type="hidden" name="y[b]" value = "bval" />
410  *
411  * @return object An XmlContent object containing the inputs.
412  */
413 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
414     $inputs = HTML();
415
416     foreach ($query_args as $key => $val) {
417         if (in_array($key,$exclude)) continue;
418         $name = $pfx ? $pfx . "[$key]" : $key;
419         if (is_array($val))
420             $inputs->pushContent(HiddenInputs($val, $name));
421         else
422             $inputs->pushContent(HTML::input(array('type' => 'hidden',
423                                                    'name' => $name,
424                                                    'value' => $val)));
425     }
426     return $inputs;
427 }
428
429 function HiddenGets ($exclude = array()) {
430     global $HTTP_GET_VARS;
431     HiddenInputs($HTTP_GET_VARS, false, $exclude);
432 }
433
434 function HiddenPosts ($exclude = array()) {
435     global $HTTP_POST_VARS;
436     HiddenInputs($HTTP_POST_VARS, false, $exclude);
437 }
438
439 // (c-file-style: "gnu")
440 // Local Variables:
441 // mode: php
442 // tab-width: 8
443 // c-basic-offset: 4
444 // c-hanging-comment-ender-p: nil
445 // indent-tabs-mode: nil
446 // End:
447 ?>