diff --git a/bin/feedio b/bin/feedio index 497def5b..0f9bce2c 100755 --- a/bin/feedio +++ b/bin/feedio @@ -40,7 +40,9 @@ function read(FeedIo $feedIo, string $url) $items = array_reverse(iterator_to_array($feed)); foreach( $items as $i => $item ) { - echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} - \033[34m{$item->getTitle()}\033[0m:" . PHP_EOL; + $title = trim(preg_replace('/\s+/', ' ', $item->getTitle())); + echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)}\033[0m - \033[34m{$title}\033[0m" . PHP_EOL; + echo "\033[36m{$item->getLink()}\033[0m" . PHP_EOL; echo "\t". strip_tags(nl2br($item->getContent())) . PHP_EOL; } diff --git a/src/FeedIo/Rule/Atom/Link.php b/src/FeedIo/Rule/Atom/Link.php index 60dd7c64..c7b97ca3 100644 --- a/src/FeedIo/Rule/Atom/Link.php +++ b/src/FeedIo/Rule/Atom/Link.php @@ -28,7 +28,7 @@ protected function selectAlternateLink(NodeInterface $node, \DOMElement $element ($element->hasAttribute('rel') && $element->getAttribute('rel') == 'alternate') || is_null($node->getLink()) ) { - $href = $element->getAttribute('href'); + $href = trim($element->getAttribute('href')); if (parse_url($href, PHP_URL_HOST) == null) { $baseUrl = $node->getHostFromLink(); if ($baseUrl !== null) { diff --git a/src/FeedIo/Rule/Link.php b/src/FeedIo/Rule/Link.php index ca65a8cc..f09fb687 100644 --- a/src/FeedIo/Rule/Link.php +++ b/src/FeedIo/Rule/Link.php @@ -17,7 +17,7 @@ class Link extends RuleAbstract */ public function setProperty(NodeInterface $node, \DOMElement $element): void { - $nodeValue = $element->nodeValue; + $nodeValue = trim($element->nodeValue); if (parse_url($nodeValue, PHP_URL_HOST) == null) { $nodeValue = $node->getHostFromLink(). $nodeValue; } diff --git a/tests/FeedIo/Rule/LinkTest.php b/tests/FeedIo/Rule/LinkTest.php index c5496f05..b4eefe17 100644 --- a/tests/FeedIo/Rule/LinkTest.php +++ b/tests/FeedIo/Rule/LinkTest.php @@ -58,4 +58,30 @@ public function testCreateElement() $this->assertXmlStringEqualsXmlString('' . self::LINK .'', $document->saveXML()); } + + public function testSetWithWhitespace() + { + $item = new Item(); + // No initial link needed since we're testing absolute URLs with whitespace + + // Test URL with leading/trailing whitespace (like in RSS feeds) + $urlWithWhitespace = "\nhttps://www.somedomain.de/test-article/view/news/123456"; + $expectedUrl = 'https://www.somedomain.de/test-article/view/news/123456'; + + $this->object->setProperty($item, new \DOMElement('link', $urlWithWhitespace)); + $this->assertEquals($expectedUrl, $item->getLink()); + } + + public function testSetWithRelativeUrl() + { + $item = new Item(); + $item->setLink('https://example.com/base'); + + // Test relative URL should still work correctly + $relativeUrl = '/test-path'; + $expectedUrl = 'https://example.com/test-path'; + + $this->object->setProperty($item, new \DOMElement('link', $relativeUrl)); + $this->assertEquals($expectedUrl, $item->getLink()); + } }