2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // +----------------------------------------------------------------------+
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 1997-2002 The PHP Group |
8 // +----------------------------------------------------------------------+
9 // | This source file is subject to version 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 // +----------------------------------------------------------------------+
25 * Decorators for dealing with parser options
26 * @package XML_HTMLSax3
28 * @see XML_HTMLSax3::set_option
31 * Trims the contents of element data from whitespace at start and end
32 * @package XML_HTMLSax3
35 class XML_HTMLSax3_Trim {
37 * Original handler object
43 * Original handler method
49 * Constructs XML_HTMLSax3_Trim
50 * @param object handler object being decorated
51 * @param string original handler method
54 function XML_HTMLSax3_Trim(&$orig_obj, $orig_method) {
55 $this->orig_obj =& $orig_obj;
56 $this->orig_method = $orig_method;
61 * @param string element data
64 function trimData(&$parser, $data) {
67 $this->orig_obj->{$this->orig_method}($parser, $data);
72 * Coverts tag names to upper case
73 * @package XML_HTMLSax3
76 class XML_HTMLSax3_CaseFolding {
78 * Original handler object
84 * Original open handler method
88 var $orig_open_method;
90 * Original close handler method
94 var $orig_close_method;
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
102 function XML_HTMLSax3_CaseFolding(&$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;
108 * Folds up open tag callbacks
109 * @param XML_HTMLSax3
110 * @param string tag name
111 * @param array tag attributes
114 function foldOpen(&$parser, $tag, $attrs=array(), $empty = FALSE) {
115 $this->orig_obj->{$this->orig_open_method}($parser, strtoupper($tag), $attrs, $empty);
118 * Folds up close tag callbacks
119 * @param XML_HTMLSax3
120 * @param string tag name
123 function foldClose(&$parser, $tag, $empty = FALSE) {
124 $this->orig_obj->{$this->orig_close_method}($parser, strtoupper($tag), $empty);
128 * Breaks up data by linefeed characters, resulting in additional
129 * calls to the data handler
130 * @package XML_HTMLSax3
133 class XML_HTMLSax3_Linefeed {
135 * Original handler object
141 * Original handler method
147 * Constructs XML_HTMLSax3_LineFeed
148 * @param object handler object being decorated
149 * @param string original handler method
152 function XML_HTMLSax3_LineFeed(&$orig_obj, $orig_method) {
153 $this->orig_obj =& $orig_obj;
154 $this->orig_method = $orig_method;
157 * Breaks the data up by linefeeds
158 * @param XML_HTMLSax3
159 * @param string element data
162 function breakData(&$parser, $data) {
163 $data = explode("\n",$data);
164 foreach ( $data as $chunk ) {
165 $this->orig_obj->{$this->orig_method}($parser, $chunk);
170 * Breaks up data by tab characters, resulting in additional
171 * calls to the data handler
172 * @package XML_HTMLSax3
175 class XML_HTMLSax3_Tab {
177 * Original handler object
183 * Original handler method
189 * Constructs XML_HTMLSax3_Tab
190 * @param object handler object being decorated
191 * @param string original handler method
194 function XML_HTMLSax3_Tab(&$orig_obj, $orig_method) {
195 $this->orig_obj =& $orig_obj;
196 $this->orig_method = $orig_method;
199 * Breaks the data up by linefeeds
200 * @param XML_HTMLSax3
201 * @param string element data
204 function breakData(&$parser, $data) {
205 $data = explode("\t",$data);
206 foreach ( $data as $chunk ) {
207 $this->orig_obj->{$this->orig_method}($this, $chunk);
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
218 class XML_HTMLSax3_Entities_Parsed {
220 * Original handler object
226 * Original handler method
232 * Constructs XML_HTMLSax3_Entities_Parsed
233 * @param object handler object being decorated
234 * @param string original handler method
237 function XML_HTMLSax3_Entities_Parsed(&$orig_obj, $orig_method) {
238 $this->orig_obj =& $orig_obj;
239 $this->orig_method = $orig_method;
242 * Breaks the data up by XML entities
243 * @param XML_HTMLSax3
244 * @param string element data
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);
251 $this->orig_obj->{$this->orig_method}($this, $chunk);
256 * Compatibility with older PHP versions
258 if (version_compare(phpversion(), '4.3', '<') && !function_exists('html_entity_decode') ) {
259 function html_entity_decode($str, $style=ENT_NOQUOTES) {
261 array_flip(get_html_translation_table(HTML_ENTITIES,$style)));
265 * Breaks up data by XML entities but leaves them unparsed,
266 * resulting in additional calls to the data handler<br />
267 * @package XML_HTMLSax3
270 class XML_HTMLSax3_Entities_Unparsed {
272 * Original handler object
278 * Original handler method
284 * Constructs XML_HTMLSax3_Entities_Unparsed
285 * @param object handler object being decorated
286 * @param string original handler method
289 function XML_HTMLSax3_Entities_Unparsed(&$orig_obj, $orig_method) {
290 $this->orig_obj =& $orig_obj;
291 $this->orig_method = $orig_method;
294 * Breaks the data up by XML entities
295 * @param XML_HTMLSax3
296 * @param string element data
299 function breakData(&$parser, $data) {
300 $data = preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
301 foreach ( $data as $chunk ) {
302 $this->orig_obj->{$this->orig_method}($this, $chunk);
308 * Strips the HTML comment markers or CDATA sections from an escape.
309 * If XML_OPTIONS_FULL_ESCAPES is on, this decorator is not used.<br />
310 * @package XML_HTMLSax3
313 class XML_HTMLSax3_Escape_Stripper {
315 * Original handler object
321 * Original handler method
327 * Constructs XML_HTMLSax3_Entities_Unparsed
328 * @param object handler object being decorated
329 * @param string original handler method
332 function XML_HTMLSax3_Escape_Stripper(&$orig_obj, $orig_method) {
333 $this->orig_obj =& $orig_obj;
334 $this->orig_method = $orig_method;
337 * Breaks the data up by XML entities
338 * @param XML_HTMLSax3
339 * @param string element data
342 function strip(&$parser, $data) {
343 // Check for HTML comments first
344 if ( substr($data,0,2) == '--' ) {
346 '/^\-\-/', // Opening comment: --
347 '/\-\-$/', // Closing comment: --
349 $data = preg_replace($patterns,'',$data);
351 // Check for XML CDATA sections (note: don't do both!)
352 } else if ( substr($data,0,1) == '[' ) {
354 '/^\[.*CDATA.*\[/s', // Opening CDATA
355 '/\].*\]$/s', // Closing CDATA
357 $data = preg_replace($patterns,'',$data);
360 $this->orig_obj->{$this->orig_method}($this, $data);