Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: image URL escaping #84

Merged
merged 2 commits into from
Jun 15, 2024
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: 3 additions & 1 deletion src/Support/MetaTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace RalphJSmit\Laravel\SEO\Support;

use Illuminate\Support\HtmlString;

class MetaTag extends Tag
{
public string $tag = 'meta';

public function __construct(
string $name,
string $content,
string | HtmlString $content,
) {
$this->attributes['name'] = $name;
$this->attributes['content'] = $content;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function render(): View
public function collectAttributes(): Collection
{
return collect($this->attributes)
->map(fn ($attribute) => trim($attribute))
->map(fn (string | HtmlString $attribute) => is_string($attribute) ? trim($attribute) : $attribute)
->sortKeysUsing(function ($a, $b) {
$indexA = array_search($a, static::ATTRIBUTES_ORDER);
$indexB = array_search($b, static::ATTRIBUTES_ORDER);
Expand Down
6 changes: 4 additions & 2 deletions src/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RalphJSmit\Laravel\SEO;

use const FILTER_VALIDATE_URL;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -51,13 +53,13 @@ public function fillSEOData(?SEOData $SEOData = null): SEOData
}
}

if ($SEOData->image && ! filter_var($SEOData->image, FILTER_VALIDATE_URL)) {
if ($SEOData->image && filter_var($SEOData->image, FILTER_VALIDATE_URL) === false) {
$SEOData->imageMeta();

$SEOData->image = secure_url($SEOData->image);
}

if ($SEOData->favicon && ! filter_var($SEOData->favicon, FILTER_VALIDATE_URL)) {
if ($SEOData->favicon && filter_var($SEOData->favicon, FILTER_VALIDATE_URL) === false) {
$SEOData->favicon = secure_url($SEOData->favicon);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/AuthorTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function initialize(?SEOData $SEOData): ?MetaTag

return new MetaTag(
name: 'author',
content: trim($author)
content: $author
);
}
}
3 changes: 2 additions & 1 deletion src/Tags/ImageTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RalphJSmit\Laravel\SEO\Tags;

use Illuminate\Support\HtmlString;
use RalphJSmit\Laravel\SEO\Support\MetaTag;
use RalphJSmit\Laravel\SEO\Support\SEOData;

Expand All @@ -17,7 +18,7 @@ public static function initialize(?SEOData $SEOData): ?MetaTag

return new MetaTag(
name: 'image',
content: trim($image)
content: new HtmlString($image),
);
}
}
11 changes: 11 additions & 0 deletions tests/Feature/Tags/ImageTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@
get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta name="image" content="' . secure_url('test/image.jpg') . '">', false);
});

it('will not change query parameters on an image URL', function () {
$page = Page::create();

$page->seo->update([
'image' => $url = 'https://website.test/images/xSVtl6ZF7fNuZIoXkZbzI2EzoAD.jpg?h=800&fit=contain&q=80&fm=webp',
]);

get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta name="image" content="' . $url . '">', false);
});
Loading