1 |
<?php |
2 |
|
3 |
/** |
4 |
* Holds the HTMLTagClass |
5 |
* |
6 |
* $Id: HTMLTagClass.inc,v 1.20 2002/10/30 21:12:53 hemna Exp $ |
7 |
* |
8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
9 |
* @package phpHtmlLib |
10 |
* |
11 |
* @copyright LGPL - See LICENCE |
12 |
* |
13 |
*/ |
14 |
|
15 |
|
16 |
/** |
17 |
* make sure we include the parent class. |
18 |
*/ |
19 |
require_once( $phphtmllib."/XMLTagClass.inc" ); |
20 |
|
21 |
/** |
22 |
* Base class for all HTML Tag classes. |
23 |
* Tag class renders an html tag, its |
24 |
* attributes, the content (if any), |
25 |
* and close tag (if needed). |
26 |
* |
27 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
28 |
* @link http://phphtmllib.sourceforge.net |
29 |
*/ |
30 |
class HTMLTagClass extends XMLTagClass { |
31 |
|
32 |
|
33 |
/** |
34 |
* Flag to tell the renderer not to |
35 |
* place the /> if we are in xhtml |
36 |
* compliant mode. |
37 |
*/ |
38 |
var $_no_finish_slash_xhtml = FALSE; |
39 |
|
40 |
|
41 |
/** |
42 |
* Flag to let us know to render XHTML 1.0 |
43 |
* compliant output. defaulted to FALSE |
44 |
* @var boolean |
45 |
* @access private |
46 |
*/ |
47 |
var $_xhtml_compliant = FALSE; |
48 |
|
49 |
/** |
50 |
* Flag to denote that this tag is |
51 |
* depricated by the HTML standard. |
52 |
* |
53 |
*/ |
54 |
var $_depricated = FALSE; |
55 |
|
56 |
/** |
57 |
* the list of tag attributes that we |
58 |
* should create a clickable link for |
59 |
* when in debug mode. |
60 |
* @var array |
61 |
* @access private |
62 |
*/ |
63 |
var $_debug_link_attributes = array("background"); |
64 |
|
65 |
/** |
66 |
* The list of attributes not to render |
67 |
* if HTML_RENDER_TYPE is "XHTML STRICT" |
68 |
* |
69 |
*/ |
70 |
var $_xhtml_strict_attributes = array(); |
71 |
|
72 |
|
73 |
/** |
74 |
* The list of attributes that we want to run |
75 |
* htmlentities() on if we are in XHTML_STRICT |
76 |
* mode. Otherwise the validator complains about |
77 |
* html characters such as &. |
78 |
* @var array. |
79 |
* @access private |
80 |
*/ |
81 |
var $_htmlentities_attributes = array(); |
82 |
|
83 |
|
84 |
/** |
85 |
* Class Constructor |
86 |
* @param array - Associative array of |
87 |
* name="value" pairs of |
88 |
* tag atributes. |
89 |
* ie array("border"=>0, "class"=>"hover"); |
90 |
* |
91 |
* @param mixed You can have any number |
92 |
* of parameters that will |
93 |
* be added to the content |
94 |
* of the tag automatically. |
95 |
* |
96 |
* @access public |
97 |
*/ |
98 |
function HTMLTagClass( $attributes=NULL ) { |
99 |
if ( $attributes ) { |
100 |
$this->set_tag_attributes( $attributes ); |
101 |
} |
102 |
|
103 |
//add the content if any. |
104 |
$num_args = func_num_args(); |
105 |
for ($i = 1; $i < $num_args; $i++) { |
106 |
$this->add(func_get_arg($i)); |
107 |
} |
108 |
|
109 |
//what version of html is this tag going to |
110 |
//be rendered as? |
111 |
//this is a magic test. It assumes that |
112 |
//someone has created the define for |
113 |
//HTML_RENDER_TYPE |
114 |
if ( HTML_RENDER_TYPE == XHTML || |
115 |
HTML_RENDER_TYPE == XHTML_STRICT ) { |
116 |
$this->_xhtml_compliant = TRUE; |
117 |
|
118 |
} |
119 |
|
120 |
//if the tag is depricated |
121 |
//we raise an alert. |
122 |
if ( $this->_depricated ) { |
123 |
trigger_error(htmlspecialchars($this->_tag) . " has been depricated in HTML 4.0", E_USER_NOTICE); |
124 |
} |
125 |
} |
126 |
|
127 |
|
128 |
/** |
129 |
* Renders the tag, attributes, content and close tag. |
130 |
* @param int $indent_level the indentation level for this tag. |
131 |
* @return string |
132 |
* @access public |
133 |
*/ |
134 |
function render($indent_level=NULL, $output_debug=0) { |
135 |
|
136 |
//try and guess the indentation flags |
137 |
//based on the data |
138 |
$this->_prepare_flags(); |
139 |
|
140 |
if ( $indent_level==NULL ) { |
141 |
$indent_level = $this->_indent_level; |
142 |
} |
143 |
|
144 |
$html = $this->_render_tag($indent_level, $output_debug); |
145 |
|
146 |
if ( $this->_content_required ) { |
147 |
$html .= $this->_render_content($indent_level, $output_debug); |
148 |
} |
149 |
if ( $this->_close_tag_required ) { |
150 |
$html .= $this->_render_close_tag($indent_level, $output_debug); |
151 |
} |
152 |
|
153 |
return $html; |
154 |
} |
155 |
|
156 |
//**************************************************************** |
157 |
// HTML specific routines |
158 |
//**************************************************************** |
159 |
|
160 |
/** |
161 |
* This function is a shorthand helper |
162 |
* to setting the style attribute on a |
163 |
* tag. |
164 |
* |
165 |
* @param string - the style value. |
166 |
*/ |
167 |
function set_style( $value ) { |
168 |
$this->set_tag_attribute("style", $value); |
169 |
} |
170 |
|
171 |
/** |
172 |
* This function is a shorthand helper |
173 |
* to setting the class attribute on a |
174 |
* tag. |
175 |
* |
176 |
* @param string - the class value. |
177 |
*/ |
178 |
function set_class( $value ) { |
179 |
$this->set_tag_attribute("class", $value); |
180 |
} |
181 |
|
182 |
/** |
183 |
* This function is a shorthand helper |
184 |
* to setting the id attribute on a |
185 |
* tag. |
186 |
* |
187 |
* @param string - the class value. |
188 |
*/ |
189 |
function set_id( $value ) { |
190 |
$this->set_tag_attribute("id", $value); |
191 |
} |
192 |
|
193 |
|
194 |
//**************************************************************** |
195 |
// Tag rendering routines. |
196 |
//**************************************************************** |
197 |
|
198 |
/** |
199 |
* renders the open tag. is <TABLE> |
200 |
* @param int $indent_level the indentation level for this tag. |
201 |
* @return string |
202 |
* @access private |
203 |
*/ |
204 |
function _render_tag($indent_level, $output_debug=0) { |
205 |
if ( $output_debug ) { |
206 |
//lets call the special render tag debug function |
207 |
return $this->_render_tag_debug( $indent_level ); |
208 |
} else { |
209 |
return XMLTag::_render_open_tag($indent_level, $this->_xhtml_compliant); |
210 |
} |
211 |
} |
212 |
|
213 |
/** |
214 |
* Renders all of the content. |
215 |
* Content can be raw strings, or tag objects. |
216 |
* @param int $indent_level the indentation level for this tag. |
217 |
* @return string |
218 |
* @access private |
219 |
*/ |
220 |
function _render_content($indent_level, $output_debug=0) { |
221 |
if ( $output_debug ) { |
222 |
return $this->_render_content_debug( $indent_level ); |
223 |
} else { |
224 |
return XMLTag::_render_content( $indent_level, $output_debug); |
225 |
} |
226 |
} |
227 |
|
228 |
/** |
229 |
* Renders the close tag (if needed) |
230 |
* @param int $indent_level the indentation level for this tag. |
231 |
* @return string |
232 |
* @access private |
233 |
*/ |
234 |
function _render_close_tag($indent_level, $output_debug=0) { |
235 |
if ( $output_debug ) { |
236 |
return $this->_render_close_tag_debug( $indent_level ); |
237 |
} else { |
238 |
return XMLTag::_render_close_tag($indent_level); |
239 |
} |
240 |
} |
241 |
|
242 |
|
243 |
/** |
244 |
* This renders that open tag in debug mode. |
245 |
* We do this as a seperate function, |
246 |
* so we can override this by the child |
247 |
* tag, so it can add a link on content or |
248 |
* one of the attributes. |
249 |
*/ |
250 |
function _render_tag_debug($indent_level) { |
251 |
|
252 |
$indent = $this->_render_indent($indent_level, TRUE); |
253 |
|
254 |
$tag_prefix = htmlspecialchars( $this->_tag_prefix ); |
255 |
$tab_postfix = htmlspecialchars( $this->_tag_postfix ); |
256 |
$str = $indent . $tag_prefix. "<span class=\"purple\" style=\"white-space:nowrap;\">"; |
257 |
$str .= $this->_tag . "</span>"; |
258 |
|
259 |
if ( $this->_xhtml_compliant && !$this->_always_upper_case ) { |
260 |
//we have to have the tag name be lower case. |
261 |
$str = strtolower( $str ); |
262 |
} |
263 |
|
264 |
foreach( $this->_attributes as $name => $value) { |
265 |
$str .= $this->_build_attribute_string($name, $value, TRUE); |
266 |
} |
267 |
|
268 |
//if we want to output xhtml compliant code, we have to |
269 |
//render a special tag closing. |
270 |
if ( $this->_xhtml_compliant ) { |
271 |
//we have to render a special close for the |
272 |
//open tag, if the tag doesn't require a close |
273 |
//tag or content. |
274 |
if ( !$this->_close_tag_required && !$this->_no_finish_slash_xhtml ) { |
275 |
$html = $str . " />"; |
276 |
} else { |
277 |
$html = $str.">"; |
278 |
} |
279 |
} else { |
280 |
$html = $str.">"; |
281 |
} |
282 |
|
283 |
if ( $this->newline_after_opentag ) { |
284 |
$html .= "<br>\n"; |
285 |
} |
286 |
return $html; |
287 |
} |
288 |
|
289 |
/** |
290 |
* This renders the content in debug mode. |
291 |
* lets us wrap the content w/ a <nobr></nobr> |
292 |
* so it acts like view source. |
293 |
* |
294 |
* Content can be raw strings, or tag objects. |
295 |
* @param int $indent_level the indentation level for this tag. |
296 |
* @return string |
297 |
* @access private |
298 |
*/ |
299 |
function _render_content_debug($indent_level) { |
300 |
|
301 |
$html = ''; |
302 |
//walk through the content |
303 |
foreach ($this->_content as $item) { |
304 |
if (method_exists($item, "render")) { |
305 |
if ($this->_collapse_flag && method_exists($item, "set_collapse")) { |
306 |
$item->set_collapse(TRUE, FALSE); |
307 |
} |
308 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
309 |
$indent = INDENT_LEFT_JUSTIFY; |
310 |
} else { |
311 |
$indent = $indent_level + 1; |
312 |
} |
313 |
$html .= $item->render($indent, TRUE); |
314 |
} else { |
315 |
if ($this->_collapse_flag) { |
316 |
$html .= htmlspecialchars($item); |
317 |
} else { |
318 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
319 |
$indent = INDENT_LEFT_JUSTIFY; |
320 |
} else { |
321 |
$indent = $indent_level + 1; |
322 |
} |
323 |
$indent = $this->_render_indent($indent, TRUE); |
324 |
if ( $this->newline_after_opentag ) { |
325 |
$item = htmlspecialchars($item); |
326 |
$item = str_replace("\n", "<br>\n" . $indent, $item); |
327 |
$html .= $indent . "<span style=\"white-space:nowrap\">" .$item . "</span><br>\n"; |
328 |
} else { |
329 |
$item = htmlspecialchars($item); |
330 |
$item = str_replace("\n", "<br>\n" . $indent, $item); |
331 |
$html .= $item; |
332 |
} |
333 |
} |
334 |
} |
335 |
} |
336 |
return $html; |
337 |
} |
338 |
|
339 |
|
340 |
/** |
341 |
* this renders the close tag in debugging mode. |
342 |
* @param int $indent_level the indentation level for this tag. |
343 |
* @return string |
344 |
* @access private |
345 |
*/ |
346 |
function _render_close_tag_debug( $indent_level ) { |
347 |
|
348 |
$indent =""; |
349 |
if ( $this->indent_flag && $this->newline_after_opentag ) { |
350 |
$indent = $this->_render_indent($indent_level, TRUE); |
351 |
} |
352 |
$str = $indent . "</" . "<span class=\"purple\">"; |
353 |
$str .= $this->_tag . "</span>>"; |
354 |
|
355 |
if ( $this->_xhtml_compliant ) { |
356 |
$str = strtolower( $str ); |
357 |
} |
358 |
|
359 |
if ( $this->newline_after_closetag ) { |
360 |
$str .= "<br>\n"; |
361 |
} |
362 |
|
363 |
return $str; |
364 |
|
365 |
} |
366 |
|
367 |
|
368 |
//**************************************************************** |
369 |
// Utility functions for attributes. |
370 |
//**************************************************************** |
371 |
|
372 |
/** |
373 |
* this builds an attribute for a tag. |
374 |
* It also filters out any attributes |
375 |
* that shouldn't be rendered if they |
376 |
* are in the $this->_xhtml_strict_attributes |
377 |
* array and HTML_RENDER_TYPE = XHTML STRICT |
378 |
* |
379 |
* @param string - $name attribute name |
380 |
* @param mixed - $value attribute value |
381 |
* @return the tag attribute name=value pair. |
382 |
* to be added to the tag. |
383 |
*/ |
384 |
function _build_attribute_string($name, $value, $debug=0) { |
385 |
|
386 |
if ( $debug ) { |
387 |
//hack to make non name-value pair work. |
388 |
//ie <option name=foo value=bar CHECKED> |
389 |
if ( is_int($name) ) { |
390 |
$returnval = " <span class=\"black\">$value</span>"; |
391 |
} else if ( $value === NULL ) { |
392 |
$returnval = " <span class=\"black\">$name</span>"; |
393 |
} else { |
394 |
if ( in_array($name, $this->_debug_link_attributes) ) { |
395 |
//lets create a clickable link for the value |
396 |
//of this attribute |
397 |
$value = "<a href=\"$value\">$value</a>"; |
398 |
$returnval = " <span class=\"black\">$name=</span><span class=\"blue\">\"$value\"</span>"; |
399 |
} else { |
400 |
$returnval = " <span class=\"black\">$name=</span><span class=\"blue\">\"$value\"</span>"; |
401 |
} |
402 |
} |
403 |
|
404 |
if ( HTML_RENDER_TYPE == XHTML_STRICT && |
405 |
in_array($name, $this->_xhtml_strict_attributes) ) { |
406 |
$returnval = NULL; |
407 |
} |
408 |
} else { |
409 |
//hack to make non name-value pair work. |
410 |
//ie <option name=foo value=bar CHECKED> |
411 |
if ( HTML_RENDER_TYPE == XHTML_STRICT && !is_int($name) ) { |
412 |
//We do this because XHTML STRICT complains about |
413 |
//html characters such as &. So we mask them. |
414 |
$value = htmlentities($value); |
415 |
} |
416 |
|
417 |
if ( HTML_RENDER_TYPE == XHTML_STRICT && |
418 |
in_array($name, $this->_xhtml_strict_attributes) ) { |
419 |
$returnval = NULL; |
420 |
} else { |
421 |
$returnval = XMLTagClass::_build_attribute_string($name, $value); |
422 |
} |
423 |
} |
424 |
return $returnval; |
425 |
} |
426 |
|
427 |
//**************************************************************** |
428 |
// Misc functions |
429 |
//**************************************************************** |
430 |
|
431 |
} |
432 |
?> |