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 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 // +----------------------------------------------------------------------+
22 // $Id: XML_HTMLSax_States.php 2291 2004-11-16 19:35:41Z joel $
25 * Main parser components
26 * @package XML_HTMLSax
27 * @version $Id: XML_HTMLSax_States.php 2291 2004-11-16 19:35:41Z joel $
30 * Define parser states
32 define('XML_HTMLSAX_STATE_STOP', 0);
33 define('XML_HTMLSAX_STATE_START', 1);
34 define('XML_HTMLSAX_STATE_TAG', 2);
35 define('XML_HTMLSAX_STATE_OPENING_TAG', 3);
36 define('XML_HTMLSAX_STATE_CLOSING_TAG', 4);
37 define('XML_HTMLSAX_STATE_ESCAPE', 6);
38 define('XML_HTMLSAX_STATE_JASP', 7);
39 define('XML_HTMLSAX_STATE_PI', 8);
41 * StartingState searches for the start of any XML tag
42 * @package XML_HTMLSax
45 class XML_HTMLSax_StartingState {
47 * @param XML_HTMLSax_StateParser subclass
48 * @return constant XML_HTMLSAX_STATE_TAG
51 function parse(&$context) {
52 $data = $context->scanUntilString('<');
54 $context->handler_object_data->
55 {$context->handler_method_data}($context->htmlsax, $data);
57 $context->IgnoreCharacter();
58 return XML_HTMLSAX_STATE_TAG;
62 * Decides which state to move one from after StartingState
63 * @package XML_HTMLSax
66 class XML_HTMLSax_TagState {
68 * @param XML_HTMLSax_StateParser subclass
69 * @return constant the next state to move into
72 function parse(&$context) {
73 switch($context->ScanCharacter()) {
75 return XML_HTMLSAX_STATE_CLOSING_TAG;
78 return XML_HTMLSAX_STATE_PI;
81 return XML_HTMLSAX_STATE_JASP;
84 return XML_HTMLSAX_STATE_ESCAPE;
87 $context->unscanCharacter();
88 return XML_HTMLSAX_STATE_OPENING_TAG;
93 * Dealing with closing XML tags
94 * @package XML_HTMLSax
97 class XML_HTMLSax_ClosingTagState {
99 * @param XML_HTMLSax_StateParser subclass
100 * @return constant XML_HTMLSAX_STATE_START
103 function parse(&$context) {
104 $tag = $context->scanUntilCharacters('/>');
106 $char = $context->scanCharacter();
108 $char = $context->scanCharacter();
110 $context->unscanCharacter();
113 $context->handler_object_element->
114 {$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
116 return XML_HTMLSAX_STATE_START;
120 * Dealing with opening XML tags
121 * @package XML_HTMLSax
124 class XML_HTMLSax_OpeningTagState {
127 * @param string attribute name
128 * @param string attribute value
131 * @see XML_HTMLSax_AttributeStartState
133 function parseAttributes(&$context) {
134 $Attributes = array();
136 $context->ignoreWhitespace();
137 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
138 while ($attributename != '') {
139 $attributevalue = NULL;
140 $context->ignoreWhitespace();
141 $char = $context->scanCharacter();
143 $context->ignoreWhitespace();
144 $char = $context->ScanCharacter();
146 $attributevalue= $context->scanUntilString('"');
147 $context->IgnoreCharacter();
148 } else if ($char == "'") {
149 $attributevalue = $context->scanUntilString("'");
150 $context->IgnoreCharacter();
152 $context->unscanCharacter();
154 $context->scanUntilCharacters("> \n\r\t");
156 } else if ($char !== NULL) {
157 $attributevalue = true;
158 $context->unscanCharacter();
160 $Attributes[$attributename] = $attributevalue;
162 $context->ignoreWhitespace();
163 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
169 * @param XML_HTMLSax_StateParser subclass
170 * @return constant XML_HTMLSAX_STATE_START
173 function parse(&$context) {
174 $tag = $context->scanUntilCharacters("/> \n\r\t");
176 $this->attrs = array();
177 $Attributes = $this->parseAttributes($context);
178 $char = $context->scanCharacter();
180 $char = $context->scanCharacter();
182 $context->unscanCharacter();
184 $context->handler_object_element->
185 {$context->handler_method_opening}($context->htmlsax, $tag,
187 $context->handler_object_element->
188 {$context->handler_method_closing}($context->htmlsax, $tag,
191 $context->handler_object_element->
192 {$context->handler_method_opening}($context->htmlsax, $tag,
196 return XML_HTMLSAX_STATE_START;
201 * Deals with XML escapes handling comments and CDATA correctly
202 * @package XML_HTMLSax
205 class XML_HTMLSax_EscapeState {
207 * @param XML_HTMLSax_StateParser subclass
208 * @return constant XML_HTMLSAX_STATE_START
211 function parse(&$context) {
212 if ($context->parser_options['XML_OPTION_FULL_ESCAPES']==0) {
213 $char = $context->ScanCharacter();
215 $char = $context->ScanCharacter();
217 $text = $context->scanUntilString('-->');
218 $context->IgnoreCharacter();
219 $context->IgnoreCharacter();
221 $context->unscanCharacter();
222 $text = $context->scanUntilString('>');
224 } else if ( $char == '[') {
225 $context->scanUntilString('CDATA[');
226 for ( $i=0;$i<6;$i++ ) {
227 $context->IgnoreCharacter();
229 $text = $context->scanUntilString(']]>');
230 $context->IgnoreCharacter();
231 $context->IgnoreCharacter();
233 $context->unscanCharacter();
234 $text = $context->scanUntilString('>');
237 $text = $context->scanUntilString('>');
239 $context->IgnoreCharacter();
241 $context->handler_object_escape->
242 {$context->handler_method_escape}($context->htmlsax, $text);
244 return XML_HTMLSAX_STATE_START;
248 * Deals with JASP/ASP markup
249 * @package XML_HTMLSax
252 class XML_HTMLSax_JaspState {
254 * @param XML_HTMLSax_StateParser subclass
255 * @return constant XML_HTMLSAX_STATE_START
258 function parse(&$context) {
259 $text = $context->scanUntilString('%>');
261 $context->handler_object_jasp->
262 {$context->handler_method_jasp}($context->htmlsax, $text);
264 $context->IgnoreCharacter();
265 $context->IgnoreCharacter();
266 return XML_HTMLSAX_STATE_START;
270 * Deals with XML processing instructions
271 * @package XML_HTMLSax
274 class XML_HTMLSax_PiState {
276 * @param XML_HTMLSax_StateParser subclass
277 * @return constant XML_HTMLSAX_STATE_START
280 function parse(&$context) {
281 $target = $context->scanUntilCharacters(" \n\r\t");
282 $data = $context->scanUntilString('?>');
284 $context->handler_object_pi->
285 {$context->handler_method_pi}($context->htmlsax, $target, $data);
287 $context->IgnoreCharacter();
288 $context->IgnoreCharacter();
289 return XML_HTMLSAX_STATE_START;