Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function add(string | Url | Sitemapable | iterable $tag): static
return $this;
}

if (is_string($tag) && trim($tag) === '') {
return $this;
}

if (is_string($tag)) {
$tag = Url::create($tag);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
assertMatchesXmlSnapshot($this->sitemap->render());
});

test('an empty string cannot be added to the sitemap', function () {
$this->sitemap->add('');
$this->sitemap->add(' ');

assertMatchesXmlSnapshot($this->sitemap->render());
});

test('an url cannot be added twice to the sitemap', function () {
$this->sitemap->add('/home');
$this->sitemap->add('/home');
Expand Down Expand Up @@ -162,6 +169,24 @@
assertMatchesXmlSnapshot($this->sitemap->render());
});

test('sitemapable object with empty string cannot be added', function () {
$this->sitemap
->add(new class implements Sitemapable {
public function toSitemapTag(): Url | string | array
{
return '';
}
})
->add(new class implements Sitemapable {
public function toSitemapTag(): Url | string | array
{
return ' ';
}
});

assertMatchesXmlSnapshot($this->sitemap->render());
});

test('sitemapable object can be added', function () {
$this->sitemap
->add(new class implements Sitemapable {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
</urlset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
</urlset>