3 require_once 'HTMLPurifier/Lexer.php';
6 * Our in-house implementation of a parser.
8 * A pure PHP parser, DirectLex has absolutely no dependencies, making
9 * it a reasonably good default for PHP4. Written with efficiency in mind,
10 * it can be four times faster than HTMLPurifier_Lexer_PEARSax3, although it
11 * pales in comparison to HTMLPurifier_Lexer_DOMLex. It will support UTF-8
12 * completely eventually.
14 * @todo Reread XML spec and document differences.
16 * @todo Determine correct behavior in transforming comment data. (preserve dashes?)
18 class HTMLPurifier_Lexer_DirectLex
extends HTMLPurifier_Lexer
22 * Whitespace characters for str(c)spn.
25 var $_whitespace = "\x20\x09\x0D\x0A";
27 function tokenizeHTML($html, $config, &$context) {
29 $html = $this->normalize($html, $config, $context);
31 $cursor = 0; // our location in the text
32 $inside_tag = false; // whether or not we're parsing the inside of a tag
33 $array = array(); // result array
35 // infinite loop protection
36 // has to be pretty big, since html docs can be big
37 // we're allow two hundred thousand tags... more than enough?
42 // infinite loop protection
43 if (++
$loops > 200000) return array();
45 $position_next_lt = strpos($html, '<', $cursor);
46 $position_next_gt = strpos($html, '>', $cursor);
48 // triggers on "<b>asdf</b>" but not "asdf <b></b>"
49 if ($position_next_lt === $cursor) {
54 if (!$inside_tag && $position_next_lt !== false) {
55 // We are not inside tag and there still is another tag to parse
57 HTMLPurifier_Token_Text(
60 $html, $cursor, $position_next_lt - $cursor
64 $cursor = $position_next_lt +
1;
67 } elseif (!$inside_tag) {
68 // We are not inside tag but there are no more tags
69 // If we're already at the end, break
70 if ($cursor === strlen($html)) break;
71 // Create Text of rest of string
73 HTMLPurifier_Token_Text(
81 } elseif ($inside_tag && $position_next_gt !== false) {
82 // We are in tag and it is well formed
83 // Grab the internals of the tag
84 $strlen_segment = $position_next_gt - $cursor;
85 $segment = substr($html, $cursor, $strlen_segment);
87 // Check if it's a comment
89 substr($segment, 0, 3) == '!--' &&
90 substr($segment, $strlen_segment-2, 2) == '--'
93 HTMLPurifier_Token_Comment(
95 $segment, 3, $strlen_segment - 5
99 $cursor = $position_next_gt +
1;
103 // Check if it's an end tag
104 $is_end_tag = (strpos($segment,'/') === 0);
106 $type = substr($segment, 1);
107 $array[] = new HTMLPurifier_Token_End($type);
109 $cursor = $position_next_gt +
1;
113 // Check leading character is alnum, if not, we may
114 // have accidently grabbed an emoticon. Translate into
115 // text and go our merry way
116 if (!ctype_alnum($segment[0])) {
118 HTMLPurifier_Token_Text(
125 $cursor = $position_next_gt +
1;
130 // Check if it is explicitly self closing, if so, remove
131 // trailing slash. Remember, we could have a tag like <br>, so
132 // any later token processing scripts must convert improperly
133 // classified EmptyTags from StartTags.
134 $is_self_closing= (strpos($segment,'/') === $strlen_segment-1);
135 if ($is_self_closing) {
137 $segment = substr($segment, 0, $strlen_segment);
140 // Check if there are any attributes
141 $position_first_space = strcspn($segment, $this->_whitespace
);
143 if ($position_first_space >= $strlen_segment) {
144 if ($is_self_closing) {
145 $array[] = new HTMLPurifier_Token_Empty($segment);
147 $array[] = new HTMLPurifier_Token_Start($segment);
150 $cursor = $position_next_gt +
1;
154 // Grab out all the data
155 $type = substr($segment, 0, $position_first_space);
159 $segment, $position_first_space
162 if ($attribute_string) {
163 $attr = $this->parseAttributeString(
171 if ($is_self_closing) {
172 $array[] = new HTMLPurifier_Token_Empty($type, $attr);
174 $array[] = new HTMLPurifier_Token_Start($type, $attr);
176 $cursor = $position_next_gt +
1;
181 HTMLPurifier_Token_Text(
184 substr($html, $cursor)
195 * Takes the inside of an HTML tag and makes an assoc array of attributes.
197 * @param $string Inside of tag excluding name.
198 * @returns Assoc array of attributes.
200 function parseAttributeString($string, $config, &$context) {
201 $string = (string) $string; // quick typecast
203 if ($string == '') return array(); // no attributes
205 // let's see if we can abort as quickly as possible
206 // one equal sign, no spaces => one attribute
207 $num_equal = substr_count($string, '=');
208 $has_space = strpos($string, ' ');
209 if ($num_equal === 0 && !$has_space) {
211 return array($string => $string);
212 } elseif ($num_equal === 1 && !$has_space) {
213 // only one attribute
214 list($key, $quoted_value) = explode('=', $string);
215 $quoted_value = trim($quoted_value);
216 if (!$key) return array();
217 if (!$quoted_value) return array($key => '');
218 $first_char = @$quoted_value[0];
219 $last_char = @$quoted_value[strlen($quoted_value)-1];
221 $same_quote = ($first_char == $last_char);
222 $open_quote = ($first_char == '"' ||
$first_char == "'");
224 if ( $same_quote && $open_quote) {
226 $value = substr($quoted_value, 1, strlen($quoted_value) - 2);
230 $value = substr($quoted_value, 1);
232 $value = $quoted_value;
235 return array($key => $value);
238 // setup loop environment
239 $array = array(); // return assoc array of attributes
240 $cursor = 0; // current position in string (moves forward)
241 $size = strlen($string); // size of the string (stays the same)
243 // if we have unquoted attributes, the parser expects a terminating
244 // space, so let's guarantee that there's always a terminating space.
247 // infinite loop protection
252 // infinite loop protection
253 if (++
$loops > 1000) return array();
255 if ($cursor >= $size) {
259 $cursor +
= ($value = strspn($string, $this->_whitespace
, $cursor));
263 $key_begin = $cursor; //we're currently at the start of the key
265 // scroll past all characters that are the key (not whitespace or =)
266 $cursor +
= strcspn($string, $this->_whitespace
. '=', $cursor);
268 $key_end = $cursor; // now at the end of the key
270 $key = substr($string, $key_begin, $key_end - $key_begin);
272 if (!$key) continue; // empty key
274 // scroll past all whitespace
275 $cursor +
= strspn($string, $this->_whitespace
, $cursor);
277 if ($cursor >= $size) {
282 // if the next character is an equal sign, we've got a regular
283 // pair, otherwise, it's a bool attribute
284 $first_char = @$string[$cursor];
286 if ($first_char == '=') {
290 $cursor +
= strspn($string, $this->_whitespace
, $cursor);
292 // we might be in front of a quote right now
294 $char = @$string[$cursor];
296 if ($char == '"' ||
$char == "'") {
297 // it's quoted, end bound is $char
299 $value_begin = $cursor;
300 $cursor = strpos($string, $char, $cursor);
301 $value_end = $cursor;
303 // it's not quoted, end bound is whitespace
304 $value_begin = $cursor;
305 $cursor +
= strcspn($string, $this->_whitespace
, $cursor);
306 $value_end = $cursor;
309 $value = substr($string, $value_begin, $value_end - $value_begin);
310 $array[$key] = $this->parseData($value);