1 <?php rcs_id('$Id: HtmlElement.php,v 1.21 2002-08-19 11:32:29 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 area map
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());
330 function area (/*...*/) {
331 $el = new HtmlElement('area');
332 return $el->_init2(func_get_args());
334 function map (/*...*/) {
335 $el = new HtmlElement('map');
336 return $el->_init2(func_get_args());
340 define('HTMLTAG_EMPTY', 1);
341 define('HTMLTAG_INLINE', 2);
342 define('HTMLTAG_ACCEPTS_INLINE', 4);
345 HTML::_setTagProperty(HTMLTAG_EMPTY,
346 'area base basefont br col frame hr img input isindex link meta param');
347 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
349 'b big i small tt ' // %fontstyle
350 . 's strike u ' // (deprecated)
351 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
352 . 'a img object br script map q sub sup span bdo '//%special
353 . 'button input label option select textarea ' //%formctl
355 // %block elements which contain inline content
356 . 'address h1 h2 h3 h4 h5 h6 p pre '
357 // %block elements which contain either block or inline content
360 // other with inline content
361 . 'caption dt label legend '
362 // other with either inline or block
363 . 'dd del ins li td th ');
365 HTML::_setTagProperty(HTMLTAG_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
375 * Generate hidden form input fields.
377 * @param $query_args hash A hash mapping names to values for the hidden inputs.
378 * Values in the hash can themselves be hashes. The will result in hidden inputs
379 * which will reconstruct the nested structure in the resulting query args as
384 * $args = array('x' => '2',
385 * 'y' => array('a' => 'aval', 'b' => 'bval'));
386 * $inputs = HiddenInputs($args);
390 * <input type="hidden" name="x" value = "2" />
391 * <input type="hidden" name="y[a]" value = "aval" />
392 * <input type="hidden" name="y[b]" value = "bval" />
394 * @return object An XmlContent object containing the inputs.
396 function HiddenInputs ($query_args, $pfx = false, $exclude = array()) {
399 foreach ($query_args as $key => $val) {
400 if (in_array($key,$exclude)) continue;
401 $name = $pfx ? $pfx . "[$key]" : $key;
403 $inputs->pushContent(HiddenInputs($val, $name));
405 $inputs->pushContent(HTML::input(array('type' => 'hidden',
412 // (c-file-style: "gnu")
417 // c-hanging-comment-ender-p: nil
418 // indent-tabs-mode: nil