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 // +----------------------------------------------------------------------+
26 * @package XML_HTMLSax3
30 * Define parser states
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);
41 * StartingState searches for the start of any XML tag
42 * @package XML_HTMLSax3
45 class XML_HTMLSax3_StartingState {
47 * @param XML_HTMLSax3_StateParser subclass
48 * @return constant XML_HTMLSAX3_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_HTMLSAX3_STATE_TAG;
62 * Decides which state to move one from after StartingState
63 * @package XML_HTMLSax3
66 class XML_HTMLSax3_TagState {
68 * @param XML_HTMLSax3_StateParser subclass
69 * @return constant the next state to move into
72 function parse(&$context) {
73 switch($context->ScanCharacter()) {
75 return XML_HTMLSAX3_STATE_CLOSING_TAG;
78 return XML_HTMLSAX3_STATE_PI;
81 return XML_HTMLSAX3_STATE_JASP;
84 return XML_HTMLSAX3_STATE_ESCAPE;
87 $context->unscanCharacter();
88 return XML_HTMLSAX3_STATE_OPENING_TAG;
93 * Dealing with closing XML tags
94 * @package XML_HTMLSax3
97 class XML_HTMLSax3_ClosingTagState {
99 * @param XML_HTMLSax3_StateParser subclass
100 * @return constant XML_HTMLSAX3_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_HTMLSAX3_STATE_START;
120 * Dealing with opening XML tags
121 * @package XML_HTMLSax3
124 class XML_HTMLSax3_OpeningTagState {
127 * @param string attribute name
128 * @param string attribute value
131 * @see XML_HTMLSax3_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 = NULL;
158 $context->unscanCharacter();
160 $Attributes[$attributename] = $attributevalue;
162 $context->ignoreWhitespace();
163 $attributename = $context->scanUntilCharacters("=/> \n\r\t");
169 * @param XML_HTMLSax3_StateParser subclass
170 * @return constant XML_HTMLSAX3_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_HTMLSAX3_STATE_START;
201 * Deals with XML escapes handling comments and CDATA correctly
202 * @package XML_HTMLSax3
205 class XML_HTMLSax3_EscapeState {
207 * @param XML_HTMLSax3_StateParser subclass
208 * @return constant XML_HTMLSAX3_STATE_START
211 function parse(&$context) {
212 $char = $context->ScanCharacter();
214 $char = $context->ScanCharacter();
216 $context->unscanCharacter();
217 $context->unscanCharacter();
218 $text = $context->scanUntilString('-->');
219 $text .= $context->scanCharacter();
220 $text .= $context->scanCharacter();
222 $context->unscanCharacter();
223 $text = $context->scanUntilString('>');
225 } else if ( $char == '[') {
226 $context->unscanCharacter();
227 $text = $context->scanUntilString(']>');
228 $text.= $context->scanCharacter();
230 $context->unscanCharacter();
231 $text = $context->scanUntilString('>');
234 $context->IgnoreCharacter();
236 $context->handler_object_escape->
237 {$context->handler_method_escape}($context->htmlsax, $text);
239 return XML_HTMLSAX3_STATE_START;
243 * Deals with JASP/ASP markup
244 * @package XML_HTMLSax3
247 class XML_HTMLSax3_JaspState {
249 * @param XML_HTMLSax3_StateParser subclass
250 * @return constant XML_HTMLSAX3_STATE_START
253 function parse(&$context) {
254 $text = $context->scanUntilString('%>');
256 $context->handler_object_jasp->
257 {$context->handler_method_jasp}($context->htmlsax, $text);
259 $context->IgnoreCharacter();
260 $context->IgnoreCharacter();
261 return XML_HTMLSAX3_STATE_START;
265 * Deals with XML processing instructions
266 * @package XML_HTMLSax3
269 class XML_HTMLSax3_PiState {
271 * @param XML_HTMLSax3_StateParser subclass
272 * @return constant XML_HTMLSAX3_STATE_START
275 function parse(&$context) {
276 $target = $context->scanUntilCharacters(" \n\r\t");
277 $data = $context->scanUntilString('?>');
279 $context->handler_object_pi->
280 {$context->handler_method_pi}($context->htmlsax, $target, $data);
282 $context->IgnoreCharacter();
283 $context->IgnoreCharacter();
284 return XML_HTMLSAX3_STATE_START;