From ef8a6e05ea743b966f9b2ae33cbd419b8457ae1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 27 Mar 2024 07:41:49 +0100 Subject: [PATCH] fixup! IBX-7987: Added extension point to skip nodes while extracting text --- .../RichText/TextExtractor/NodeFilter/NodePathFilter.php | 8 ++------ .../TextExtractor/NodeFilter/NodePathFilterTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php b/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php index e68cddae..d7ac240d 100644 --- a/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php +++ b/src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php @@ -28,17 +28,13 @@ public function __construct(string ...$path) public function filter(DOMNode $node): bool { foreach ($this->path as $name) { - if ($node === null) { + if ($node === null || $node->nodeName !== $name) { return false; } - if ($node->nodeName !== $name) { - return true; - } - $node = $node->parentNode; } - return false; + return true; } } diff --git a/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php b/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php index f48aaf77..2a136ba6 100644 --- a/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php +++ b/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php @@ -23,13 +23,13 @@ public function testFilter(): void $document = new DOMDocument(); $document->loadXML(''); + $nodeA = $this->getNode($document, '//a'); $nodeB = $this->getNode($document, '//b'); $nodeC = $this->getNode($document, '//c'); - $filter = new NodePathFilter('b', 'c'); - - self::assertTrue($filter->filter($nodeB)); - self::assertFalse($filter->filter($nodeC)); + self::assertFalse((new NodePathFilter('b', 'c'))->filter($nodeB)); + self::assertTrue((new NodePathFilter('b', 'c'))->filter($nodeC)); + self::assertFalse((new NodePathFilter('a', 'b', 'c', 'd'))->filter($nodeA)); } private function getNode(DOMDocument $document, string $expression): DOMNode