Fix extant broken PEARSax3 parsing patterns.
[htmlpurifier.git] / library / HTMLPurifier / Lexer / PEARSax3.php
blobb87c8ae4fe06b1b595f989f0d6c78b3e0fd7fd49
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;
31 private $stack = array();
33 public function tokenizeHTML($string, $config, $context) {
35 $this->tokens = array();
37 $string = $this->normalize($string, $config, $context);
39 $this->parent_handler = set_error_handler(array($this, 'muteStrictErrorHandler'));
41 $parser = new XML_HTMLSax3();
42 $parser->set_object($this);
43 $parser->set_element_handler('openHandler','closeHandler');
44 $parser->set_data_handler('dataHandler');
45 $parser->set_escape_handler('escapeHandler');
47 // doesn't seem to work correctly for attributes
48 $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
50 $parser->parse($string);
52 restore_error_handler();
54 return $this->tokens;
58 /**
59 * Open tag event handler, interface is defined by PEAR package.
61 public function openHandler(&$parser, $name, $attrs, $closed) {
62 // entities are not resolved in attrs
63 foreach ($attrs as $key => $attr) {
64 $attrs[$key] = $this->parseData($attr);
66 if ($closed) {
67 $this->tokens[] = new HTMLPurifier_Token_Empty($name, $attrs);
68 } else {
69 $this->tokens[] = new HTMLPurifier_Token_Start($name, $attrs);
71 $this->stack[] = $name;
72 return true;
75 /**
76 * Close tag event handler, interface is defined by PEAR package.
78 public function closeHandler(&$parser, $name) {
79 // HTMLSax3 seems to always send empty tags an extra close tag
80 // check and ignore if you see it:
81 // [TESTME] to make sure it doesn't overreach
82 if ($this->tokens[count($this->tokens)-1] instanceof HTMLPurifier_Token_Empty) {
83 return true;
85 $this->tokens[] = new HTMLPurifier_Token_End($name);
86 if (!empty($this->stack)) array_pop($this->stack);
87 return true;
90 /**
91 * Data event handler, interface is defined by PEAR package.
93 public function dataHandler(&$parser, $data) {
94 $this->tokens[] = new HTMLPurifier_Token_Text($data);
95 return true;
98 /**
99 * Escaped text handler, interface is defined by PEAR package.
101 public function escapeHandler(&$parser, $data) {
102 if (strpos($data, '--') === 0) {
103 // remove trailing and leading double-dashes
104 $data = substr($data, 2);
105 if (strlen($data) >= 2 && substr($data, -2) == "--") {
106 $data = substr($data, 0, -2);
108 if (isset($this->stack[sizeof($this->stack) - 1]) &&
109 $this->stack[sizeof($this->stack) - 1] == "style") {
110 $this->tokens[] = new HTMLPurifier_Token_Text($data);
111 } else {
112 $this->tokens[] = new HTMLPurifier_Token_Comment($data);
115 // CDATA is handled elsewhere, but if it was handled here:
116 //if (strpos($data, '[CDATA[') === 0) {
117 // $this->tokens[] = new HTMLPurifier_Token_Text(
118 // substr($data, 7, strlen($data) - 9) );
120 return true;
124 * An error handler that mutes strict errors
126 public function muteStrictErrorHandler($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
127 if ($errno == E_STRICT) return;
128 return call_user_func($this->parent_handler, $errno, $errstr, $errfile, $errline, $errcontext);
133 // vim: et sw=4 sts=4