third_party
4 years ago
IQuery.php
4 years ago
LICENSE
4 years ago
gan_formatter.php
4 years ago
gan_node_html.php
4 years ago
gan_parser_html.php
4 years ago
gan_selector_html.php
4 years ago
gan_tokenizer.php
4 years ago
gan_xml2array.php
4 years ago
ganon.php
4 years ago
index.php
4 years ago
pQuery.php
4 years ago
gan_parser_html.php
841 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @author Niels A.D. |
| 4 | * @author Todd Burry <todd@vanillaforums.com> |
| 5 | * @copyright 2010 Niels A.D., 2014 Todd Burry |
| 6 | * @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1 |
| 7 | * @package pQuery |
| 8 | */ |
| 9 | |
| 10 | namespace MailPoetVendor\pQuery; |
| 11 | |
| 12 | if (!defined('ABSPATH')) exit; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Parses a HTML document |
| 17 | * |
| 18 | * Functionality can be extended by overriding functions or adjusting the tag map. |
| 19 | * Document may contain small errors, the parser will try to recover and resume parsing. |
| 20 | */ |
| 21 | class HtmlParserBase extends TokenizerBase { |
| 22 | |
| 23 | /** |
| 24 | * Tag open token, used for "<" |
| 25 | */ |
| 26 | const TOK_TAG_OPEN = 100; |
| 27 | /** |
| 28 | * Tag close token, used for ">" |
| 29 | */ |
| 30 | const TOK_TAG_CLOSE = 101; |
| 31 | /** |
| 32 | * Forward slash token, used for "/" |
| 33 | */ |
| 34 | const TOK_SLASH_FORWARD = 103; |
| 35 | /** |
| 36 | * Backslash token, used for "\" |
| 37 | */ |
| 38 | const TOK_SLASH_BACKWARD = 104; |
| 39 | /** |
| 40 | * String token, used for attribute values (" and ') |
| 41 | */ |
| 42 | const TOK_STRING = 104; |
| 43 | /** |
| 44 | * Equals token, used for "=" |
| 45 | */ |
| 46 | const TOK_EQUALS = 105; |
| 47 | |
| 48 | /** |
| 49 | * Sets HTML identifiers, tags/attributes are considered identifiers |
| 50 | * @see TokenizerBase::$identifiers |
| 51 | * @access private |
| 52 | */ |
| 53 | var $identifiers = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:-_!?%'; |
| 54 | |
| 55 | /** |
| 56 | * Status of the parser (tagname, closing tag, etc) |
| 57 | * @var array |
| 58 | */ |
| 59 | var $status = array(); |
| 60 | |
| 61 | /** |
| 62 | * Map characters to match their tokens |
| 63 | * @see TokenizerBase::$custom_char_map |
| 64 | * @access private |
| 65 | */ |
| 66 | var $custom_char_map = array( |
| 67 | '<' => self::TOK_TAG_OPEN, |
| 68 | '>' => self::TOK_TAG_CLOSE, |
| 69 | "'" => 'parse_string', |
| 70 | '"' => 'parse_string', |
| 71 | '/' => self::TOK_SLASH_FORWARD, |
| 72 | '\\' => self::TOK_SLASH_BACKWARD, |
| 73 | '=' => self::TOK_EQUALS |
| 74 | ); |
| 75 | |
| 76 | function __construct($doc = '', $pos = 0) { |
| 77 | parent::__construct($doc, $pos); |
| 78 | $this->parse_all(); |
| 79 | } |
| 80 | |
| 81 | #php4 PHP4 class constructor compatibility |
| 82 | #function HtmlParserBase($doc = '', $pos = 0) {return $this->__construct($doc, $pos);} |
| 83 | #php4e |
| 84 | |
| 85 | /** |
| 86 | Callback functions for certain tags |
| 87 | @var array (TAG_NAME => FUNCTION_NAME) |
| 88 | @internal Function should be a method in the class |
| 89 | @internal Tagname should be lowercase and is everything after <, e.g. "?php" or "!doctype" |
| 90 | @access private |
| 91 | */ |
| 92 | var $tag_map = array( |
| 93 | '!doctype' => 'parse_doctype', |
| 94 | '?' => 'parse_php', |
| 95 | '?php' => 'parse_php', |
| 96 | '%' => 'parse_asp', |
| 97 | 'style' => 'parse_style', |
| 98 | 'script' => 'parse_script' |
| 99 | ); |
| 100 | |
| 101 | /** |
| 102 | * Parse a HTML string (attributes) |
| 103 | * @internal Gets called with ' and " |
| 104 | * @return int |
| 105 | */ |
| 106 | protected function parse_string() { |
| 107 | if ($this->next_pos($this->doc[$this->pos], false) !== self::TOK_UNKNOWN) { |
| 108 | --$this->pos; |
| 109 | } |
| 110 | return self::TOK_STRING; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Parse text between tags |
| 115 | * @internal Gets called between tags, uses {@link $status}[last_pos] |
| 116 | * @internal Stores text in {@link $status}[text] |
| 117 | */ |
| 118 | function parse_text() { |
| 119 | $len = $this->pos - 1 - $this->status['last_pos']; |
| 120 | $this->status['text'] = (($len > 0) ? substr($this->doc, $this->status['last_pos'] + 1, $len) : ''); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Parse comment tags |
| 125 | * @internal Gets called with HTML comments ("<!--") |
| 126 | * @internal Stores text in {@link $status}[comment] |
| 127 | * @return bool |
| 128 | */ |
| 129 | function parse_comment() { |
| 130 | $this->pos += 3; |
| 131 | if ($this->next_pos('-->', false) !== self::TOK_UNKNOWN) { |
| 132 | $this->status['comment'] = $this->getTokenString(1, -1); |
| 133 | --$this->pos; |
| 134 | } else { |
| 135 | $this->status['comment'] = $this->getTokenString(1, -1); |
| 136 | $this->pos += 2; |
| 137 | } |
| 138 | $this->status['last_pos'] = $this->pos; |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Parse doctype tag |
| 145 | * @internal Gets called with doctype ("<!doctype") |
| 146 | * @internal Stores text in {@link $status}[dtd] |
| 147 | * @return bool |
| 148 | */ |
| 149 | function parse_doctype() { |
| 150 | $start = $this->pos; |
| 151 | if ($this->next_search('[>', false) === self::TOK_UNKNOWN) { |
| 152 | if ($this->doc[$this->pos] === '[') { |
| 153 | if (($this->next_pos(']', false) !== self::TOK_UNKNOWN) || ($this->next_pos('>', false) !== self::TOK_UNKNOWN)) { |
| 154 | $this->addError('Invalid doctype'); |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $this->token_start = $start; |
| 160 | $this->status['dtd'] = $this->getTokenString(2, -1); |
| 161 | $this->status['last_pos'] = $this->pos; |
| 162 | return true; |
| 163 | } else { |
| 164 | $this->addError('Invalid doctype'); |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Parse cdata tag |
| 171 | * @internal Gets called with cdata ("<![cdata") |
| 172 | * @internal Stores text in {@link $status}[cdata] |
| 173 | * @return bool |
| 174 | */ |
| 175 | function parse_cdata() { |
| 176 | if ($this->next_pos(']]>', false) === self::TOK_UNKNOWN) { |
| 177 | $this->status['cdata'] = $this->getTokenString(9, -1); |
| 178 | $this->status['last_pos'] = $this->pos + 2; |
| 179 | return true; |
| 180 | } else { |
| 181 | $this->addError('Invalid cdata tag'); |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Parse php tags |
| 188 | * @internal Gets called with php tags ("<?php") |
| 189 | * @return bool |
| 190 | */ |
| 191 | function parse_php() { |
| 192 | $start = $this->pos; |
| 193 | if ($this->next_pos('?>', false) !== self::TOK_UNKNOWN) { |
| 194 | $this->pos -= 2; //End of file |
| 195 | } |
| 196 | |
| 197 | $len = $this->pos - 1 - $start; |
| 198 | $this->status['text'] = (($len > 0) ? substr($this->doc, $start + 1, $len) : ''); |
| 199 | $this->status['last_pos'] = ++$this->pos; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Parse asp tags |
| 205 | * @internal Gets called with asp tags ("<%") |
| 206 | * @return bool |
| 207 | */ |
| 208 | function parse_asp() { |
| 209 | $start = $this->pos; |
| 210 | if ($this->next_pos('%>', false) !== self::TOK_UNKNOWN) { |
| 211 | $this->pos -= 2; //End of file |
| 212 | } |
| 213 | |
| 214 | $len = $this->pos - 1 - $start; |
| 215 | $this->status['text'] = (($len > 0) ? substr($this->doc, $start + 1, $len) : ''); |
| 216 | $this->status['last_pos'] = ++$this->pos; |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Parse style tags |
| 222 | * @internal Gets called with php tags ("<style>") |
| 223 | * @return bool |
| 224 | */ |
| 225 | function parse_style() { |
| 226 | if ($this->parse_attributes() && ($this->token === self::TOK_TAG_CLOSE) && ($start = $this->pos) && ($this->next_pos('</style>', false) === self::TOK_UNKNOWN)) { |
| 227 | $len = $this->pos - 1 - $start; |
| 228 | $this->status['text'] = (($len > 0) ? substr($this->doc, $start + 1, $len) : ''); |
| 229 | |
| 230 | $this->pos += 7; |
| 231 | $this->status['last_pos'] = $this->pos; |
| 232 | return true; |
| 233 | } else { |
| 234 | $this->addError('No end for style tag found'); |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Parse script tags |
| 241 | * @internal Gets called with php tags ("<script>") |
| 242 | * @return bool |
| 243 | */ |
| 244 | function parse_script() { |
| 245 | if ($this->parse_attributes() && ($this->token === self::TOK_TAG_CLOSE) && ($start = $this->pos) && ($this->next_pos('</script>', false) === self::TOK_UNKNOWN)) { |
| 246 | $len = $this->pos - 1 - $start; |
| 247 | $this->status['text'] = (($len > 0) ? substr($this->doc, $start + 1, $len) : ''); |
| 248 | |
| 249 | $this->pos += 8; |
| 250 | $this->status['last_pos'] = $this->pos; |
| 251 | return true; |
| 252 | } else { |
| 253 | $this->addError('No end for script tag found'); |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Parse conditional tags (+ all conditional tags inside) |
| 260 | * @internal Gets called with IE conditionals ("<![if]" and "<!--[if]") |
| 261 | * @internal Stores condition in {@link $status}[tag_condition] |
| 262 | * @return bool |
| 263 | */ |
| 264 | function parse_conditional() { |
| 265 | if ($this->status['closing_tag']) { |
| 266 | $this->pos += 8; |
| 267 | } else { |
| 268 | $this->pos += (($this->status['comment']) ? 5 : 3); |
| 269 | if ($this->next_pos(']', false) !== self::TOK_UNKNOWN) { |
| 270 | $this->addError('"]" not found in conditional tag'); |
| 271 | return false; |
| 272 | } |
| 273 | $this->status['tag_condition'] = $this->getTokenString(0, -1); |
| 274 | } |
| 275 | |
| 276 | if ($this->next_no_whitespace() !== self::TOK_TAG_CLOSE) { |
| 277 | $this->addError('No ">" tag found 2 for conditional tag'); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if ($this->status['comment']) { |
| 282 | $this->status['last_pos'] = $this->pos; |
| 283 | if ($this->next_pos('-->', false) !== self::TOK_UNKNOWN) { |
| 284 | $this->addError('No ending tag found for conditional tag'); |
| 285 | $this->pos = $this->size - 1; |
| 286 | |
| 287 | $len = $this->pos - 1 - $this->status['last_pos']; |
| 288 | $this->status['text'] = (($len > 0) ? substr($this->doc, $this->status['last_pos'] + 1, $len) : ''); |
| 289 | } else { |
| 290 | $len = $this->pos - 10 - $this->status['last_pos']; |
| 291 | $this->status['text'] = (($len > 0) ? substr($this->doc, $this->status['last_pos'] + 1, $len) : ''); |
| 292 | $this->pos += 2; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | $this->status['last_pos'] = $this->pos; |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Parse attributes (names + value) |
| 302 | * @internal Stores attributes in {@link $status}[attributes] (array(ATTR => VAL)) |
| 303 | * @return bool |
| 304 | */ |
| 305 | function parse_attributes() { |
| 306 | $this->status['attributes'] = array(); |
| 307 | |
| 308 | while ($this->next_no_whitespace() === self::TOK_IDENTIFIER) { |
| 309 | $attr = $this->getTokenString(); |
| 310 | if (($attr === '?') || ($attr === '%')) { |
| 311 | //Probably closing tags |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if ($this->next_no_whitespace() === self::TOK_EQUALS) { |
| 316 | if ($this->next_no_whitespace() === self::TOK_STRING) { |
| 317 | $val = $this->getTokenString(1, -1); |
| 318 | } else { |
| 319 | $this->token_start = $this->pos; |
| 320 | if (!isset($stop)) { |
| 321 | $stop = $this->whitespace; |
| 322 | $stop['<'] = true; |
| 323 | $stop['>'] = true; |
| 324 | } |
| 325 | |
| 326 | while ((++$this->pos < $this->size) && (!isset($stop[$this->doc[$this->pos]]))) { |
| 327 | // Do nothing. |
| 328 | } |
| 329 | --$this->pos; |
| 330 | |
| 331 | $val = $this->getTokenString(); |
| 332 | |
| 333 | if (trim($val) === '') { |
| 334 | $this->addError('Invalid attribute value'); |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | } else { |
| 339 | $val = $attr; |
| 340 | $this->pos = (($this->token_start) ? $this->token_start : $this->pos) - 1; |
| 341 | } |
| 342 | |
| 343 | $this->status['attributes'][$attr] = $val; |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Default callback for tags |
| 351 | * @internal Gets called after the tagname (<html*ENTERS_HERE* attribute="value">) |
| 352 | * @return bool |
| 353 | */ |
| 354 | function parse_tag_default() { |
| 355 | if ($this->status['closing_tag']) { |
| 356 | $this->status['attributes'] = array(); |
| 357 | $this->next_no_whitespace(); |
| 358 | } else { |
| 359 | if (!$this->parse_attributes()) { |
| 360 | return false; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if ($this->token !== self::TOK_TAG_CLOSE) { |
| 365 | if ($this->token === self::TOK_SLASH_FORWARD) { |
| 366 | $this->status['self_close'] = true; |
| 367 | $this->next(); |
| 368 | } elseif ((($this->status['tag_name'][0] === '?') && ($this->doc[$this->pos] === '?')) || (($this->status['tag_name'][0] === '%') && ($this->doc[$this->pos] === '%'))) { |
| 369 | $this->status['self_close'] = true; |
| 370 | $this->pos++; |
| 371 | |
| 372 | if (isset($this->char_map[$this->doc[$this->pos]]) && (!is_string($this->char_map[$this->doc[$this->pos]]))) { |
| 373 | $this->token = $this->char_map[$this->doc[$this->pos]]; |
| 374 | } else { |
| 375 | $this->token = self::TOK_UNKNOWN; |
| 376 | } |
| 377 | }/* else { |
| 378 | $this->status['self_close'] = false; |
| 379 | }*/ |
| 380 | } |
| 381 | |
| 382 | if ($this->token !== self::TOK_TAG_CLOSE) { |
| 383 | $this->addError('Expected ">", but found "'.$this->getTokenString().'"'); |
| 384 | if ($this->next_pos('>', false) !== self::TOK_UNKNOWN) { |
| 385 | $this->addError('No ">" tag found for "'.$this->status['tag_name'].'" tag'); |
| 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Parse tag |
| 395 | * @internal Gets called after opening tag (<*ENTERS_HERE*html attribute="value">) |
| 396 | * @internal Stores information about the tag in {@link $status} (comment, closing_tag, tag_name) |
| 397 | * @return bool |
| 398 | */ |
| 399 | function parse_tag() { |
| 400 | $start = $this->pos; |
| 401 | $this->status['self_close'] = false; |
| 402 | $this->parse_text(); |
| 403 | |
| 404 | $next = (($this->pos + 1) < $this->size) ? $this->doc[$this->pos + 1] : ''; |
| 405 | if ($next === '!') { |
| 406 | $this->status['closing_tag'] = false; |
| 407 | |
| 408 | if (substr($this->doc, $this->pos + 2, 2) === '--') { |
| 409 | $this->status['comment'] = true; |
| 410 | |
| 411 | if (($this->doc[$this->pos + 4] === '[') && (strcasecmp(substr($this->doc, $this->pos + 5, 2), 'if') === 0)) { |
| 412 | return $this->parse_conditional(); |
| 413 | } else { |
| 414 | return $this->parse_comment(); |
| 415 | } |
| 416 | } else { |
| 417 | $this->status['comment'] = false; |
| 418 | |
| 419 | if ($this->doc[$this->pos + 2] === '[') { |
| 420 | if (strcasecmp(substr($this->doc, $this->pos + 3, 2), 'if') === 0) { |
| 421 | return $this->parse_conditional(); |
| 422 | } elseif (strcasecmp(substr($this->doc, $this->pos + 3, 5), 'endif') === 0) { |
| 423 | $this->status['closing_tag'] = true; |
| 424 | return $this->parse_conditional(); |
| 425 | } elseif (strcasecmp(substr($this->doc, $this->pos + 3, 5), 'cdata') === 0) { |
| 426 | return $this->parse_cdata(); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } elseif ($next === '/') { |
| 431 | $this->status['closing_tag'] = true; |
| 432 | ++$this->pos; |
| 433 | } else { |
| 434 | $this->status['closing_tag'] = false; |
| 435 | } |
| 436 | |
| 437 | if ($this->next() !== self::TOK_IDENTIFIER) { |
| 438 | $this->addError('Tagname expected'); |
| 439 | //if ($this->next_pos('>', false) === self::TOK_UNKNOWN) { |
| 440 | $this->status['last_pos'] = $start - 1; |
| 441 | return true; |
| 442 | //} else { |
| 443 | // return false; |
| 444 | //} |
| 445 | } |
| 446 | |
| 447 | $tag = $this->getTokenString(); |
| 448 | $this->status['tag_name'] = $tag; |
| 449 | $tag = strtolower($tag); |
| 450 | |
| 451 | if (isset($this->tag_map[$tag])) { |
| 452 | $res = $this->{$this->tag_map[$tag]}(); |
| 453 | } else { |
| 454 | $res = $this->parse_tag_default(); |
| 455 | } |
| 456 | |
| 457 | $this->status['last_pos'] = $this->pos; |
| 458 | return $res; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Parse full document |
| 463 | * @return bool |
| 464 | */ |
| 465 | function parse_all() { |
| 466 | $this->errors = array(); |
| 467 | $this->status['last_pos'] = -1; |
| 468 | |
| 469 | if (($this->token === self::TOK_TAG_OPEN) || ($this->next_pos('<', false) === self::TOK_UNKNOWN)) { |
| 470 | do { |
| 471 | if (!$this->parse_tag()) { |
| 472 | return false; |
| 473 | } |
| 474 | } while ($this->next_pos('<') !== self::TOK_NULL); |
| 475 | } |
| 476 | |
| 477 | $this->pos = $this->size; |
| 478 | $this->parse_text(); |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Parses a HTML document into a HTML DOM |
| 486 | */ |
| 487 | class HtmlParser extends HtmlParserBase { |
| 488 | |
| 489 | /** |
| 490 | * Root object |
| 491 | * @internal If string, then it will create a new instance as root |
| 492 | * @var DomNode |
| 493 | */ |
| 494 | var $root = 'MailPoetVendor\\pQuery\\DomNode'; |
| 495 | |
| 496 | /** |
| 497 | * Current parsing hierarchy |
| 498 | * @internal Root is always at index 0, current tag is at the end of the array |
| 499 | * @var array |
| 500 | * @access private |
| 501 | */ |
| 502 | var $hierarchy = array(); |
| 503 | |
| 504 | /** |
| 505 | * Tags that don't need closing tags |
| 506 | * @var array |
| 507 | * @access private |
| 508 | */ |
| 509 | var $tags_selfclose = array( |
| 510 | 'area' => true, |
| 511 | 'base' => true, |
| 512 | 'basefont' => true, |
| 513 | 'br' => true, |
| 514 | 'col' => true, |
| 515 | 'command' => true, |
| 516 | 'embed' => true, |
| 517 | 'frame' => true, |
| 518 | 'hr' => true, |
| 519 | 'img' => true, |
| 520 | 'input' => true, |
| 521 | 'ins' => true, |
| 522 | 'keygen' => true, |
| 523 | 'link' => true, |
| 524 | 'meta' => true, |
| 525 | 'param' => true, |
| 526 | 'source' => true, |
| 527 | 'track' => true, |
| 528 | 'wbr' => true |
| 529 | ); |
| 530 | |
| 531 | /** |
| 532 | * Class constructor |
| 533 | * @param string $doc Document to be tokenized |
| 534 | * @param int $pos Position to start parsing |
| 535 | * @param DomNode $root Root node, null to auto create |
| 536 | */ |
| 537 | function __construct($doc = '', $pos = 0, $root = null) { |
| 538 | if ($root === null) { |
| 539 | $root = new $this->root('~root~', null); |
| 540 | } |
| 541 | $this->root =& $root; |
| 542 | |
| 543 | parent::__construct($doc, $pos); |
| 544 | } |
| 545 | |
| 546 | #php4 PHP4 class constructor compatibility |
| 547 | #function HtmlParser($doc = '', $pos = 0, $root = null) {return $this->__construct($doc, $pos, $root);} |
| 548 | #php4e |
| 549 | |
| 550 | /** |
| 551 | * Class magic invoke method, performs {@link select()} |
| 552 | * @return array |
| 553 | * @access private |
| 554 | */ |
| 555 | function __invoke($query = '*') { |
| 556 | return $this->select($query); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Class magic toString method, performs {@link DomNode::toString()} |
| 561 | * @return string |
| 562 | * @access private |
| 563 | */ |
| 564 | function __toString() { |
| 565 | return $this->root->getInnerText(); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Performs a css select query on the root node |
| 570 | * @see DomNode::select() |
| 571 | * @return array |
| 572 | */ |
| 573 | function select($query = '*', $index = false, $recursive = true, $check_self = false) { |
| 574 | return $this->root->select($query, $index, $recursive, $check_self); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Updates the current hierarchy status and checks for |
| 579 | * correct opening/closing of tags |
| 580 | * @param bool $self_close Is current tag self closing? Null to use {@link tags_selfclose} |
| 581 | * @internal This is were most of the nodes get added |
| 582 | * @access private |
| 583 | */ |
| 584 | protected function parse_hierarchy($self_close = null) { |
| 585 | if ($self_close === null) { |
| 586 | $this->status['self_close'] = ($self_close = isset($this->tags_selfclose[strtolower($this->status['tag_name'])])); |
| 587 | } |
| 588 | |
| 589 | if ($self_close) { |
| 590 | if ($this->status['closing_tag']) { |
| 591 | |
| 592 | //$c = end($this->hierarchy)->children |
| 593 | $c = $this->hierarchy[count($this->hierarchy) - 1]->children; |
| 594 | $found = false; |
| 595 | for ($count = count($c), $i = $count - 1; $i >= 0; $i--) { |
| 596 | if (strcasecmp($c[$i]->tag, $this->status['tag_name']) === 0) { |
| 597 | for($ii = $i + 1; $ii < $count; $ii++) { |
| 598 | $index = null; //Needs to be passed by ref |
| 599 | $c[$i + 1]->changeParent($c[$i], $index); |
| 600 | } |
| 601 | $c[$i]->self_close = false; |
| 602 | |
| 603 | $found = true; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (!$found) { |
| 609 | $this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open'); |
| 610 | } |
| 611 | |
| 612 | } elseif ($this->status['tag_name'][0] === '?') { |
| 613 | //end($this->hierarchy)->addXML($this->status['tag_name'], '', $this->status['attributes']); |
| 614 | $index = null; //Needs to be passed by ref |
| 615 | $this->hierarchy[count($this->hierarchy) - 1]->addXML($this->status['tag_name'], '', $this->status['attributes'], $index); |
| 616 | } elseif ($this->status['tag_name'][0] === '%') { |
| 617 | //end($this->hierarchy)->addASP($this->status['tag_name'], '', $this->status['attributes']); |
| 618 | $index = null; //Needs to be passed by ref |
| 619 | $this->hierarchy[count($this->hierarchy) - 1]->addASP($this->status['tag_name'], '', $this->status['attributes'], $index); |
| 620 | } else { |
| 621 | //end($this->hierarchy)->addChild($this->status); |
| 622 | $index = null; //Needs to be passed by ref |
| 623 | $this->hierarchy[count($this->hierarchy) - 1]->addChild($this->status, $index); |
| 624 | } |
| 625 | } elseif ($this->status['closing_tag']) { |
| 626 | $found = false; |
| 627 | for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) { |
| 628 | if (strcasecmp($this->hierarchy[$i]->tag, $this->status['tag_name']) === 0) { |
| 629 | |
| 630 | for($ii = ($count - $i - 1); $ii >= 0; $ii--) { |
| 631 | $e = array_pop($this->hierarchy); |
| 632 | if ($ii > 0) { |
| 633 | $this->addError('Closing tag "'.$this->status['tag_name'].'" while "'.$e->tag.'" is not closed yet'); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | $found = true; |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (!$found) { |
| 643 | $this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open'); |
| 644 | } |
| 645 | |
| 646 | } else { |
| 647 | //$this->hierarchy[] = end($this->hierarchy)->addChild($this->status); |
| 648 | $index = null; //Needs to be passed by ref |
| 649 | $this->hierarchy[] = $this->hierarchy[count($this->hierarchy) - 1]->addChild($this->status, $index); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | function parse_cdata() { |
| 654 | if (!parent::parse_cdata()) {return false;} |
| 655 | |
| 656 | //end($this->hierarchy)->addCDATA($this->status['cdata']); |
| 657 | $index = null; //Needs to be passed by ref |
| 658 | $this->hierarchy[count($this->hierarchy) - 1]->addCDATA($this->status['cdata'], $index); |
| 659 | return true; |
| 660 | } |
| 661 | |
| 662 | function parse_comment() { |
| 663 | if (!parent::parse_comment()) {return false;} |
| 664 | |
| 665 | //end($this->hierarchy)->addComment($this->status['comment']); |
| 666 | $index = null; //Needs to be passed by ref |
| 667 | $this->hierarchy[count($this->hierarchy) - 1]->addComment($this->status['comment'], $index); |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | function parse_conditional() { |
| 672 | if (!parent::parse_conditional()) {return false;} |
| 673 | |
| 674 | if ($this->status['comment']) { |
| 675 | //$e = end($this->hierarchy)->addConditional($this->status['tag_condition'], true); |
| 676 | $index = null; //Needs to be passed by ref |
| 677 | $e = $this->hierarchy[count($this->hierarchy) - 1]->addConditional($this->status['tag_condition'], true, $index); |
| 678 | if ($this->status['text'] !== '') { |
| 679 | $index = null; //Needs to be passed by ref |
| 680 | $e->addText($this->status['text'], $index); |
| 681 | } |
| 682 | } else { |
| 683 | if ($this->status['closing_tag']) { |
| 684 | $this->parse_hierarchy(false); |
| 685 | } else { |
| 686 | //$this->hierarchy[] = end($this->hierarchy)->addConditional($this->status['tag_condition'], false); |
| 687 | $index = null; //Needs to be passed by ref |
| 688 | $this->hierarchy[] = $this->hierarchy[count($this->hierarchy) - 1]->addConditional($this->status['tag_condition'], false, $index); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | function parse_doctype() { |
| 696 | if (!parent::parse_doctype()) {return false;} |
| 697 | |
| 698 | //end($this->hierarchy)->addDoctype($this->status['dtd']); |
| 699 | $index = null; //Needs to be passed by ref |
| 700 | $this->hierarchy[count($this->hierarchy) - 1]->addDoctype($this->status['dtd'], $index); |
| 701 | return true; |
| 702 | } |
| 703 | |
| 704 | function parse_php() { |
| 705 | if (!parent::parse_php()) {return false;} |
| 706 | |
| 707 | //end($this->hierarchy)->addXML('php', $this->status['text']); |
| 708 | $index = null; //Needs to be passed by ref |
| 709 | $this->hierarchy[count($this->hierarchy) - 1]->addXML('php', $this->status['text'], $index); |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | function parse_asp() { |
| 714 | if (!parent::parse_asp()) {return false;} |
| 715 | |
| 716 | //end($this->hierarchy)->addASP('', $this->status['text']); |
| 717 | $index = null; //Needs to be passed by ref |
| 718 | $this->hierarchy[count($this->hierarchy) - 1]->addASP('', $this->status['text'], $index); |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | function parse_script() { |
| 723 | if (!parent::parse_script()) {return false;} |
| 724 | |
| 725 | //$e = end($this->hierarchy)->addChild($this->status); |
| 726 | $index = null; //Needs to be passed by ref |
| 727 | $e = $this->hierarchy[count($this->hierarchy) - 1]->addChild($this->status, $index); |
| 728 | if ($this->status['text'] !== '') { |
| 729 | $index = null; //Needs to be passed by ref |
| 730 | $e->addText($this->status['text'], $index); |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
| 735 | function parse_style() { |
| 736 | if (!parent::parse_style()) {return false;} |
| 737 | |
| 738 | //$e = end($this->hierarchy)->addChild($this->status); |
| 739 | $index = null; //Needs to be passed by ref |
| 740 | $e = $this->hierarchy[count($this->hierarchy) - 1]->addChild($this->status, $index); |
| 741 | if ($this->status['text'] !== '') { |
| 742 | $index = null; //Needs to be passed by ref |
| 743 | $e->addText($this->status['text'], $index); |
| 744 | } |
| 745 | return true; |
| 746 | } |
| 747 | |
| 748 | function parse_tag_default() { |
| 749 | if (!parent::parse_tag_default()) {return false;} |
| 750 | |
| 751 | $this->parse_hierarchy(($this->status['self_close']) ? true : null); |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | function parse_text() { |
| 756 | parent::parse_text(); |
| 757 | if ($this->status['text'] !== '') { |
| 758 | //end($this->hierarchy)->addText($this->status['text']); |
| 759 | $index = null; //Needs to be passed by ref |
| 760 | $this->hierarchy[count($this->hierarchy) - 1]->addText($this->status['text'], $index); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | function parse_all() { |
| 765 | $this->hierarchy = array(&$this->root); |
| 766 | return ((parent::parse_all()) ? $this->root : false); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * HTML5 specific parser (adds support for omittable closing tags) |
| 772 | */ |
| 773 | class Html5Parser extends HtmlParser { |
| 774 | |
| 775 | /** |
| 776 | * Tags with ommitable closing tags |
| 777 | * @var array array('tag2' => 'tag1') will close tag1 if following (not child) tag is tag2 |
| 778 | * @access private |
| 779 | */ |
| 780 | var $tags_optional_close = array( |
| 781 | //Current tag => Previous tag |
| 782 | 'li' => array('li' => true), |
| 783 | 'dt' => array('dt' => true, 'dd' => true), |
| 784 | 'dd' => array('dt' => true, 'dd' => true), |
| 785 | 'address' => array('p' => true), |
| 786 | 'article' => array('p' => true), |
| 787 | 'aside' => array('p' => true), |
| 788 | 'blockquote' => array('p' => true), |
| 789 | 'dir' => array('p' => true), |
| 790 | 'div' => array('p' => true), |
| 791 | 'dl' => array('p' => true), |
| 792 | 'fieldset' => array('p' => true), |
| 793 | 'footer' => array('p' => true), |
| 794 | 'form' => array('p' => true), |
| 795 | 'h1' => array('p' => true), |
| 796 | 'h2' => array('p' => true), |
| 797 | 'h3' => array('p' => true), |
| 798 | 'h4' => array('p' => true), |
| 799 | 'h5' => array('p' => true), |
| 800 | 'h6' => array('p' => true), |
| 801 | 'header' => array('p' => true), |
| 802 | 'hgroup' => array('p' => true), |
| 803 | 'hr' => array('p' => true), |
| 804 | 'menu' => array('p' => true), |
| 805 | 'nav' => array('p' => true), |
| 806 | 'ol' => array('p' => true), |
| 807 | 'p' => array('p' => true), |
| 808 | 'pre' => array('p' => true), |
| 809 | 'section' => array('p' => true), |
| 810 | 'table' => array('p' => true), |
| 811 | 'ul' => array('p' => true), |
| 812 | 'rt' => array('rt' => true, 'rp' => true), |
| 813 | 'rp' => array('rt' => true, 'rp' => true), |
| 814 | 'optgroup' => array('optgroup' => true, 'option' => true), |
| 815 | 'option' => array('option'), |
| 816 | 'tbody' => array('thread' => true, 'tbody' => true, 'tfoot' => true), |
| 817 | 'tfoot' => array('thread' => true, 'tbody' => true), |
| 818 | 'tr' => array('tr' => true), |
| 819 | 'td' => array('td' => true, 'th' => true), |
| 820 | 'th' => array('td' => true, 'th' => true), |
| 821 | 'body' => array('head' => true) |
| 822 | ); |
| 823 | |
| 824 | protected function parse_hierarchy($self_close = null) { |
| 825 | $tag_curr = strtolower($this->status['tag_name']); |
| 826 | if ($self_close === null) { |
| 827 | $this->status['self_close'] = ($self_close = isset($this->tags_selfclose[$tag_curr])); |
| 828 | } |
| 829 | |
| 830 | if (! ($self_close || $this->status['closing_tag'])) { |
| 831 | //$tag_prev = strtolower(end($this->hierarchy)->tag); |
| 832 | $tag_prev = strtolower($this->hierarchy[count($this->hierarchy) - 1]->tag); |
| 833 | if (isset($this->tags_optional_close[$tag_curr]) && isset($this->tags_optional_close[$tag_curr][$tag_prev])) { |
| 834 | array_pop($this->hierarchy); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return parent::parse_hierarchy($self_close); |
| 839 | } |
| 840 | } |
| 841 |