]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
Added entry for bsdWiki.
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.15 2002-02-02 01:58:17 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 && 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     function select (/*...*/) {
293         $el = new HtmlElement('select');
294         return $el->_init2(func_get_args());
295     }
296     function option (/*...*/) {
297         $el = new HtmlElement('option');
298         return $el->_init2(func_get_args());
299     }
300 }
301
302 define('HTMLTAG_EMPTY', 1);
303 define('HTMLTAG_INLINE', 2);
304 define('HTMLTAG_ACCEPTS_INLINE', 4);
305
306
307 HTML::_setTagProperty(HTMLTAG_EMPTY,
308                       'area base basefont br col frame hr img input isindex link meta param');
309 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
310                       // %inline elements:
311                       'b big i small tt ' // %fontstyle
312                       . 's strike u ' // (deprecated)
313                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
314                       . 'a img object br script map q sub sup span bdo '//%special
315                       . 'button input label select textarea ' //%formctl
316
317                       // %block elements which contain inline content
318                       . 'address h1 h2 h3 h4 h5 h6 p pre '
319                       // %block elements which contain either block or inline content
320                       . 'div fieldset '
321
322                       // other with inline content
323                       . 'caption dt label legend '
324                       // other with either inline or block
325                       . 'dd del ins li td th ');
326
327 HTML::_setTagProperty(HTMLTAG_INLINE,
328                       // %inline elements:
329                       'b big i small tt ' // %fontstyle
330                       . 's strike u ' // (deprecated)
331                       . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
332                       . 'a img object br script map q sub sup span bdo '//%special
333                       . 'button input label select textarea ' //%formctl
334                       );
335
336 /**
337  * Generate hidden form input fields.
338  *
339  * @param $query_args hash  A hash mapping names to values for the hidden inputs.
340  * Values in the hash can themselves be hashes.  The will result in hidden inputs
341  * which will reconstruct the nested structure in the resulting query args (as
342  * processed by PHP.
343  *
344  * Example:
345  *
346  * $args = array('x' => '2',
347  *               'y' => array('a' => 'aval', 'b' => 'bval'));
348  * $inputs = HiddenInputs($args);
349  *
350  * Will result in:
351  *
352  *  <input type="hidden" name="x" value = "2" />
353  *  <input type="hidden" name="y[a]" value = "aval" />
354  *  <input type="hidden" name="y[b]" value = "bval" />
355  *
356  * @return object An XmlContent object containing the inputs.
357  */
358 function HiddenInputs ($query_args, $pfx = false) {
359     $inputs = HTML();
360     
361     foreach ($query_args as $key => $val) {
362         $name = $pfx ? $pfx . "[$key]" : $key;
363         if (is_array($val))
364             $inputs->pushContent(HiddenInputs($val, $name));
365         else
366             $inputs->pushContent(HTML::input(array('type' => 'hidden',
367                                                    'name' => $name,
368                                                    'value' => $val)));
369     }
370     return $inputs;
371 }
372
373 // (c-file-style: "gnu")
374 // Local Variables:
375 // mode: php
376 // tab-width: 8
377 // c-basic-offset: 4
378 // c-hanging-comment-ender-p: nil
379 // indent-tabs-mode: nil
380 // End:   
381 ?>