9f16380e91288ba1ba5dae8c2ccb835a37cc59f4
[lhc/web/www.git] / www / plugins-dist / safehtml / lib / safehtml / classes / HTMLSax3 / States.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: States.php,v 1.3 2007/10/29 21:41:35 hfuecks Exp $
23 //
24 /**
25 * Parsing states.
26 * @package XML_HTMLSax3
27 * @version $Id: States.php,v 1.3 2007/10/29 21:41:35 hfuecks 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 = [];
135
136 $context->ignoreWhitespace();
137 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
138 while ($attributename != '') {
139 $attributevalue = null;
140 $context->ignoreWhitespace();
141 $char = $context->scanCharacter();
142 if ($char == '=') {
143 $context->ignoreWhitespace();
144 $char = $context->ScanCharacter();
145 if ($char == '"') {
146 $attributevalue= $context->scanUntilString('"');
147 $context->IgnoreCharacter();
148 } else if ($char == "'") {
149 $attributevalue = $context->scanUntilString("'");
150 $context->IgnoreCharacter();
151 } else {
152 $context->unscanCharacter();
153 $attributevalue =
154 $context->scanUntilCharacters("> \n\r\t");
155 }
156 } else if ($char !== null) {
157 $attributevalue = null;
158 $context->unscanCharacter();
159 }
160 $Attributes[$attributename] = $attributevalue;
161
162 $context->ignoreWhitespace();
163 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
164 }
165 return $Attributes;
166 }
167
168 /**
169 * @param XML_HTMLSax3_StateParser subclass
170 * @return constant XML_HTMLSAX3_STATE_START
171 * @access protected
172 */
173 function parse(&$context) {
174 $tag = $context->scanUntilCharacters("/> \n\r\t");
175 if ($tag != '') {
176 $this->attrs = [];
177 $Attributes = $this->parseAttributes($context);
178 $char = $context->scanCharacter();
179 if ($char == '/') {
180 $char = $context->scanCharacter();
181 if ($char != '>') {
182 $context->unscanCharacter();
183 }
184 $context->handler_object_element->
185 {$context->handler_method_opening}($context->htmlsax, $tag,
186 $Attributes, TRUE);
187 $context->handler_object_element->
188 {$context->handler_method_closing}($context->htmlsax, $tag,
189 TRUE);
190 } else {
191 $context->handler_object_element->
192 {$context->handler_method_opening}($context->htmlsax, $tag,
193 $Attributes, FALSE);
194 }
195 }
196 return XML_HTMLSAX3_STATE_START;
197 }
198 }
199
200 /**
201 * Deals with XML escapes handling comments and CDATA correctly
202 * @package XML_HTMLSax3
203 * @access protected
204 */
205 class XML_HTMLSax3_EscapeState {
206 /**
207 * @param XML_HTMLSax3_StateParser subclass
208 * @return constant XML_HTMLSAX3_STATE_START
209 * @access protected
210 */
211 function parse(&$context) {
212 $char = $context->ScanCharacter();
213 if ($char == '-') {
214 $char = $context->ScanCharacter();
215 if ($char == '-') {
216 $context->unscanCharacter();
217 $context->unscanCharacter();
218 $text = $context->scanUntilString('-->');
219 $text .= $context->scanCharacter();
220 $text .= $context->scanCharacter();
221 } else {
222 $context->unscanCharacter();
223 $text = $context->scanUntilString('>');
224 }
225 } else if ( $char == '[') {
226 $context->unscanCharacter();
227 $text = $context->scanUntilString(']>');
228 $text.= $context->scanCharacter();
229 } else {
230 $context->unscanCharacter();
231 $text = $context->scanUntilString('>');
232 }
233
234 $context->IgnoreCharacter();
235 if ($text != '') {
236 $context->handler_object_escape->
237 {$context->handler_method_escape}($context->htmlsax, $text);
238 }
239 return XML_HTMLSAX3_STATE_START;
240 }
241 }
242 /**
243 * Deals with JASP/ASP markup
244 * @package XML_HTMLSax3
245 * @access protected
246 */
247 class XML_HTMLSax3_JaspState {
248 /**
249 * @param XML_HTMLSax3_StateParser subclass
250 * @return constant XML_HTMLSAX3_STATE_START
251 * @access protected
252 */
253 function parse(&$context) {
254 $text = $context->scanUntilString('%>');
255 if ($text != '') {
256 $context->handler_object_jasp->
257 {$context->handler_method_jasp}($context->htmlsax, $text);
258 }
259 $context->IgnoreCharacter();
260 $context->IgnoreCharacter();
261 return XML_HTMLSAX3_STATE_START;
262 }
263 }
264 /**
265 * Deals with XML processing instructions
266 * @package XML_HTMLSax3
267 * @access protected
268 */
269 class XML_HTMLSax3_PiState {
270 /**
271 * @param XML_HTMLSax3_StateParser subclass
272 * @return constant XML_HTMLSAX3_STATE_START
273 * @access protected
274 */
275 function parse(&$context) {
276 $target = $context->scanUntilCharacters(" \n\r\t");
277 $data = $context->scanUntilString('?>');
278 if ($data != '') {
279 $context->handler_object_pi->
280 {$context->handler_method_pi}($context->htmlsax, $target, $data);
281 }
282 $context->IgnoreCharacter();
283 $context->IgnoreCharacter();
284 return XML_HTMLSAX3_STATE_START;
285 }
286 }