diff options
-rw-r--r-- | src/Nodes/NodeTrait.php | 32 | ||||
-rw-r--r-- | src/Readability.php | 4 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/Nodes/NodeTrait.php b/src/Nodes/NodeTrait.php index 13611c9..9f24abc 100644 --- a/src/Nodes/NodeTrait.php +++ b/src/Nodes/NodeTrait.php @@ -51,6 +51,21 @@ trait NodeTrait ]; /** + * The commented out elements qualify as phrasing content but tend to be + * removed by readability when put into paragraphs, so we ignore them here. + * + * @var array + */ + private $phrasing_elems = [ + // 'CANVAS', 'IFRAME', 'SVG', 'VIDEO', + 'abbr', 'audio', 'b', 'bdo', 'br', 'button', 'cite', 'code', 'data', + 'datalist', 'dfn', 'em', 'embed', 'i', 'img', 'input', 'kbd', 'label', + 'mark', 'math', 'meter', 'noscript', 'object', 'output', 'progress', 'q', + 'ruby', 'samp', 'script', 'select', 'small', 'span', 'strong', 'sub', + 'sup', 'textarea', 'time', 'var', 'wbr' + ]; + + /** * initialized getter. * * @return bool @@ -431,4 +446,21 @@ trait NodeTrait ); } + + /** + * Determine if a node qualifies as phrasing content. + * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content + * + * @return bool + */ + public function isPhrasingContent() + { + return $this->nodeType === XML_TEXT_NODE || !in_array($this->tagName, $this->phrasing_elems) || + (!is_null($this->childNodes) && + ($this->tagName === 'a' || $this->tagName === 'del' || $this->tagName === 'ins') && + array_reduce(iterator_to_array($this->childNodes), function ($carry, $node) { + return $carry || $node->isPhrasingContent(); + }) + ); + } } diff --git a/src/Readability.php b/src/Readability.php index 3f4d807..e0c9abf 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -831,6 +831,10 @@ class Readability } } + if ($next->isPhrasingContent()) { + break; + } + $this->logger->debug('[PrepDocument] Replacing BR with a P node...'); // Otherwise, make this node a child of the new <p>. |