1 <?php rcs_id('$Id: HtmlElement.php,v 1.32 2004-02-15 21:34:37 rurban Exp $');
3 * Code for writing XML.
5 require_once("lib/XmlElement.php");
10 class HtmlElement extends XmlElement
12 //function HtmlElement ($tagname /* , $attr_or_content , ...*/) {
13 // $this->_init(func_get_args());
14 // $this->_properties = HTML::getTagProperties($tagname);
18 function _init ($args) {
19 XmlElement::_init($args);
20 $this->_properties = HTML::getTagProperties($this->_tag);
25 * This is used by the static factory methods is class HTML.
27 function _init2 ($args) {
29 if (is_array($args[0]))
30 $this->_attr = array_shift($args);
31 elseif ($args[0] === false)
35 if (count($args) == 1 && is_array($args[0]))
37 $this->_content = $args;
41 /** Add a "tooltip" to an element.
43 * @param $tooltip_text string The tooltip text.
45 function addTooltip ($tooltip_text) {
46 $this->setAttr('title', $tooltip_text);
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;");
57 function emptyTag () {
58 if (($this->_properties & HTMLTAG_EMPTY) == 0)
59 return $this->startTag() . "</$this->_tag>";
61 return substr($this->startTag(), 0, -1) . " />";
64 function hasInlineContent () {
65 return ($this->_properties & HTMLTAG_ACCEPTS_INLINE) != 0;
68 function isInlineElement () {
69 return ($this->_properties & HTMLTAG_INLINE) != 0;
73 function HTML (/* $content, ... */) {
74 return new XmlContent(func_get_args());
77 class HTML extends HtmlElement {
78 function raw ($html_text) {
79 return new RawXML($html_text);
82 function getTagProperties($tag) {
83 $props = &$GLOBALS['HTML_TagProperties'];
84 return isset($props[$tag]) ? $props[$tag] : 0;
87 function _setTagProperty($prop_flag, $tags) {
88 $props = &$GLOBALS['HTML_TagProperties'];
90 $tags = preg_split('/\s+/', $tags);
91 foreach ($tags as $tag) {
92 if (isset($props[$tag]))
93 $props[$tag] |= $prop_flag;
95 $props[$tag] = $prop_flag;
100 // Shell script to generate the following static methods:
103 // function mkfuncs () {
106 // echo " function $tag (/*...*/) {"
107 // echo " \$el = new HtmlElement('$tag');"
108 // echo " return \$el->_init2(func_get_args());"
113 // /****************************************/'
114 // mkfuncs link meta style script noscript
116 // mkfuncs a img br span
118 // mkfuncs h1 h2 h3 h4 h5 h6
120 // mkfuncs hr div p pre blockquote
122 // mkfuncs em strong small
124 // mkfuncs tt u sup sub
126 // mkfuncs ul ol dl li dt dd
128 // mkfuncs table caption thead tbody tfoot tr td th colgroup col
130 // mkfuncs form input option select textarea
132 // mkfuncs area map frame frameset iframe nobody
134 function link (/*...*/) {
135 $el = new HtmlElement('link');
136 return $el->_init2(func_get_args());
138 function meta (/*...*/) {
139 $el = new HtmlElement('meta');
140 return $el->_init2(func_get_args());
142 function style (/*...*/) {
143 $el = new HtmlElement('style');
144 return $el->_init2(func_get_args());
146 function script (/*...*/) {
147 $el = new HtmlElement('script');
148 return $el->_init2(func_get_args());
150 function noscript (/*...*/) {
151 $el = new HtmlElement('noscript');
152 return $el->_init2(func_get_args());
155 /****************************************/
156 function a (/*...*/) {
157 $el = new HtmlElement('a');
158 return $el->_init2(func_get_args());
160 function img (/*...*/) {
161 $el = new HtmlElement('img');
162 return $el->_init2(func_get_args());
164 function br (/*...*/) {
165 $el = new HtmlElement('br');
166 return $el->_init2(func_get_args());
168 function span (/*...*/) {
169 $el = new HtmlElement('span');
170 return $el->_init2(func_get_args());
173 /****************************************/
174 function h1 (/*...*/) {
175 $el = new HtmlElement('h1');
176 return $el->_init2(func_get_args());
178 function h2 (/*...*/) {
179 $el = new HtmlElement('h2');
180 return $el->_init2(func_get_args());
182 function h3 (/*...*/) {
183 $el = new HtmlElement('h3');
184 return $el->_init2(func_get_args());
186 function h4 (/*...*/) {
187 $el = new HtmlElement('h4');
188 return $el->_init2(func_get_args());
190 function h5 (/*...*/) {
191 $el = new HtmlElement('h5');
192 return $el->_init2(func_get_args());
194 function h6 (/*...*/) {
195 $el = new HtmlElement('h6');
196 return $el->_init2(func_get_args());
199 /****************************************/
200 function hr (/*...*/) {
201 $el = new HtmlElement('hr');
202 return $el->_init2(func_get_args());
204 function div (/*...*/) {
205 $el = new HtmlElement('div');
206 return $el->_init2(func_get_args());
208 function p (/*...*/) {
209 $el = new HtmlElement('p');
210 return $el->_init2(func_get_args());
212 function pre (/*...*/) {
213 $el = new HtmlElement('pre');
214 return $el->_init2(func_get_args());
216 function blockquote (/*...*/) {
217 $el = new HtmlElement('blockquote');
218 return $el->_init2(func_get_args());
221 /****************************************/
222 function em (/*...*/) {
223 $el = new HtmlElement('em');
224 return $el->_init2(func_get_args());
226 function strong (/*...*/) {
227 $el = new HtmlElement('strong');
228 return $el->_init2(func_get_args());
230 function small (/*...*/) {
231 $el = new HtmlElement('small');
232 return $el->_init2(func_get_args());
235 /****************************************/
236 function tt (/*...*/) {
237 $el = new HtmlElement('tt');
238 return $el->_init2(func_get_args());
240 function u (/*...*/) {
241 $el = new HtmlElement('u');
242 return $el->_init2(func_get_args());
244 function sup (/*...*/) {
245 $el = new HtmlElement('sup');
246 return $el->_init2(func_get_args());
248 function sub (/*...*/) {
249 $el = new HtmlElement('sub');
250 return $el->_init2(func_get_args());
253 /****************************************/
254 function ul (/*...*/) {
255 $el = new HtmlElement('ul');
256 return $el->_init2(func_get_args());
258 function ol (/*...*/) {
259 $el = new HtmlElement('ol');
260 return $el->_init2(func_get_args());
262 function dl (/*...*/) {
263 $el = new HtmlElement('dl');
264 return $el->_init2(func_get_args());
266 function li (/*...*/) {
267 $el = new HtmlElement('li');
268 return $el->_init2(func_get_args());
270 function dt (/*...*/) {
271 $el = new HtmlElement('dt');
272 return $el->_init2(func_get_args());
274 function dd (/*...*/) {
275 $el = new HtmlElement('dd');
276 return $el->_init2(func_get_args());
279 /****************************************/
280 function table (/*...*/) {
281 $el = new HtmlElement('table');
282 return $el->_init2(func_get_args());
284 function caption (/*...*/) {
285 $el = new HtmlElement('caption');
286 return $el->_init2(func_get_args());
288 function thead (/*...*/) {
289 $el = new HtmlElement('thead');
290 return $el->_init2(func_get_args());
292 function tbody (/*...*/) {
293 $el = new HtmlElement('tbody');
294 return $el->_init2(func_get_args());
296 function tfoot (/*...*/) {
297 $el = new HtmlElement('tfoot');
298 return $el->_init2(func_get_args());
300 function tr (/*...*/) {
301 $el = new HtmlElement('tr');
302 return $el->_init2(func_get_args());
304 function td (/*...*/) {
305 $el = new HtmlElement('td');
306 return $el->_init2(func_get_args());
308 function th (/*...*/) {
309 $el = new HtmlElement('th');
310 return $el->_init2(func_get_args());
312 function colgroup (/*...*/) {
313 $el = new HtmlElement('colgroup');
314 return $el->_init2(func_get_args());
316 function col (/*...*/) {
317 $el = new HtmlElement('col');
318 return $el->_init2(func_get_args());
321 /****************************************/
322 function form (/*...*/) {
323 $el = new HtmlElement('form');
324 return $el->_init2(func_get_args());
326 function input (/*...*/) {
327 $el = new HtmlElement('input');
328 return $el->_init2(func_get_args());
330 function option (/*...*/) {
331 $el = new HtmlElement('option');
332 return $el->_init2(func_get_args());
334 function select (/*...*/) {
335 $el = new HtmlElement('select');
336 return $el->_init2(func_get_args());
338 function textarea (/*...*/) {
339 $el = new HtmlElement('textarea');
340 return $el->_init2(func_get_args());
343 /****************************************/
344 function area (/*...*/) {
345 $el = new HtmlElement('area');
346 return $el->_init2(func_get_args());
348 function map (/*...*/) {
349 $el = new HtmlElement('map');
350 return $el->_init2(func_get_args());
352 function frame (/*...*/) {
353 $el = new HtmlElement('frame');
354 return $el->_init2(func_get_args());
356 function frameset (/*...*/) {
357 $el = new HtmlElement('frameset');
358 return $el->_init2(func_get_args());
360 function iframe (/*...*/) {
361 $el = new HtmlElement('iframe');
362 return $el->_init2(func_get_args());
364 function nobody (/*...*/) {
365 $el = new HtmlElement('nobody');
366 return $el->_init2(func_get_args());
370 define('HTMLTAG_EMPTY', 1);
371 define('HTMLTAG_INLINE', 2);
372 define('HTMLTAG_ACCEPTS_INLINE', 4);
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,
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
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'
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 ');
395 HTML::_setTagProperty(HTMLTAG_INLINE,
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
406 * Generate hidden form input fields.
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
415 * $args = array('x' => '2',
416 * 'y' => array('a' => 'aval', 'b' => 'bval'));
417 * $inputs = HiddenInputs($args);
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" />
425 * @return object An XmlContent object containing the inputs.
427 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
430 foreach ($query_args as $key => $val) {
431 if (in_array($key,$exclude)) continue;
432 $name = $pfx ? $pfx . "[$key]" : $key;
434 $inputs->pushContent(HiddenInputs($val, $name));
436 $inputs->pushContent(HTML::input(array('type' => 'hidden',
444 /** Generate a <script> tag containing javascript.
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.
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;
457 return HTML::script($script_args);
459 return HTML::script($script_args,
460 new RawXml("<!-- //\n${js}\n// -->"));
463 /** Conditionally display content based of whether javascript is supported.
465 * This conditionally (on the client side) displays one of two alternate
466 * contents depending on whether the client supports javascript.
469 * The content you pass as arguments to this function must be block-level.
470 * (This is because the <noscript> tag is block-level.)
472 * @param mixed $if_content Content to display if the browser supports
475 * @param mixed $else_content Content to display if the browser does
476 * not support javascript.
480 function IfJavaScript($if_content = false, $else_content = false) {
483 $xml = AsXML($if_content);
484 $js = sprintf('document.write("%s");',
485 addcslashes($xml, "\0..\37!@\\\177..\377"));
486 $html[] = JavaScript($js);
489 $html[] = HTML::noscript(false, $else_content);
495 $Log: not supported by cvs2svn $
496 Revision 1.31 2003/02/27 22:47:26 dairiki
497 New functions in HtmlElement:
500 Helper for generating javascript.
502 IfJavaScript($if_content, $else_content)
503 Helper for generating
504 <script>document.write('...')</script><noscript>...</noscript>
507 Revision 1.30 2003/02/17 06:02:25 dairiki
508 Remove functions HiddenGets() and HiddenPosts().
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,
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
518 Revision 1.29 2003/02/15 01:54:19 dairiki
519 Added HTML::meta() for <meta> tag.
521 Revision 1.28 2003/01/04 02:32:30 carstenklapp
522 Added 'col' and 'colgroup' table elements used by PluginManager.
526 // (c-file-style: "gnu")
531 // c-hanging-comment-ender-p: nil
532 // indent-tabs-mode: nil