1 <?php rcs_id('$Id: HtmlElement.php,v 1.12 2002-01-26 01:51:13 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 () {
58 elseif (($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 ($tag /* , ... */) {
74 $el = new HtmlElement($tag);
75 if (func_num_args() > 1)
76 $el->_init(func_get_args());
80 define('NBSP', "\xA0"); // iso-8859-x non-breaking space.
82 class HTML extends HtmlElement {
83 function raw ($html_text) {
84 return new RawXML($html_text);
87 function getTagProperties($tag) {
88 $props = &$GLOBALS['HTML_TagProperties'];
89 return isset($props[$tag]) ? $props[$tag] : 0;
92 function _setTagProperty($prop_flag, $tags) {
93 $props = &$GLOBALS['HTML_TagProperties'];
95 $tags = preg_split('/\s+/', $tags);
96 foreach ($tags as $tag) {
97 if (isset($props[$tag]))
98 $props[$tag] |= $prop_flag;
100 $props[$tag] = $prop_flag;
105 // Shell script to generate the following static methods:
107 // function mkfuncs () {
110 // echo " function $tag (/*...*/) {"
111 // echo " \$el = new HtmlElement('$tag');"
112 // echo " return \$el->_init2(func_get_args());"
116 // mkfuncs link style script noscript
117 // mkfuncs a img br span
118 // mkfuncs h1 h2 h3 h4 h5 h6
119 // mkfuncs hr div p pre blockquote
120 // mkfuncs em strong small
121 // mkfuncs tt u sup sub
122 // mkfuncs ul ol dl li dt dd
123 // mkfuncs table caption thead tbody tfoot tr td th
124 // mkfuncs form input
125 function link (/*...*/) {
126 $el = new HtmlElement('link');
127 return $el->_init2(func_get_args());
129 function style (/*...*/) {
130 $el = new HtmlElement('style');
131 return $el->_init2(func_get_args());
133 function script (/*...*/) {
134 $el = new HtmlElement('script');
135 return $el->_init2(func_get_args());
137 function noscript (/*...*/) {
138 $el = new HtmlElement('noscript');
139 return $el->_init2(func_get_args());
141 function a (/*...*/) {
142 $el = new HtmlElement('a');
143 return $el->_init2(func_get_args());
145 function img (/*...*/) {
146 $el = new HtmlElement('img');
147 return $el->_init2(func_get_args());
149 function br (/*...*/) {
150 $el = new HtmlElement('br');
151 return $el->_init2(func_get_args());
153 function span (/*...*/) {
154 $el = new HtmlElement('span');
155 return $el->_init2(func_get_args());
157 function h1 (/*...*/) {
158 $el = new HtmlElement('h1');
159 return $el->_init2(func_get_args());
161 function h2 (/*...*/) {
162 $el = new HtmlElement('h2');
163 return $el->_init2(func_get_args());
165 function h3 (/*...*/) {
166 $el = new HtmlElement('h3');
167 return $el->_init2(func_get_args());
169 function h4 (/*...*/) {
170 $el = new HtmlElement('h4');
171 return $el->_init2(func_get_args());
173 function h5 (/*...*/) {
174 $el = new HtmlElement('h5');
175 return $el->_init2(func_get_args());
177 function h6 (/*...*/) {
178 $el = new HtmlElement('h6');
179 return $el->_init2(func_get_args());
181 function hr (/*...*/) {
182 $el = new HtmlElement('hr');
183 return $el->_init2(func_get_args());
185 function div (/*...*/) {
186 $el = new HtmlElement('div');
187 return $el->_init2(func_get_args());
189 function p (/*...*/) {
190 $el = new HtmlElement('p');
191 return $el->_init2(func_get_args());
193 function pre (/*...*/) {
194 $el = new HtmlElement('pre');
195 return $el->_init2(func_get_args());
197 function blockquote (/*...*/) {
198 $el = new HtmlElement('blockquote');
199 return $el->_init2(func_get_args());
201 function em (/*...*/) {
202 $el = new HtmlElement('em');
203 return $el->_init2(func_get_args());
205 function strong (/*...*/) {
206 $el = new HtmlElement('strong');
207 return $el->_init2(func_get_args());
209 function small (/*...*/) {
210 $el = new HtmlElement('small');
211 return $el->_init2(func_get_args());
213 function tt (/*...*/) {
214 $el = new HtmlElement('tt');
215 return $el->_init2(func_get_args());
217 function u (/*...*/) {
218 $el = new HtmlElement('u');
219 return $el->_init2(func_get_args());
221 function sup (/*...*/) {
222 $el = new HtmlElement('sup');
223 return $el->_init2(func_get_args());
225 function sub (/*...*/) {
226 $el = new HtmlElement('sub');
227 return $el->_init2(func_get_args());
229 function ul (/*...*/) {
230 $el = new HtmlElement('ul');
231 return $el->_init2(func_get_args());
233 function ol (/*...*/) {
234 $el = new HtmlElement('ol');
235 return $el->_init2(func_get_args());
237 function dl (/*...*/) {
238 $el = new HtmlElement('dl');
239 return $el->_init2(func_get_args());
241 function li (/*...*/) {
242 $el = new HtmlElement('li');
243 return $el->_init2(func_get_args());
245 function dt (/*...*/) {
246 $el = new HtmlElement('dt');
247 return $el->_init2(func_get_args());
249 function dd (/*...*/) {
250 $el = new HtmlElement('dd');
251 return $el->_init2(func_get_args());
253 function table (/*...*/) {
254 $el = new HtmlElement('table');
255 return $el->_init2(func_get_args());
257 function caption (/*...*/) {
258 $el = new HtmlElement('caption');
259 return $el->_init2(func_get_args());
261 function thead (/*...*/) {
262 $el = new HtmlElement('thead');
263 return $el->_init2(func_get_args());
265 function tbody (/*...*/) {
266 $el = new HtmlElement('tbody');
267 return $el->_init2(func_get_args());
269 function tfoot (/*...*/) {
270 $el = new HtmlElement('tfoot');
271 return $el->_init2(func_get_args());
273 function tr (/*...*/) {
274 $el = new HtmlElement('tr');
275 return $el->_init2(func_get_args());
277 function td (/*...*/) {
278 $el = new HtmlElement('td');
279 return $el->_init2(func_get_args());
281 function th (/*...*/) {
282 $el = new HtmlElement('th');
283 return $el->_init2(func_get_args());
285 function form (/*...*/) {
286 $el = new HtmlElement('form');
287 return $el->_init2(func_get_args());
289 function input (/*...*/) {
290 $el = new HtmlElement('input');
291 return $el->_init2(func_get_args());
295 define('HTMLTAG_EMPTY', 1);
296 define('HTMLTAG_INLINE', 2);
297 define('HTMLTAG_ACCEPTS_INLINE', 4);
300 HTML::_setTagProperty(HTMLTAG_EMPTY,
301 'area base basefont br col frame hr img input isindex link meta param');
302 HTML::_setTagProperty(HTMLTAG_ACCEPTS_INLINE,
304 'b big i small tt ' // %fontstyle
305 . 's strike u ' // (deprecated)
306 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
307 . 'a img object br script map q sub sup span bdo '//%special
308 . 'button input label select textarea ' //%formctl
310 // %block elements which contain inline content
311 . 'address h1 h2 h3 h4 h5 h6 p pre '
312 // %block elements which contain either block or inline content
315 // other with inline content
316 . 'caption dt label legend '
317 // other with either inline or block
318 . 'dd del ins li td th ');
320 HTML::_setTagProperty(HTMLTAG_INLINE,
322 'b big i small tt ' // %fontstyle
323 . 's strike u ' // (deprecated)
324 . 'abbr acronym cite code dfn em kbd samp strong var ' //%phrase
325 . 'a img object br script map q sub sup span bdo '//%special
326 . 'button input label select textarea ' //%formctl
330 // (c-file-style: "gnu")
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil