1 <?php rcs_id('$Id: HtmlElement.php,v 1.27 2002-10-31 03:28:30 carstenklapp 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 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
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 style (/*...*/) {
139 $el = new HtmlElement('style');
140 return $el->_init2(func_get_args());
142 function script (/*...*/) {
143 $el = new HtmlElement('script');
144 return $el->_init2(func_get_args());
146 function noscript (/*...*/) {
147 $el = new HtmlElement('noscript');
148 return $el->_init2(func_get_args());
151 /****************************************/
152 function a (/*...*/) {
153 $el = new HtmlElement('a');
154 return $el->_init2(func_get_args());
156 function img (/*...*/) {
157 $el = new HtmlElement('img');
158 return $el->_init2(func_get_args());
160 function br (/*...*/) {
161 $el = new HtmlElement('br');
162 return $el->_init2(func_get_args());
164 function span (/*...*/) {
165 $el = new HtmlElement('span');
166 return $el->_init2(func_get_args());
169 /****************************************/
170 function h1 (/*...*/) {
171 $el = new HtmlElement('h1');
172 return $el->_init2(func_get_args());
174 function h2 (/*...*/) {
175 $el = new HtmlElement('h2');
176 return $el->_init2(func_get_args());
178 function h3 (/*...*/) {
179 $el = new HtmlElement('h3');
180 return $el->_init2(func_get_args());
182 function h4 (/*...*/) {
183 $el = new HtmlElement('h4');
184 return $el->_init2(func_get_args());
186 function h5 (/*...*/) {
187 $el = new HtmlElement('h5');
188 return $el->_init2(func_get_args());
190 function h6 (/*...*/) {
191 $el = new HtmlElement('h6');
192 return $el->_init2(func_get_args());
195 /****************************************/
196 function hr (/*...*/) {
197 $el = new HtmlElement('hr');
198 return $el->_init2(func_get_args());
200 function div (/*...*/) {
201 $el = new HtmlElement('div');
202 return $el->_init2(func_get_args());
204 function p (/*...*/) {
205 $el = new HtmlElement('p');
206 return $el->_init2(func_get_args());
208 function pre (/*...*/) {
209 $el = new HtmlElement('pre');
210 return $el->_init2(func_get_args());
212 function blockquote (/*...*/) {
213 $el = new HtmlElement('blockquote');
214 return $el->_init2(func_get_args());
217 /****************************************/
218 function em (/*...*/) {
219 $el = new HtmlElement('em');
220 return $el->_init2(func_get_args());
222 function strong (/*...*/) {
223 $el = new HtmlElement('strong');
224 return $el->_init2(func_get_args());
226 function small (/*...*/) {
227 $el = new HtmlElement('small');
228 return $el->_init2(func_get_args());
231 /****************************************/
232 function tt (/*...*/) {
233 $el = new HtmlElement('tt');
234 return $el->_init2(func_get_args());
236 function u (/*...*/) {
237 $el = new HtmlElement('u');
238 return $el->_init2(func_get_args());
240 function sup (/*...*/) {
241 $el = new HtmlElement('sup');
242 return $el->_init2(func_get_args());
244 function sub (/*...*/) {
245 $el = new HtmlElement('sub');
246 return $el->_init2(func_get_args());
249 /****************************************/
250 function ul (/*...*/) {
251 $el = new HtmlElement('ul');
252 return $el->_init2(func_get_args());
254 function ol (/*...*/) {
255 $el = new HtmlElement('ol');
256 return $el->_init2(func_get_args());
258 function dl (/*...*/) {
259 $el = new HtmlElement('dl');
260 return $el->_init2(func_get_args());
262 function li (/*...*/) {
263 $el = new HtmlElement('li');
264 return $el->_init2(func_get_args());
266 function dt (/*...*/) {
267 $el = new HtmlElement('dt');
268 return $el->_init2(func_get_args());
270 function dd (/*...*/) {
271 $el = new HtmlElement('dd');
272 return $el->_init2(func_get_args());
275 /****************************************/
276 function table (/*...*/) {
277 $el = new HtmlElement('table');
278 return $el->_init2(func_get_args());
280 function caption (/*...*/) {
281 $el = new HtmlElement('caption');
282 return $el->_init2(func_get_args());
284 function thead (/*...*/) {
285 $el = new HtmlElement('thead');
286 return $el->_init2(func_get_args());
288 function tbody (/*...*/) {
289 $el = new HtmlElement('tbody');
290 return $el->_init2(func_get_args());
292 function tfoot (/*...*/) {
293 $el = new HtmlElement('tfoot');
294 return $el->_init2(func_get_args());
296 function tr (/*...*/) {
297 $el = new HtmlElement('tr');
298 return $el->_init2(func_get_args());
300 function td (/*...*/) {
301 $el = new HtmlElement('td');
302 return $el->_init2(func_get_args());
304 function th (/*...*/) {
305 $el = new HtmlElement('th');
306 return $el->_init2(func_get_args());
309 /****************************************/
310 function form (/*...*/) {
311 $el = new HtmlElement('form');
312 return $el->_init2(func_get_args());
314 function input (/*...*/) {
315 $el = new HtmlElement('input');
316 return $el->_init2(func_get_args());
318 function option (/*...*/) {
319 $el = new HtmlElement('option');
320 return $el->_init2(func_get_args());
322 function select (/*...*/) {
323 $el = new HtmlElement('select');
324 return $el->_init2(func_get_args());
326 function textarea (/*...*/) {
327 $el = new HtmlElement('textarea');
328 return $el->_init2(func_get_args());
331 /****************************************/
332 function area (/*...*/) {
333 $el = new HtmlElement('area');
334 return $el->_init2(func_get_args());
336 function map (/*...*/) {
337 $el = new HtmlElement('map');
338 return $el->_init2(func_get_args());
340 function frame (/*...*/) {
341 $el = new HtmlElement('frame');
342 return $el->_init2(func_get_args());
344 function frameset (/*...*/) {
345 $el = new HtmlElement('frameset');
346 return $el->_init2(func_get_args());
348 function iframe (/*...*/) {
349 $el = new HtmlElement('iframe');
350 return $el->_init2(func_get_args());
352 function nobody (/*...*/) {
353 $el = new HtmlElement('nobody');
354 return $el->_init2(func_get_args());
358 define('HTMLTAG_EMPTY', 1);
359 define('HTMLTAG_INLINE', 2);
360 define('HTMLTAG_ACCEPTS_INLINE', 4);
363 HTML::_setTagProperty(HTMLTAG_EMPTY,
364 'area base basefont br col frame hr img input isindex link meta param');
365 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
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
373 // %block elements which contain inline content
374 . 'address h1 h2 h3 h4 h5 h6 p pre '
375 // %block elements which contain either block or inline content
376 . 'div fieldset frameset'
378 // other with inline content
379 . 'caption dt label legend '
380 // other with either inline or block
381 . 'dd del ins li td th ');
383 HTML::_setTagProperty(HTMLTAG_INLINE,
385 'b big i small tt ' // %fontstyle
386 . 's strike u ' // (deprecated)
387 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
388 . 'a img object br script map q sub sup span bdo '//%special
389 . 'button input label option select textarea ' //%formctl
394 * Generate hidden form input fields.
396 * @param $query_args hash A hash mapping names to values for the hidden inputs.
397 * Values in the hash can themselves be hashes. The will result in hidden inputs
398 * which will reconstruct the nested structure in the resulting query args as
403 * $args = array('x' => '2',
404 * 'y' => array('a' => 'aval', 'b' => 'bval'));
405 * $inputs = HiddenInputs($args);
409 * <input type="hidden" name="x" value = "2" />
410 * <input type="hidden" name="y[a]" value = "aval" />
411 * <input type="hidden" name="y[b]" value = "bval" />
413 * @return object An XmlContent object containing the inputs.
415 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
418 foreach ($query_args as $key => $val) {
419 if (in_array($key,$exclude)) continue;
420 $name = $pfx ? $pfx . "[$key]" : $key;
422 $inputs->pushContent(HiddenInputs($val, $name));
424 $inputs->pushContent(HTML::input(array('type' => 'hidden',
431 function HiddenGets ($exclude = array()) {
432 global $HTTP_GET_VARS;
433 HiddenInputs($HTTP_GET_VARS, false, $exclude);
436 function HiddenPosts ($exclude = array()) {
437 global $HTTP_POST_VARS;
438 HiddenInputs($HTTP_POST_VARS, false, $exclude);
441 // (c-file-style: "gnu")
446 // c-hanging-comment-ender-p: nil
447 // indent-tabs-mode: nil