| [ Index ] |
|
Code source de PRADO 3.0.6 |
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 © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: THtmlWriter.php 1397 2006-09-07 07:55:53Z wei $ 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 1397 2006-09-07 07:55:53Z wei $ 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 /** 115 * Adds a list of attributes to be rendered. 116 * @param array list of attributes to be rendered 117 */ 118 public function addAttributes($attrs) 119 { 120 foreach($attrs as $name=>$value) 121 $this->_attributes[$name]=isset(self::$_attrEncode[$name])?THttpUtility::htmlEncode($value):$value; 122 } 123 124 /** 125 * Adds an attribute to be rendered. 126 * @param string name of the attribute 127 * @param string value of the attribute 128 */ 129 public function addAttribute($name,$value) 130 { 131 $this->_attributes[$name]=isset(self::$_attrEncode[$name])?THttpUtility::htmlEncode($value):$value; 132 } 133 134 /** 135 * Removes the named attribute from rendering 136 * @param string name of the attribute to be removed 137 */ 138 public function removeAttribute($name) 139 { 140 unset($this->_attributes[$name]); 141 } 142 143 /** 144 * Adds a list of stylesheet attributes to be rendered. 145 * @param array list of stylesheet attributes to be rendered 146 */ 147 public function addStyleAttributes($attrs) 148 { 149 foreach($attrs as $name=>$value) 150 $this->_styles[$name]=isset(self::$_styleEncode[$name])?THttpUtility::htmlEncode($value):$value; 151 } 152 153 /** 154 * Adds a stylesheet attribute to be rendered 155 * @param string stylesheet attribute name 156 * @param string stylesheet attribute value 157 */ 158 public function addStyleAttribute($name,$value) 159 { 160 $this->_styles[$name]=isset(self::$_styleEncode[$name])?THttpUtility::htmlEncode($value):$value; 161 } 162 163 /** 164 * Removes the named stylesheet attribute from rendering 165 * @param string name of the stylesheet attribute to be removed 166 */ 167 public function removeStyleAttribute($name) 168 { 169 unset($this->_styles[$name]); 170 } 171 172 /** 173 * Flushes the rendering result. 174 * This will invoke the underlying writer's flush method. 175 */ 176 public function flush() 177 { 178 $this->_writer->flush(); 179 } 180 181 /** 182 * Renders a string. 183 * @param string string to be rendered 184 */ 185 public function write($str) 186 { 187 $this->_writer->write($str); 188 } 189 190 /** 191 * Renders a string and appends a newline to it. 192 * @param string string to be rendered 193 */ 194 public function writeLine($str='') 195 { 196 $this->_writer->write($str."\n"); 197 } 198 199 /** 200 * Renders an HTML break. 201 */ 202 public function writeBreak() 203 { 204 $this->_writer->write('<br/>'); 205 } 206 207 /** 208 * Renders the openning tag. 209 * @param string tag name 210 */ 211 public function renderBeginTag($tagName) 212 { 213 $str='<'.$tagName; 214 foreach($this->_attributes as $name=>$value) 215 $str.=' '.$name.'="'.$value.'"'; 216 if(!empty($this->_styles)) 217 { 218 $str.=' style="'; 219 foreach($this->_styles as $name=>$value) 220 $str.=$name.':'.$value.';'; 221 $str.='"'; 222 } 223 if(isset(self::$_simpleTags[$tagName])) 224 { 225 $str.=' />'; 226 array_push($this->_openTags,''); 227 } 228 else 229 { 230 $str.='>'; 231 array_push($this->_openTags,$tagName); 232 } 233 $this->_writer->write($str); 234 $this->_attributes=array(); 235 $this->_styles=array(); 236 } 237 238 /** 239 * Renders the closing tag. 240 */ 241 public function renderEndTag() 242 { 243 if(!empty($this->_openTags) && ($tagName=array_pop($this->_openTags))!=='') 244 $this->_writer->write('</'.$tagName.'>'); 245 } 246 } 247 248 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |