1 <?php rcs_id('$Id: HtmlElement.php,v 1.24 2002-09-02 12:39:02 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 define('NBSP', "\xA0"); // iso-8859-x non-breaking space.
79 class HTML extends HtmlElement {
80 function raw ($html_text) {
81 return new RawXML($html_text);
84 function getTagProperties($tag) {
85 $props = &$GLOBALS['HTML_TagProperties'];
86 return isset($props[$tag]) ? $props[$tag] : 0;
89 function _setTagProperty($prop_flag, $tags) {
90 $props = &$GLOBALS['HTML_TagProperties'];
92 $tags = preg_split('/\s+/', $tags);
93 foreach ($tags as $tag) {
94 if (isset($props[$tag]))
95 $props[$tag] |= $prop_flag;
97 $props[$tag] = $prop_flag;
102 // Shell script to generate the following static methods:
105 // function mkfuncs () {
108 // echo " function $tag (/*...*/) {"
109 // echo " \$el = new HtmlElement('$tag');"
110 // echo " return \$el->_init2(func_get_args());"
115 // /****************************************/'
116 // mkfuncs link style script noscript
118 // mkfuncs a img br span
120 // mkfuncs h1 h2 h3 h4 h5 h6
122 // mkfuncs hr div p pre blockquote
124 // mkfuncs em strong small
126 // mkfuncs tt u sup sub
128 // mkfuncs ul ol dl li dt dd
130 // mkfuncs table caption thead tbody tfoot tr td th
132 // mkfuncs form input option select textarea
134 // mkfuncs area map frame frameset nobody
136 function link (/*...*/) {
137 $el = new HtmlElement('link');
138 return $el->_init2(func_get_args());
140 function style (/*...*/) {
141 $el = new HtmlElement('style');
142 return $el->_init2(func_get_args());
144 function script (/*...*/) {
145 $el = new HtmlElement('script');
146 return $el->_init2(func_get_args());
148 function noscript (/*...*/) {
149 $el = new HtmlElement('noscript');
150 return $el->_init2(func_get_args());
153 /****************************************/
154 function a (/*...*/) {
155 $el = new HtmlElement('a');
156 return $el->_init2(func_get_args());
158 function img (/*...*/) {
159 $el = new HtmlElement('img');
160 return $el->_init2(func_get_args());
162 function br (/*...*/) {
163 $el = new HtmlElement('br');
164 return $el->_init2(func_get_args());
166 function span (/*...*/) {
167 $el = new HtmlElement('span');
168 return $el->_init2(func_get_args());
171 /****************************************/
172 function h1 (/*...*/) {
173 $el = new HtmlElement('h1');
174 return $el->_init2(func_get_args());
176 function h2 (/*...*/) {
177 $el = new HtmlElement('h2');
178 return $el->_init2(func_get_args());
180 function h3 (/*...*/) {
181 $el = new HtmlElement('h3');
182 return $el->_init2(func_get_args());
184 function h4 (/*...*/) {
185 $el = new HtmlElement('h4');
186 return $el->_init2(func_get_args());
188 function h5 (/*...*/) {
189 $el = new HtmlElement('h5');
190 return $el->_init2(func_get_args());
192 function h6 (/*...*/) {
193 $el = new HtmlElement('h6');
194 return $el->_init2(func_get_args());
197 /****************************************/
198 function hr (/*...*/) {
199 $el = new HtmlElement('hr');
200 return $el->_init2(func_get_args());
202 function div (/*...*/) {
203 $el = new HtmlElement('div');
204 return $el->_init2(func_get_args());
206 function p (/*...*/) {
207 $el = new HtmlElement('p');
208 return $el->_init2(func_get_args());
210 function pre (/*...*/) {
211 $el = new HtmlElement('pre');
212 return $el->_init2(func_get_args());
214 function blockquote (/*...*/) {
215 $el = new HtmlElement('blockquote');
216 return $el->_init2(func_get_args());
219 /****************************************/
220 function em (/*...*/) {
221 $el = new HtmlElement('em');
222 return $el->_init2(func_get_args());
224 function strong (/*...*/) {
225 $el = new HtmlElement('strong');
226 return $el->_init2(func_get_args());
228 function small (/*...*/) {
229 $el = new HtmlElement('small');
230 return $el->_init2(func_get_args());
233 /****************************************/
234 function tt (/*...*/) {
235 $el = new HtmlElement('tt');
236 return $el->_init2(func_get_args());
238 function u (/*...*/) {
239 $el = new HtmlElement('u');
240 return $el->_init2(func_get_args());
242 function sup (/*...*/) {
243 $el = new HtmlElement('sup');
244 return $el->_init2(func_get_args());
246 function sub (/*...*/) {
247 $el = new HtmlElement('sub');
248 return $el->_init2(func_get_args());
251 /****************************************/
252 function ul (/*...*/) {
253 $el = new HtmlElement('ul');
254 return $el->_init2(func_get_args());
256 function ol (/*...*/) {
257 $el = new HtmlElement('ol');
258 return $el->_init2(func_get_args());
260 function dl (/*...*/) {
261 $el = new HtmlElement('dl');
262 return $el->_init2(func_get_args());
264 function li (/*...*/) {
265 $el = new HtmlElement('li');
266 return $el->_init2(func_get_args());
268 function dt (/*...*/) {
269 $el = new HtmlElement('dt');
270 return $el->_init2(func_get_args());
272 function dd (/*...*/) {
273 $el = new HtmlElement('dd');
274 return $el->_init2(func_get_args());
277 /****************************************/
278 function table (/*...*/) {
279 $el = new HtmlElement('table');
280 return $el->_init2(func_get_args());
282 function caption (/*...*/) {
283 $el = new HtmlElement('caption');
284 return $el->_init2(func_get_args());
286 function thead (/*...*/) {
287 $el = new HtmlElement('thead');
288 return $el->_init2(func_get_args());
290 function tbody (/*...*/) {
291 $el = new HtmlElement('tbody');
292 return $el->_init2(func_get_args());
294 function tfoot (/*...*/) {
295 $el = new HtmlElement('tfoot');
296 return $el->_init2(func_get_args());
298 function tr (/*...*/) {
299 $el = new HtmlElement('tr');
300 return $el->_init2(func_get_args());
302 function td (/*...*/) {
303 $el = new HtmlElement('td');
304 return $el->_init2(func_get_args());
306 function th (/*...*/) {
307 $el = new HtmlElement('th');
308 return $el->_init2(func_get_args());
311 /****************************************/
312 function form (/*...*/) {
313 $el = new HtmlElement('form');
314 return $el->_init2(func_get_args());
316 function input (/*...*/) {
317 $el = new HtmlElement('input');
318 return $el->_init2(func_get_args());
320 function option (/*...*/) {
321 $el = new HtmlElement('option');
322 return $el->_init2(func_get_args());
324 function select (/*...*/) {
325 $el = new HtmlElement('select');
326 return $el->_init2(func_get_args());
328 function textarea (/*...*/) {
329 $el = new HtmlElement('textarea');
330 return $el->_init2(func_get_args());
333 /****************************************/
334 function area (/*...*/) {
335 $el = new HtmlElement('area');
336 return $el->_init2(func_get_args());
338 function map (/*...*/) {
339 $el = new HtmlElement('map');
340 return $el->_init2(func_get_args());
342 function frame (/*...*/) {
343 $el = new HtmlElement('frame');
344 return $el->_init2(func_get_args());
346 function frameset (/*...*/) {
347 $el = new HtmlElement('frameset');
348 return $el->_init2(func_get_args());
350 function nobody (/*...*/) {
351 $el = new HtmlElement('nobody');
352 return $el->_init2(func_get_args());
356 define('HTMLTAG_EMPTY', 1);
357 define('HTMLTAG_INLINE', 2);
358 define('HTMLTAG_ACCEPTS_INLINE', 4);
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,
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
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'
376 // other with inline content
377 . 'caption dt label legend '
378 // other with either inline or block
379 . 'dd del ins li td th ');
381 HTML::_setTagProperty(HTMLTAG_INLINE,
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
392 * Generate hidden form input fields.
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
401 * $args = array('x' => '2',
402 * 'y' => array('a' => 'aval', 'b' => 'bval'));
403 * $inputs = HiddenInputs($args);
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" />
411 * @return object An XmlContent object containing the inputs.
413 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
416 foreach ($query_args as $key => $val) {
417 if (in_array($key,$exclude)) continue;
418 $name = $pfx ? $pfx . "[$key]" : $key;
420 $inputs->pushContent(HiddenInputs($val, $name));
422 $inputs->pushContent(HTML::input(array('type' => 'hidden',
429 function HiddenGets ($exclude = array()) {
430 global $HTTP_GET_VARS;
431 HiddenInputs($HTTP_GET_VARS, false, $exclude);
434 function HiddenPosts ($exclude = array()) {
435 global $HTTP_POST_VARS;
436 HiddenInputs($HTTP_POST_VARS, false, $exclude);
439 // (c-file-style: "gnu")
444 // c-hanging-comment-ender-p: nil
445 // indent-tabs-mode: nil