Add boolean value flag for PEARSax3 for testing if a token is empty.
[htmlpurifier.git] / library / HTMLPurifier / Lexer / PEARSax3.php
blob1d358c7b6bea2c36896c64ad99f2872b69bb0e9c
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();
29 protected $last_token_was_empty;
31 private $parent_handler;
32 private $stack = array();
34 public function tokenizeHTML($string, $config, $context) {
36 $this->tokens = array();
37 $this->last_token_was_empty = false;
39 $string = $this->normalize($string, $config, $context);
41 $this->parent_handler = set_error_handler(array($this, 'muteStrictErrorHandler'));
43 $parser = new XML_HTMLSax3();
44 $parser->set_object($this);
45 $parser->set_element_handler('openHandler','closeHandler');
46 $parser->set_data_handler('dataHandler');
47 $parser->set_escape_handler('escapeHandler');
49 // doesn't seem to work correctly for attributes
50 $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
52 $parser->parse($string);
54 restore_error_handler();
56 return $this->tokens;
60 /**
61 * Open tag event handler, interface is defined by PEAR package.
63 public function openHandler(&$parser, $name, $attrs, $closed) {
64 // entities are not resolved in attrs
65 foreach ($attrs as $key => $attr) {
66 $attrs[$key] = $this->parseData($attr);
68 if ($closed) {
69 $this->tokens[] = new HTMLPurifier_Token_Empty($name, $attrs);
70 $this->last_token_was_empty = true;
71 } else {
72 $this->tokens[] = new HTMLPurifier_Token_Start($name, $attrs);
74 $this->stack[] = $name;
75 return true;
78 /**
79 * Close tag event handler, interface is defined by PEAR package.
81 public function closeHandler(&$parser, $name) {
82 // HTMLSax3 seems to always send empty tags an extra close tag
83 // check and ignore if you see it:
84 // [TESTME] to make sure it doesn't overreach
85 if ($this->last_token_was_empty) {
86 $this->last_token_was_empty = false;
87 return true;
89 $this->tokens[] = new HTMLPurifier_Token_End($name);
90 if (!empty($this->stack)) array_pop($this->stack);
91 return true;
94 /**
95 * Data event handler, interface is defined by PEAR package.
97 public function dataHandler(&$parser, $data) {
98 $this->last_token_was_empty = false;
99 $this->tokens[] = new HTMLPurifier_Token_Text($data);
100 return true;
104 * Escaped text handler, interface is defined by PEAR package.
106 public function escapeHandler(&$parser, $data) {
107 if (strpos($data, '--') === 0) {
108 // remove trailing and leading double-dashes
109 $data = substr($data, 2);
110 if (strlen($data) >= 2 && substr($data, -2) == "--") {
111 $data = substr($data, 0, -2);
113 if (isset($this->stack[sizeof($this->stack) - 1]) &&
114 $this->stack[sizeof($this->stack) - 1] == "style") {
115 $this->tokens[] = new HTMLPurifier_Token_Text($data);
116 } else {
117 $this->tokens[] = new HTMLPurifier_Token_Comment($data);
119 $this->last_token_was_empty = false;
121 // CDATA is handled elsewhere, but if it was handled here:
122 //if (strpos($data, '[CDATA[') === 0) {
123 // $this->tokens[] = new HTMLPurifier_Token_Text(
124 // substr($data, 7, strlen($data) - 9) );
126 return true;
130 * An error handler that mutes strict errors
132 public function muteStrictErrorHandler($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
133 if ($errno == E_STRICT) return;
134 return call_user_func($this->parent_handler, $errno, $errstr, $errfile, $errline, $errcontext);
139 // vim: et sw=4 sts=4