Skip to content
Snippets Groups Projects
Commit a1c0f40a authored by Romain Beauxis's avatar Romain Beauxis Committed by David Prévot
Browse files

Imported Debian patch 3.0.0+cvs01112007-2

parent b8c9c447
No related branches found
No related tags found
No related merge requests found
Showing
with 2514 additions and 0 deletions
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject toversion 3.0 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,v 1.2 2007/10/29 21:41:34 hfuecks Exp $
//
/**
* Main parser components
* @package XML_HTMLSax3
* @version $Id: HTMLSax3.php,v 1.2 2007/10/29 21:41:34 hfuecks Exp $
*/
/**
* Required classes
*/
if (!defined('XML_HTMLSAX3')) {
define('XML_HTMLSAX3', 'XML/');
}
require_once(XML_HTMLSAX3 . 'HTMLSax3/States.php');
require_once(XML_HTMLSAX3 . 'HTMLSax3/Decorators.php');
/**
* Base State Parser
* @package XML_HTMLSax3
* @access protected
* @abstract
*/
class XML_HTMLSax3_StateParser {
/**
* Instance of user front end class to be passed to callbacks
* @var XML_HTMLSax3
* @access private
*/
var $htmlsax;
/**
* User defined object for handling elements
* @var object
* @access private
*/
var $handler_object_element;
/**
* User defined open tag handler method
* @var string
* @access private
*/
var $handler_method_opening;
/**
* User defined close tag handler method
* @var string
* @access private
*/
var $handler_method_closing;
/**
* User defined object for handling data in elements
* @var object
* @access private
*/
var $handler_object_data;
/**
* User defined data handler method
* @var string
* @access private
*/
var $handler_method_data;
/**
* User defined object for handling processing instructions
* @var object
* @access private
*/
var $handler_object_pi;
/**
* User defined processing instruction handler method
* @var string
* @access private
*/
var $handler_method_pi;
/**
* User defined object for handling JSP/ASP tags
* @var object
* @access private
*/
var $handler_object_jasp;
/**
* User defined JSP/ASP handler method
* @var string
* @access private
*/
var $handler_method_jasp;
/**
* User defined object for handling XML escapes
* @var object
* @access private
*/
var $handler_object_escape;
/**
* User defined XML escape handler method
* @var string
* @access private
*/
var $handler_method_escape;
/**
* User defined handler object or NullHandler
* @var object
* @access private
*/
var $handler_default;
/**
* Parser options determining parsing behavior
* @var array
* @access private
*/
var $parser_options = array();
/**
* XML document being parsed
* @var string
* @access private
*/
var $rawtext;
/**
* Position in XML document relative to start (0)
* @var int
* @access private
*/
var $position;
/**
* Length of the XML document in characters
* @var int
* @access private
*/
var $length;
/**
* Array of state objects
* @var array
* @access private
*/
var $State = array();
/**
* Constructs XML_HTMLSax3_StateParser setting up states
* @var XML_HTMLSax3 instance of user front end class
* @access protected
*/
function XML_HTMLSax3_StateParser (& $htmlsax) {
$this->htmlsax = & $htmlsax;
$this->State[XML_HTMLSAX3_STATE_START] =& new XML_HTMLSax3_StartingState();
$this->State[XML_HTMLSAX3_STATE_CLOSING_TAG] =& new XML_HTMLSax3_ClosingTagState();
$this->State[XML_HTMLSAX3_STATE_TAG] =& new XML_HTMLSax3_TagState();
$this->State[XML_HTMLSAX3_STATE_OPENING_TAG] =& new XML_HTMLSax3_OpeningTagState();
$this->State[XML_HTMLSAX3_STATE_PI] =& new XML_HTMLSax3_PiState();
$this->State[XML_HTMLSAX3_STATE_JASP] =& new XML_HTMLSax3_JaspState();
$this->State[XML_HTMLSAX3_STATE_ESCAPE] =& new XML_HTMLSax3_EscapeState();
}
/**
* Moves the position back one character
* @access protected
* @return void
*/
function unscanCharacter() {
$this->position -= 1;
}
/**
* Moves the position forward one character
* @access protected
* @return void
*/
function ignoreCharacter() {
$this->position += 1;
}
/**
* Returns the next character from the XML document or void if at end
* @access protected
* @return mixed
*/
function scanCharacter() {
if ($this->position < $this->length) {
return $this->rawtext{$this->position++};
}
}
/**
* Returns a string from the current position to the next occurance
* of the supplied string
* @param string string to search until
* @access protected
* @return string
*/
function scanUntilString($string) {
$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
* @access protected
* @return string
* @abstract
*/
function scanUntilCharacters($string) {}
/**
* Moves the position forward past any whitespace characters
* @access protected
* @return void
* @abstract
*/
function ignoreWhitespace() {}
/**
* Begins the parsing operation, setting up any decorators, depending on
* parse options invoking _parse() to execute parsing
* @param string XML document to parse
* @access protected
* @return void
*/
function parse($data) {
if ($this->parser_options['XML_OPTION_TRIM_DATA_NODES']==1) {
$decorator =& new XML_HTMLSax3_Trim(
$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) {
$open_decor =& new XML_HTMLSax3_CaseFolding(
$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) {
$decorator =& new XML_HTMLSax3_Linefeed(
$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) {
$decorator =& new XML_HTMLSax3_Tab(
$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) {
$decorator =& new XML_HTMLSax3_Entities_Unparsed(
$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) {
$decorator =& new XML_HTMLSax3_Entities_Parsed(
$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) {
$decorator =& new XML_HTMLSax3_Escape_Stripper(
$this->handler_object_escape,
$this->handler_method_escape);
$this->handler_object_escape =& $decorator;
$this->handler_method_escape = 'strip';
}
$this->rawtext = $data;
$this->length = strlen($data);
$this->position = 0;
$this->_parse();
}
/**
* Performs the parsing itself, delegating calls to a specific parser
* state
* @param constant state object to parse with
* @access protected
* @return void
*/
function _parse($state = XML_HTMLSAX3_STATE_START) {
do {
$state = $this->State[$state]->parse($this);
} while ($state != XML_HTMLSAX3_STATE_STOP &&
$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
* @package XML_HTMLSax3
* @access protected
* @see XML_HTMLSax3_StateParser_Gtet430
*/
class XML_HTMLSax3_StateParser_Lt430 extends XML_HTMLSax3_StateParser {
/**
* Constructs XML_HTMLSax3_StateParser_Lt430 defining available
* parser options
* @var XML_HTMLSax3 instance of user front end class
* @access protected
*/
function XML_HTMLSax3_StateParser_Lt430(& $htmlsax) {
parent::XML_HTMLSax3_StateParser($htmlsax);
$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
* @access protected
* @return string
*/
function scanUntilCharacters($string) {
$startpos = $this->position;
while ($this->position < $this->length && strpos($string, $this->rawtext{$this->position}) === FALSE) {
$this->position++;
}
return substr($this->rawtext, $startpos, $this->position - $startpos);
}
/**
* Moves the position forward past any whitespace characters
* @access protected
* @return void
*/
function ignoreWhitespace() {
while ($this->position < $this->length &&
strpos(" \n\r\t", $this->rawtext{$this->position}) !== FALSE) {
$this->position++;
}
}
/**
* 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
* @access protected
* @return void
*/
function parse($data) {
parent::parse($data);
}
}
/**
* 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
* @package XML_HTMLSax3
* @access protected
* @see XML_HTMLSax3_StateParser_Lt430
*/
class XML_HTMLSax3_StateParser_Gtet430 extends XML_HTMLSax3_StateParser {
/**
* Constructs XML_HTMLSax3_StateParser_Gtet430 defining available
* parser options
* @var XML_HTMLSax3 instance of user front end class
* @access protected
*/
function XML_HTMLSax3_StateParser_Gtet430(& $htmlsax) {
parent::XML_HTMLSax3_StateParser($htmlsax);
$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
* @access protected
* @return string
*/
function scanUntilCharacters($string) {
$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
* @access protected
* @return void
*/
function ignoreWhitespace() {
$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
* to parent
* @param string XML document to parse
* @access protected
* @return void
*/
function parse($data) {
parent::parse($data);
}
}
/**
* Default NullHandler for methods which were not set by user
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_NullHandler {
/**
* Generic handler method which does nothing
* @access protected
* @return void
*/
function DoNothing() {
}
}
/**
* User interface class. All user calls should only be made to this class
* @package XML_HTMLSax3
* @access public
*/
class XML_HTMLSax3 {
/**
* Instance of concrete subclass of XML_HTMLSax3_StateParser
* @var XML_HTMLSax3_StateParser
* @access private
*/
var $state_parser;
/**
* Constructs XML_HTMLSax3 selecting concrete StateParser subclass
* depending on PHP version being used as well as setting the default
* NullHandler for all callbacks<br />
* <b>Example:</b>
* <pre>
* $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');
* $parser->parser($xml);
* </pre>
* @access public
*/
function XML_HTMLSax3() {
if (version_compare(phpversion(), '4.3', 'ge')) {
$this->state_parser =& new XML_HTMLSax3_StateParser_Gtet430($this);
} else {
$this->state_parser =& new XML_HTMLSax3_StateParser_Lt430($this);
}
$nullhandler =& new XML_HTMLSax3_NullHandler();
$this->set_object($nullhandler);
$this->set_element_handler('DoNothing', 'DoNothing');
$this->set_data_handler('DoNothing');
$this->set_pi_handler('DoNothing');
$this->set_jasp_handler('DoNothing');
$this->set_escape_handler('DoNothing');
}
/**
* 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
* @access public
* @return mixed
*/
function set_object(&$object) {
if ( is_object($object) ) {
$this->state_parser->handler_default =& $object;
return true;
} else {
require_once('PEAR.php');
PEAR::raiseError('XML_HTMLSax3::set_object requires '.
'an object instance');
}
}
/**
* Sets a parser option. By default all options are switched off.
* Returns a PEAR Error if option is invalid<br />
* <b>Available options:</b>
* <ul>
* <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
* handler calls</li>
* <li>XML_OPTION_TAB_BREAK: tabs result in additional data handler
* calls</li>
* <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>
* </ul>
* 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
* @access public
* @return boolean
*/
function set_option($name, $value=1) {
if ( array_key_exists($name,$this->state_parser->parser_options) ) {
$this->state_parser->parser_options[$name] = $value;
return true;
} else {
require_once('PEAR.php');
PEAR::raiseError('XML_HTMLSax3::set_option('.$name.') illegal');
}
}
/**
* Sets the data handler method which deals with the contents of XML
* elements.<br />
* The handler method must accept two arguments, the first being an
* instance of XML_HTMLSax3 and the second being the contents of an
* XML element e.g.
* <pre>
* function myDataHander(& $parser,$data){}
* </pre>
* @param string name of method
* @access public
* @return void
* @see set_object
*/
function set_data_handler($data_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.
* <pre>
* function myOpenHander(& $parser,$tagname,$attrs=array()){}
* </pre>
* The close handler method must accept two arguments; the parser and
* the tag name e.g.
* <pre>
* function myCloseHander(& $parser,$tagname){}
* </pre>
* @param string name of open method
* @param string name of close method
* @access public
* @return void
* @see set_object
*/
function set_element_handler($opening_method, $closing_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
* and close tags<br />
* The handler method must accept three arguments; the parser, the
* PI target and data inside the PI
* <pre>
* function myPIHander(& $parser,$target, $data){}
* </pre>
* @param string name of method
* @access public
* @return void
* @see set_object
*/
function set_pi_handler($pi_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
* declarations<br />
* The handler method must accept two arguments; the parser and the
* contents of the escaped section
* <pre>
* function myEscapeHander(& $parser, $data){}
* </pre>
* @param string name of method
* @access public
* @return void
* @see set_object
*/
function set_escape_handler($escape_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
* body of the JASP tag
* <pre>
* function myJaspHander(& $parser, $data){}
* </pre>
* @param string name of method
* @access public
* @return void
* @see set_object
*/
function set_jasp_handler ($jasp_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
* document
* <br />Intended for use from within a user defined handler called
* via the $parser reference e.g.
* <pre>
* function myDataHandler(& $parser,$data) {
* echo( 'Current position: '.$parser->get_current_position() );
* }
* </pre>
* @access public
* @return int
* @see get_length
*/
function get_current_position() {
return $this->state_parser->position;
}
/**
* Returns the string length of the XML document being parsed
* @access public
* @return int
*/
function get_length() {
return $this->state_parser->length;
}
/**
* Start parsing some XML
* @param string XML document
* @access public
* @return void
*/
function parse($data) {
$this->state_parser->parse($data);
}
}
?>
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject toversion 3.0 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: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
//
/**
* Decorators for dealing with parser options
* @package XML_HTMLSax3
* @version $Id: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
* @see XML_HTMLSax3::set_option
*/
/**
* Trims the contents of element data from whitespace at start and end
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Trim {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_Trim
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_Trim(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Trims the data
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function trimData(&$parser, $data) {
$data = trim($data);
if ($data != '') {
$this->orig_obj->{$this->orig_method}($parser, $data);
}
}
}
/**
* Coverts tag names to upper case
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_CaseFolding {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original open handler method
* @var string
* @access private
*/
var $orig_open_method;
/**
* Original close handler method
* @var string
* @access private
*/
var $orig_close_method;
/**
* Constructs XML_HTMLSax3_CaseFolding
* @param object handler object being decorated
* @param string original open handler method
* @param string original close handler method
* @access protected
*/
function XML_HTMLSax3_CaseFolding(&$orig_obj, $orig_open_method, $orig_close_method) {
$this->orig_obj =& $orig_obj;
$this->orig_open_method = $orig_open_method;
$this->orig_close_method = $orig_close_method;
}
/**
* Folds up open tag callbacks
* @param XML_HTMLSax3
* @param string tag name
* @param array tag attributes
* @access protected
*/
function foldOpen(&$parser, $tag, $attrs=array(), $empty = FALSE) {
$this->orig_obj->{$this->orig_open_method}($parser, strtoupper($tag), $attrs, $empty);
}
/**
* Folds up close tag callbacks
* @param XML_HTMLSax3
* @param string tag name
* @access protected
*/
function foldClose(&$parser, $tag, $empty = FALSE) {
$this->orig_obj->{$this->orig_close_method}($parser, strtoupper($tag), $empty);
}
}
/**
* Breaks up data by linefeed characters, resulting in additional
* calls to the data handler
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Linefeed {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_LineFeed
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_LineFeed(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Breaks the data up by linefeeds
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function breakData(&$parser, $data) {
$data = explode("\n",$data);
foreach ( $data as $chunk ) {
$this->orig_obj->{$this->orig_method}($parser, $chunk);
}
}
}
/**
* Breaks up data by tab characters, resulting in additional
* calls to the data handler
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Tab {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_Tab
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_Tab(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Breaks the data up by linefeeds
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function breakData(&$parser, $data) {
$data = explode("\t",$data);
foreach ( $data as $chunk ) {
$this->orig_obj->{$this->orig_method}($this, $chunk);
}
}
}
/**
* Breaks up data by XML entities and parses them with html_entity_decode(),
* resulting in additional calls to the data handler<br />
* Requires PHP 4.3.0+
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Entities_Parsed {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_Entities_Parsed
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_Entities_Parsed(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Breaks the data up by XML entities
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function breakData(&$parser, $data) {
$data = preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ( $data as $chunk ) {
$chunk = html_entity_decode($chunk,ENT_NOQUOTES);
$this->orig_obj->{$this->orig_method}($this, $chunk);
}
}
}
/**
* Compatibility with older PHP versions
*/
if (version_compare(phpversion(), '4.3', '<') && !function_exists('html_entity_decode') ) {
function html_entity_decode($str, $style=ENT_NOQUOTES) {
return strtr($str,
array_flip(get_html_translation_table(HTML_ENTITIES,$style)));
}
}
/**
* Breaks up data by XML entities but leaves them unparsed,
* resulting in additional calls to the data handler<br />
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Entities_Unparsed {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_Entities_Unparsed
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_Entities_Unparsed(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Breaks the data up by XML entities
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function breakData(&$parser, $data) {
$data = preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach ( $data as $chunk ) {
$this->orig_obj->{$this->orig_method}($this, $chunk);
}
}
}
/**
* Strips the HTML comment markers or CDATA sections from an escape.
* If XML_OPTIONS_FULL_ESCAPES is on, this decorator is not used.<br />
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Escape_Stripper {
/**
* Original handler object
* @var object
* @access private
*/
var $orig_obj;
/**
* Original handler method
* @var string
* @access private
*/
var $orig_method;
/**
* Constructs XML_HTMLSax3_Entities_Unparsed
* @param object handler object being decorated
* @param string original handler method
* @access protected
*/
function XML_HTMLSax3_Escape_Stripper(&$orig_obj, $orig_method) {
$this->orig_obj =& $orig_obj;
$this->orig_method = $orig_method;
}
/**
* Breaks the data up by XML entities
* @param XML_HTMLSax3
* @param string element data
* @access protected
*/
function strip(&$parser, $data) {
// Check for HTML comments first
if ( substr($data,0,2) == '--' ) {
$patterns = array(
'/^\-\-/', // Opening comment: --
'/\-\-$/', // Closing comment: --
);
$data = preg_replace($patterns,'',$data);
// Check for XML CDATA sections (note: don't do both!)
} else if ( substr($data,0,1) == '[' ) {
$patterns = array(
'/^\[.*CDATA.*\[/s', // Opening CDATA
'/\].*\]$/s', // Closing CDATA
);
$data = preg_replace($patterns,'',$data);
}
$this->orig_obj->{$this->orig_method}($this, $data);
}
}
?>
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject toversion 3.0 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: Grammar.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
//
/**
* For dealing with HTML's special cases
* @package XML_HTMLSax3
* @version $Id: Grammar.php,v 1.2 2007/10/29 21:41:35 hfuecks Exp $
*/
/**
* Passed as fourth argument to opening and closing handler to signify
* a tag which was immediately closed like <this />
* @package XML_HTMLSax3
*/
define ('XML_HTMLSAX3_EMTPY',1);
/**
* Passed as fourth argument to opening and closing handler to signify
* special HTML tags which are not supposed to have a closing tag such
* as the hr tag. Only used when XML_OPTION_HTML_SPECIALS is on
* @package XML_HTMLSax3
*/
define ('XML_HTMLSAX3_ENDTAG_FORBIDDEN',2);
/**
* Passed as fourth argument to closing handler only, to signify
* special HTML tags which are not supposed to have a closing tag such
* as the hr tag but where a closing tag has been used. Only used when
* XML_OPTION_HTML_SPECIALS is on
* @package XML_HTMLSax3
*/
define ('XML_HTMLSAX3_ENDTAG_FORBIDDEN_WARNING',3);
/**
* Global array for lookups on tags which should not have closing tags
* @package XML_HTMLSax3
*/
$GLOBALS['_XML_HTMLSAX3_ENDTAG_FORBIDDEN'] = array (
'area',
'base',
'basefont',
'br',
'col',
'frame',
'hr',
'img',
'input',
'isindex',
'link',
'meta',
'param',
);
?>
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject toversion 3.0 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: States.php,v 1.3 2007/10/29 21:41:35 hfuecks Exp $
//
/**
* Parsing states.
* @package XML_HTMLSax3
* @version $Id: States.php,v 1.3 2007/10/29 21:41:35 hfuecks Exp $
*/
/**
* Define parser states
*/
define('XML_HTMLSAX3_STATE_STOP', 0);
define('XML_HTMLSAX3_STATE_START', 1);
define('XML_HTMLSAX3_STATE_TAG', 2);
define('XML_HTMLSAX3_STATE_OPENING_TAG', 3);
define('XML_HTMLSAX3_STATE_CLOSING_TAG', 4);
define('XML_HTMLSAX3_STATE_ESCAPE', 6);
define('XML_HTMLSAX3_STATE_JASP', 7);
define('XML_HTMLSAX3_STATE_PI', 8);
/**
* StartingState searches for the start of any XML tag
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_StartingState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_TAG
* @access protected
*/
function parse(&$context) {
$data = $context->scanUntilString('<');
if ($data != '') {
$context->handler_object_data->
{$context->handler_method_data}($context->htmlsax, $data);
}
$context->IgnoreCharacter();
return XML_HTMLSAX3_STATE_TAG;
}
}
/**
* Decides which state to move one from after StartingState
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_TagState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant the next state to move into
* @access protected
*/
function parse(&$context) {
switch($context->ScanCharacter()) {
case '/':
return XML_HTMLSAX3_STATE_CLOSING_TAG;
break;
case '?':
return XML_HTMLSAX3_STATE_PI;
break;
case '%':
return XML_HTMLSAX3_STATE_JASP;
break;
case '!':
return XML_HTMLSAX3_STATE_ESCAPE;
break;
default:
$context->unscanCharacter();
return XML_HTMLSAX3_STATE_OPENING_TAG;
}
}
}
/**
* Dealing with closing XML tags
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_ClosingTagState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_START
* @access protected
*/
function parse(&$context) {
$tag = $context->scanUntilCharacters('/>');
if ($tag != '') {
$char = $context->scanCharacter();
if ($char == '/') {
$char = $context->scanCharacter();
if ($char != '>') {
$context->unscanCharacter();
}
}
$context->handler_object_element->
{$context->handler_method_closing}($context->htmlsax, $tag, FALSE);
}
return XML_HTMLSAX3_STATE_START;
}
}
/**
* Dealing with opening XML tags
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_OpeningTagState {
/**
* Handles attributes
* @param string attribute name
* @param string attribute value
* @return void
* @access protected
* @see XML_HTMLSax3_AttributeStartState
*/
function parseAttributes(&$context) {
$Attributes = array();
$context->ignoreWhitespace();
$attributename = $context->scanUntilCharacters("=/> \n\r\t");
while ($attributename != '') {
$attributevalue = NULL;
$context->ignoreWhitespace();
$char = $context->scanCharacter();
if ($char == '=') {
$context->ignoreWhitespace();
$char = $context->ScanCharacter();
if ($char == '"') {
$attributevalue= $context->scanUntilString('"');
$context->IgnoreCharacter();
} else if ($char == "'") {
$attributevalue = $context->scanUntilString("'");
$context->IgnoreCharacter();
} else {
$context->unscanCharacter();
$attributevalue =
$context->scanUntilCharacters("> \n\r\t");
}
} else if ($char !== NULL) {
$attributevalue = NULL;
$context->unscanCharacter();
}
$Attributes[$attributename] = $attributevalue;
$context->ignoreWhitespace();
$attributename = $context->scanUntilCharacters("=/> \n\r\t");
}
return $Attributes;
}
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_START
* @access protected
*/
function parse(&$context) {
$tag = $context->scanUntilCharacters("/> \n\r\t");
if ($tag != '') {
$this->attrs = array();
$Attributes = $this->parseAttributes($context);
$char = $context->scanCharacter();
if ($char == '/') {
$char = $context->scanCharacter();
if ($char != '>') {
$context->unscanCharacter();
}
$context->handler_object_element->
{$context->handler_method_opening}($context->htmlsax, $tag,
$Attributes, TRUE);
$context->handler_object_element->
{$context->handler_method_closing}($context->htmlsax, $tag,
TRUE);
} else {
$context->handler_object_element->
{$context->handler_method_opening}($context->htmlsax, $tag,
$Attributes, FALSE);
}
}
return XML_HTMLSAX3_STATE_START;
}
}
/**
* Deals with XML escapes handling comments and CDATA correctly
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_EscapeState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_START
* @access protected
*/
function parse(&$context) {
$char = $context->ScanCharacter();
if ($char == '-') {
$char = $context->ScanCharacter();
if ($char == '-') {
$context->unscanCharacter();
$context->unscanCharacter();
$text = $context->scanUntilString('-->');
$text .= $context->scanCharacter();
$text .= $context->scanCharacter();
} else {
$context->unscanCharacter();
$text = $context->scanUntilString('>');
}
} else if ( $char == '[') {
$context->unscanCharacter();
$text = $context->scanUntilString(']>');
$text.= $context->scanCharacter();
} else {
$context->unscanCharacter();
$text = $context->scanUntilString('>');
}
$context->IgnoreCharacter();
if ($text != '') {
$context->handler_object_escape->
{$context->handler_method_escape}($context->htmlsax, $text);
}
return XML_HTMLSAX3_STATE_START;
}
}
/**
* Deals with JASP/ASP markup
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_JaspState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_START
* @access protected
*/
function parse(&$context) {
$text = $context->scanUntilString('%>');
if ($text != '') {
$context->handler_object_jasp->
{$context->handler_method_jasp}($context->htmlsax, $text);
}
$context->IgnoreCharacter();
$context->IgnoreCharacter();
return XML_HTMLSAX3_STATE_START;
}
}
/**
* Deals with XML processing instructions
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_PiState {
/**
* @param XML_HTMLSax3_StateParser subclass
* @return constant XML_HTMLSAX3_STATE_START
* @access protected
*/
function parse(&$context) {
$target = $context->scanUntilCharacters(" \n\r\t");
$data = $context->scanUntilString('?>');
if ($data != '') {
$context->handler_object_pi->
{$context->handler_method_pi}($context->htmlsax, $target, $data);
}
$context->IgnoreCharacter();
$context->IgnoreCharacter();
return XML_HTMLSAX3_STATE_START;
}
}
?>
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject toversion 3.0 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: XML_HTMLSax.php,v 1.3 2007/10/29 21:41:34 hfuecks Exp $
//
/**
* Kept for backward compatibility on includes. Result of mistake in
* following PEAR guidelines
* @package XML_HTMLSax
* @version $Id: XML_HTMLSax.php,v 1.3 2007/10/29 21:41:34 hfuecks Exp $
*/
/**
* Required classes
*/
if (!defined('XML_HTMLSAX')) {
define('XML_HTMLSAX', 'XML/');
}
require_once(XML_HTMLSAX . 'HTMLSax.php');
?>
\ No newline at end of file
$Id: Readme,v 1.4 2004/06/02 14:33:38 hfuecks Exp $
++Introduction
XML_HTMLSax3 is a SAX based XML parser for badly formed XML documents,
such as HTML.
The original code base was developed by Alexander Zhukov and published at
http://sourceforge.net/projects/phpshelve/. Alexander kindly gave permission
to modify the code and license for inclusion in PEAR.
PEAR::XML_HTMLSax3 provides an API very similar to the native PHP SAX
extension (http://www.php.net/xml), allowing handlers using one to be
easily adapted to the other. The key difference is HTMLSax will not
break on badly formed XML, allowing it to be used for parsing HTML
documents. Otherwise HTMLSax supports all the handlers available from
Expat except namespace and external entity handlers. Provides methods
for handling XML escapes as well as JSP/ASP opening and close tags.
Version 1.x introduced an API similar to the native SAX extension but
used a slow character by character approach to parsing.
Version 2.x has had it's internals completely overhauled to use a Lexer,
delivering performance *approaching* that of the native XML extension,
as well as a radically improved, modular design that makes adding
further functionality easy.
Version 3.x is about fine tuning the API, behaviour and providing a
mechanism to distinguish HTML "quirks" from badly formed HTML
A big thanks to Jeff Moore (lead developer of WACT:
http://wact.sourceforge.net) who's largely responsible for new design, as well
input from members at Sitepoint's Advanced PHP forums:
http://www.sitepointforums.com/showthread.php?threadid=121246.
Thanks also to Marcus Baker (lead developer of SimpleTest:
http://www.lastcraft.com/simple_test.php) for sorting out the unit tests.
++Uses
Some particular situations where XML_HTMLSax3 can be useful include;
- Template Engines (see WACT for example: http://wact.sf.net)
- Parsing XML documents (such as those online) where the source is
out of your control and Expat is choking because it's badly formed.
- Converting HTML to XHTML
- Reading HTML based content from a database and converting to PDF (with
help from a PDF generation library and probably PEAR::XML_SaxFilters as
well)
- Parsing ASP(.NET) and JSP pages.
- Creating a PHP-GTK based web browser? A PHP CSS Parser exists:
http://www.phpclasses.org/browse.html/package/1081.html
++Features
- Won't "break" on badly formed XML. May in some instances get it "wrong"
(see Limitations) but will continue parsing.
- Provides an API similar to the native PHP XML extension so switching code
from one to the other is typically minimal effort.
- Can be instructed to behave in more or less the same manner as SAX,
when dealing with linefeeds, tabs and XML entities
- In addition to handling basic XML elements attributes and data also
capable of dealing with;
- Processing instructions e.g. <?php ?> / <?xml ?> etc. Within PI's
XML entities are not parsed (i.e. ignore < and > )
- XML Escape markup such as <! >, <!-- --> and <![CDATA[ ]]>. Within
this XML entities are not parsed (useful for JavaScript, for example)
- JSP / ASP (JASP) marked up with <% %>. Note: You will need to
deal with <%@ %> and <%= %> yourself. With JASP markup XML entities
are not parsed
++Usage Notes
- Performance-wise, it runs faster on PHP 4.3.0 thanks to strspn() and
strcspn() supporting position arguments. For older PHP versions while
loops are used to achieve the same effect, meaning a slightly higher
overhead. Note also that setting XML options with XML_HTMLSax3::set_option()
also slows down the parser, the options being handled by "decorators"
which perform some further formatting on XML events which have already
been parsed.
- By default, no parser options are set
- Regarding the XML_OPTION_ENTITIES_PARSED, this uses the html_entity_decode()
function which is only available in PHP 4.3.0+. To get round this, HTMLSax
checks your PHP version and for the function name html_entity_decode. If not
found, it defines a function which mirrors the behavior of the native PHP
html_entity_decode().
Both XML_OPTION_ENTITIES_PARSED and XML_OPTION_ENTITIES_UNPARSED can be used
down to PHP version 4.0.5, due to the regular expression used to find entities.
- For attributes which have just a name but no value e.g.
<option value="bar" selected>
HTMLSax will return a NULL value for that attribute name, when
calling the opening tag handler;
function myOpenHandler($parser,$name,$attrs) {
print_r ( $attrs );
}
This would produce;
Array
(
[value] => bar
[selected] => NULL
)
- JASP directives like <%@ and <%= will be not be regarded as special.
I.e. you will get back the @ or % from the contents of the JASP block
and have to deal with these yourself.
++ Limitations
- XML_HTMLSax3 only supports use of PHP classes as callback handlers;
there is no support for using PHP functions as handlers.
- The only weird behaviour is for attributes which only have a left quote
or apostrophe e.g. <tag foo="bar>Some Text</tag> will give you an
attribute like $attrs['foo']="bar>Some Text"; This is a trade off against
allowing XML entities like < and > to appear inside attributes.
- Although the package name might suggest otherwise, XML_HTMLSax3 currently
has no special knowledge of HTML (i.e. there is no understanding of whether
a given HTML document is well formed or not, according to HTML's rules).
XML_HTMLSax3 is primarily intended as a SAX based parser that will not
complain about structure of a document it is asked to parse. It even does
a fair job of parsing http://static.php.net/www.php.net/images/php.gif ...
Version 3.x will introduce some basic knowledge of HTML grammar to help with
identifying HTML "quirks".
- <script /> elements containing < or > characters; these will be treated as new
elements triggering the listeners. Make sure that any JavaScript inside is marked
either with an XML comment <!-- --> or a CDATA block <[CDATA[ ]]>
</script>
Alternatively define open / close handlers which watch for <script /> elements
as a special case, so that any further events triggered within them are handled
as part of the <script /> element.
- If you change the handlers once parsing has started, you will need to re-set
and parser options you have defined
++ Example Use
Further examples are available in the examples directory of this package.
<?php
// Include HTMLSax
require_once('XML/HTMLSax3.php');
// Define a customer handler class
class MyHandler {
function MyHandler(){}
// Opening tags
function openHandler(& $parser,$name,$attrs) {
echo ( 'Open Tag Handler: '.$name );
echo ( 'Attrs:' );
print_r($attrs);
}
// Closing tags
function closeHandler(& $parser,$name) {
echo ( 'Close Tag Handler: '.$name );
}
// Text node handler
function dataHandler(& $parser,$data) {
echo ( 'Data Handler: '.$data );
}
// XML escape handler (e.g. HTML comments)
function escapeHandler(& $parser,$data) {
echo ( 'Escape Handler: '.$data );
}
// Processing instruction handler
function piHandler(& $parser,$target,$data) {
echo ( 'PI Handler: '.$target.' - '.$data );
}
// JSP / ASP markup handler
function jaspHandler(& $parser,$data) {
echo ( 'Jasp Handler: '.$data );
}
}
// Get some HTML document
$doc = file_get_contents('http://www.php.net');
// Instantiate the handler
$handler=new MyHandler();
// Instantiate the parser
$parser=& new XML_HTMLSax3();
// Register the handler with the parser
$parser->set_object($handler);
// Set a parser option
$parser->set_option('XML_OPTION_TRIM_DATA_NODES');
// Set the callback handlers (MyHandler methods)
$parser->set_element_handler('openHandler','closeHandler');
$parser->set_data_handler('dataHandler');
$parser->set_escape_handler('escapeHandler');
$parser->set_pi_handler('piHandler');
$parser->set_jasp_handler('jaspHandler');
// Parse the document
$parser->parse($doc);
?>
\ No newline at end of file
<?php
/**
* @version $Id: ExpatvsHtmlSax.php,v 1.3 2004/06/02 14:33:38 hfuecks Exp $
* Shows HTMLSax in a race against Expat. Note that HTMLSax performance
* gets slower on PHP < 4.3.0 or if parser options are being used
*/
require_once('XML/HTMLSax3.php');
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// The PHP general RDF feed
$doc = file_get_contents('http://news.php.net/group.php?group=php.general&format=rdf');
/* Simple handler that does nothing */
class MyHandler {
function openHandler(& $parser,$name,$attrs) {}
function closeHandler(& $parser,$name) {}
function dataHandler(& $parser,$data) {}
}
$handler=& new MyHandler();
$parser = xml_parser_create();
xml_set_object($parser, $handler);
xml_set_element_handler($parser, 'openHandler', 'closeHandler' );
xml_set_character_data_handler($parser, 'dataHandler' );
echo ('<pre>');
// Time Expat
$start = getmicrotime();
xml_parse($parser, $doc);
$end = getmicrotime();
echo ( "Expat took:\t\t".(getmicrotime()-$start)."<br />" );
$start = getmicrotime();
$parser =& new XML_HTMLSax3();
$parser->set_object($handler);
$parser->set_element_handler('openHandler','closeHandler');
$parser->set_data_handler('dataHandler');
// Time HTMLSax
$start = getmicrotime();
$parser->parse($doc);
echo ( "HTMLSax took:\t\t".(getmicrotime()-$start) );
echo ('</pre>');
?>
\ No newline at end of file
<?php
/**
* $Id: HTMLtoXHTML.php,v 1.3 2004/06/02 14:33:38 hfuecks Exp $
* Demonstrates conversion of HTML to XHTML
*/
require_once('XML/HTMLSax3.php');
class HTMLtoXHTMLHandler {
var $xhtml;
var $inTitle;
var $pCounter;
function HTMLtoXHTMLHandler(){
$this->xhtml='';
$this->inTitle = false;
$this->pCounter=0;
}
// Handles the writing of attributes - called from $this->openHandler()
function writeAttrs ($attrs) {
if ( is_array($attrs) ) {
foreach ( $attrs as $name => $value ) {
// Watch for 'checked'
if ( $name == 'checked' ) {
$this->xhtml.=' checked="checked"';
// Watch for 'selected'
} else if ( $name == 'selected' ) {
$this->xhtml.=' selected="selected"';
} else {
$this->xhtml.=' '.$name.'="'.$value.'"';
}
}
}
}
// Opening tag handler
function openHandler(& $parser,$name,$attrs) {
if ( (isset ( $attrs['id'] ) && $attrs['id'] == 'title') || $name == 'title' )
$this->inTitle=true;
switch ( $name ) {
case 'br':
$this->xhtml.="<br />\n";
break;
case 'html':
$this->xhtml.="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"eng\">\n";
break;
case 'p':
if ( $this->pCounter != 0 ) {
$this->xhtml.="</p>\n";
}
$this->xhtml.="<p>";
$this->pCounter++;
break;
default:
$this->xhtml.="<".$name;
$this->writeAttrs($attrs);
$this->xhtml.=">\n";
break;
}
}
// Closing tag handler
function closeHandler(& $parser,$name) {
if ( $this->inTitle ) {
$this->inTitle=false;
}
if ($name == 'body' && $this->pCounter != 0)
$this->xhtml.="</p>\n";
$this->xhtml.="</".$name.">\n";
}
// Character data handler
function dataHandler(& $parser,$data) {
if ( $this->inTitle )
$this->xhtml.='This is XHTML 1.0';
else
$this->xhtml.=$data;
}
// Escape handler
function escapeHandler(& $parser,$data) {
if ( $data == 'doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"' )
$this->xhtml.='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
}
// Return the XHTML document
function getXHTML () {
return $this->xhtml;
}
}
// Get the HTML file
$doc=file_get_contents('example.html');
// Instantiate the handler
$handler=& new HTMLtoXHTMLHandler();
// Instantiate the parser
$parser=& new XML_HTMLSax3();
// Register the handler with the parser
$parser->set_object($handler);
// Set the handlers
$parser->set_element_handler('openHandler','closeHandler');
$parser->set_data_handler('dataHandler');
$parser->set_escape_handler('escapeHandler');
// Parse the document
$parser->parse($doc);
echo ( $handler->getXHTML() );
?>
\ No newline at end of file
<?php
/***
* $Id: SimpleExample.php,v 1.3 2004/06/02 14:33:38 hfuecks Exp $
* Shows all the handlers in use with a simple document
*/
require_once('XML/HTMLSax3.php');
class MyHandler {
function MyHandler(){}
function openHandler(& $parser,$name,$attrs) {
echo ( 'Open Tag Handler: '.$name.'<br />' );
echo ( 'Attrs:<pre>' );
print_r($attrs);
echo ( '</pre>' );
}
function closeHandler(& $parser,$name) {
echo ( 'Close Tag Handler: '.$name.'<br />' );
}
function dataHandler(& $parser,$data) {
echo ( 'Data Handler: '.$data.'<br />' );
}
function escapeHandler(& $parser,$data) {
echo ( 'Escape Handler: '.$data.'<br />' );
}
function piHandler(& $parser,$target,$data) {
echo ( 'PI Handler: '.$target.' - '.$data.'<br />' );
}
function jaspHandler(& $parser,$data) {
echo ( 'Jasp Handler: '.$data.'<br />' );
}
}
$doc=<<<EOD
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>HTML Sax in Action</title>
<meta name="Description" content="Example for HTML Sax">
<!-- Some JavaScript inside a CDATA block coming right up... -->
<script type="application/x-javascript">
<![CDATA[
document.write('<b>Hello World!</b>');
]]>
</script>
</head>
<body>
<?php
echo ( '<b>This is a processing instruction</b>' );
?>
<a href="http://www.php.net">PHP</a>
<%
document.write('<i>Hello World!</i>');
%>
</body>
</html>
EOD;
// Instantiate the handler
$handler=new MyHandler();
// Instantiate the parser
$parser=& new XML_HTMLSax3();
// Register the handler with the parser
$parser->set_object($handler);
// Set a parser option
$parser->set_option('XML_OPTION_TRIM_DATA_NODES');
// Set the handlers
$parser->set_element_handler('openHandler','closeHandler');
$parser->set_data_handler('dataHandler');
$parser->set_escape_handler('escapeHandler');
$parser->set_pi_handler('piHandler');
$parser->set_jasp_handler('jaspHandler');
// Parse the document
$parser->parse($doc);
?>
\ No newline at end of file
<?php
/***
* $Id: SimpleTemplate.php,v 1.2 2004/06/02 14:33:38 hfuecks Exp $
* Shows how HTMLSax can be used for template parsing
*/
require_once('XML/HTMLSax3.php');
class SimpleTemplate {
var $vars = array();
var $output = '';
function setVar($name,$value) {
$this->vars[$name] = $value;
}
function display() {
echo $this->output;
}
// Notice fourth argument
function open(& $parser,$name,$attrs,$empty) {
// Should check more carefully but this is just an example...
if ( $name == 'var' ) {
if ( isset($this->vars[$attrs['name']]) ) {
$this->output.= $this->vars[$attrs['name']];
}
} else {
$tag = "<$name";
foreach ( $attrs as $key => $value ) {
if ( is_null($value) ) {
$tag .= ' '.$key;
} else {
$tag .= " $key=\"$value\"";
}
}
if ( $empty ) {
$tag .= '/>';
} else {
$tag .= '>';
}
$this->output .= $tag;
}
}
// Notice fourth argument
function close(& $parser,$name,$empty) {
if ( !$empty ) {
$this->output.= "</$name>";
}
}
function data(& $parser,$data) {
$this->output .= $data;
}
function escape(& $parser,$data) {
$this->output .= "<!$data>";
}
function pi(& $parser,$target,$data) {
$this->output .= "<?$target $data?>";
}
function jasp(& $parser,$data) {
$this->output .= "<%$data%>";
}
}
$tpl=new SimpleTemplate();
$tpl->setVar('title','HTMLSax as a Template Parser');
$para1 = <<<EOD
HTMLSax can be used as the basis for a template engine,
as with <a href="http://wact.sf.net">WACT</a> and
<a href="http://phpoot.sourceforge.jp/">PHPOOT</a>. For
the most part is allows you to preserve the structure of
original template, preserving whitespace and so on with
one or two minor exceptions, such as whitespace between
attributes and the quotes used for attributes. Compare
the source template for this example with the output.
EOD;
$tpl->setVar('para1',$para1);
$para2 = <<<EOD
Notice also how the fourth argument to the open and close handlers
is used (see the PHP source) - this allows you to correctly
"rebuild" tags like &lt;div /&gt; vs. &lt;div&gt;&lt;/div&gt;
EOD;
$tpl->setVar('para2',$para2);
// Instantiate the parser
$parser=& new XML_HTMLSax3();
// Register the handler with the parser
$parser->set_object($tpl);
// Set a parser option
$parser->set_option('XML_OPTION_FULL_ESCAPES');
// Set the handlers
$parser->set_element_handler('open','close');
$parser->set_data_handler('data');
$parser->set_escape_handler('escape');
$parser->set_pi_handler('pi');
$parser->set_jasp_handler('jasp');
// Parse the document
$parser->parse(file_get_contents('simpletemplate.tpl'));
$tpl->display();
?>
\ No newline at end of file
<?php
/***
* $Id: WordDoc.php,v 1.4 2004/06/02 14:33:38 hfuecks Exp $
* Shows HTMLSax parsing Word generated HTML
*/
require_once('XML/HTMLSax3.php');
class MyHandler {
function escape($parser,$data) {
echo('<pre>'.$data."\n\n\n</pre>");
}
}
$h = & new MyHandler();
// Instantiate the parser
$parser=& new XML_HTMLSax3();
$parser->set_object($h);
$parser->set_escape_handler('escape');
if ( isset($_GET['strip_escapes']) ) {
$parser->set_option('XML_OPTION_STRIP_ESCAPES');
}
?>
<h1>Parsing Word Documents</h1>
<p>Shows HTMLSax parsing a simple Word generated HTML document and the impact of the option 'XML_OPTION_STRIP_ESCAPES' which can be set like;
<pre>
$parser->set_option('XML_OPTION_STRIP_ESCAPES');
</pre>
</p>
<p>Word generates some strange XML / HTML escape sequences like &lt;![endif]&gt; - now (3.0.0+) handled by HTMLSax correctly.</p>
<p>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">XML_OPTION_STRIP_ESCAPES = 0</a> :
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?strip_escapes=1">XML_OPTION_STRIP_ESCAPES = 1</a>
</p>
<p>Starting to parse...</p>
<?php
// Parse the document
$parser->parse(file_get_contents('worddoc.htm'));
?>
<p>Parsing completed</p>
\ No newline at end of file
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>This is HTML 4.0</title>
</head>
<body>
<h1 id="title">This page is HTML 4.0</h1>
<p>It contains a number of classic "failings" in terms of well formed XML,
such as<br>
- Tags which aren't closed<br>
- Attributes which have no value<br>
<p>A standard XML parser will complain about but using HTMLSax it can be
converted to XHTML 1.0.
<form>
Do you like XHTML?
<input
type="checkbox"
name="likeIt"
checked>
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> <var name="title"> </title>
</head>
<body>
<h1><var name="title"></h1>
<font
color='blue'
size = "3"
face=Verdana>
<var name="para1">
</font>
<br />
<var name="para2">
<br />
<form>
<select>
<option selected>Selected Option - note the attribute
</select>
</form>
<?php
// PI Handler deals with this
echo "Hello World!"
?>
<%
// JASP handler deals with this
document.write("Hello World!");
%>
</list>
</body>
</html>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<link rel=File-List href="./worddoc_files/filelist.xml">
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:DrawingGridHorizontalSpacing>5 pt</w:DrawingGridHorizontalSpacing>
<w:DrawingGridVerticalSpacing>6.8 pt</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:Compatibility>
<w:FootnoteLayoutLikeWW8/>
<w:ShapeLayoutLikeWW8/>
<w:AlignTablesRowByRow/>
<w:ForgetLastTabAlignment/>
<w:LayoutRawTableWidth/>
<w:LayoutTableRowsApart/>
</w:Compatibility>
</w:WordDocument>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:EN-US;}
@page Section1
{size:595.45pt 841.7pt;
margin:72.0pt 89.85pt 72.0pt 89.85pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<![CDATA[ testing CDATA ]]>
<body lang=EN-GB style='tab-interval:36.0pt'>
<div class=Section1>
<p class=MsoNormal><span lang=EN-US><![if !supportEmptyParas]>&nbsp;<![endif]><o:p></o:p></span></p>
</div>
</body>
</html>
<?php
/**
* @package XML
* @version $Id: index.php,v 1.1 2004/05/27 15:13:31 hfuecks Exp $
*/
if (!defined('SIMPLE_TEST')) {
define('SIMPLE_TEST', '/htdocs/simpletest/');
}
include('unit_tests.php');
?>
\ No newline at end of file
<?php
/**
* Requires SimpleTest version 1.0Alpha8 or higher.
* Unit Tests using the SimpleTest framework:
* http://www.lastcraft.com/simple_test.php
* @package XML
* @version $Id: unit_tests.php,v 1.3 2004/06/02 14:23:48 hfuecks Exp $
*/
if (!defined('SIMPLE_TEST')) {
define('SIMPLE_TEST', 'simpletest/'); // Add to php.ini path (should be the default).
}
require_once(SIMPLE_TEST . 'unit_tester.php');
require_once(SIMPLE_TEST . 'mock_objects.php');
require_once(SIMPLE_TEST . 'reporter.php');
if (!defined('XML_HTMLSAX3')) {
define('XML_HTMLSAX3', '../');
}
require_once(XML_HTMLSAX3 . 'HTMLSax3.php');
require_once(XML_HTMLSAX3 . 'HTMLSax3/States.php');
require_once(XML_HTMLSAX3 . 'HTMLSax3/Decorators.php');
$test = &new GroupTest('XML::HTMLSax3 Tests');
$test->addTestFile('xml_htmlsax_test.php');
$test->run(new HtmlReporter());
?>
\ No newline at end of file
<?php
/**
* @package XML
* @version $Id: xml_htmlsax_test.php,v 1.3 2004/05/28 11:53:48 hfuecks Exp $
*/
class ListenerInterface {
function ListenerInterface() { }
function startHandler($parser, $name, $attrs) { }
function endHandler($parser, $name) { }
function dataHandler($parser, $data) { }
function piHandler($parser, $target, $data) { }
function escapeHandler($parser, $data) { }
function jaspHandler($parser, $data) { }
}
Mock::generate('ListenerInterface', 'MockListener');
class ParserTestCase extends UnitTestCase {
var $parser;
var $listener;
function ParserTestCase($name = false) {
$this->UnitTestCase($name);
}
function setUp() {
$this->listener = &new MockListener($this);
$this->parser = &new XML_HTMLSax();
$this->parser->set_object($this->listener);
$this->parser->set_element_handler('startHandler','endHandler');
$this->parser->set_data_handler('dataHandler');
$this->parser->set_escape_handler('escapeHandler');
$this->parser->set_pi_handler('piHandler');
$this->parser->set_jasp_handler('jaspHandler');
}
function tearDown() {
$this->listener->tally();
}
}
SimpleTestOptions::ignore('ParserTestCase');
class TestOfContent extends ParserTestCase {
function TestOfContent() {
$this->ParserTestCase();
}
function testSimple() {
$this->listener->expectOnce('dataHandler', array('*', 'stuff'));
$this->parser->parse('stuff');
}
function testPreservingWhiteSpace() {
$this->listener->expectOnce('dataHandler', array('*', " stuff\t\r\n "));
$this->parser->parse(" stuff\t\r\n ");
}
function testTrimmingWhiteSpace() {
$this->listener->expectOnce('dataHandler', array('*', "stuff"));
$this->parser->set_option('XML_OPTION_TRIM_DATA_NODES');
$this->parser->parse(" stuff\t\r\n ");
}
}
class TestOfElements extends ParserTestCase {
function TestOfElements() {
$this->ParserTestCase();
}
function testEmptyElement() {
$this->listener->expectOnce('startHandler', array('*', 'tag', array(),FALSE));
$this->listener->expectOnce('endHandler', array('*', 'tag',FALSE));
$this->listener->expectNever('dataHandler');
$this->parser->parse('<tag></tag>');
}
function testElementWithContent() {
$this->listener->expectOnce('startHandler', array('*', 'tag', array(),FALSE));
$this->listener->expectOnce('dataHandler', array('*', 'stuff'));
$this->listener->expectOnce('endHandler', array('*', 'tag',FALSE));
$this->parser->parse('<tag>stuff</tag>');
}
function testMismatchedElements() {
$this->listener->expectArgumentsAt(0, 'startHandler', array('*', 'b', array(),FALSE));
$this->listener->expectArgumentsAt(1, 'startHandler', array('*', 'i', array(),FALSE));
$this->listener->expectArgumentsAt(0, 'endHandler', array('*', 'b',FALSE));
$this->listener->expectArgumentsAt(1, 'endHandler', array('*', 'i',FALSE));
$this->listener->expectCallCount('startHandler', 2);
$this->listener->expectCallCount('endHandler', 2);
$this->parser->parse('<b><i>stuff</b></i>');
}
function testCaseFolding() {
$this->listener->expectOnce('startHandler', array('*', 'TAG', array(),FALSE));
$this->listener->expectOnce('dataHandler', array('*', 'stuff'));
$this->listener->expectOnce('endHandler', array('*', 'TAG',FALSE));
$this->parser->set_option('XML_OPTION_CASE_FOLDING');
$this->parser->parse('<tag>stuff</tag>');
}
function testEmptyTag() {
$this->listener->expectOnce('startHandler', array('*', 'tag', array(),TRUE));
$this->listener->expectNever('dataHandler');
$this->listener->expectOnce('endHandler', array('*', 'tag',TRUE));
$this->parser->parse('<tag />');
}
function testAttributes() {
$this->listener->expectOnce(
'startHandler',
array('*', 'tag', array("a" => "A", "b" => "B", "c" => "C"),FALSE));
$this->parser->parse('<tag a="A" b=\'B\' c = "C">');
}
function testEmptyAttributes() {
$this->listener->expectOnce(
'startHandler',
array('*', 'tag', array("a" => NULL, "b" => NULL, "c" => NULL),FALSE));
$this->parser->parse('<tag a b c>');
}
function testNastyAttributes() {
$this->listener->expectOnce(
'startHandler',
array('*', 'tag', array("a" => "&%$'?<>", "b" => "\r\n\t\"", "c" => ""),FALSE));
$this->parser->parse("<tag a=\"&%$'?<>\" b='\r\n\t\"' c = ''>");
}
function testAttributesPadding() {
$this->listener->expectOnce(
'startHandler',
array('*', 'tag', array("a" => "A", "b" => "B", "c" => "C"),FALSE));
$this->parser->parse("<tag\ta=\"A\"\rb='B'\nc = \"C\"\n>");
}
}
class TestOfProcessingInstructions extends ParserTestCase {
function TestOfProcessingInstructions() {
$this->ParserTestCase();
}
function testAllPi() { // Not correct on whitespace.
$this->listener->expectOnce('piHandler', array('*', 'php', ' print "Hello"; '));
$this->listener->expectNever('dataHandler');
$this->listener->expectNever('startHandler');
$this->listener->expectNever('endHandler');
$this->parser->parse('<?php print "Hello"; ?>');
}
function testNestedPi() { // Not correct on whitespace.
$this->listener->expectOnce('piHandler', array('*', 'php', ' print "Hello"; '));
$this->listener->expectArgumentsAt(0, 'dataHandler', array('*', 'a'));
$this->listener->expectArgumentsAt(1, 'dataHandler', array('*', 'b'));
$this->listener->expectCallCount('dataHandler', 2);
$this->listener->expectNever('startHandler');
$this->listener->expectNever('endHandler');
$this->parser->parse('a<?php print "Hello"; ?>b');
}
function testEscapeHandler() {
$this->listener->expectOnce(
'escapeHandler',
array('*', 'doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"'));
$this->parser->parse('<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">');
}
function testNestedEscapeHandler() {
$this->listener->expectOnce(
'escapeHandler',
array('*', 'doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"'));
$this->listener->expectArgumentsAt(0, 'dataHandler', array('*', 'a'));
$this->listener->expectArgumentsAt(1, 'dataHandler', array('*', 'b'));
$this->listener->expectCallCount('dataHandler', 2);
$this->parser->parse('a<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">b');
}
}
class TestOfComments extends ParserTestCase {
function TestOfComments() {
$this->ParserTestCase();
}
function testSimple() {
$this->listener->expectOnce('escapeHandler', array('*', ' A comment '));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('<!-- A comment -->');
}
function testNasty() {
$this->listener->expectOnce(
'escapeHandler',
array('*', ' <tag></tag><?php ?><' . '% %> '));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('<tag><!-- <tag></tag><?php ?><' . '% %> --></tag>');
}
function testFullEscapes() {
$this->listener->expectOnce('escapeHandler', array('*', '-- A comment --'));
$this->parser->parse('a<!-- A comment -->b');
}
function testWordEscape() {
$this->listener->expectOnce('escapeHandler', array('*', '[endif]'));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('a<![endif]>b');
}
function testWordEscapeNasty() {
$this->listener->expectOnce('escapeHandler', array('*', '[if gte mso 9]><xml></xml><![endif]'));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('a<!--[if gte mso 9]><xml></xml><![endif]-->b');
}
/**
* Parser should probably report some kind of error here.
*/
function testBadlyFormedComment() {
$this->listener->expectOnce('escapeHandler', array('*', ' This is badly formed>b'));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('a<!-- This is badly formed>b');
}
/**
* Parser should probably report some kind of error here.
*/
function testBadlyFormedCDATA() {
$this->listener->expectOnce('escapeHandler', array('*', ' This is badly formed>b'));
$this->parser->set_option('XML_OPTION_STRIP_ESCAPES');
$this->parser->parse('a<![CDATA[ This is badly formed>b');
}
}
class TestOfJasp extends ParserTestCase {
function TestOfJasp() {
$this->ParserTestCase();
}
function testSimple() {
$this->listener->expectOnce(
'jaspHandler',
array('*', ' document.write("Hello World");'));
$this->listener->expectNever('piHandler');
$this->listener->expectNever('escapeHandler');
$this->listener->expectNever('dataHandler');
$this->listener->expectNever('startHandler');
$this->listener->expectNever('endHandler');
$this->parser->parse('<' . '% document.write("Hello World");%>');
}
function testNasty() {
$this->listener->expectOnce(
'jaspHandler',
array('*', ' <tag a="A"><?php ?></tag><!-- comment --> '));
$this->listener->expectNever('piHandler');
$this->listener->expectNever('escapeHandler');
$this->listener->expectNever('dataHandler');
$this->listener->expectNever('startHandler');
$this->listener->expectNever('endHandler');
$this->parser->parse('<' . '% <tag a="A"><?php ?></tag><!-- comment --> %>');
}
function testInTag() {
$this->listener->expectOnce(
'jaspHandler',
array('*', ' document.write("Hello World");'));
$this->listener->expectNever('piHandler');
$this->listener->expectNever('escapeHandler');
$this->listener->expectNever('dataHandler');
$this->listener->expectOnce('startHandler');
$this->listener->expectOnce('endHandler');
$this->parser->parse('<tag><' . '% document.write("Hello World");%></tag>');
}
}
?>
\ No newline at end of file
php-xml-htmlsax3 (3.0.0+cvs01112007-2) unstable; urgency=low
* Bumped package version after ftp-master is back ! (Package
was just accepted before it went down..)
-- Romain Beauxis <toots@rastageeks.org> Wed, 14 Nov 2007 02:24:02 +0100
php-xml-htmlsax3 (3.0.0+cvs01112007-1) unstable; urgency=low
* New upstream release
* Fixed licence headers
-- Romain Beauxis <toots@rastageeks.org> Thu, 01 Nov 2007 01:01:59 +0100
php-xml-htmlsax3 (3.0.0~rc1-1) unstable; urgency=low
* Initial release (Closes: #445904)
-- Romain Beauxis <toots@rastageeks.org> Tue, 09 Oct 2007 03:01:05 +0200
5
Source: php-xml-htmlsax3
Section: web
Priority: extra
Maintainer: Romain Beauxis <toots@rastageeks.org>
Build-Depends: debhelper (>= 5), cdbs, dh-make-php
Standards-Version: 3.7.2
Package: php-xml-htmlsax3
Architecture: all
Depends: php-pear
Homepage: http://pear.php.net/package/XML_HTMLSax3/
Description: SAX parser for HTML and other badly formed XML documents
php-xml-htmlsax3 provides an API very similar to the native
PHP XML extension (http://www.php.net/xml), allowing handlers
using one to be easily adapted to the other.
.
The key difference is php-xml-htmlsax3 will not break
on badly formed XML, allowing it to be used for parsing
HTML documents.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment