From 1d86d0898c8d0d1c90735db541d3a0f17868737f Mon Sep 17 00:00:00 2001 From: Andres Rey Date: Wed, 7 Nov 2018 19:11:50 +0000 Subject: Add third parameter of "hasAncestorTag" as a callable optional function --- src/Nodes/NodeTrait.php | 8 +++++--- src/Readability.php | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Nodes/NodeTrait.php b/src/Nodes/NodeTrait.php index c08b59f..f641b2d 100644 --- a/src/Nodes/NodeTrait.php +++ b/src/Nodes/NodeTrait.php @@ -365,13 +365,13 @@ trait NodeTrait * Check if a given node has one of its ancestor tag name matching the * provided one. * - * @param DOMElement $node * @param string $tagName * @param int $maxDepth + * @param callable $filterFn * * @return bool */ - public function hasAncestorTag($tagName, $maxDepth = 3) + public function hasAncestorTag($tagName, $maxDepth = 3, callable $filterFn = null) { $depth = 0; $node = $this; @@ -380,9 +380,11 @@ trait NodeTrait if ($maxDepth > 0 && $depth > $maxDepth) { return false; } - if ($node->parentNode->nodeName === $tagName) { + + if ($node->parentNode->nodeName === $tagName && (!$filterFn || $filterFn($node->parentNode))) { return true; } + $node = $node->parentNode; $depth++; } diff --git a/src/Readability.php b/src/Readability.php index f4f38ae..eadd50d 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -1460,7 +1460,9 @@ class Readability $node = $DOMNodeList->item($length - 1 - $i); // First check if we're in a data table, in which case don't remove us. - if ($node->hasAncestorTag('table', -1) && $node->isReadabilityDataTable()) { + if ($node->hasAncestorTag('table', -1, function($node){ + return $node->isReadabilityDataTable(); + })) { continue; } -- cgit v1.2.3