function __toString() { // Skip empty text frames // if ( $this->is_text_node() && // preg_replace("/\s/", "", $this->_node->data) === "" ) // return ""; $str = "<b>" . $this->_node->nodeName . ":</b><br/>"; //$str .= spl_object_hash($this->_node) . "<br/>"; $str .= "Id: " . $this->get_id() . "<br/>"; $str .= "Class: " . get_class($this) . "<br/>"; if ($this->is_text_node()) { $tmp = htmlspecialchars($this->_node->nodeValue); $str .= "<pre>'" . mb_substr($tmp, 0, 70) . (mb_strlen($tmp) > 70 ? "..." : "") . "'</pre>"; } elseif ($css_class = $this->_node->getAttribute("class")) { $str .= "CSS class: '{$css_class}'<br/>"; } if ($this->_parent) { $str .= "\nParent:" . $this->_parent->_node->nodeName . " (" . spl_object_hash($this->_parent->_node) . ") " . "<br/>"; } if ($this->_prev_sibling) { $str .= "Prev: " . $this->_prev_sibling->_node->nodeName . " (" . spl_object_hash($this->_prev_sibling->_node) . ") " . "<br/>"; } if ($this->_next_sibling) { $str .= "Next: " . $this->_next_sibling->_node->nodeName . " (" . spl_object_hash($this->_next_sibling->_node) . ") " . "<br/>"; } $d = $this->get_decorator(); while ($d && $d != $d->get_decorator()) { $str .= "Decorator: " . get_class($d) . "<br/>"; $d = $d->get_decorator(); } $str .= "Position: " . pre_r($this->_position, true); $str .= "\nContaining block: " . pre_r($this->_containing_block, true); $str .= "\nMargin width: " . pre_r($this->get_margin_width(), true); $str .= "\nMargin height: " . pre_r($this->get_margin_height(), true); $str .= "\nStyle: <pre>" . $this->_style->__toString() . "</pre>"; if ($this->_decorator instanceof Block_Frame_Decorator) { $str .= "Lines:<pre>"; foreach ($this->_decorator->get_line_boxes() as $line) { foreach ($line->get_frames() as $frame) { if ($frame instanceof Text_Frame_Decorator) { $str .= "\ntext: "; $str .= "'" . htmlspecialchars($frame->get_text()) . "'"; } else { $str .= "\nBlock: " . $frame->get_node()->nodeName . " (" . spl_object_hash($frame->get_node()) . ")"; } } $str .= "\ny => " . $line->y . "\n" . "w => " . $line->w . "\n" . "h => " . $line->h . "\n" . "left => " . $line->left . "\n" . "right => " . $line->right . "\n"; } $str .= "</pre>"; } $str .= "\n"; if (php_sapi_name() === "cli") { $str = strip_tags(str_replace(array("<br/>", "<b>", "</b>"), array("\n", "", ""), $str)); } return $str; }
/** * This method attempts to return a better 'innerText' representation than DOMNode::textContent * * @param DOMElement|DOMText $el * @param bool $implied when parsing for implied name for h-*, rules may be slightly different * @see: https://github.com/glennjones/microformat-shiv/blob/dev/lib/text.js */ public function innerText($el, $implied = false) { $out = ''; $blockLevelTags = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'hr', 'pre', 'table', 'address', 'article', 'aside', 'blockquote', 'caption', 'col', 'colgroup', 'dd', 'div', 'dt', 'dir', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'header', 'hgroup', 'hr', 'li', 'map', 'menu', 'nav', 'optgroup', 'option', 'section', 'tbody', 'testarea', 'tfoot', 'th', 'thead', 'tr', 'td', 'ul', 'ol', 'dl', 'details'); $excludeTags = array('noframe', 'noscript', 'script', 'style', 'frames', 'frameset'); // PHP DOMDocument doesn’t correctly handle whitespace around elements it doesn’t recognise. $unsupportedTags = array('data'); if (isset($el->tagName)) { if (in_array(strtolower($el->tagName), $excludeTags)) { return $out; } else { if ($el->tagName == 'img') { if ($el->getAttribute('alt') !== '') { return $el->getAttribute('alt'); } else { if (!$implied && $el->getAttribute('src') !== '') { return $this->resolveUrl($el->getAttribute('src')); } } } else { if ($el->tagName == 'area' and $el->getAttribute('alt') !== '') { return $el->getAttribute('alt'); } else { if ($el->tagName == 'abbr' and $el->getAttribute('title') !== '') { return $el->getAttribute('title'); } } } } } // if node is a text node get its text if (isset($el->nodeType) && $el->nodeType === 3) { $out .= $el->textContent; } // get the text of the child nodes if ($el->childNodes && $el->childNodes->length > 0) { for ($j = 0; $j < $el->childNodes->length; $j++) { $text = $this->innerText($el->childNodes->item($j), $implied); if (!is_null($text)) { $out .= $text; } } } if (isset($el->tagName)) { // if its a block level tag add an additional space at the end if (in_array(strtolower($el->tagName), $blockLevelTags)) { $out .= ' '; } elseif ($implied and in_array(strtolower($el->tagName), $unsupportedTags)) { $out .= ' '; } else { if (strtolower($el->tagName) == 'br') { // else if its a br, replace with newline $out .= "\n"; } } } return $out === '' ? NULL : $out; }