PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ASTRA_Demo_Server/udrive/www/astra/interact/includes/safe_html/HTMLSax3/States.php

https://github.com/shafiqissani/ASTRA-College-Website
PHP | 287 lines | 174 code | 5 blank | 108 comment | 42 complexity | 3c0b08ae3376696664c7f120aaf3b940 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: States.php,v 1.1 2006/08/14 01:42:55 glendavies Exp $
  23. //
  24. /**
  25. * Parsing states.
  26. * @package XML_HTMLSax3
  27. * @version $Id: States.php,v 1.1 2006/08/14 01:42:55 glendavies Exp $
  28. */
  29. /**
  30. * Define parser states
  31. */
  32. define('XML_HTMLSAX3_STATE_STOP', 0);
  33. define('XML_HTMLSAX3_STATE_START', 1);
  34. define('XML_HTMLSAX3_STATE_TAG', 2);
  35. define('XML_HTMLSAX3_STATE_OPENING_TAG', 3);
  36. define('XML_HTMLSAX3_STATE_CLOSING_TAG', 4);
  37. define('XML_HTMLSAX3_STATE_ESCAPE', 6);
  38. define('XML_HTMLSAX3_STATE_JASP', 7);
  39. define('XML_HTMLSAX3_STATE_PI', 8);
  40. /**
  41. * StartingState searches for the start of any XML tag
  42. * @package XML_HTMLSax3
  43. * @access protected
  44. */
  45. class XML_HTMLSax3_StartingState {
  46. /**
  47. * @param XML_HTMLSax3_StateParser subclass
  48. * @return constant XML_HTMLSAX3_STATE_TAG
  49. * @access protected
  50. */
  51. function parse(&$context) {
  52. $data = $context->scanUntilString('<');
  53. if ($data != '') {
  54. $context->handler_object_data->
  55. {$context->handler_method_data}($context->htmlsax, $data);
  56. }
  57. $context->IgnoreCharacter();
  58. return XML_HTMLSAX3_STATE_TAG;
  59. }
  60. }
  61. /**
  62. * Decides which state to move one from after StartingState
  63. * @package XML_HTMLSax3
  64. * @access protected
  65. */
  66. class XML_HTMLSax3_TagState {
  67. /**
  68. * @param XML_HTMLSax3_StateParser subclass
  69. * @return constant the next state to move into
  70. * @access protected
  71. */
  72. function parse(&$context) {
  73. switch($context->ScanCharacter()) {
  74. case '/':
  75. return XML_HTMLSAX3_STATE_CLOSING_TAG;
  76. break;
  77. case '?':
  78. return XML_HTMLSAX3_STATE_PI;
  79. break;
  80. case '%':
  81. return XML_HTMLSAX3_STATE_JASP;
  82. break;
  83. case '!':
  84. return XML_HTMLSAX3_STATE_ESCAPE;
  85. break;
  86. default:
  87. $context->unscanCharacter();
  88. return XML_HTMLSAX3_STATE_OPENING_TAG;
  89. }
  90. }
  91. }
  92. /**
  93. * Dealing with closing XML tags
  94. * @package XML_HTMLSax3
  95. * @access protected
  96. */
  97. class XML_HTMLSax3_ClosingTagState {
  98. /**
  99. * @param XML_HTMLSax3_StateParser subclass
  100. * @return constant XML_HTMLSAX3_STATE_START
  101. * @access protected
  102. */
  103. function parse(&$context) {
  104. $tag = $context->scanUntilCharacters('/>');
  105. if ($tag != '') {
  106. $char = $context->scanCharacter();
  107. if ($char == '/') {
  108. $char = $context->scanCharacter();
  109. if ($char != '>') {
  110. $context->unscanCharacter();
  111. }
  112. }
  113. $context->handler_object_element->
  114. {$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
  115. }
  116. return XML_HTMLSAX3_STATE_START;
  117. }
  118. }
  119. /**
  120. * Dealing with opening XML tags
  121. * @package XML_HTMLSax3
  122. * @access protected
  123. */
  124. class XML_HTMLSax3_OpeningTagState {
  125. /**
  126. * Handles attributes
  127. * @param string attribute name
  128. * @param string attribute value
  129. * @return void
  130. * @access protected
  131. * @see XML_HTMLSax3_AttributeStartState
  132. */
  133. function parseAttributes(&$context) {
  134. $Attributes = array();
  135. $context->ignoreWhitespace();
  136. $attributename = $context->scanUntilCharacters("=/> \n\r\t");
  137. while ($attributename != '') {
  138. $attributevalue = NULL;
  139. $context->ignoreWhitespace();
  140. $char = $context->scanCharacter();
  141. if ($char == '=') {
  142. $context->ignoreWhitespace();
  143. $char = $context->ScanCharacter();
  144. if ($char == '"') {
  145. $attributevalue= $context->scanUntilString('"');
  146. $context->IgnoreCharacter();
  147. } else if ($char == "'") {
  148. $attributevalue = $context->scanUntilString("'");
  149. $context->IgnoreCharacter();
  150. } else {
  151. $context->unscanCharacter();
  152. $attributevalue =
  153. $context->scanUntilCharacters("> \n\r\t");
  154. }
  155. } else if ($char !== NULL) {
  156. $attributevalue = NULL;
  157. $context->unscanCharacter();
  158. }
  159. $Attributes[$attributename] = $attributevalue;
  160. $context->ignoreWhitespace();
  161. $attributename = $context->scanUntilCharacters("=/> \n\r\t");
  162. }
  163. return $Attributes;
  164. }
  165. /**
  166. * @param XML_HTMLSax3_StateParser subclass
  167. * @return constant XML_HTMLSAX3_STATE_START
  168. * @access protected
  169. */
  170. function parse(&$context) {
  171. $tag = $context->scanUntilCharacters("/> \n\r\t");
  172. if ($tag != '') {
  173. $this->attrs = array();
  174. $Attributes = $this->parseAttributes($context);
  175. $char = $context->scanCharacter();
  176. if ($char == '/') {
  177. $char = $context->scanCharacter();
  178. if ($char != '>') {
  179. $context->unscanCharacter();
  180. }
  181. $context->handler_object_element->
  182. {$context->handler_method_opening}($context->htmlsax, $tag,
  183. $Attributes, TRUE);
  184. $context->handler_object_element->
  185. {$context->handler_method_closing}($context->htmlsax, $tag,
  186. TRUE);
  187. } else {
  188. $context->handler_object_element->
  189. {$context->handler_method_opening}($context->htmlsax, $tag,
  190. $Attributes, FALSE);
  191. }
  192. }
  193. return XML_HTMLSAX3_STATE_START;
  194. }
  195. }
  196. /**
  197. * Deals with XML escapes handling comments and CDATA correctly
  198. * @package XML_HTMLSax3
  199. * @access protected
  200. */
  201. class XML_HTMLSax3_EscapeState {
  202. /**
  203. * @param XML_HTMLSax3_StateParser subclass
  204. * @return constant XML_HTMLSAX3_STATE_START
  205. * @access protected
  206. */
  207. function parse(&$context) {
  208. $char = $context->ScanCharacter();
  209. if ($char == '-') {
  210. $char = $context->ScanCharacter();
  211. if ($char == '-') {
  212. $context->unscanCharacter();
  213. $context->unscanCharacter();
  214. $text = $context->scanUntilString('-->');
  215. $text .= $context->scanCharacter();
  216. $text .= $context->scanCharacter();
  217. } else {
  218. $context->unscanCharacter();
  219. $text = $context->scanUntilString('>');
  220. }
  221. } else if ( $char == '[') {
  222. $context->unscanCharacter();
  223. $text = $context->scanUntilString(']>');
  224. $text.= $context->scanCharacter();
  225. } else {
  226. $context->unscanCharacter();
  227. $text = $context->scanUntilString('>');
  228. }
  229. $context->IgnoreCharacter();
  230. if ($text != '') {
  231. $context->handler_object_escape->
  232. {$context->handler_method_escape}($context->htmlsax, $text);
  233. }
  234. return XML_HTMLSAX3_STATE_START;
  235. }
  236. }
  237. /**
  238. * Deals with JASP/ASP markup
  239. * @package XML_HTMLSax3
  240. * @access protected
  241. */
  242. class XML_HTMLSax3_JaspState {
  243. /**
  244. * @param XML_HTMLSax3_StateParser subclass
  245. * @return constant XML_HTMLSAX3_STATE_START
  246. * @access protected
  247. */
  248. function parse(&$context) {
  249. $text = $context->scanUntilString('%>');
  250. if ($text != '') {
  251. $context->handler_object_jasp->
  252. {$context->handler_method_jasp}($context->htmlsax, $text);
  253. }
  254. $context->IgnoreCharacter();
  255. $context->IgnoreCharacter();
  256. return XML_HTMLSAX3_STATE_START;
  257. }
  258. }
  259. /**
  260. * Deals with XML processing instructions
  261. * @package XML_HTMLSax3
  262. * @access protected
  263. */
  264. class XML_HTMLSax3_PiState {
  265. /**
  266. * @param XML_HTMLSax3_StateParser subclass
  267. * @return constant XML_HTMLSAX3_STATE_START
  268. * @access protected
  269. */
  270. function parse(&$context) {
  271. $target = $context->scanUntilCharacters(" \n\r\t");
  272. $data = $context->scanUntilString('?>');
  273. if ($data != '') {
  274. $context->handler_object_pi->
  275. {$context->handler_method_pi}($context->htmlsax, $target, $data);
  276. }
  277. $context->IgnoreCharacter();
  278. $context->IgnoreCharacter();
  279. return XML_HTMLSAX3_STATE_START;
  280. }
  281. }
  282. ?>