| Total Complexity | 245 |
| Total Lines | 948 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 2 | Features | 0 |
Complex classes like simple_html_dom_node often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use simple_html_dom_node, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 112 | class simple_html_dom_node |
||
| 113 | { |
||
| 114 | public $nodetype = HDOM_TYPE_TEXT; |
||
| 115 | public $tag = 'text'; |
||
| 116 | public $attr = array(); |
||
| 117 | public $children = array(); |
||
| 118 | public $nodes = array(); |
||
| 119 | public $parent = null; |
||
| 120 | // The "info" array - see HDOM_INFO_... for what each element contains. |
||
| 121 | public $_ = array(); |
||
| 122 | public $tag_start = 0; |
||
| 123 | private $dom = null; |
||
| 124 | |||
| 125 | public function __construct($dom) |
||
| 126 | { |
||
| 127 | $this->dom = $dom; |
||
| 128 | $dom->nodes[] = $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function __destruct() |
||
| 132 | { |
||
| 133 | $this->clear(); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function __toString() |
||
| 137 | { |
||
| 138 | return $this->outertext(); |
||
| 139 | } |
||
| 140 | |||
| 141 | // clean up memory due to php5 circular references memory leak... |
||
| 142 | public function clear() |
||
| 143 | { |
||
| 144 | $this->dom = null; |
||
| 145 | $this->nodes = null; |
||
| 146 | $this->parent = null; |
||
| 147 | $this->children = null; |
||
| 148 | } |
||
| 149 | |||
| 150 | // dump node's tree |
||
| 151 | public function dump($show_attr = true, $deep = 0) |
||
| 152 | { |
||
| 153 | $lead = str_repeat(' ', $deep); |
||
| 154 | |||
| 155 | echo $lead . $this->tag; |
||
| 156 | if ($show_attr && count($this->attr) > 0) { |
||
| 157 | echo '('; |
||
| 158 | foreach ($this->attr as $k => $v) { |
||
| 159 | echo "[$k]=>\"" . $this->$k . '", '; |
||
| 160 | } |
||
| 161 | echo ')'; |
||
| 162 | } |
||
| 163 | echo "\n"; |
||
| 164 | |||
| 165 | if ($this->nodes) { |
||
| 166 | foreach ($this->nodes as $c) { |
||
| 167 | $c->dump($show_attr, $deep + 1); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // Debugging function to dump a single dom node with a bunch of information about it. |
||
| 173 | public function dump_node($node, $echo = true) |
||
| 174 | { |
||
| 175 | $string = $this->tag; |
||
| 176 | if (count($this->attr) > 0) { |
||
| 177 | $string .= '('; |
||
| 178 | foreach ($this->attr as $k => $v) { |
||
| 179 | $string .= "[$k]=>\"" . $this->$k . '", '; |
||
| 180 | } |
||
| 181 | $string .= ')'; |
||
| 182 | } |
||
| 183 | if (count($this->_) > 0) { |
||
| 184 | $string .= ' $_ ('; |
||
| 185 | foreach ($this->_ as $k => $v) { |
||
| 186 | if (is_array($v)) { |
||
| 187 | $string .= "[$k]=>("; |
||
| 188 | foreach ($v as $k2 => $v2) { |
||
| 189 | $string .= "[$k2]=>\"" . $v2 . '", '; |
||
| 190 | } |
||
| 191 | $string .= ")"; |
||
| 192 | } else { |
||
| 193 | $string .= "[$k]=>\"" . $v . '", '; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | $string .= ")"; |
||
| 197 | } |
||
| 198 | |||
| 199 | if (isset($this->text)) { |
||
| 200 | $string .= " text: (" . $this->text . ")"; |
||
| 201 | } |
||
| 202 | |||
| 203 | $string .= " HDOM_INNER_INFO: '"; |
||
| 204 | if (isset($node->_[HDOM_INFO_INNER])) { |
||
| 205 | $string .= $node->_[HDOM_INFO_INNER] . "'"; |
||
| 206 | } else { |
||
| 207 | $string .= ' NULL '; |
||
| 208 | } |
||
| 209 | |||
| 210 | $string .= " children: " . count($this->children); |
||
| 211 | $string .= " nodes: " . count($this->nodes); |
||
| 212 | $string .= " tag_start: " . $this->tag_start; |
||
| 213 | $string .= "\n"; |
||
| 214 | |||
| 215 | if ($echo) { |
||
| 216 | echo $string; |
||
| 217 | return; |
||
| 218 | } else { |
||
| 219 | return $string; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | // returns the parent of node |
||
| 224 | // If a node is passed in, it will reset the parent of the current node to that one. |
||
| 225 | public function parent($parent = null) |
||
| 226 | { |
||
| 227 | // I am SURE that this doesn't work properly. |
||
| 228 | // It fails to unset the current node from it's current parents nodes or children list first. |
||
| 229 | if ($parent !== null) { |
||
| 230 | $this->parent = $parent; |
||
| 231 | $this->parent->nodes[] = $this; |
||
| 232 | $this->parent->children[] = $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | return $this->parent; |
||
| 236 | } |
||
| 237 | |||
| 238 | // verify that node has children |
||
| 239 | public function has_child() |
||
| 240 | { |
||
| 241 | return !empty($this->children); |
||
| 242 | } |
||
| 243 | |||
| 244 | // returns children of node |
||
| 245 | public function children($idx = -1) |
||
| 246 | { |
||
| 247 | if ($idx === -1) { |
||
| 248 | return $this->children; |
||
| 249 | } |
||
| 250 | if (isset($this->children[$idx])) { |
||
| 251 | return $this->children[$idx]; |
||
| 252 | } |
||
| 253 | return null; |
||
| 254 | } |
||
| 255 | |||
| 256 | // returns the first child of node |
||
| 257 | public function first_child() |
||
| 258 | { |
||
| 259 | if (count($this->children) > 0) { |
||
| 260 | return $this->children[0]; |
||
| 261 | } |
||
| 262 | return null; |
||
| 263 | } |
||
| 264 | |||
| 265 | // returns the last child of node |
||
| 266 | public function last_child() |
||
| 267 | { |
||
| 268 | if (($count=count($this->children))>0) { |
||
| 269 | return $this->children[$count-1]; |
||
| 270 | } |
||
| 271 | return null; |
||
| 272 | } |
||
| 273 | |||
| 274 | // returns the next sibling of node |
||
| 275 | public function next_sibling() |
||
| 276 | { |
||
| 277 | if ($this->parent===null) { |
||
| 278 | return null; |
||
| 279 | } |
||
| 280 | |||
| 281 | $idx = 0; |
||
| 282 | $count = count($this->parent->children); |
||
| 283 | while ($idx<$count && $this!==$this->parent->children[$idx]) { |
||
| 284 | ++$idx; |
||
| 285 | } |
||
| 286 | if (++$idx>=$count) { |
||
| 287 | return null; |
||
| 288 | } |
||
| 289 | return $this->parent->children[$idx]; |
||
| 290 | } |
||
| 291 | |||
| 292 | // returns the previous sibling of node |
||
| 293 | public function prev_sibling() |
||
| 294 | { |
||
| 295 | if ($this->parent===null) { |
||
| 296 | return null; |
||
| 297 | } |
||
| 298 | $idx = 0; |
||
| 299 | $count = count($this->parent->children); |
||
| 300 | while ($idx<$count && $this!==$this->parent->children[$idx]) { |
||
| 301 | ++$idx; |
||
| 302 | } |
||
| 303 | if (--$idx<0) { |
||
| 304 | return null; |
||
| 305 | } |
||
| 306 | return $this->parent->children[$idx]; |
||
| 307 | } |
||
| 308 | |||
| 309 | // function to locate a specific ancestor tag in the path to the root. |
||
| 310 | public function find_ancestor_tag($tag) |
||
| 311 | { |
||
| 312 | global $debug_object; |
||
| 313 | if (is_object($debug_object)) { |
||
| 314 | $debug_object->debug_log_entry(1); |
||
| 315 | } |
||
| 316 | |||
| 317 | // Start by including ourselves in the comparison. |
||
| 318 | $returnDom = $this; |
||
| 319 | |||
| 320 | while (!is_null($returnDom)) { |
||
| 321 | if (is_object($debug_object)) { |
||
| 322 | $debug_object->debug_log(2, "Current tag is: " . $returnDom->tag); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($returnDom->tag == $tag) { |
||
| 326 | break; |
||
| 327 | } |
||
| 328 | $returnDom = $returnDom->parent; |
||
| 329 | } |
||
| 330 | return $returnDom; |
||
| 331 | } |
||
| 332 | |||
| 333 | // get dom node's inner html |
||
| 334 | public function innertext() |
||
| 335 | { |
||
| 336 | if (isset($this->_[HDOM_INFO_INNER])) { |
||
| 337 | return $this->_[HDOM_INFO_INNER]; |
||
| 338 | } |
||
| 339 | if (isset($this->_[HDOM_INFO_TEXT])) { |
||
| 340 | return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
||
| 341 | } |
||
| 342 | |||
| 343 | $ret = ''; |
||
| 344 | foreach ($this->nodes as $n) { |
||
| 345 | $ret .= $n->outertext(); |
||
| 346 | } |
||
| 347 | return $ret; |
||
| 348 | } |
||
| 349 | |||
| 350 | // get dom node's outer text (with tag) |
||
| 351 | public function outertext() |
||
| 352 | { |
||
| 353 | global $debug_object; |
||
| 354 | if (is_object($debug_object)) { |
||
| 355 | $text = ''; |
||
| 356 | if ($this->tag == 'text') { |
||
| 357 | if (!empty($this->text)) { |
||
| 358 | $text = " with text: " . $this->text; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | $debug_object->debug_log(1, 'Innertext of tag: ' . $this->tag . $text); |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($this->tag==='root') { |
||
| 365 | return $this->innertext(); |
||
| 366 | } |
||
| 367 | // trigger callback |
||
| 368 | if ($this->dom && $this->dom->callback!==null) { |
||
| 369 | call_user_func_array($this->dom->callback, array($this)); |
||
| 370 | } |
||
| 371 | |||
| 372 | if (isset($this->_[HDOM_INFO_OUTER])) { |
||
| 373 | return $this->_[HDOM_INFO_OUTER]; |
||
| 374 | } |
||
| 375 | if (isset($this->_[HDOM_INFO_TEXT])) { |
||
| 376 | return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
||
| 377 | } |
||
| 378 | |||
| 379 | // render begin tag |
||
| 380 | if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]) { |
||
| 381 | $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup(); |
||
| 382 | } else { |
||
| 383 | $ret = ""; |
||
| 384 | } |
||
| 385 | |||
| 386 | // render inner text |
||
| 387 | if (isset($this->_[HDOM_INFO_INNER])) { |
||
| 388 | // If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added. |
||
| 389 | if ($this->tag != "br") { |
||
| 390 | $ret .= $this->_[HDOM_INFO_INNER]; |
||
| 391 | } |
||
| 392 | } else { |
||
| 393 | if ($this->nodes) { |
||
| 394 | foreach ($this->nodes as $n) { |
||
| 395 | $ret .= $this->convert_text($n->outertext()); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | // render end tag |
||
| 401 | if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) { |
||
| 402 | $ret .= '</'.$this->tag.'>'; |
||
| 403 | } |
||
| 404 | return $ret; |
||
| 405 | } |
||
| 406 | |||
| 407 | // get dom node's plain text |
||
| 408 | public function text() |
||
| 409 | { |
||
| 410 | if (isset($this->_[HDOM_INFO_INNER])) { |
||
| 411 | return $this->_[HDOM_INFO_INNER]; |
||
| 412 | } |
||
| 413 | switch ($this->nodetype) { |
||
| 414 | case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
||
| 415 | case HDOM_TYPE_COMMENT: return ''; |
||
| 416 | case HDOM_TYPE_UNKNOWN: return ''; |
||
| 417 | } |
||
| 418 | if (strcasecmp($this->tag, 'script')===0) { |
||
| 419 | return ''; |
||
| 420 | } |
||
| 421 | if (strcasecmp($this->tag, 'style')===0) { |
||
| 422 | return ''; |
||
| 423 | } |
||
| 424 | |||
| 425 | $ret = ''; |
||
| 426 | // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL. |
||
| 427 | // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening. |
||
| 428 | // WHY is this happening? |
||
| 429 | if (!is_null($this->nodes)) { |
||
| 430 | foreach ($this->nodes as $n) { |
||
| 431 | $ret .= $this->convert_text($n->text()); |
||
| 432 | } |
||
| 433 | |||
| 434 | // If this node is a span... add a space at the end of it so multiple spans don't run into each other. This is plaintext after all. |
||
| 435 | if ($this->tag == "span") { |
||
| 436 | $ret .= $this->dom->default_span_text; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | return $ret; |
||
| 440 | } |
||
| 441 | |||
| 442 | public function xmltext() |
||
| 443 | { |
||
| 444 | $ret0 = $this->innertext(); |
||
| 445 | $ret1 = str_ireplace('<![CDATA[', '', $ret0); |
||
| 446 | $ret2 = str_replace(']]>', '', $ret1); |
||
| 447 | return $ret2; |
||
| 448 | } |
||
| 449 | |||
| 450 | // build node's text with tag |
||
| 451 | public function makeup() |
||
| 452 | { |
||
| 453 | // text, comment, unknown |
||
| 454 | if (isset($this->_[HDOM_INFO_TEXT])) { |
||
| 455 | return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); |
||
| 456 | } |
||
| 457 | $ret = '<'.$this->tag; |
||
| 458 | $i = -1; |
||
| 459 | |||
| 460 | foreach ($this->attr as $key=>$val) { |
||
| 461 | ++$i; |
||
| 462 | |||
| 463 | // skip removed attribute |
||
| 464 | if ($val===null || $val===false) { |
||
| 465 | continue; |
||
| 466 | } |
||
| 467 | $ret .= $this->_[HDOM_INFO_SPACE][$i][0]; |
||
| 468 | //no value attr: nowrap, checked selected... |
||
| 469 | if ($val===true) { |
||
| 470 | $ret .= $key; |
||
| 471 | } else { |
||
| 472 | switch ($this->_[HDOM_INFO_QUOTE][$i]) { |
||
| 473 | case HDOM_QUOTE_DOUBLE: $quote = '"'; break; |
||
| 474 | case HDOM_QUOTE_SINGLE: $quote = '\''; break; |
||
| 475 | default: $quote = ''; |
||
| 476 | } |
||
| 477 | $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | $ret = $this->dom->restore_noise($ret); |
||
| 481 | return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>'; |
||
| 482 | } |
||
| 483 | |||
| 484 | // find elements by css selector |
||
| 485 | //PaperG - added ability for find to lowercase the value of the selector. |
||
| 486 | public function find($selector, $idx=null, $lowercase=false) |
||
| 487 | { |
||
| 488 | $selectors = $this->parse_selector($selector); |
||
| 489 | if (($count=count($selectors))===0) { |
||
| 490 | return array(); |
||
| 491 | } |
||
| 492 | $found_keys = array(); |
||
| 493 | |||
| 494 | // find each selector |
||
| 495 | for ($c=0; $c<$count; ++$c) { |
||
| 496 | // The change on the below line was documented on the sourceforge code tracker id 2788009 |
||
| 497 | // used to be: if (($levle=count($selectors[0]))===0) return array(); |
||
| 498 | if (($levle=count($selectors[$c]))===0) { |
||
| 499 | return array(); |
||
| 500 | } |
||
| 501 | if (!isset($this->_[HDOM_INFO_BEGIN])) { |
||
| 502 | return array(); |
||
| 503 | } |
||
| 504 | |||
| 505 | $head = array($this->_[HDOM_INFO_BEGIN]=>1); |
||
| 506 | |||
| 507 | // handle descendant selectors, no recursive! |
||
| 508 | for ($l=0; $l<$levle; ++$l) { |
||
| 509 | $ret = array(); |
||
| 510 | foreach ($head as $k=>$v) { |
||
| 511 | $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k]; |
||
| 512 | //PaperG - Pass this optional parameter on to the seek function. |
||
| 513 | $n->seek($selectors[$c][$l], $ret, $lowercase); |
||
| 514 | } |
||
| 515 | $head = $ret; |
||
| 516 | } |
||
| 517 | |||
| 518 | foreach ($head as $k=>$v) { |
||
| 519 | if (!isset($found_keys[$k])) { |
||
| 520 | $found_keys[$k] = 1; |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | // sort keys |
||
| 526 | ksort($found_keys); |
||
| 527 | |||
| 528 | $found = array(); |
||
| 529 | foreach ($found_keys as $k=>$v) { |
||
| 530 | $found[] = $this->dom->nodes[$k]; |
||
| 531 | } |
||
| 532 | // return nth-element or array |
||
| 533 | if (is_null($idx)) { |
||
| 534 | return $found; |
||
| 535 | } elseif ($idx<0) { |
||
| 536 | $idx = count($found) + $idx; |
||
| 537 | } |
||
| 538 | return (isset($found[$idx])) ? $found[$idx] : null; |
||
| 539 | } |
||
| 540 | |||
| 541 | // seek for given conditions |
||
| 542 | // PaperG - added parameter to allow for case insensitive testing of the value of a selector. |
||
| 543 | protected function seek($selector, &$ret, $lowercase=false) |
||
| 544 | { |
||
| 545 | global $debug_object; |
||
| 546 | if (is_object($debug_object)) { |
||
| 547 | $debug_object->debug_log_entry(1); |
||
| 548 | } |
||
| 549 | |||
| 550 | list($tag, $key, $val, $exp, $no_key) = $selector; |
||
| 551 | |||
| 552 | // xpath index |
||
| 553 | if ($tag && $key && is_numeric($key)) { |
||
| 554 | $count = 0; |
||
| 555 | foreach ($this->children as $c) { |
||
| 556 | if ($tag==='*' || $tag===$c->tag) { |
||
| 557 | if (++$count==$key) { |
||
| 558 | $ret[$c->_[HDOM_INFO_BEGIN]] = 1; |
||
| 559 | return; |
||
| 560 | } |
||
| 561 | } |
||
| 562 | } |
||
| 563 | return; |
||
| 564 | } |
||
| 565 | |||
| 566 | $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0; |
||
| 567 | if ($end==0) { |
||
| 568 | $parent = $this->parent; |
||
| 569 | while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) { |
||
| 570 | $end -= 1; |
||
| 571 | $parent = $parent->parent; |
||
| 572 | } |
||
| 573 | $end += $parent->_[HDOM_INFO_END]; |
||
| 574 | } |
||
| 575 | |||
| 576 | for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) { |
||
| 577 | $node = $this->dom->nodes[$i]; |
||
| 578 | |||
| 579 | $pass = true; |
||
| 580 | |||
| 581 | if ($tag==='*' && !$key) { |
||
| 582 | if (in_array($node, $this->children, true)) { |
||
| 583 | $ret[$i] = 1; |
||
| 584 | } |
||
| 585 | continue; |
||
| 586 | } |
||
| 587 | |||
| 588 | // compare tag |
||
| 589 | if ($tag && $tag!=$node->tag && $tag!=='*') { |
||
| 590 | $pass=false; |
||
| 591 | } |
||
| 592 | // compare key |
||
| 593 | if ($pass && $key) { |
||
| 594 | if ($no_key) { |
||
| 595 | if (isset($node->attr[$key])) { |
||
| 596 | $pass=false; |
||
| 597 | } |
||
| 598 | } else { |
||
| 599 | if (($key != "plaintext") && !isset($node->attr[$key])) { |
||
| 600 | $pass=false; |
||
| 601 | } |
||
| 602 | } |
||
| 603 | } |
||
| 604 | // compare value |
||
| 605 | if ($pass && $key && $val && $val!=='*') { |
||
| 606 | // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right? |
||
| 607 | if ($key == "plaintext") { |
||
| 608 | // $node->plaintext actually returns $node->text(); |
||
| 609 | $nodeKeyValue = $node->text(); |
||
| 610 | } else { |
||
| 611 | // this is a normal search, we want the value of that attribute of the tag. |
||
| 612 | $nodeKeyValue = $node->attr[$key]; |
||
| 613 | } |
||
| 614 | if (is_object($debug_object)) { |
||
| 615 | $debug_object->debug_log(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue); |
||
| 616 | } |
||
| 617 | |||
| 618 | //PaperG - If lowercase is set, do a case insensitive test of the value of the selector. |
||
| 619 | if ($lowercase) { |
||
| 620 | $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue)); |
||
| 621 | } else { |
||
| 622 | $check = $this->match($exp, $val, $nodeKeyValue); |
||
| 623 | } |
||
| 624 | if (is_object($debug_object)) { |
||
| 625 | $debug_object->debug_log(2, "after match: " . ($check ? "true" : "false")); |
||
| 626 | } |
||
| 627 | |||
| 628 | // handle multiple class |
||
| 629 | if (!$check && strcasecmp($key, 'class')===0) { |
||
| 630 | foreach (explode(' ', $node->attr[$key]) as $k) { |
||
| 631 | // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form. |
||
| 632 | if (!empty($k)) { |
||
| 633 | if ($lowercase) { |
||
| 634 | $check = $this->match($exp, strtolower($val), strtolower($k)); |
||
| 635 | } else { |
||
| 636 | $check = $this->match($exp, $val, $k); |
||
| 637 | } |
||
| 638 | if ($check) { |
||
| 639 | break; |
||
| 640 | } |
||
| 641 | } |
||
| 642 | } |
||
| 643 | } |
||
| 644 | if (!$check) { |
||
| 645 | $pass = false; |
||
| 646 | } |
||
| 647 | } |
||
| 648 | if ($pass) { |
||
| 649 | $ret[$i] = 1; |
||
| 650 | } |
||
| 651 | unset($node); |
||
| 652 | } |
||
| 653 | // It's passed by reference so this is actually what this function returns. |
||
| 654 | if (is_object($debug_object)) { |
||
| 655 | $debug_object->debug_log(1, "EXIT - ret: ", $ret); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | protected function match($exp, $pattern, $value) |
||
| 660 | { |
||
| 661 | global $debug_object; |
||
| 662 | if (is_object($debug_object)) { |
||
| 663 | $debug_object->debug_log_entry(1); |
||
| 664 | } |
||
| 665 | |||
| 666 | switch ($exp) { |
||
| 667 | case '=': |
||
| 668 | return ($value===$pattern); |
||
| 669 | case '!=': |
||
| 670 | return ($value!==$pattern); |
||
| 671 | case '^=': |
||
| 672 | return preg_match("/^".preg_quote($pattern, '/')."/", $value); |
||
| 673 | case '$=': |
||
| 674 | return preg_match("/".preg_quote($pattern, '/')."$/", $value); |
||
| 675 | case '*=': |
||
| 676 | if ($pattern[0]=='/') { |
||
| 677 | return preg_match($pattern, $value); |
||
| 678 | } |
||
| 679 | return preg_match("/".$pattern."/i", $value); |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | protected function parse_selector($selector_string) |
||
| 684 | { |
||
| 685 | global $debug_object; |
||
| 686 | if (is_object($debug_object)) { |
||
| 687 | $debug_object->debug_log_entry(1); |
||
| 688 | } |
||
| 689 | |||
| 690 | // pattern of CSS selectors, modified from mootools |
||
| 691 | // Paperg: Add the colon to the attrbute, so that it properly finds <tag attr:ibute="something" > like google does. |
||
| 692 | // Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check. |
||
| 693 | // Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured. |
||
| 694 | // This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression. |
||
| 695 | // farther study is required to determine of this should be documented or removed. |
||
| 696 | // $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; |
||
| 697 | $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; |
||
| 698 | preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER); |
||
| 699 | if (is_object($debug_object)) { |
||
| 700 | $debug_object->debug_log(2, "Matches Array: ", $matches); |
||
| 701 | } |
||
| 702 | |||
| 703 | $selectors = array(); |
||
| 704 | $result = array(); |
||
| 705 | //print_r($matches); |
||
| 706 | |||
| 707 | foreach ($matches as $m) { |
||
| 708 | $m[0] = trim($m[0]); |
||
| 709 | if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') { |
||
| 710 | continue; |
||
| 711 | } |
||
| 712 | // for browser generated xpath |
||
| 713 | if ($m[1]==='tbody') { |
||
| 714 | continue; |
||
| 715 | } |
||
| 716 | list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false); |
||
| 717 | if (!empty($m[2])) { |
||
| 718 | $key='id'; |
||
| 719 | $val=$m[2]; |
||
| 720 | } |
||
| 721 | if (!empty($m[3])) { |
||
| 722 | $key='class'; |
||
| 723 | $val=$m[3]; |
||
| 724 | } |
||
| 725 | if (!empty($m[4])) { |
||
| 726 | $key=$m[4]; |
||
| 727 | } |
||
| 728 | if (!empty($m[5])) { |
||
| 729 | $exp=$m[5]; |
||
| 730 | } |
||
| 731 | if (!empty($m[6])) { |
||
| 732 | $val=$m[6]; |
||
| 733 | } |
||
| 734 | |||
| 735 | // convert to lowercase |
||
| 736 | if ($this->dom->lowercase) { |
||
| 737 | $tag=strtolower($tag); |
||
| 738 | $key=strtolower($key); |
||
| 739 | } |
||
| 740 | //elements that do NOT have the specified attribute |
||
| 741 | if (isset($key[0]) && $key[0]==='!') { |
||
| 742 | $key=substr($key, 1); |
||
| 743 | $no_key=true; |
||
| 744 | } |
||
| 745 | |||
| 746 | $result[] = array($tag, $key, $val, $exp, $no_key); |
||
| 747 | if (trim($m[7])===',') { |
||
| 748 | $selectors[] = $result; |
||
| 749 | $result = array(); |
||
| 750 | } |
||
| 751 | } |
||
| 752 | if (count($result)>0) { |
||
| 753 | $selectors[] = $result; |
||
| 754 | } |
||
| 755 | return $selectors; |
||
| 756 | } |
||
| 757 | |||
| 758 | public function __get($name) |
||
| 759 | { |
||
| 760 | if (isset($this->attr[$name])) { |
||
| 761 | return $this->convert_text($this->attr[$name]); |
||
| 762 | } |
||
| 763 | switch ($name) { |
||
| 764 | case 'outertext': return $this->outertext(); |
||
| 765 | case 'innertext': return $this->innertext(); |
||
| 766 | case 'plaintext': return $this->text(); |
||
| 767 | case 'xmltext': return $this->xmltext(); |
||
| 768 | default: return array_key_exists($name, $this->attr); |
||
| 769 | } |
||
| 770 | } |
||
| 771 | |||
| 772 | public function __set($name, $value) |
||
| 773 | { |
||
| 774 | global $debug_object; |
||
| 775 | if (is_object($debug_object)) { |
||
| 776 | $debug_object->debug_log_entry(1); |
||
| 777 | } |
||
| 778 | |||
| 779 | switch ($name) { |
||
| 780 | case 'outertext': |
||
| 781 | return $this->_[HDOM_INFO_OUTER] = $value; |
||
| 782 | case 'innertext': |
||
| 783 | if (isset($this->_[HDOM_INFO_TEXT])) { |
||
| 784 | return $this->_[HDOM_INFO_TEXT] = $value; |
||
| 785 | } |
||
| 786 | return $this->_[HDOM_INFO_INNER] = $value; |
||
| 787 | } |
||
| 788 | if (!isset($this->attr[$name])) { |
||
| 789 | $this->_[HDOM_INFO_SPACE][] = array(' ', '', ''); |
||
| 790 | $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE; |
||
| 791 | } |
||
| 792 | $this->attr[$name] = $value; |
||
| 793 | } |
||
| 794 | |||
| 795 | public function __isset($name) |
||
| 796 | { |
||
| 797 | switch ($name) { |
||
| 798 | case 'outertext': return true; |
||
| 799 | case 'innertext': return true; |
||
| 800 | case 'plaintext': return true; |
||
| 801 | } |
||
| 802 | //no value attr: nowrap, checked selected... |
||
| 803 | return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]); |
||
| 804 | } |
||
| 805 | |||
| 806 | public function __unset($name) |
||
| 807 | { |
||
| 808 | if (isset($this->attr[$name])) { |
||
| 809 | unset($this->attr[$name]); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | // PaperG - Function to convert the text from one character set to another if the two sets are not the same. |
||
| 814 | public function convert_text($text) |
||
| 815 | { |
||
| 816 | global $debug_object; |
||
| 817 | if (is_object($debug_object)) { |
||
| 818 | $debug_object->debug_log_entry(1); |
||
| 819 | } |
||
| 820 | |||
| 821 | $converted_text = $text; |
||
| 822 | |||
| 823 | $sourceCharset = ""; |
||
| 824 | $targetCharset = ""; |
||
| 825 | |||
| 826 | if ($this->dom) { |
||
| 827 | $sourceCharset = strtoupper($this->dom->_charset); |
||
| 828 | $targetCharset = strtoupper($this->dom->_target_charset); |
||
| 829 | } |
||
| 830 | if (is_object($debug_object)) { |
||
| 831 | $debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset); |
||
| 832 | } |
||
| 833 | |||
| 834 | if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0)) { |
||
| 835 | // Check if the reported encoding could have been incorrect and the text is actually already UTF-8 |
||
| 836 | if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text))) { |
||
| 837 | $converted_text = $text; |
||
| 838 | } else { |
||
| 839 | $converted_text = iconv($sourceCharset, $targetCharset, $text); |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output. |
||
| 844 | if ($targetCharset == 'UTF-8') { |
||
| 845 | if (substr($converted_text, 0, 3) == "\xef\xbb\xbf") { |
||
| 846 | $converted_text = substr($converted_text, 3); |
||
| 847 | } |
||
| 848 | if (substr($converted_text, -3) == "\xef\xbb\xbf") { |
||
| 849 | $converted_text = substr($converted_text, 0, -3); |
||
| 850 | } |
||
| 851 | } |
||
| 852 | |||
| 853 | return $converted_text; |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Returns true if $string is valid UTF-8 and false otherwise. |
||
| 858 | * |
||
| 859 | * @param mixed $str String to be tested |
||
| 860 | * @return boolean |
||
| 861 | */ |
||
| 862 | public static function is_utf8($str) |
||
| 863 | { |
||
| 864 | $c=0; |
||
| 865 | $b=0; |
||
| 866 | $bits=0; |
||
| 867 | $len=strlen($str); |
||
| 868 | for ($i=0; $i<$len; $i++) { |
||
| 869 | $c=ord($str[$i]); |
||
| 870 | if ($c > 128) { |
||
| 871 | if (($c >= 254)) { |
||
| 872 | return false; |
||
| 873 | } elseif ($c >= 252) { |
||
| 874 | $bits=6; |
||
| 875 | } elseif ($c >= 248) { |
||
| 876 | $bits=5; |
||
| 877 | } elseif ($c >= 240) { |
||
| 878 | $bits=4; |
||
| 879 | } elseif ($c >= 224) { |
||
| 880 | $bits=3; |
||
| 881 | } elseif ($c >= 192) { |
||
| 882 | $bits=2; |
||
| 883 | } else { |
||
| 884 | return false; |
||
| 885 | } |
||
| 886 | if (($i+$bits) > $len) { |
||
| 887 | return false; |
||
| 888 | } |
||
| 889 | while ($bits > 1) { |
||
| 890 | $i++; |
||
| 891 | $b=ord($str[$i]); |
||
| 892 | if ($b < 128 || $b > 191) { |
||
| 893 | return false; |
||
| 894 | } |
||
| 895 | $bits--; |
||
| 896 | } |
||
| 897 | } |
||
| 898 | } |
||
| 899 | return true; |
||
| 900 | } |
||
| 901 | |||
| 902 | /* |
||
| 903 | function is_utf8($string) |
||
| 904 | { |
||
| 905 | //this is buggy |
||
| 906 | return (utf8_encode(utf8_decode($string)) == $string); |
||
| 907 | } |
||
| 908 | */ |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Function to try a few tricks to determine the displayed size of an img on the page. |
||
| 912 | * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. |
||
| 913 | * |
||
| 914 | * @author John Schlick |
||
| 915 | * @version April 19 2012 |
||
| 916 | * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. |
||
| 917 | */ |
||
| 918 | public function get_display_size() |
||
| 919 | { |
||
| 920 | global $debug_object; |
||
| 921 | |||
| 922 | $width = -1; |
||
| 923 | $height = -1; |
||
| 924 | |||
| 925 | if ($this->tag !== 'img') { |
||
| 926 | return false; |
||
| 927 | } |
||
| 928 | |||
| 929 | // See if there is aheight or width attribute in the tag itself. |
||
| 930 | if (isset($this->attr['width'])) { |
||
| 931 | $width = $this->attr['width']; |
||
| 932 | } |
||
| 933 | |||
| 934 | if (isset($this->attr['height'])) { |
||
| 935 | $height = $this->attr['height']; |
||
| 936 | } |
||
| 937 | |||
| 938 | // Now look for an inline style. |
||
| 939 | if (isset($this->attr['style'])) { |
||
| 940 | // Thanks to user gnarf from stackoverflow for this regular expression. |
||
| 941 | $attributes = array(); |
||
| 942 | preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER); |
||
| 943 | foreach ($matches as $match) { |
||
| 944 | $attributes[$match[1]] = $match[2]; |
||
| 945 | } |
||
| 946 | |||
| 947 | // If there is a width in the style attributes: |
||
| 948 | if (isset($attributes['width']) && $width == -1) { |
||
| 949 | // check that the last two characters are px (pixels) |
||
| 950 | if (strtolower(substr($attributes['width'], -2)) == 'px') { |
||
| 951 | $proposed_width = substr($attributes['width'], 0, -2); |
||
| 952 | // Now make sure that it's an integer and not something stupid. |
||
| 953 | if (filter_var($proposed_width, FILTER_VALIDATE_INT)) { |
||
| 954 | $width = $proposed_width; |
||
| 955 | } |
||
| 956 | } |
||
| 957 | } |
||
| 958 | |||
| 959 | // If there is a width in the style attributes: |
||
| 960 | if (isset($attributes['height']) && $height == -1) { |
||
| 961 | // check that the last two characters are px (pixels) |
||
| 962 | if (strtolower(substr($attributes['height'], -2)) == 'px') { |
||
| 963 | $proposed_height = substr($attributes['height'], 0, -2); |
||
| 964 | // Now make sure that it's an integer and not something stupid. |
||
| 965 | if (filter_var($proposed_height, FILTER_VALIDATE_INT)) { |
||
| 966 | $height = $proposed_height; |
||
| 967 | } |
||
| 968 | } |
||
| 969 | } |
||
| 970 | } |
||
| 971 | |||
| 972 | // Future enhancement: |
||
| 973 | // Look in the tag to see if there is a class or id specified that has a height or width attribute to it. |
||
| 974 | |||
| 975 | // Far future enhancement |
||
| 976 | // Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width |
||
| 977 | // Note that in this case, the class or id will have the img subselector for it to apply to the image. |
||
| 978 | |||
| 979 | // ridiculously far future development |
||
| 980 | // If the class or id is specified in a SEPARATE css file thats not on the page, go get it and do what we were just doing for the ones on the page. |
||
| 981 | |||
| 982 | $result = array('height' => $height, |
||
| 983 | 'width' => $width); |
||
| 984 | return $result; |
||
| 985 | } |
||
| 986 | |||
| 987 | // camel naming conventions |
||
| 988 | public function getAllAttributes() |
||
| 989 | { |
||
| 990 | return $this->attr; |
||
| 991 | } |
||
| 992 | public function getAttribute($name) |
||
| 993 | { |
||
| 994 | return $this->__get($name); |
||
| 995 | } |
||
| 996 | public function setAttribute($name, $value) |
||
| 997 | { |
||
| 998 | $this->__set($name, $value); |
||
| 999 | } |
||
| 1000 | public function hasAttribute($name) |
||
| 1001 | { |
||
| 1002 | return $this->__isset($name); |
||
| 1003 | } |
||
| 1004 | public function removeAttribute($name) |
||
| 1005 | { |
||
| 1006 | $this->__set($name, null); |
||
| 1007 | } |
||
| 1008 | public function getElementById($id) |
||
| 1009 | { |
||
| 1010 | return $this->find("#$id", 0); |
||
| 1011 | } |
||
| 1012 | public function getElementsById($id, $idx=null) |
||
| 1013 | { |
||
| 1014 | return $this->find("#$id", $idx); |
||
| 1015 | } |
||
| 1016 | public function getElementByTagName($name) |
||
| 1017 | { |
||
| 1018 | return $this->find($name, 0); |
||
| 1019 | } |
||
| 1020 | public function getElementsByTagName($name, $idx=null) |
||
| 1021 | { |
||
| 1022 | return $this->find($name, $idx); |
||
| 1023 | } |
||
| 1024 | public function parentNode() |
||
| 1025 | { |
||
| 1026 | return $this->parent(); |
||
| 1027 | } |
||
| 1028 | public function childNodes($idx=-1) |
||
| 1029 | { |
||
| 1030 | return $this->children($idx); |
||
| 1031 | } |
||
| 1032 | public function firstChild() |
||
| 1033 | { |
||
| 1034 | return $this->first_child(); |
||
| 1035 | } |
||
| 1036 | public function lastChild() |
||
| 1039 | } |
||
| 1040 | public function nextSibling() |
||
| 1041 | { |
||
| 1042 | return $this->next_sibling(); |
||
| 1043 | } |
||
| 1044 | public function previousSibling() |
||
| 1045 | { |
||
| 1046 | return $this->prev_sibling(); |
||
| 1047 | } |
||
| 1048 | public function hasChildNodes() |
||
| 1049 | { |
||
| 1050 | return $this->has_child(); |
||
| 1051 | } |
||
| 1052 | public function nodeName() |
||
| 1053 | { |
||
| 1054 | return $this->tag; |
||
| 1055 | } |
||
| 1056 | public function appendChild($node) |
||
| 1057 | { |
||
| 1058 | $node->parent($this); |
||
| 1059 | return $node; |
||
| 1060 | } |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * simple html dom parser |
||
| 1065 | * Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector. |
||
| 1066 | * Paperg - change $size from protected to public so we can easily access it |
||
| 1067 | * Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it. |
||
| 1068 | * |
||
| 1069 | * @package PlaceLocalInclude |
||
| 1070 | */ |
||
| 1071 | class simple_html_dom |
||
| 1072 | { |
||
| 1073 | public $root = null; |
||
| 1074 | public $nodes = array(); |
||
| 1075 | public $callback = null; |
||
| 1076 | public $lowercase = false; |
||
| 1077 | // Used to keep track of how large the text was when we started. |
||
| 1078 | public $original_size; |
||
| 1079 | public $size; |
||
| 1080 | protected $pos; |
||
| 1081 | protected $doc; |
||
| 1082 | protected $char; |
||
| 1083 | protected $cursor; |
||
| 1084 | protected $parent; |
||
| 1085 | protected $noise = array(); |
||
| 1086 | protected $token_blank = " \t\r\n"; |
||
| 1872 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.