1 <?php rcs_id('$Id: HtmlElement.php,v 1.14 2002-01-30 00:43:07 dairiki 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) {
28 if ($args && is_array($args[0]))
29 $this->_attr = array_shift($args);
30 elseif (count($args) > 1 && ! $args[0])
33 if (count($args) == 1 && is_array($args[0]))
35 $this->_content = $args;
39 /** Add a "tooltip" to an element.
41 * @param $tooltip_text string The tooltip text.
43 function addTooltip ($tooltip_text) {
44 $this->setAttr('title', $tooltip_text);
46 // FIXME: this should be initialized from title by an onLoad() function.
47 // (though, that may not be possible.)
48 $qtooltip = str_replace("'", "\\'", $tooltip_text);
49 $this->setAttr('onmouseover',
50 sprintf('window.status="%s"; return true;',
51 addslashes($tooltip_text)));
52 $this->setAttr('onmouseout', "window.status='';return true;");
55 function emptyTag () {
56 if (($this->_properties & HTMLTAG_EMPTY) == 0)
57 return $this->startTag() . "</$this->_tag>";
59 return substr($this->startTag(), 0, -1) . " />";
62 function hasInlineContent () {
63 return ($this->_properties & HTMLTAG_ACCEPTS_INLINE) != 0;
66 function isInlineElement () {
67 return ($this->_properties & HTMLTAG_INLINE) != 0;
71 function HTML (/* $content, ... */) {
72 return new XmlContent(func_get_args());
75 define('NBSP', "\xA0"); // iso-8859-x non-breaking space.
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:
102 // function mkfuncs () {
105 // echo " function $tag (/*...*/) {"
106 // echo " \$el = new HtmlElement('$tag');"
107 // echo " return \$el->_init2(func_get_args());"
111 // mkfuncs link style script noscript
112 // mkfuncs a img br span
113 // mkfuncs h1 h2 h3 h4 h5 h6
114 // mkfuncs hr div p pre blockquote
115 // mkfuncs em strong small
116 // mkfuncs tt u sup sub
117 // mkfuncs ul ol dl li dt dd
118 // mkfuncs table caption thead tbody tfoot tr td th
119 // mkfuncs form input textarea
120 function link (/*...*/) {
121 $el = new HtmlElement('link');
122 return $el->_init2(func_get_args());
124 function style (/*...*/) {
125 $el = new HtmlElement('style');
126 return $el->_init2(func_get_args());
128 function script (/*...*/) {
129 $el = new HtmlElement('script');
130 return $el->_init2(func_get_args());
132 function noscript (/*...*/) {
133 $el = new HtmlElement('noscript');
134 return $el->_init2(func_get_args());
136 function a (/*...*/) {
137 $el = new HtmlElement('a');
138 return $el->_init2(func_get_args());
140 function img (/*...*/) {
141 $el = new HtmlElement('img');
142 return $el->_init2(func_get_args());
144 function br (/*...*/) {
145 $el = new HtmlElement('br');
146 return $el->_init2(func_get_args());
148 function span (/*...*/) {
149 $el = new HtmlElement('span');
150 return $el->_init2(func_get_args());
152 function h1 (/*...*/) {
153 $el = new HtmlElement('h1');
154 return $el->_init2(func_get_args());
156 function h2 (/*...*/) {
157 $el = new HtmlElement('h2');
158 return $el->_init2(func_get_args());
160 function h3 (/*...*/) {
161 $el = new HtmlElement('h3');
162 return $el->_init2(func_get_args());
164 function h4 (/*...*/) {
165 $el = new HtmlElement('h4');
166 return $el->_init2(func_get_args());
168 function h5 (/*...*/) {
169 $el = new HtmlElement('h5');
170 return $el->_init2(func_get_args());
172 function h6 (/*...*/) {
173 $el = new HtmlElement('h6');
174 return $el->_init2(func_get_args());
176 function hr (/*...*/) {
177 $el = new HtmlElement('hr');
178 return $el->_init2(func_get_args());
180 function div (/*...*/) {
181 $el = new HtmlElement('div');
182 return $el->_init2(func_get_args());
184 function p (/*...*/) {
185 $el = new HtmlElement('p');
186 return $el->_init2(func_get_args());
188 function pre (/*...*/) {
189 $el = new HtmlElement('pre');
190 return $el->_init2(func_get_args());
192 function blockquote (/*...*/) {
193 $el = new HtmlElement('blockquote');
194 return $el->_init2(func_get_args());
196 function em (/*...*/) {
197 $el = new HtmlElement('em');
198 return $el->_init2(func_get_args());
200 function strong (/*...*/) {
201 $el = new HtmlElement('strong');
202 return $el->_init2(func_get_args());
204 function small (/*...*/) {
205 $el = new HtmlElement('small');
206 return $el->_init2(func_get_args());
208 function tt (/*...*/) {
209 $el = new HtmlElement('tt');
210 return $el->_init2(func_get_args());
212 function u (/*...*/) {
213 $el = new HtmlElement('u');
214 return $el->_init2(func_get_args());
216 function sup (/*...*/) {
217 $el = new HtmlElement('sup');
218 return $el->_init2(func_get_args());
220 function sub (/*...*/) {
221 $el = new HtmlElement('sub');
222 return $el->_init2(func_get_args());
224 function ul (/*...*/) {
225 $el = new HtmlElement('ul');
226 return $el->_init2(func_get_args());
228 function ol (/*...*/) {
229 $el = new HtmlElement('ol');
230 return $el->_init2(func_get_args());
232 function dl (/*...*/) {
233 $el = new HtmlElement('dl');
234 return $el->_init2(func_get_args());
236 function li (/*...*/) {
237 $el = new HtmlElement('li');
238 return $el->_init2(func_get_args());
240 function dt (/*...*/) {
241 $el = new HtmlElement('dt');
242 return $el->_init2(func_get_args());
244 function dd (/*...*/) {
245 $el = new HtmlElement('dd');
246 return $el->_init2(func_get_args());
248 function table (/*...*/) {
249 $el = new HtmlElement('table');
250 return $el->_init2(func_get_args());
252 function caption (/*...*/) {
253 $el = new HtmlElement('caption');
254 return $el->_init2(func_get_args());
256 function thead (/*...*/) {
257 $el = new HtmlElement('thead');
258 return $el->_init2(func_get_args());
260 function tbody (/*...*/) {
261 $el = new HtmlElement('tbody');
262 return $el->_init2(func_get_args());
264 function tfoot (/*...*/) {
265 $el = new HtmlElement('tfoot');
266 return $el->_init2(func_get_args());
268 function tr (/*...*/) {
269 $el = new HtmlElement('tr');
270 return $el->_init2(func_get_args());
272 function td (/*...*/) {
273 $el = new HtmlElement('td');
274 return $el->_init2(func_get_args());
276 function th (/*...*/) {
277 $el = new HtmlElement('th');
278 return $el->_init2(func_get_args());
280 function form (/*...*/) {
281 $el = new HtmlElement('form');
282 return $el->_init2(func_get_args());
284 function input (/*...*/) {
285 $el = new HtmlElement('input');
286 return $el->_init2(func_get_args());
288 function textarea (/*...*/) {
289 $el = new HtmlElement('textarea');
290 return $el->_init2(func_get_args());
294 define('HTMLTAG_EMPTY', 1);
295 define('HTMLTAG_INLINE', 2);
296 define('HTMLTAG_ACCEPTS_INLINE', 4);
299 HTML::_setTagProperty(HTMLTAG_EMPTY,
300 'area base basefont br col frame hr img input isindex link meta param');
301 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
303 'b big i small tt ' // %fontstyle
304 . 's strike u ' // (deprecated)
305 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
306 . 'a img object br script map q sub sup span bdo '//%special
307 . 'button input label select textarea ' //%formctl
309 // %block elements which contain inline content
310 . 'address h1 h2 h3 h4 h5 h6 p pre '
311 // %block elements which contain either block or inline content
314 // other with inline content
315 . 'caption dt label legend '
316 // other with either inline or block
317 . 'dd del ins li td th ');
319 HTML::_setTagProperty(HTMLTAG_INLINE,
321 'b big i small tt ' // %fontstyle
322 . 's strike u ' // (deprecated)
323 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
324 . 'a img object br script map q sub sup span bdo '//%special
325 . 'button input label select textarea ' //%formctl
329 * Generate hidden form input fields.
331 * @param $query_args hash A hash mapping names to values for the hidden inputs.
332 * Values in the hash can themselves be hashes. The will result in hidden inputs
333 * which will reconstruct the nested structure in the resulting query args (as
338 * $args = array('x' => '2',
339 * 'y' => array('a' => 'aval', 'b' => 'bval'));
340 * $inputs = HiddenInputs($args);
344 * <input type="hidden" name="x" value = "2" />
345 * <input type="hidden" name="y[a]" value = "aval" />
346 * <input type="hidden" name="y[b]" value = "bval" />
348 * @return object An XmlContent object containing the inputs.
350 function HiddenInputs ($query_args, $pfx = false) {
353 foreach ($query_args as $key => $val) {
354 $name = $pfx ? $pfx . "[$key]" : $key;
356 $inputs->pushContent(HiddenInputs($val, $name));
358 $inputs->pushContent(HTML::input(array('type' => 'hidden',
365 // (c-file-style: "gnu")
370 // c-hanging-comment-ender-p: nil
371 // indent-tabs-mode: nil