Source for file HTMLSax3.php
Documentation is available at HTMLSax3.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
// | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more |
// | Authors: Many @ Sitepointforums Advanced PHP Forums |
// +----------------------------------------------------------------------+
// $Id: HTMLSax3.php 22138 2007-06-01 10:19:14Z markwest $
* @version $Id: HTMLSax3.php 22138 2007-06-01 10:19:14Z markwest $
define('XML_HTMLSAX3', 'XML/');
* Instance of user front end class to be passed to callbacks
* User defined object for handling elements
var $handler_object_element;
* User defined open tag handler method
var $handler_method_opening;
* User defined close tag handler method
var $handler_method_closing;
* User defined object for handling data in elements
var $handler_object_data;
* User defined data handler method
var $handler_method_data;
* User defined object for handling processing instructions
* User defined processing instruction handler method
* User defined object for handling JSP/ASP tags
var $handler_object_jasp;
* User defined JSP/ASP handler method
var $handler_method_jasp;
* User defined object for handling XML escapes
var $handler_object_escape;
* User defined XML escape handler method
var $handler_method_escape;
* User defined handler object or NullHandler
* Parser options determining parsing behavior
var $parser_options = array();
* XML document being parsed
* Position in XML document relative to start (0)
* Length of the XML document in characters
* Constructs XML_HTMLSax3_StateParser setting up states
* @var XML_HTMLSax3 instance of user front end class
$this->htmlsax = & $htmlsax;
* Moves the position back one character
* Moves the position forward one character
* Returns the next character from the XML document or void if at end
if ($this->position < $this->length) {
return $this->rawtext{$this->position++ };
* Returns a string from the current position to the next occurance
* @param string string to search until
$start = $this->position;
$this->position = strpos($this->rawtext, $string, $start);
if ($this->position === FALSE) {
$this->position = $this->length;
return substr($this->rawtext, $start, $this->position - $start);
* Returns a string from the current position until the first instance of
* one of the characters in the supplied string argument
* @param string string to search until
* Moves the position forward past any whitespace characters
* Begins the parsing operation, setting up any decorators, depending on
* parse options invoking _parse() to execute parsing
* @param string XML document to parse
if ($this->parser_options['XML_OPTION_TRIM_DATA_NODES']== 1) {
$this->handler_object_data,
$this->handler_method_data);
$this->handler_object_data = & $decorator;
$this->handler_method_data = 'trimData';
if ($this->parser_options['XML_OPTION_CASE_FOLDING']== 1) {
$this->handler_object_element,
$this->handler_method_opening,
$this->handler_method_closing);
$this->handler_object_element = & $open_decor;
$this->handler_method_opening = 'foldOpen';
$this->handler_method_closing = 'foldClose';
if ($this->parser_options['XML_OPTION_LINEFEED_BREAK']== 1) {
$this->handler_object_data,
$this->handler_method_data);
$this->handler_object_data = & $decorator;
$this->handler_method_data = 'breakData';
if ($this->parser_options['XML_OPTION_TAB_BREAK']== 1) {
$this->handler_object_data,
$this->handler_method_data);
$this->handler_object_data = & $decorator;
$this->handler_method_data = 'breakData';
if ($this->parser_options['XML_OPTION_ENTITIES_UNPARSED']== 1) {
$this->handler_object_data,
$this->handler_method_data);
$this->handler_object_data = & $decorator;
$this->handler_method_data = 'breakData';
if ($this->parser_options['XML_OPTION_ENTITIES_PARSED']== 1) {
$this->handler_object_data,
$this->handler_method_data);
$this->handler_object_data = & $decorator;
$this->handler_method_data = 'breakData';
// Note switched on by default
if ($this->parser_options['XML_OPTION_STRIP_ESCAPES']== 1) {
$this->handler_object_escape,
$this->handler_method_escape);
$this->handler_object_escape = & $decorator;
$this->handler_method_escape = 'strip';
$this->length = strlen($data);
* Performs the parsing itself, delegating calls to a specific parser
* @param constant state object to parse with
function _parse($state = XML_HTMLSAX3_STATE_START) {
$state = $this->State[$state]->parse($this);
$this->position < $this->length);
* Parser for PHP Versions below 4.3.0. Uses a slower parsing mechanism than
* the equivalent PHP 4.3.0+ subclass of StateParser
* @see XML_HTMLSax3_StateParser_Gtet430
* Constructs XML_HTMLSax3_StateParser_Lt430 defining available
* @var XML_HTMLSax3 instance of user front end class
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
* Returns a string from the current position until the first instance of
* one of the characters in the supplied string argument
* @param string string to search until
$startpos = $this->position;
while ($this->position < $this->length && strpos($string, $this->rawtext{$this->position}) === FALSE) {
return substr($this->rawtext, $startpos, $this->position - $startpos);
* Moves the position forward past any whitespace characters
while ($this->position < $this->length &&
strpos(" \n\r\t", $this->rawtext{$this->position}) !== FALSE) {
* Begins the parsing operation, setting up the unparsed XML entities
* decorator if necessary then delegating further work to parent
* @param string XML document to parse
* Parser for PHP Versions equal to or greater than 4.3.0. Uses a faster
* parsing mechanism than the equivalent PHP < 4.3.0 subclass of StateParser
* @see XML_HTMLSax3_StateParser_Lt430
* Constructs XML_HTMLSax3_StateParser_Gtet430 defining available
* @var XML_HTMLSax3 instance of user front end class
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
* Returns a string from the current position until the first instance of
* one of the characters in the supplied string argument.
* @param string string to search until
$startpos = $this->position;
$length = strcspn($this->rawtext, $string, $startpos);
$this->position += $length;
return substr($this->rawtext, $startpos, $length);
* Moves the position forward past any whitespace characters
$this->position += strspn($this->rawtext, " \n\r\t", $this->position);
* Begins the parsing operation, setting up the parsed and unparsed
* XML entity decorators if necessary then delegating further work
* @param string XML document to parse
* Default NullHandler for methods which were not set by user
* Generic handler method which does nothing
* User interface class. All user calls should only be made to this class
* Instance of concrete subclass of XML_HTMLSax3_StateParser
* @var XML_HTMLSax3_StateParser
* Constructs XML_HTMLSax3 selecting concrete StateParser subclass
* depending on PHP version being used as well as setting the default
* NullHandler for all callbacks<br />
* $myHandler = & new MyHandler();
* $parser = new XML_HTMLSax3();
* $parser->set_object($myHandler);
* $parser->set_option('XML_OPTION_CASE_FOLDING');
* $parser->set_element_handler('myOpenHandler','myCloseHandler');
* $parser->set_data_handler('myDataHandler');
* Sets the user defined handler object. Returns a PEAR Error
* if supplied argument is not an object.
* @param object handler object containing SAX callback methods
$this->state_parser->handler_default = & $object;
PEAR::raiseError('XML_HTMLSax3::set_object requires '.
* Sets a parser option. By default all options are switched off.
* Returns a PEAR Error if option is invalid<br />
* <b>Available options:</b>
* <li>XML_OPTION_TRIM_DATA_NODES: trim whitespace off the beginning
* and end of data passed to the data handler</li>
* <li>XML_OPTION_LINEFEED_BREAK: linefeeds result in additional data
* <li>XML_OPTION_TAB_BREAK: tabs result in additional data handler
* <li>XML_OPTION_ENTITIES_UNPARSED: XML entities are returned as
* seperate data handler calls in unparsed form</li>
* <li>XML_OPTION_ENTITIES_PARSED: (PHP 4.3.0+ only) XML entities are
* returned as seperate data handler calls and are parsed with
* PHP's html_entity_decode() function</li>
* <li>XML_OPTION_STRIP_ESCAPES: strips out the -- -- comment markers
* or CDATA markup inside an XML escape, if found.</li>
* To get HTMLSax to behave in the same way as the native PHP SAX parser,
* using it's default state, you need to switch on XML_OPTION_LINEFEED_BREAK,
* XML_OPTION_ENTITIES_PARSED and XML_OPTION_CASE_FOLDING
* @param string name of parser option
* @param int (optional) 1 to switch on, 0 for off
$this->state_parser->parser_options[$name] = $value;
PEAR::raiseError('XML_HTMLSax3::set_option('. $name. ') illegal');
* Sets the data handler method which deals with the contents of XML
* The handler method must accept two arguments, the first being an
* instance of XML_HTMLSax3 and the second being the contents of an
* function myDataHander(& $parser,$data){}
* @param string name of method
$this->state_parser->handler_object_data = & $this->state_parser->handler_default;
$this->state_parser->handler_method_data = $data_method;
* Sets the open and close tag handlers
* <br />The open handler method must accept three arguments; the parser,
* the tag name and an array of attributes e.g.
* function myOpenHander(& $parser,$tagname,$attrs=array()){}
* The close handler method must accept two arguments; the parser and
* function myCloseHander(& $parser,$tagname){}
* @param string name of open method
* @param string name of close method
$this->state_parser->handler_object_element = & $this->state_parser->handler_default;
$this->state_parser->handler_method_opening = $opening_method;
$this->state_parser->handler_method_closing = $closing_method;
* Sets the processing instruction handler method e.g. for PHP open
* The handler method must accept three arguments; the parser, the
* PI target and data inside the PI
* function myPIHander(& $parser,$target, $data){}
* @param string name of method
$this->state_parser->handler_object_pi = & $this->state_parser->handler_default;
$this->state_parser->handler_method_pi = $pi_method;
* Sets the XML escape handler method e.g. for comments and doctype
* The handler method must accept two arguments; the parser and the
* contents of the escaped section
* function myEscapeHander(& $parser, $data){}
* @param string name of method
$this->state_parser->handler_object_escape = & $this->state_parser->handler_default;
$this->state_parser->handler_method_escape = $escape_method;
* Sets the JSP/ASP markup handler<br />
* The handler method must accept two arguments; the parser and
* function myJaspHander(& $parser, $data){}
* @param string name of method
$this->state_parser->handler_object_jasp = & $this->state_parser->handler_default;
$this->state_parser->handler_method_jasp = $jasp_method;
* Returns the current string position of the "cursor" inside the XML
* <br />Intended for use from within a user defined handler called
* via the $parser reference e.g.
* function myDataHandler(& $parser,$data) {
* echo( 'Current position: '.$parser->get_current_position() );
return $this->state_parser->position;
* Returns the string length of the XML document being parsed
return $this->state_parser->length;
* @param string XML document
$this->state_parser->parse($data);
|