Manually work around PEARSax3 E_STRICT errors.
[htmlpurifier.git] / library / HTMLPurifier / Lexer / PEARSax3.php
blob1b5da7e876e950b1829b09f9298e572d4bc82bd9
1 <?php
3 /**
4 * Proof-of-concept lexer that uses the PEAR package XML_HTMLSax3 to parse HTML.
6 * PEAR, not suprisingly, also has a SAX parser for HTML. I don't know
7 * very much about implementation, but it's fairly well written. However, that
8 * abstraction comes at a price: performance. You need to have it installed,
9 * and if the API changes, it might break our adapter. Not sure whether or not
10 * it's UTF-8 aware, but it has some entity parsing trouble (in all areas,
11 * text and attributes).
13 * Quite personally, I don't recommend using the PEAR class, and the defaults
14 * don't use it. The unit tests do perform the tests on the SAX parser too, but
15 * whatever it does for poorly formed HTML is up to it.
17 * @todo Generalize so that XML_HTMLSax is also supported.
19 * @warning Entity-resolution inside attributes is broken.
22 class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
25 /**
26 * Internal accumulator array for SAX parsers.
28 protected $tokens = array();
30 private $parent_handler;
32 public function tokenizeHTML($string, $config, $context) {
34 $this->tokens = array();
36 $string = $this->normalize($string, $config, $context);
38 $this->parent_handler = set_error_handler(array($this, 'muteStrictErrorHandler'));
40 $parser = new XML_HTMLSax3();
41 $parser->set_object($this);
42 $parser->set_element_handler('openHandler','closeHandler');
43 $parser->set_data_handler('dataHandler');
44 $parser->set_escape_handler('escapeHandler');
46 // doesn't seem to work correctly for attributes
47 $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
49 $parser->parse($string);
51 restore_error_handler();
53 return $this->tokens;
57 /**
58 * Open tag event handler, interface is defined by PEAR package.
60 public function openHandler(&$parser, $name, $attrs, $closed) {
61 // entities are not resolved in attrs
62 foreach ($attrs as $key => $attr) {
63 $attrs[$key] = $this->parseData($attr);
65 if ($closed) {
66 $this->tokens[] = new HTMLPurifier_Token_Empty($name, $attrs);
67 } else {
68 $this->tokens[] = new HTMLPurifier_Token_Start($name, $attrs);
70 return true;
73 /**
74 * Close tag event handler, interface is defined by PEAR package.
76 public function closeHandler(&$parser, $name) {
77 // HTMLSax3 seems to always send empty tags an extra close tag
78 // check and ignore if you see it:
79 // [TESTME] to make sure it doesn't overreach
80 if ($this->tokens[count($this->tokens)-1] instanceof HTMLPurifier_Token_Empty) {
81 return true;
83 $this->tokens[] = new HTMLPurifier_Token_End($name);
84 return true;
87 /**
88 * Data event handler, interface is defined by PEAR package.
90 public function dataHandler(&$parser, $data) {
91 $this->tokens[] = new HTMLPurifier_Token_Text($data);
92 return true;
95 /**
96 * Escaped text handler, interface is defined by PEAR package.
98 public function escapeHandler(&$parser, $data) {
99 if (strpos($data, '--') === 0) {
100 $this->tokens[] = new HTMLPurifier_Token_Comment($data);
102 // CDATA is handled elsewhere, but if it was handled here:
103 //if (strpos($data, '[CDATA[') === 0) {
104 // $this->tokens[] = new HTMLPurifier_Token_Text(
105 // substr($data, 7, strlen($data) - 9) );
107 return true;
111 * An error handler that mutes strict errors
113 public function muteStrictErrorHandler($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
114 if ($errno == E_STRICT) return;
115 return call_user_func($this->parent_handler, $errno, $errstr, $errfile, $errline, $errcontext);
120 // vim: et sw=4 sts=4