1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22: class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
23: {
24:
25: 26: 27:
28: protected $tokens = array();
29: protected $last_token_was_empty;
30:
31: private $parent_handler;
32: private $stack = array();
33:
34: public function tokenizeHTML($string, $config, $context) {
35:
36: $this->tokens = array();
37: $this->last_token_was_empty = false;
38:
39: $string = $this->normalize($string, $config, $context);
40:
41: $this->parent_handler = set_error_handler(array($this, 'muteStrictErrorHandler'));
42:
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');
48:
49:
50: $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
51:
52: $parser->parse($string);
53:
54: restore_error_handler();
55:
56: return $this->tokens;
57:
58: }
59:
60: 61: 62:
63: public function openHandler(&$parser, $name, $attrs, $closed) {
64:
65: foreach ($attrs as $key => $attr) {
66: $attrs[$key] = $this->parseData($attr);
67: }
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);
73: }
74: $this->stack[] = $name;
75: return true;
76: }
77:
78: 79: 80:
81: public function closeHandler(&$parser, $name) {
82:
83:
84:
85: if ($this->last_token_was_empty) {
86: $this->last_token_was_empty = false;
87: return true;
88: }
89: $this->tokens[] = new HTMLPurifier_Token_End($name);
90: if (!empty($this->stack)) array_pop($this->stack);
91: return true;
92: }
93:
94: 95: 96:
97: public function dataHandler(&$parser, $data) {
98: $this->last_token_was_empty = false;
99: $this->tokens[] = new HTMLPurifier_Token_Text($data);
100: return true;
101: }
102:
103: 104: 105:
106: public function escapeHandler(&$parser, $data) {
107: if (strpos($data, '--') === 0) {
108:
109: $data = substr($data, 2);
110: if (strlen($data) >= 2 && substr($data, -2) == "--") {
111: $data = substr($data, 0, -2);
112: }
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);
118: }
119: $this->last_token_was_empty = false;
120: }
121:
122:
123:
124:
125:
126: return true;
127: }
128:
129: 130: 131:
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);
135: }
136:
137: }
138:
139:
140: