/[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.3 - (hide annotations)
Thu May 6 12:58:02 2004 UTC (20 years, 2 months ago) by jonen
Branch: MAIN
Changes since 1.2: +41 -11 lines
 updated to v2.3.0 - July 31, 2003

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

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