PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/XML/HTMLSax3/Decorators.php

https://github.com/limb-php-framework/limb-example-macro
PHP | 363 lines | 119 code | 3 blank | 241 comment | 9 complexity | 991955ef57adeeff9d05ad7216fb1c7e MD5 | raw file
  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 to version 2.02 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.1 2004/06/02 14:09:19 hfuecks Exp $
  23. //
  24. /**
  25. * Decorators for dealing with parser options
  26. * @package XML_HTMLSax3
  27. * @version $Id: Decorators.php,v 1.1 2004/06/02 14:09:19 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 XML_HTMLSax3_Trim(&$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 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;
  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 XML_HTMLSax3_LineFeed(&$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 XML_HTMLSax3_Tab(&$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 XML_HTMLSax3_Entities_Parsed(&$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);
  251. $this->orig_obj->{$this->orig_method}($this, $chunk);
  252. }
  253. }
  254. }
  255. /**
  256. * Compatibility with older PHP versions
  257. */
  258. if (version_compare(phpversion(), '4.3', '<') && !function_exists('html_entity_decode') ) {
  259. function html_entity_decode($str, $style=ENT_NOQUOTES) {
  260. return strtr($str,
  261. array_flip(get_html_translation_table(HTML_ENTITIES,$style)));
  262. }
  263. }
  264. /**
  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
  268. * @access protected
  269. */
  270. class XML_HTMLSax3_Entities_Unparsed {
  271. /**
  272. * Original handler object
  273. * @var object
  274. * @access private
  275. */
  276. var $orig_obj;
  277. /**
  278. * Original handler method
  279. * @var string
  280. * @access private
  281. */
  282. var $orig_method;
  283. /**
  284. * Constructs XML_HTMLSax3_Entities_Unparsed
  285. * @param object handler object being decorated
  286. * @param string original handler method
  287. * @access protected
  288. */
  289. function XML_HTMLSax3_Entities_Unparsed(&$orig_obj, $orig_method) {
  290. $this->orig_obj =& $orig_obj;
  291. $this->orig_method = $orig_method;
  292. }
  293. /**
  294. * Breaks the data up by XML entities
  295. * @param XML_HTMLSax3
  296. * @param string element data
  297. * @access protected
  298. */
  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);
  303. }
  304. }
  305. }
  306. /**
  307. * Strips the HTML comment markers or CDATA sections from an escape.
  308. * If XML_OPTIONS_FULL_ESCAPES is on, this decorator is not used.<br />
  309. * @package XML_HTMLSax3
  310. * @access protected
  311. */
  312. class XML_HTMLSax3_Escape_Stripper {
  313. /**
  314. * Original handler object
  315. * @var object
  316. * @access private
  317. */
  318. var $orig_obj;
  319. /**
  320. * Original handler method
  321. * @var string
  322. * @access private
  323. */
  324. var $orig_method;
  325. /**
  326. * Constructs XML_HTMLSax3_Entities_Unparsed
  327. * @param object handler object being decorated
  328. * @param string original handler method
  329. * @access protected
  330. */
  331. function XML_HTMLSax3_Escape_Stripper(&$orig_obj, $orig_method) {
  332. $this->orig_obj =& $orig_obj;
  333. $this->orig_method = $orig_method;
  334. }
  335. /**
  336. * Breaks the data up by XML entities
  337. * @param XML_HTMLSax3
  338. * @param string element data
  339. * @access protected
  340. */
  341. function strip(&$parser, $data) {
  342. // Check for HTML comments first
  343. if ( substr($data,0,2) == '--' ) {
  344. $patterns = array(
  345. '/^\-\-/', // Opening comment: --
  346. '/\-\-$/', // Closing comment: --
  347. );
  348. $data = preg_replace($patterns,'',$data);
  349. // Check for XML CDATA sections (note: don't do both!)
  350. } else if ( substr($data,0,1) == '[' ) {
  351. $patterns = array(
  352. '/^\[.*CDATA.*\[/s', // Opening CDATA
  353. '/\].*\]$/s', // Closing CDATA
  354. );
  355. $data = preg_replace($patterns,'',$data);
  356. }
  357. $this->orig_obj->{$this->orig_method}($this, $data);
  358. }
  359. }
  360. ?>