summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/THtmlWriter.php
blob: 27b6ed46d749330d3dea7d20cba0692a856061f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
 * THtmlWriter class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI
 */

/**
 * THtmlWriter class
 *
 * THtmlWriter is a writer that renders valid XHTML outputs.
 * It provides functions to render tags, their attributes and stylesheet fields.
 * Attribute and stylesheet values will be automatically HTML-encoded if
 * they require so. For example, the 'value' attribute in an input tag
 * will be encoded.
 *
 * A common usage of THtmlWriter is as the following sequence:
 * <code>
 *  $writer->addAttribute($name1,$value1);
 *  $writer->addAttribute($name2,$value2);
 *  $writer->renderBeginTag($tagName);
 *  // ... render contents enclosed within the tag here
 *  $writer->renderEndTag();
 * </code>
 * Make sure each invocation of {@link renderBeginTag} is accompanied with
 * a {@link renderEndTag} and they are properly nested, like nesting
 * tags in HTML and XHTML.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI
 * @since 3.0
 */
class THtmlWriter extends TApplicationComponent implements ITextWriter
{
	/**
	 * @var array list of tags are do not need a closing tag
	 */
	private static $_simpleTags=array(
		'area'=>true,
		'base'=>true,
		'basefont'=>true,
		'bgsound'=>true,
		'col'=>true,
		'embed'=>true,
		'frame'=>true,
		'hr'=>true,
		'img'=>true,
		'input'=>true,
		'isindex'=>true,
		'link'=>true,
		'meta'=>true,
		'wbr'=>true,
	);
	/**
	 * @var array list of attributes to be rendered for a tag
	 */
	private $_attributes=array();
	/**
	 * @var array list of openning tags
	 */
	private $_openTags=array();
	/**
	 * @var array list of style attributes
	 */
	private $_styles=array();
	/**
	 * @var ITextWriter writer
	 */
	private $_writer=null;

	/**
	 * Constructor.
	 * @param ITextWriter a writer that THtmlWriter will pass its rendering result to
	 */
	public function __construct($writer)
	{
		$this->_writer=$writer;
	}

	public function getWriter()
	{
		return $this->_writer;
	}

	public function setWriter($writer)
	{
		$this->_writer = $writer;
	}
	/**
	 * Adds a list of attributes to be rendered.
	 * @param array list of attributes to be rendered
	 */
	public function addAttributes($attrs)
	{
		foreach($attrs as $name=>$value)
			$this->_attributes[THttpUtility::htmlStrip($name)]=THttpUtility::htmlEncode($value);
	}

	/**
	 * Adds an attribute to be rendered.
	 * @param string name of the attribute
	 * @param string value of the attribute
	 */
	public function addAttribute($name,$value)
	{
		$this->_attributes[THttpUtility::htmlStrip($name)]=THttpUtility::htmlEncode($value);
	}

	/**
	 * Removes the named attribute from rendering
	 * @param string name of the attribute to be removed
	 */
	public function removeAttribute($name)
	{
		unset($this->_attributes[THttpUtility::htmlStrip($name)]);
	}

	/**
	 * Adds a list of stylesheet attributes to be rendered.
	 * @param array list of stylesheet attributes to be rendered
	 */
	public function addStyleAttributes($attrs)
	{
		foreach($attrs as $name=>$value)
			$this->_styles[THttpUtility::htmlStrip($name)]=THttpUtility::htmlEncode($value);
	}

	/**
	 * Adds a stylesheet attribute to be rendered
	 * @param string stylesheet attribute name
	 * @param string stylesheet attribute value
	 */
	public function addStyleAttribute($name,$value)
	{
		$this->_styles[THttpUtility::htmlStrip($name)]=THttpUtility::htmlEncode($value);
	}

	/**
	 * Removes the named stylesheet attribute from rendering
	 * @param string name of the stylesheet attribute to be removed
	 */
	public function removeStyleAttribute($name)
	{
		unset($this->_styles[THttpUtility::htmlStrip($name)]);
	}

	/**
	 * Flushes the rendering result.
	 * This will invoke the underlying writer's flush method.
	 * @return string the content being flushed
	 */
	public function flush()
	{
		return $this->_writer->flush();
	}

	/**
	 * Renders a string.
	 * @param string string to be rendered
	 */
	public function write($str)
	{
		$this->_writer->write($str);
	}

	/**
	 * Renders a string and appends a newline to it.
	 * @param string string to be rendered
	 */
	public function writeLine($str='')
	{
		$this->_writer->write($str."\n");
	}

	/**
	 * Renders an HTML break.
	 */
	public function writeBreak()
	{
		$this->_writer->write('<br/>');
	}

	/**
	 * Renders the openning tag.
	 * @param string tag name
	 */
	public function renderBeginTag($tagName)
	{
		$str='<'.$tagName;
		foreach($this->_attributes as $name=>$value)
			$str.=' '.$name.'="'.$value.'"';
		if(!empty($this->_styles))
		{
			$str.=' style="';
			foreach($this->_styles as $name=>$value)
				$str.=$name.':'.$value.';';
			$str.='"';
		}
		if(isset(self::$_simpleTags[$tagName]))
		{
			$str.=' />';
			$this->_openTags[] = '';
		}
		else
		{
			$str.='>';
			$this->_openTags[] = $tagName;
		}
		$this->_writer->write($str);
		$this->_attributes=array();
		$this->_styles=array();
	}

	/**
	 * Renders the closing tag.
	 */
	public function renderEndTag()
	{
		if(!empty($this->_openTags) && ($tagName=array_pop($this->_openTags))!=='')
			$this->_writer->write('</'.$tagName.'>');
	}
}