[SPIP] v3.2.12 -> v3.2.12 - Reinstallation avec le spip_loader
[lhc/web/www.git] / www / plugins-dist / safehtml / lib / safehtml / classes / HTMLSax3 / Decorators.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // +----------------------------------------------------------------------+
5 // | PHP Version 4 |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 1997-2002 The PHP Group |
8 // +----------------------------------------------------------------------+
9 // | This source file is subject toversion 3.0 of the PHP license, |
10 // | that is bundled with this package in the file LICENSE, and is |
11 // | available at through the world-wide-web at |
12 // | http://www.php.net/license/3_0.txt. |
13 // | If you did not receive a copy of the PHP license and are unable to |
14 // | obtain it through the world-wide-web, please send a note to |
15 // | license@php.net so we can mail you a copy immediately. |
16 // +----------------------------------------------------------------------+
17 // | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
18 // | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more |
19 // | Authors: Many @ Sitepointforums Advanced PHP Forums |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
23 //
24 /**
25 * Decorators for dealing with parser options
26 * @package XML_HTMLSax3
27 * @version $Id: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
28 * @see XML_HTMLSax3::set_option
29 */
30 /**
31 * Trims the contents of element data from whitespace at start and end
32 * @package XML_HTMLSax3
33 * @access protected
34 */
35 class XML_HTMLSax3_Trim {
36 /**
37 * Original handler object
38 * @var object
39 * @access private
40 */
41 var $orig_obj;
42 /**
43 * Original handler method
44 * @var string
45 * @access private
46 */
47 var $orig_method;
48 /**
49 * Constructs XML_HTMLSax3_Trim
50 * @param object handler object being decorated
51 * @param string original handler method
52 * @access protected
53 */
54 function __construct(&$orig_obj, $orig_method) {
55 $this->orig_obj =& $orig_obj;
56 $this->orig_method = $orig_method;
57 }
58 /**
59 * Trims the data
60 * @param XML_HTMLSax3
61 * @param string element data
62 * @access protected
63 */
64 function trimData(&$parser, $data) {
65 $data = trim($data);
66 if ($data != '') {
67 $this->orig_obj->{$this->orig_method}($parser, $data);
68 }
69 }
70 }
71 /**
72 * Coverts tag names to upper case
73 * @package XML_HTMLSax3
74 * @access protected
75 */
76 class XML_HTMLSax3_CaseFolding {
77 /**
78 * Original handler object
79 * @var object
80 * @access private
81 */
82 var $orig_obj;
83 /**
84 * Original open handler method
85 * @var string
86 * @access private
87 */
88 var $orig_open_method;
89 /**
90 * Original close handler method
91 * @var string
92 * @access private
93 */
94 var $orig_close_method;
95 /**
96 * Constructs XML_HTMLSax3_CaseFolding
97 * @param object handler object being decorated
98 * @param string original open handler method
99 * @param string original close handler method
100 * @access protected
101 */
102 function __construct(&$orig_obj, $orig_open_method, $orig_close_method) {
103 $this->orig_obj =& $orig_obj;
104 $this->orig_open_method = $orig_open_method;
105 $this->orig_close_method = $orig_close_method;
106 }
107 /**
108 * Folds up open tag callbacks
109 * @param XML_HTMLSax3
110 * @param string tag name
111 * @param array tag attributes
112 * @access protected
113 */
114 function foldOpen(&$parser, $tag, $attrs=array(), $empty = FALSE) {
115 $this->orig_obj->{$this->orig_open_method}($parser, strtoupper($tag), $attrs, $empty);
116 }
117 /**
118 * Folds up close tag callbacks
119 * @param XML_HTMLSax3
120 * @param string tag name
121 * @access protected
122 */
123 function foldClose(&$parser, $tag, $empty = FALSE) {
124 $this->orig_obj->{$this->orig_close_method}($parser, strtoupper($tag), $empty);
125 }
126 }
127 /**
128 * Breaks up data by linefeed characters, resulting in additional
129 * calls to the data handler
130 * @package XML_HTMLSax3
131 * @access protected
132 */
133 class XML_HTMLSax3_Linefeed {
134 /**
135 * Original handler object
136 * @var object
137 * @access private
138 */
139 var $orig_obj;
140 /**
141 * Original handler method
142 * @var string
143 * @access private
144 */
145 var $orig_method;
146 /**
147 * Constructs XML_HTMLSax3_LineFeed
148 * @param object handler object being decorated
149 * @param string original handler method
150 * @access protected
151 */
152 function __construct(&$orig_obj, $orig_method) {
153 $this->orig_obj =& $orig_obj;
154 $this->orig_method = $orig_method;
155 }
156 /**
157 * Breaks the data up by linefeeds
158 * @param XML_HTMLSax3
159 * @param string element data
160 * @access protected
161 */
162 function breakData(&$parser, $data) {
163 $data = explode("\n",$data);
164 foreach ( $data as $chunk ) {
165 $this->orig_obj->{$this->orig_method}($parser, $chunk);
166 }
167 }
168 }
169 /**
170 * Breaks up data by tab characters, resulting in additional
171 * calls to the data handler
172 * @package XML_HTMLSax3
173 * @access protected
174 */
175 class XML_HTMLSax3_Tab {
176 /**
177 * Original handler object
178 * @var object
179 * @access private
180 */
181 var $orig_obj;
182 /**
183 * Original handler method
184 * @var string
185 * @access private
186 */
187 var $orig_method;
188 /**
189 * Constructs XML_HTMLSax3_Tab
190 * @param object handler object being decorated
191 * @param string original handler method
192 * @access protected
193 */
194 function __construct(&$orig_obj, $orig_method) {
195 $this->orig_obj =& $orig_obj;
196 $this->orig_method = $orig_method;
197 }
198 /**
199 * Breaks the data up by linefeeds
200 * @param XML_HTMLSax3
201 * @param string element data
202 * @access protected
203 */
204 function breakData(&$parser, $data) {
205 $data = explode("\t",$data);
206 foreach ( $data as $chunk ) {
207 $this->orig_obj->{$this->orig_method}($this, $chunk);
208 }
209 }
210 }
211 /**
212 * Breaks up data by XML entities and parses them with html_entity_decode(),
213 * resulting in additional calls to the data handler<br />
214 * Requires PHP 4.3.0+
215 * @package XML_HTMLSax3
216 * @access protected
217 */
218 class XML_HTMLSax3_Entities_Parsed {
219 /**
220 * Original handler object
221 * @var object
222 * @access private
223 */
224 var $orig_obj;
225 /**
226 * Original handler method
227 * @var string
228 * @access private
229 */
230 var $orig_method;
231 /**
232 * Constructs XML_HTMLSax3_Entities_Parsed
233 * @param object handler object being decorated
234 * @param string original handler method
235 * @access protected
236 */
237 function __construct(&$orig_obj, $orig_method) {
238 $this->orig_obj =& $orig_obj;
239 $this->orig_method = $orig_method;
240 }
241 /**
242 * Breaks the data up by XML entities
243 * @param XML_HTMLSax3
244 * @param string element data
245 * @access protected
246 */
247 function breakData(&$parser, $data) {
248 $data = preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
249 foreach ( $data as $chunk ) {
250 $chunk = html_entity_decode($chunk, ENT_NOQUOTES, HTML_ENTITIES_CHARSET);
251 $this->orig_obj->{$this->orig_method}($this, $chunk);
252 }
253 }
254 }
255 /**
256 * Breaks up data by XML entities but leaves them unparsed,
257 * resulting in additional calls to the data handler<br />
258 * @package XML_HTMLSax3
259 * @access protected
260 */
261 class XML_HTMLSax3_Entities_Unparsed {
262 /**
263 * Original handler object
264 * @var object
265 * @access private
266 */
267 var $orig_obj;
268 /**
269 * Original handler method
270 * @var string
271 * @access private
272 */
273 var $orig_method;
274 /**
275 * Constructs XML_HTMLSax3_Entities_Unparsed
276 * @param object handler object being decorated
277 * @param string original handler method
278 * @access protected
279 */
280 function __construct(&$orig_obj, $orig_method) {
281 $this->orig_obj =& $orig_obj;
282 $this->orig_method = $orig_method;
283 }
284 /**
285 * Breaks the data up by XML entities
286 * @param XML_HTMLSax3
287 * @param string element data
288 * @access protected
289 */
290 function breakData(&$parser, $data) {
291 $data = preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
292 foreach ( $data as $chunk ) {
293 $this->orig_obj->{$this->orig_method}($this, $chunk);
294 }
295 }
296 }
297
298 /**
299 * Strips the HTML comment markers or CDATA sections from an escape.
300 * If XML_OPTIONS_FULL_ESCAPES is on, this decorator is not used.<br />
301 * @package XML_HTMLSax3
302 * @access protected
303 */
304 class XML_HTMLSax3_Escape_Stripper {
305 /**
306 * Original handler object
307 * @var object
308 * @access private
309 */
310 var $orig_obj;
311 /**
312 * Original handler method
313 * @var string
314 * @access private
315 */
316 var $orig_method;
317 /**
318 * Constructs XML_HTMLSax3_Entities_Unparsed
319 * @param object handler object being decorated
320 * @param string original handler method
321 * @access protected
322 */
323 function __construct(&$orig_obj, $orig_method) {
324 $this->orig_obj =& $orig_obj;
325 $this->orig_method = $orig_method;
326 }
327 /**
328 * Breaks the data up by XML entities
329 * @param XML_HTMLSax3
330 * @param string element data
331 * @access protected
332 */
333 function strip(&$parser, $data) {
334 // Check for HTML comments first
335 if ( substr($data,0,2) == '--' ) {
336 $patterns = [
337 '/^\-\-/', // Opening comment: --
338 '/\-\-$/', // Closing comment: --
339 ];
340 $data = preg_replace($patterns,'',$data);
341
342 // Check for XML CDATA sections (note: don't do both!)
343 } else if ( substr($data,0,1) == '[' ) {
344 $patterns = [
345 '/^\[.*CDATA.*\[/s', // Opening CDATA
346 '/\].*\]$/s', // Closing CDATA
347 ];
348 $data = preg_replace($patterns,'',$data);
349 }
350
351 $this->orig_obj->{$this->orig_method}($this, $data);
352 }
353 }