1 <?php rcs_id('$Id: HtmlElement.php,v 1.26 2002-10-29 01:12:23 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);
83 if (CHARSET == 'utf-8')
84 return new RawXML("\xC2\xA0"); // utf-8 non-breaking space.
85 elseif (CHARSET == 'iso-8859-1')
86 return new RawXML("\xA0"); // iso-8859-x non-breaking space.
88 return new RawXML(" "); // html-entity non-breaking space.
91 function getTagProperties($tag) {
92 $props = &$GLOBALS['HTML_TagProperties'];
93 return isset($props[$tag]) ? $props[$tag] : 0;
96 function _setTagProperty($prop_flag, $tags) {
97 $props = &$GLOBALS['HTML_TagProperties'];
99 $tags = preg_split('/\s+/', $tags);
100 foreach ($tags as $tag) {
101 if (isset($props[$tag]))
102 $props[$tag] |= $prop_flag;
104 $props[$tag] = $prop_flag;
109 // Shell script to generate the following static methods:
112 // function mkfuncs () {
115 // echo " function $tag (/*...*/) {"
116 // echo " \$el = new HtmlElement('$tag');"
117 // echo " return \$el->_init2(func_get_args());"
122 // /****************************************/'
123 // mkfuncs link style script noscript
125 // mkfuncs a img br span
127 // mkfuncs h1 h2 h3 h4 h5 h6
129 // mkfuncs hr div p pre blockquote
131 // mkfuncs em strong small
133 // mkfuncs tt u sup sub
135 // mkfuncs ul ol dl li dt dd
137 // mkfuncs table caption thead tbody tfoot tr td th
139 // mkfuncs form input option select textarea
141 // mkfuncs area map frame frameset iframe nobody
143 function link (/*...*/) {
144 $el = new HtmlElement('link');
145 return $el->_init2(func_get_args());
147 function style (/*...*/) {
148 $el = new HtmlElement('style');
149 return $el->_init2(func_get_args());
151 function script (/*...*/) {
152 $el = new HtmlElement('script');
153 return $el->_init2(func_get_args());
155 function noscript (/*...*/) {
156 $el = new HtmlElement('noscript');
157 return $el->_init2(func_get_args());
160 /****************************************/
161 function a (/*...*/) {
162 $el = new HtmlElement('a');
163 return $el->_init2(func_get_args());
165 function img (/*...*/) {
166 $el = new HtmlElement('img');
167 return $el->_init2(func_get_args());
169 function br (/*...*/) {
170 $el = new HtmlElement('br');
171 return $el->_init2(func_get_args());
173 function span (/*...*/) {
174 $el = new HtmlElement('span');
175 return $el->_init2(func_get_args());
178 /****************************************/
179 function h1 (/*...*/) {
180 $el = new HtmlElement('h1');
181 return $el->_init2(func_get_args());
183 function h2 (/*...*/) {
184 $el = new HtmlElement('h2');
185 return $el->_init2(func_get_args());
187 function h3 (/*...*/) {
188 $el = new HtmlElement('h3');
189 return $el->_init2(func_get_args());
191 function h4 (/*...*/) {
192 $el = new HtmlElement('h4');
193 return $el->_init2(func_get_args());
195 function h5 (/*...*/) {
196 $el = new HtmlElement('h5');
197 return $el->_init2(func_get_args());
199 function h6 (/*...*/) {
200 $el = new HtmlElement('h6');
201 return $el->_init2(func_get_args());
204 /****************************************/
205 function hr (/*...*/) {
206 $el = new HtmlElement('hr');
207 return $el->_init2(func_get_args());
209 function div (/*...*/) {
210 $el = new HtmlElement('div');
211 return $el->_init2(func_get_args());
213 function p (/*...*/) {
214 $el = new HtmlElement('p');
215 return $el->_init2(func_get_args());
217 function pre (/*...*/) {
218 $el = new HtmlElement('pre');
219 return $el->_init2(func_get_args());
221 function blockquote (/*...*/) {
222 $el = new HtmlElement('blockquote');
223 return $el->_init2(func_get_args());
226 /****************************************/
227 function em (/*...*/) {
228 $el = new HtmlElement('em');
229 return $el->_init2(func_get_args());
231 function strong (/*...*/) {
232 $el = new HtmlElement('strong');
233 return $el->_init2(func_get_args());
235 function small (/*...*/) {
236 $el = new HtmlElement('small');
237 return $el->_init2(func_get_args());
240 /****************************************/
241 function tt (/*...*/) {
242 $el = new HtmlElement('tt');
243 return $el->_init2(func_get_args());
245 function u (/*...*/) {
246 $el = new HtmlElement('u');
247 return $el->_init2(func_get_args());
249 function sup (/*...*/) {
250 $el = new HtmlElement('sup');
251 return $el->_init2(func_get_args());
253 function sub (/*...*/) {
254 $el = new HtmlElement('sub');
255 return $el->_init2(func_get_args());
258 /****************************************/
259 function ul (/*...*/) {
260 $el = new HtmlElement('ul');
261 return $el->_init2(func_get_args());
263 function ol (/*...*/) {
264 $el = new HtmlElement('ol');
265 return $el->_init2(func_get_args());
267 function dl (/*...*/) {
268 $el = new HtmlElement('dl');
269 return $el->_init2(func_get_args());
271 function li (/*...*/) {
272 $el = new HtmlElement('li');
273 return $el->_init2(func_get_args());
275 function dt (/*...*/) {
276 $el = new HtmlElement('dt');
277 return $el->_init2(func_get_args());
279 function dd (/*...*/) {
280 $el = new HtmlElement('dd');
281 return $el->_init2(func_get_args());
284 /****************************************/
285 function table (/*...*/) {
286 $el = new HtmlElement('table');
287 return $el->_init2(func_get_args());
289 function caption (/*...*/) {
290 $el = new HtmlElement('caption');
291 return $el->_init2(func_get_args());
293 function thead (/*...*/) {
294 $el = new HtmlElement('thead');
295 return $el->_init2(func_get_args());
297 function tbody (/*...*/) {
298 $el = new HtmlElement('tbody');
299 return $el->_init2(func_get_args());
301 function tfoot (/*...*/) {
302 $el = new HtmlElement('tfoot');
303 return $el->_init2(func_get_args());
305 function tr (/*...*/) {
306 $el = new HtmlElement('tr');
307 return $el->_init2(func_get_args());
309 function td (/*...*/) {
310 $el = new HtmlElement('td');
311 return $el->_init2(func_get_args());
313 function th (/*...*/) {
314 $el = new HtmlElement('th');
315 return $el->_init2(func_get_args());
318 /****************************************/
319 function form (/*...*/) {
320 $el = new HtmlElement('form');
321 return $el->_init2(func_get_args());
323 function input (/*...*/) {
324 $el = new HtmlElement('input');
325 return $el->_init2(func_get_args());
327 function option (/*...*/) {
328 $el = new HtmlElement('option');
329 return $el->_init2(func_get_args());
331 function select (/*...*/) {
332 $el = new HtmlElement('select');
333 return $el->_init2(func_get_args());
335 function textarea (/*...*/) {
336 $el = new HtmlElement('textarea');
337 return $el->_init2(func_get_args());
340 /****************************************/
341 function area (/*...*/) {
342 $el = new HtmlElement('area');
343 return $el->_init2(func_get_args());
345 function map (/*...*/) {
346 $el = new HtmlElement('map');
347 return $el->_init2(func_get_args());
349 function frame (/*...*/) {
350 $el = new HtmlElement('frame');
351 return $el->_init2(func_get_args());
353 function frameset (/*...*/) {
354 $el = new HtmlElement('frameset');
355 return $el->_init2(func_get_args());
357 function iframe (/*...*/) {
358 $el = new HtmlElement('iframe');
359 return $el->_init2(func_get_args());
361 function nobody (/*...*/) {
362 $el = new HtmlElement('nobody');
363 return $el->_init2(func_get_args());
367 define('HTMLTAG_EMPTY', 1);
368 define('HTMLTAG_INLINE', 2);
369 define('HTMLTAG_ACCEPTS_INLINE', 4);
372 HTML::_setTagProperty(HTMLTAG_EMPTY,
373 'area base basefont br col frame hr img input isindex link meta param');
374 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
376 'b big i small tt ' // %fontstyle
377 . 's strike u ' // (deprecated)
378 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
379 . 'a img object br script map q sub sup span bdo '//%special
380 . 'button input label option select textarea ' //%formctl
382 // %block elements which contain inline content
383 . 'address h1 h2 h3 h4 h5 h6 p pre '
384 // %block elements which contain either block or inline content
385 . 'div fieldset frameset'
387 // other with inline content
388 . 'caption dt label legend '
389 // other with either inline or block
390 . 'dd del ins li td th ');
392 HTML::_setTagProperty(HTMLTAG_INLINE,
394 'b big i small tt ' // %fontstyle
395 . 's strike u ' // (deprecated)
396 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
397 . 'a img object br script map q sub sup span bdo '//%special
398 . 'button input label option select textarea ' //%formctl
403 * Generate hidden form input fields.
405 * @param $query_args hash A hash mapping names to values for the hidden inputs.
406 * Values in the hash can themselves be hashes. The will result in hidden inputs
407 * which will reconstruct the nested structure in the resulting query args as
412 * $args = array('x' => '2',
413 * 'y' => array('a' => 'aval', 'b' => 'bval'));
414 * $inputs = HiddenInputs($args);
418 * <input type="hidden" name="x" value = "2" />
419 * <input type="hidden" name="y[a]" value = "aval" />
420 * <input type="hidden" name="y[b]" value = "bval" />
422 * @return object An XmlContent object containing the inputs.
424 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
427 foreach ($query_args as $key => $val) {
428 if (in_array($key,$exclude)) continue;
429 $name = $pfx ? $pfx . "[$key]" : $key;
431 $inputs->pushContent(HiddenInputs($val, $name));
433 $inputs->pushContent(HTML::input(array('type' => 'hidden',
440 function HiddenGets ($exclude = array()) {
441 global $HTTP_GET_VARS;
442 HiddenInputs($HTTP_GET_VARS, false, $exclude);
445 function HiddenPosts ($exclude = array()) {
446 global $HTTP_POST_VARS;
447 HiddenInputs($HTTP_POST_VARS, false, $exclude);
450 // (c-file-style: "gnu")
455 // c-hanging-comment-ender-p: nil
456 // indent-tabs-mode: nil