Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * THtmlWriter class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: THtmlWriter.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI
11
 */
12
 
13
/**
14
 * THtmlWriter class
15
 *
16
 * THtmlWriter is a writer that renders valid XHTML outputs.
17
 * It provides functions to render tags, their attributes and stylesheet fields.
18
 * Attribute and stylesheet values will be automatically HTML-encoded if
19
 * they require so. For example, the 'value' attribute in an input tag
20
 * will be encoded.
21
 *
22
 * A common usage of THtmlWriter is as the following sequence:
23
 * <code>
24
 *  $writer->addAttribute($name1,$value1);
25
 *  $writer->addAttribute($name2,$value2);
26
 *  $writer->renderBeginTag($tagName);
27
 *  // ... render contents enclosed within the tag here
28
 *  $writer->renderEndTag();
29
 * </code>
30
 * Make sure each invocation of {@link renderBeginTag} is accompanied with
31
 * a {@link renderEndTag} and they are properly nested, like nesting
32
 * tags in HTML and XHTML.
33
 *
34
 * @author Qiang Xue <qiang.xue@gmail.com>
35
 * @version $Id: THtmlWriter.php 2541 2008-10-21 15:05:13Z qiang.xue $
36
 * @package System.Web.UI
37
 * @since 3.0
38
 */
39
class THtmlWriter extends TApplicationComponent implements ITextWriter
40
{
41
	/**
42
	 * @var array list of tags are do not need a closing tag
43
	 */
44
	private static $_simpleTags=array(
45
		'area'=>true,
46
		'base'=>true,
47
		'basefont'=>true,
48
		'bgsound'=>true,
49
		'col'=>true,
50
		'embed'=>true,
51
		'frame'=>true,
52
		'hr'=>true,
53
		'img'=>true,
54
		'input'=>true,
55
		'isindex'=>true,
56
		'link'=>true,
57
		'meta'=>true,
58
		'wbr'=>true,
59
	);
60
	/**
61
	 * @var array list of attributes that need HTML encoding
62
	 */
63
	private static $_attrEncode=array(
64
		'abbr'=>true,
65
		'accesskey'=>true,
66
		'alt'=>true,
67
		'axis'=>true,
68
		'background'=>true,
69
		'class'=>true,
70
		'content'=>true,
71
		'headers'=>true,
72
		'href'=>true,
73
		'longdesc'=>true,
74
		'onclick'=>true,
75
		'onchange'=>true,
76
		'src'=>true,
77
		'title'=>true,
78
		'label'=>true,
79
		'value'=>true
80
	);
81
	/**
82
	 * @var array list of stylesheet attributes that need HTML encoding
83
	 */
84
	private static $_styleEncode=array(
85
		'background-image'=>true,
86
		'list-style-image'=>true
87
	);
88
	/**
89
	 * @var array list of attributes to be rendered for a tag
90
	 */
91
	private $_attributes=array();
92
	/**
93
	 * @var array list of openning tags
94
	 */
95
	private $_openTags=array();
96
	/**
97
	 * @var array list of style attributes
98
	 */
99
	private $_styles=array();
100
	/**
101
	 * @var ITextWriter writer
102
	 */
103
	private $_writer=null;
104
 
105
	/**
106
	 * Constructor.
107
	 * @param ITextWriter a writer that THtmlWriter will pass its rendering result to
108
	 */
109
	public function __construct($writer)
110
	{
111
		$this->_writer=$writer;
112
	}
113
 
114
	public function getWriter()
115
	{
116
		return $this->_writer;
117
	}
118
 
119
	public function setWriter($writer)
120
	{
121
		$this->_writer = $writer;
122
	}
123
	/**
124
	 * Adds a list of attributes to be rendered.
125
	 * @param array list of attributes to be rendered
126
	 */
127
	public function addAttributes($attrs)
128
	{
129
		foreach($attrs as $name=>$value)
130
			$this->_attributes[$name]=isset(self::$_attrEncode[$name])?THttpUtility::htmlEncode($value):$value;
131
	}
132
 
133
	/**
134
	 * Adds an attribute to be rendered.
135
	 * @param string name of the attribute
136
	 * @param string value of the attribute
137
	 */
138
	public function addAttribute($name,$value)
139
	{
140
		$this->_attributes[$name]=isset(self::$_attrEncode[$name])?THttpUtility::htmlEncode($value):$value;
141
	}
142
 
143
	/**
144
	 * Removes the named attribute from rendering
145
	 * @param string name of the attribute to be removed
146
	 */
147
	public function removeAttribute($name)
148
	{
149
		unset($this->_attributes[$name]);
150
	}
151
 
152
	/**
153
	 * Adds a list of stylesheet attributes to be rendered.
154
	 * @param array list of stylesheet attributes to be rendered
155
	 */
156
	public function addStyleAttributes($attrs)
157
	{
158
		foreach($attrs as $name=>$value)
159
			$this->_styles[$name]=isset(self::$_styleEncode[$name])?THttpUtility::htmlEncode($value):$value;
160
	}
161
 
162
	/**
163
	 * Adds a stylesheet attribute to be rendered
164
	 * @param string stylesheet attribute name
165
	 * @param string stylesheet attribute value
166
	 */
167
	public function addStyleAttribute($name,$value)
168
	{
169
		$this->_styles[$name]=isset(self::$_styleEncode[$name])?THttpUtility::htmlEncode($value):$value;
170
	}
171
 
172
	/**
173
	 * Removes the named stylesheet attribute from rendering
174
	 * @param string name of the stylesheet attribute to be removed
175
	 */
176
	public function removeStyleAttribute($name)
177
	{
178
		unset($this->_styles[$name]);
179
	}
180
 
181
	/**
182
	 * Flushes the rendering result.
183
	 * This will invoke the underlying writer's flush method.
184
	 */
185
	public function flush()
186
	{
187
		$this->_writer->flush();
188
	}
189
 
190
	/**
191
	 * Renders a string.
192
	 * @param string string to be rendered
193
	 */
194
	public function write($str)
195
	{
196
		$this->_writer->write($str);
197
	}
198
 
199
	/**
200
	 * Renders a string and appends a newline to it.
201
	 * @param string string to be rendered
202
	 */
203
	public function writeLine($str='')
204
	{
205
		$this->_writer->write($str."\n");
206
	}
207
 
208
	/**
209
	 * Renders an HTML break.
210
	 */
211
	public function writeBreak()
212
	{
213
		$this->_writer->write('<br/>');
214
	}
215
 
216
	/**
217
	 * Renders the openning tag.
218
	 * @param string tag name
219
	 */
220
	public function renderBeginTag($tagName)
221
	{
222
		$str='<'.$tagName;
223
		foreach($this->_attributes as $name=>$value)
224
			$str.=' '.$name.'="'.$value.'"';
225
		if(!empty($this->_styles))
226
		{
227
			$str.=' style="';
228
			foreach($this->_styles as $name=>$value)
229
				$str.=$name.':'.$value.';';
230
			$str.='"';
231
		}
232
		if(isset(self::$_simpleTags[$tagName]))
233
		{
234
			$str.=' />';
235
			array_push($this->_openTags,'');
236
		}
237
		else
238
		{
239
			$str.='>';
240
			array_push($this->_openTags,$tagName);
241
		}
242
		$this->_writer->write($str);
243
		$this->_attributes=array();
244
		$this->_styles=array();
245
	}
246
 
247
	/**
248
	 * Renders the closing tag.
249
	 */
250
	public function renderEndTag()
251
	{
252
		if(!empty($this->_openTags) && ($tagName=array_pop($this->_openTags))!=='')
253
			$this->_writer->write('</'.$tagName.'>');
254
	}
255
}
256