Skip to content

Commit

Permalink
Fix image escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphjsmit committed Jun 15, 2024
1 parent 1bd284e commit 77062d7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
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
4 changes: 3 additions & 1 deletion 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,7 +53,7 @@ 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);
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);
});

0 comments on commit 77062d7

Please sign in to comment.