]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/HtmlElement.php
New markup code is now more or less working.
[SourceForge/phpwiki.git] / lib / HtmlElement.php
1 <?php rcs_id('$Id: HtmlElement.php,v 1.12 2002-01-26 01:51:13 dairiki Exp $');
2 /*
3  * Code for writing XML.
4  */
5 require_once("lib/XmlElement.php");
6 /**
7  * An XML element.
8  */
9
10 class HtmlElement extends XmlElement
11 {
12     //function HtmlElement ($tagname /* , $attr_or_content , ...*/) {
13     //    $this->_init(func_get_args());
14     //    $this->_properties = HTML::getTagProperties($tagname);
15     //} 
16     
17
18     function _init ($args) {
19         XmlElement::_init($args);
20         $this->_properties = HTML::getTagProperties($this->_tag);
21     }
22         
23     /**
24      * @access protected
25      * This is used by the static factory methods is class HTML.
26      */
27     function _init2 ($args) {
28         if ($args && is_array($args[0]))
29             $this->_attr = array_shift($args);
30         elseif (count($args) > 1 && ! $args[0])
31             array_shift($args);
32
33         if (count($args) == 1 && is_array($args[0]))
34             $args = $args[0];
35         $this->_content = $args;
36         return $this;
37     }
38         
39     /** Add a "tooltip" to an element.
40      *
41      * @param $tooltip_text string The tooltip text.
42      */
43     function addTooltip ($tooltip_text) {
44         $this->setAttr('title', $tooltip_text);
45
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;");
53     }
54
55     function _emptyTag () {
56         if (!$this->_tag)
57             return '';
58         elseif (($this->_properties & HTMLTAG_EMPTY) == 0)
59             return $this->_startTag() . "</$this->_tag>";
60
61         return substr($this->_startTag(), 0, -1) . " />";
62     }
63
64     function hasInlineContent () {
65         return ($this->_properties & HTMLTAG_ACCEPTS_INLINE) != 0;
66     }
67
68     function isInlineElement () {
69         return ($this->_properties & HTMLTAG_INLINE) != 0;
70     }
71 };
72
73 function HTML ($tag /* , ... */) {
74     $el = new HtmlElement($tag);
75     if (func_num_args() > 1)
76         $el->_init(func_get_args());
77     return $el;
78 }
79
80 define('NBSP', "\xA0");         // iso-8859-x non-breaking space.
81
82 class HTML extends HtmlElement {
83     function raw ($html_text) {
84         return new RawXML($html_text);
85     }
86
87     function getTagProperties($tag) {
88         $props = &$GLOBALS['HTML_TagProperties'];
89         return isset($props[$tag]) ? $props[$tag] : 0;
90     }
91     
92     function _setTagProperty($prop_flag, $tags) {
93         $props = &$GLOBALS['HTML_TagProperties'];
94         if (is_string($tags))
95             $tags = preg_split('/\s+/', $tags);
96         foreach ($tags as $tag) {
97             if (isset($props[$tag]))
98                 $props[$tag] |= $prop_flag;
99             else
100                 $props[$tag] = $prop_flag;
101         }
102     }
103
104     //
105     // Shell script to generate the following static methods:
106     //
107     // function mkfuncs () {
108     //     for tag in "$@"
109     //     do
110     //         echo "    function $tag (/*...*/) {"
111     //         echo "        \$el = new HtmlElement('$tag');"
112     //         echo "        return \$el->_init2(func_get_args());"
113     //         echo "    }"
114     //     done
115     // }
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());
128     }
129     function style (/*...*/) {
130         $el = new HtmlElement('style');
131         return $el->_init2(func_get_args());
132     }
133     function script (/*...*/) {
134         $el = new HtmlElement('script');
135         return $el->_init2(func_get_args());
136     }
137     function noscript (/*...*/) {
138         $el = new HtmlElement('noscript');
139         return $el->_init2(func_get_args());
140     }
141     function a (/*...*/) {
142         $el = new HtmlElement('a');
143         return $el->_init2(func_get_args());
144     }
145     function img (/*...*/) {
146         $el = new HtmlElement('img');
147         return $el->_init2(func_get_args());
148     }
149     function br (/*...*/) {
150         $el = new HtmlElement('br');
151         return $el->_init2(func_get_args());
152     }
153     function span (/*...*/) {
154         $el = new HtmlElement('span');
155         return $el->_init2(func_get_args());
156     }
157     function h1 (/*...*/) {
158         $el = new HtmlElement('h1');
159         return $el->_init2(func_get_args());
160     }
161     function h2 (/*...*/) {
162         $el = new HtmlElement('h2');
163         return $el->_init2(func_get_args());
164     }
165     function h3 (/*...*/) {
166         $el = new HtmlElement('h3');
167         return $el->_init2(func_get_args());
168     }
169     function h4 (/*...*/) {
170         $el = new HtmlElement('h4');
171         return $el->_init2(func_get_args());
172     }
173     function h5 (/*...*/) {
174         $el = new HtmlElement('h5');
175         return $el->_init2(func_get_args());
176     }
177     function h6 (/*...*/) {
178         $el = new HtmlElement('h6');
179         return $el->_init2(func_get_args());
180     }
181     function hr (/*...*/) {
182         $el = new HtmlElement('hr');
183         return $el->_init2(func_get_args());
184     }
185     function div (/*...*/) {
186         $el = new HtmlElement('div');
187         return $el->_init2(func_get_args());
188     }
189     function p (/*...*/) {
190         $el = new HtmlElement('p');
191         return $el->_init2(func_get_args());
192     }
193     function pre (/*...*/) {
194         $el = new HtmlElement('pre');
195         return $el->_init2(func_get_args());
196     }
197     function blockquote (/*...*/) {
198         $el = new HtmlElement('blockquote');
199         return $el->_init2(func_get_args());
200     }
201     function em (/*...*/) {
202         $el = new HtmlElement('em');
203         return $el->_init2(func_get_args());
204     }
205     function strong (/*...*/) {
206         $el = new HtmlElement('strong');
207         return $el->_init2(func_get_args());
208     }
209     function small (/*...*/) {
210         $el = new HtmlElement('small');
211         return $el->_init2(func_get_args());
212     }
213     function tt (/*...*/) {
214         $el = new HtmlElement('tt');
215         return $el->_init2(func_get_args());
216     }
217     function u (/*...*/) {
218         $el = new HtmlElement('u');
219         return $el->_init2(func_get_args());
220     }
221     function sup (/*...*/) {
222         $el = new HtmlElement('sup');
223         return $el->_init2(func_get_args());
224     }
225     function sub (/*...*/) {
226         $el = new HtmlElement('sub');
227         return $el->_init2(func_get_args());
228     }
229     function ul (/*...*/) {
230         $el = new HtmlElement('ul');
231         return $el->_init2(func_get_args());
232     }
233     function ol (/*...*/) {
234         $el = new HtmlElement('ol');
235         return $el->_init2(func_get_args());
236     }
237     function dl (/*...*/) {
238         $el = new HtmlElement('dl');
239         return $el->_init2(func_get_args());
240     }
241     function li (/*...*/) {
242         $el = new HtmlElement('li');
243         return $el->_init2(func_get_args());
244     }
245     function dt (/*...*/) {
246         $el = new HtmlElement('dt');
247         return $el->_init2(func_get_args());
248     }
249     function dd (/*...*/) {
250         $el = new HtmlElement('dd');
251         return $el->_init2(func_get_args());
252     }
253     function table (/*...*/) {
254         $el = new HtmlElement('table');
255         return $el->_init2(func_get_args());
256     }
257     function caption (/*...*/) {
258         $el = new HtmlElement('caption');
259         return $el->_init2(func_get_args());
260     }
261     function thead (/*...*/) {
262         $el = new HtmlElement('thead');
263         return $el->_init2(func_get_args());
264     }
265     function tbody (/*...*/) {
266         $el = new HtmlElement('tbody');
267         return $el->_init2(func_get_args());
268     }
269     function tfoot (/*...*/) {
270         $el = new HtmlElement('tfoot');
271         return $el->_init2(func_get_args());
272     }
273     function tr (/*...*/) {
274         $el = new HtmlElement('tr');
275         return $el->_init2(func_get_args());
276     }
277     function td (/*...*/) {
278         $el = new HtmlElement('td');
279         return $el->_init2(func_get_args());
280     }
281     function th (/*...*/) {
282         $el = new HtmlElement('th');
283         return $el->_init2(func_get_args());
284     }
285     function form (/*...*/) {
286         $el = new HtmlElement('form');
287         return $el->_init2(func_get_args());
288     }
289     function input (/*...*/) {
290         $el = new HtmlElement('input');
291         return $el->_init2(func_get_args());
292     }
293 }
294
295 define('HTMLTAG_EMPTY', 1);
296 define('HTMLTAG_INLINE', 2);
297 define('HTMLTAG_ACCEPTS_INLINE', 4);
298
299
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,
303                       // %inline elements:
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
309
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
313                       . 'div fieldset '
314
315                       // other with inline content
316                       . 'caption dt label legend '
317                       // other with either inline or block
318                       . 'dd del ins li td th ');
319
320 HTML::_setTagProperty(HTMLTAG_INLINE,
321                       // %inline elements:
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
327                       );
328
329       
330 // (c-file-style: "gnu")
331 // Local Variables:
332 // mode: php
333 // tab-width: 8
334 // c-basic-offset: 4
335 // c-hanging-comment-ender-p: nil
336 // indent-tabs-mode: nil
337 // End:   
338 ?>