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
26 * Internal accumulator array for SAX parsers.
28 protected $tokens = array();
30 public function tokenizeHTML($string, $config, $context) {
32 $this->tokens
= array();
34 $string = $this->normalize($string, $config, $context);
36 $parser = new XML_HTMLSax3();
37 $parser->set_object($this);
38 $parser->set_element_handler('openHandler','closeHandler');
39 $parser->set_data_handler('dataHandler');
40 $parser->set_escape_handler('escapeHandler');
42 // doesn't seem to work correctly for attributes
43 $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
45 $parser->parse($string);
52 * Open tag event handler, interface is defined by PEAR package.
54 public function openHandler(&$parser, $name, $attrs, $closed) {
55 // entities are not resolved in attrs
56 foreach ($attrs as $key => $attr) {
57 $attrs[$key] = $this->parseData($attr);
60 $this->tokens
[] = new HTMLPurifier_Token_Empty($name, $attrs);
62 $this->tokens
[] = new HTMLPurifier_Token_Start($name, $attrs);
68 * Close tag event handler, interface is defined by PEAR package.
70 public function closeHandler(&$parser, $name) {
71 // HTMLSax3 seems to always send empty tags an extra close tag
72 // check and ignore if you see it:
73 // [TESTME] to make sure it doesn't overreach
74 if ($this->tokens
[count($this->tokens
)-1] instanceof HTMLPurifier_Token_Empty
) {
77 $this->tokens
[] = new HTMLPurifier_Token_End($name);
82 * Data event handler, interface is defined by PEAR package.
84 public function dataHandler(&$parser, $data) {
85 $this->tokens
[] = new HTMLPurifier_Token_Text($data);
90 * Escaped text handler, interface is defined by PEAR package.
92 public function escapeHandler(&$parser, $data) {
93 if (strpos($data, '--') === 0) {
94 $this->tokens
[] = new HTMLPurifier_Token_Comment($data);
96 // CDATA is handled elsewhere, but if it was handled here:
97 //if (strpos($data, '[CDATA[') === 0) {
98 // $this->tokens[] = new HTMLPurifier_Token_Text(
99 // substr($data, 7, strlen($data) - 9) );
106 // vim: et sw=4 sts=4