/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/HTMLTagClass.inc
ViewVC logotype

Annotation of /nfo/php/libs/com.newsblob.phphtmllib/HTMLTagClass.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Thu May 6 16:23:38 2004 UTC (20 years, 2 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +59 -19 lines
 updated all to v2.4.1 - Apr 01, 2004

1 jonen 1.1 <?php
2    
3     /**
4     * Holds the HTMLTagClass
5     *
6 jonen 1.4 * $Id: HTMLTagClass.inc,v 1.31 2003/08/19 22:35:46 hemna Exp $
7 jonen 1.1 *
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 jonen 1.2 * @package phpHtmlLib
30 jonen 1.1 */
31     class HTMLTagClass extends XMLTagClass {
32    
33     /**
34     * the list of tag attributes that we
35     * should create a clickable link for
36     * when in debug mode.
37 jonen 1.2 *
38     * We comment this declaration out
39     * to save memory, since only a small
40     * number of tags use it
41     *
42 jonen 1.1 * @var array
43     * @access private
44     */
45 jonen 1.2 //var $_debug_link_attributes = array("background");
46 jonen 1.1
47     /**
48     * The list of attributes not to render
49 jonen 1.3 * if $GLOBALS["HTML_RENDER_TYPE"] is "XHTML STRICT"
50 jonen 1.1 *
51 jonen 1.2 * We comment this declaration out
52     * to save memory, since only a small
53     * number of tags use it
54     *
55 jonen 1.1 */
56 jonen 1.2 //var $_xhtml_strict_attributes = array();
57 jonen 1.1
58    
59     /**
60     * The list of attributes that we want to run
61     * htmlentities() on if we are in XHTML_STRICT
62     * mode. Otherwise the validator complains about
63     * html characters such as &.
64 jonen 1.2 *
65     * We comment this declaration out
66     * to save memory, since only a small
67     * number of tags use it
68     *
69 jonen 1.1 * @var array.
70     * @access private
71     */
72 jonen 1.2 //var $_htmlentities_attributes = array();
73 jonen 1.1
74    
75     /**
76     * Class Constructor
77 jonen 1.4 *
78     * {@source }
79     *
80 jonen 1.1 * @param array - Associative array of
81     * name="value" pairs of
82     * tag atributes.
83     * ie array("border"=>0, "class"=>"hover");
84     *
85     * @param mixed You can have any number
86     * of parameters that will
87     * be added to the content
88     * of the tag automatically.
89     */
90     function HTMLTagClass( $attributes=NULL ) {
91     if ( $attributes ) {
92     $this->set_tag_attributes( $attributes );
93     }
94    
95 jonen 1.2 //set the default tag options
96     $this->_set_flags();
97    
98 jonen 1.1 //add the content if any.
99     $num_args = func_num_args();
100     for ($i = 1; $i < $num_args; $i++) {
101     $this->add(func_get_arg($i));
102     }
103    
104     //what version of html is this tag going to
105     //be rendered as?
106     //this is a magic test. It assumes that
107     //someone has created the define for
108     //HTML_RENDER_TYPE
109 jonen 1.3 if ( $GLOBALS["HTML_RENDER_TYPE"] == XHTML ||
110     $GLOBALS["HTML_RENDER_TYPE"] == XHTML_STRICT ) {
111 jonen 1.2 $this->_flags |= _XHTMLCOMPLIANT;
112 jonen 1.1
113     }
114    
115     //if the tag is depricated
116     //we raise an alert.
117 jonen 1.2 if ( $this->_flags & _DEPRICATED ) {
118 jonen 1.1 trigger_error(htmlspecialchars($this->_tag) . " has been depricated in HTML 4.0", E_USER_NOTICE);
119     }
120     }
121    
122    
123     /**
124     * Renders the tag, attributes, content and close tag.
125 jonen 1.4 *
126     * {@source }
127     *
128 jonen 1.1 * @param int $indent_level the indentation level for this tag.
129 jonen 1.4 * @param boolean output in html viewable mode
130 jonen 1.1 * @return string
131     */
132     function render($indent_level=NULL, $output_debug=0) {
133    
134     //try and guess the indentation flags
135     //based on the data
136     $this->_prepare_flags();
137    
138     if ( $indent_level==NULL ) {
139 jonen 1.2 $indent_level = 0;
140 jonen 1.1 }
141    
142 jonen 1.2 $html = $this->_render_tag($indent_level, $output_debug);
143 jonen 1.1
144 jonen 1.2 if ( $this->_flags & _CONTENTREQUIRED) {
145 jonen 1.1 $html .= $this->_render_content($indent_level, $output_debug);
146     }
147 jonen 1.2 if ( $this->_flags & _CLOSETAGREQUIRED ) {
148 jonen 1.1 $html .= $this->_render_close_tag($indent_level, $output_debug);
149     }
150    
151     return $html;
152     }
153    
154     //****************************************************************
155     // HTML specific routines
156     //****************************************************************
157    
158     /**
159     * This function is a shorthand helper
160     * to setting the style attribute on a
161     * tag.
162 jonen 1.4 *
163     * {@source }
164 jonen 1.1 *
165     * @param string - the style value.
166 jonen 1.4 * @return none
167 jonen 1.1 */
168     function set_style( $value ) {
169     $this->set_tag_attribute("style", $value);
170     }
171    
172     /**
173     * This function is a shorthand helper
174     * to setting the class attribute on a
175     * tag.
176 jonen 1.4 *
177     * {@source }
178 jonen 1.1 *
179     * @param string - the class value.
180 jonen 1.4 * @return none
181 jonen 1.1 */
182     function set_class( $value ) {
183     $this->set_tag_attribute("class", $value);
184     }
185    
186     /**
187     * This function is a shorthand helper
188     * to setting the id attribute on a
189     * tag.
190 jonen 1.4 *
191     * {@source }
192 jonen 1.1 *
193     * @param string - the class value.
194 jonen 1.4 * @return none
195 jonen 1.1 */
196     function set_id( $value ) {
197     $this->set_tag_attribute("id", $value);
198     }
199    
200    
201     //****************************************************************
202     // Tag rendering routines.
203     //****************************************************************
204    
205     /**
206     * renders the open tag. is <TABLE>
207 jonen 1.4 *
208     * {@source }
209     * @access private
210     *
211     * @param int the indentation level for this tag.
212     * @return string
213 jonen 1.1 */
214     function _render_tag($indent_level, $output_debug=0) {
215     if ( $output_debug ) {
216     //lets call the special render tag debug function
217     return $this->_render_tag_debug( $indent_level );
218     } else {
219 jonen 1.2 return XMLTag::_render_open_tag($indent_level, $this->_flags & _XHTMLCOMPLIANT);
220 jonen 1.1 }
221     }
222    
223     /**
224     * Renders all of the content.
225     * Content can be raw strings, or tag objects.
226 jonen 1.4 *
227     * {@source }
228     * @access private
229     *
230     * @param int the indentation level for this tag.
231 jonen 1.1 * @return string
232     */
233     function _render_content($indent_level, $output_debug=0) {
234     if ( $output_debug ) {
235     return $this->_render_content_debug( $indent_level );
236     } else {
237     return XMLTag::_render_content( $indent_level, $output_debug);
238     }
239     }
240    
241     /**
242     * Renders the close tag (if needed)
243 jonen 1.4 *
244     * {@source }
245     * @access private
246     *
247     * @param int the indentation level for this tag.
248     * @return string
249 jonen 1.1 */
250     function _render_close_tag($indent_level, $output_debug=0) {
251     if ( $output_debug ) {
252     return $this->_render_close_tag_debug( $indent_level );
253     } else {
254     return XMLTag::_render_close_tag($indent_level);
255     }
256     }
257    
258    
259     /**
260     * This renders that open tag in debug mode.
261     * We do this as a seperate function,
262     * so we can override this by the child
263     * tag, so it can add a link on content or
264     * one of the attributes.
265 jonen 1.4 *
266     * {@source }
267     * @access private
268     *
269     * @param int the indentation level
270     * @return string
271 jonen 1.1 */
272     function _render_tag_debug($indent_level) {
273    
274     $indent = $this->_render_indent($indent_level, TRUE);
275    
276 jonen 1.3 $tag_prefix = htmlspecialchars( (@$this->_tag_prefix ? $this->_tag_prefix : _TAG_PREFIX));
277     $tab_postfix = htmlspecialchars( (@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX));
278 jonen 1.1 $str = $indent . $tag_prefix. "<span class=\"purple\" style=\"white-space:nowrap;\">";
279     $str .= $this->_tag . "</span>";
280    
281 jonen 1.2 if ( ($this->_flags & _XHTMLCOMPLIANT) && !($this->_flags & _ALWAYS_UPPERCASE) ) {
282 jonen 1.1 //we have to have the tag name be lower case.
283     $str = strtolower( $str );
284     }
285    
286     foreach( $this->_attributes as $name => $value) {
287     $str .= $this->_build_attribute_string($name, $value, TRUE);
288     }
289    
290     //if we want to output xhtml compliant code, we have to
291     //render a special tag closing.
292 jonen 1.2 if ( $this->_flags & _XHTMLCOMPLIANT ) {
293 jonen 1.1 //we have to render a special close for the
294     //open tag, if the tag doesn't require a close
295     //tag or content.
296 jonen 1.2 if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML) ) {
297 jonen 1.1 $html = $str . "&nbsp;/&gt;";
298     } else {
299     $html = $str."&gt;";
300     }
301     } else {
302     $html = $str."&gt;";
303     }
304    
305 jonen 1.2 if ( $this->_flags & _NEWLINEAFTEROPENTAG ) {
306 jonen 1.1 $html .= "<br>\n";
307     }
308     return $html;
309     }
310    
311     /**
312     * This renders the content in debug mode.
313     * lets us wrap the content w/ a <nobr></nobr>
314     * so it acts like view source.
315     *
316     * Content can be raw strings, or tag objects.
317 jonen 1.4 *
318     * {@source }
319     * @access private
320     *
321     * @param int the indentation level for this tag.
322     * @return string
323 jonen 1.1 */
324     function _render_content_debug($indent_level) {
325    
326     $html = '';
327     //walk through the content
328 jonen 1.2 for ($x=0; $x<=$this->_data_count; $x++) {
329     $item = &$this->_content[$x];
330 jonen 1.1 if (method_exists($item, "render")) {
331 jonen 1.2 if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) {
332 jonen 1.1 $item->set_collapse(TRUE, FALSE);
333     }
334     if ($indent_level == INDENT_LEFT_JUSTIFY) {
335     $indent = INDENT_LEFT_JUSTIFY;
336     } else {
337     $indent = $indent_level + 1;
338     }
339     $html .= $item->render($indent, TRUE);
340     } else {
341 jonen 1.2 if ($this->_flags & _COLLAPSE) {
342 jonen 1.1 $html .= htmlspecialchars($item);
343     } else {
344     if ($indent_level == INDENT_LEFT_JUSTIFY) {
345     $indent = INDENT_LEFT_JUSTIFY;
346     } else {
347     $indent = $indent_level + 1;
348     }
349     $indent = $this->_render_indent($indent, TRUE);
350 jonen 1.2 if ( $this->_flags & _NEWLINEAFTEROPENTAG ) {
351 jonen 1.1 $item = htmlspecialchars($item);
352     $item = str_replace("\n", "<br>\n" . $indent, $item);
353     $html .= $indent . "<span style=\"white-space:nowrap\">" .$item . "</span><br>\n";
354     } else {
355     $item = htmlspecialchars($item);
356     $item = str_replace("\n", "<br>\n" . $indent, $item);
357     $html .= $item;
358     }
359     }
360     }
361     }
362     return $html;
363     }
364    
365    
366     /**
367     * this renders the close tag in debugging mode.
368 jonen 1.4 *
369     * {@source }
370     * @access private
371     *
372     * @param int the indentation level for this tag.
373     * @return string
374 jonen 1.1 */
375     function _render_close_tag_debug( $indent_level ) {
376    
377     $indent ="";
378 jonen 1.2 if ( ($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG) ) {
379 jonen 1.1 $indent = $this->_render_indent($indent_level, TRUE);
380     }
381     $str = $indent . "&lt;/" . "<span class=\"purple\">";
382     $str .= $this->_tag . "</span>&gt;";
383    
384 jonen 1.2 if ( $this->_flags & _XHTMLCOMPLIANT ) {
385 jonen 1.1 $str = strtolower( $str );
386     }
387    
388 jonen 1.2 if ( $this->_flags & _NEWLINEAFTERCLOSETAG ) {
389 jonen 1.1 $str .= "<br>\n";
390     }
391    
392     return $str;
393    
394     }
395    
396    
397     //****************************************************************
398     // Utility functions for attributes.
399     //****************************************************************
400    
401     /**
402     * this builds an attribute for a tag.
403     * It also filters out any attributes
404     * that shouldn't be rendered if they
405     * are in the $this->_xhtml_strict_attributes
406     * array and HTML_RENDER_TYPE = XHTML STRICT
407 jonen 1.4 *
408     * {@source }
409     * @access private
410 jonen 1.1 *
411     * @param string - $name attribute name
412     * @param mixed - $value attribute value
413     * @return the tag attribute name=value pair.
414     * to be added to the tag.
415     */
416     function _build_attribute_string($name, $value, $debug=0) {
417    
418     if ( $debug ) {
419     //hack to make non name-value pair work.
420     //ie <option name=foo value=bar CHECKED>
421     if ( is_int($name) ) {
422     $returnval = " <span class=\"black\">$value</span>";
423     } else if ( $value === NULL ) {
424     $returnval = " <span class=\"black\">$name</span>";
425     } else {
426 jonen 1.2 if ( @in_array($name, $this->_debug_link_attributes) ) {
427 jonen 1.1 //lets create a clickable link for the value
428     //of this attribute
429     $value = "<a href=\"$value\">$value</a>";
430     $returnval = " <span class=\"black\">$name=</span><span class=\"blue\">\"$value\"</span>";
431     } else {
432     $returnval = " <span class=\"black\">$name=</span><span class=\"blue\">\"$value\"</span>";
433     }
434     }
435    
436 jonen 1.3 if ( $GLOBALS["HTML_RENDER_TYPE"] == XHTML_STRICT &&
437 jonen 1.2 @in_array($name, $this->_xhtml_strict_attributes) ) {
438 jonen 1.1 $returnval = NULL;
439     }
440     } else {
441     //hack to make non name-value pair work.
442     //ie <option name=foo value=bar CHECKED>
443 jonen 1.3 if ( $GLOBALS["HTML_RENDER_TYPE"] == XHTML_STRICT && !is_int($name) ) {
444 jonen 1.1 //We do this because XHTML STRICT complains about
445     //html characters such as &. So we mask them.
446 jonen 1.3 $value = htmlspecialchars($value);
447 jonen 1.1 }
448    
449 jonen 1.3 if ( $GLOBALS["HTML_RENDER_TYPE"] == XHTML_STRICT &&
450 jonen 1.2 @in_array($name, $this->_xhtml_strict_attributes) ) {
451 jonen 1.1 $returnval = NULL;
452     } else {
453 jonen 1.3 if ( ((int)$name - 0) === $name) {
454     $returnval = " ".$value;
455     } else if ( $value === NULL ) {
456     $returnval = " ".$name;
457     } else {
458     $returnval= " ".$name."=\"".$value."\"";
459     }
460 jonen 1.1 }
461     }
462     return $returnval;
463     }
464    
465     //****************************************************************
466     // Misc functions
467     //****************************************************************
468 jonen 1.3
469    
470     /**
471     * This function checks to see if
472     * there is only 1 content data, and
473     * its not an object, then it auto
474     * sets some of the indentation flags
475 jonen 1.4 *
476     * {@source }
477     * @access private
478     * @return none
479 jonen 1.3 */
480     function _prepare_flags() {
481     if ($this->_flags & _CONTENTREQUIRED) {
482     if ($this->count_content() == 1) {
483     if (!is_object($this->_content[0])) {
484     //ok looks like this object has only
485     //1 data for content and its a string.
486     if ( !strstr($this->_content[0], "\n") ) {
487     $this->_flags &= ~_NEWLINEAFTEROPENTAG;
488     }
489     }
490     } else if ($this->count_content() == 0) {
491     $this->_flags &= ~_NEWLINEAFTEROPENTAG;
492     }
493     }
494     }
495 jonen 1.1
496     }
497     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed