diff options
author | Andres Rey <[email protected]> | 2016-10-30 11:16:34 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2016-10-30 11:16:34 +0000 |
commit | f46b43d0b5351af668c1c7229a0cddaef3ba5582 (patch) | |
tree | b90559bbe41d7f665247b5bc92476a76fcf68ea6 /src/Readability.php | |
parent | 6e404fd0bfd4f16c8bc908d9d257471873719f60 (diff) |
Lots of small bugfixes to match the real functionality of readability.js
Diffstat (limited to 'src/Readability.php')
-rw-r--r-- | src/Readability.php | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/src/Readability.php b/src/Readability.php index 8a40a7c..d3b7270 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -121,11 +121,19 @@ class Readability extends Element implements ReadabilityInterface /** * Returns all links from the current element. * - * @return Readability|null + * @return array|null */ public function getAllLinks() { - return ($this->isText()) ? null : $this->node->getElementsByTagName('a'); + if (($this->isText())) { + return null; + } else { + $links = []; + foreach ($this->node->getElementsByTagName('a') as $link) { + $links[] = new Readability($link); + }; + return $links; + } } /** @@ -232,7 +240,8 @@ class Readability extends Element implements ReadabilityInterface */ public function setContentScore($score) { - $this->contentScore = $score; + // To prevent the -0 value + $this->contentScore = ($score === (double)-0) ? 0 : $score; return $this->contentScore; } @@ -240,10 +249,36 @@ class Readability extends Element implements ReadabilityInterface /** * Returns the full text of the node. * + * @param bool $normalize Normalize white space? + * * @return string */ - public function getTextContent() + public function getTextContent($normalize = false) + { + $nodeValue = $this->node->nodeValue; + if ($normalize) { + $nodeValue = trim(preg_replace('/\s{2,}/', ' ', $nodeValue)); + } + return $nodeValue; + } + + /** + * Sets the node name + * + * @param string $value + */ + public function setNodeName($value) + { + $this->node->nodeName = $value; + } + + /** + * Returns the current DOMNode + * + * @return \DOMNode + */ + public function getDOMNode() { - return $this->getChildrenAsString(); + return $this->node; } } |