| 1 | <?php |
|---|
| 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 | class THtmlWriter extends TApplicationComponent implements ITextWriter |
|---|
| 40 | { |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 83 | |
|---|
| 84 | private static $_styleEncode=array( |
|---|
| 85 | 'background-image'=>true, |
|---|
| 86 | 'list-style-image'=>true |
|---|
| 87 | ); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | private $_attributes=array(); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | private $_openTags=array(); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | private $_styles=array(); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | private $_writer=null; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 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 | |
|---|
| 125 | |
|---|
| 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 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | public function addAttribute($name,$value) |
|---|
| 139 | { |
|---|
| 140 | $this->_attributes[$name]=isset(self::$_attrEncode[$name])?THttpUtility::htmlEncode($value):$value; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | public function removeAttribute($name) |
|---|
| 148 | { |
|---|
| 149 | unset($this->_attributes[$name]); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 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 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | public function addStyleAttribute($name,$value) |
|---|
| 168 | { |
|---|
| 169 | $this->_styles[$name]=isset(self::$_styleEncode[$name])?THttpUtility::htmlEncode($value):$value; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | public function removeStyleAttribute($name) |
|---|
| 177 | { |
|---|
| 178 | unset($this->_styles[$name]); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | public function flush() |
|---|
| 186 | { |
|---|
| 187 | $this->_writer->flush(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | public function write($str) |
|---|
| 195 | { |
|---|
| 196 | $this->_writer->write($str); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | public function writeLine($str='') |
|---|
| 204 | { |
|---|
| 205 | $this->_writer->write($str."\n"); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | public function writeBreak() |
|---|
| 212 | { |
|---|
| 213 | $this->_writer->write('<br/>'); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 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 | |
|---|
| 249 | |
|---|
| 250 | public function renderEndTag() |
|---|
| 251 | { |
|---|
| 252 | if(!empty($this->_openTags) && ($tagName=array_pop($this->_openTags))!=='') |
|---|
| 253 | $this->_writer->write('</'.$tagName.'>'); |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | ?> |
|---|