Skip to content

Commit

Permalink
fix: inconsistency with escaping (#75)
Browse files Browse the repository at this point in the history
* WIP

* Style

* Add tests

* Fix
  • Loading branch information
ralphjsmit authored May 4, 2024
1 parent 6c0e413 commit 1aa28ef
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources/views/tags/tag.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<{{ $tag }}<?php foreach($attributes as $name => $value) : ?> {{ $name }}="{{ $value }}"<?php endforeach ?>><?php if ($inner) : ?>{!! $inner !!}</{{ $tag }}><?php endif ?>
<{{ $tag }}<?php foreach($attributes as $name => $value) : ?> {{ $name }}="{{ $value }}"<?php endforeach ?>><?php if ($inner) : ?>{{ $inner }}</{{ $tag }}><?php endif ?>
7 changes: 5 additions & 2 deletions src/Schema/ArticleSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use RalphJSmit\Laravel\SEO\Support\SEOData;

class ArticleSchema extends Schema
Expand Down Expand Up @@ -75,9 +76,9 @@ public function initializeMarkup(SEOData $SEOData, array $markupBuilders): void
}
}

public function generateInner(): string
public function generateInner(): HtmlString
{
return collect([
$inner = collect([
'@context' => 'https://schema.org',
'@type' => $this->type,
'mainEntityOfPage' => [
Expand All @@ -94,5 +95,7 @@ public function generateInner(): string
->when($this->articleBody, fn (Collection $collection): Collection => $collection->put('articleBody', $this->articleBody))
->pipeThrough($this->markupTransformers)
->toJson();

return new HtmlString($inner);
}
}
7 changes: 5 additions & 2 deletions src/Schema/BreadcrumbListSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RalphJSmit\Laravel\SEO\Schema;

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

class BreadcrumbListSchema extends Schema
Expand All @@ -27,9 +28,9 @@ public function initializeMarkup(SEOData $SEOData, array $markupBuilders): void
]);
}

public function generateInner(): string
public function generateInner(): HtmlString
{
return collect([
$inner = collect([
'@context' => 'https://schema.org',
'@type' => $this->type,
'itemListElement' => $this->breadcrumbs
Expand All @@ -44,6 +45,8 @@ public function generateInner(): string
])
->pipeThrough($this->markupTransformers)
->toJson();

return new HtmlString($inner);
}

public function prependBreadcrumbs(array $breadcrumbs): static
Expand Down
5 changes: 4 additions & 1 deletion src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use RalphJSmit\Helpers\Laravel\Pipe\Pipeable;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use RalphJSmit\Laravel\SEO\Support\Tag;
Expand All @@ -24,6 +25,8 @@ abstract class Schema extends Tag

public string $tag = 'script';

public HtmlString $inner;

public function __construct(SEOData $SEOData, array $markupBuilders = [])
{
$this->initializeMarkup($SEOData, $markupBuilders);
Expand All @@ -33,7 +36,7 @@ public function __construct(SEOData $SEOData, array $markupBuilders = [])
$this->inner = $this->generateInner();
}

abstract public function generateInner(): string;
abstract public function generateInner(): HtmlString;

abstract public function initializeMarkup(SEOData $SEOData, array $markupBuilders): void;

Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/Tags/OpenGraphTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,17 @@
get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta property="og:title" content="My OG title | Laravel SEO">', false);
});

it('will escape the title', function () {
config()->set('seo.title.suffix', ' - A & B');

$page = Page::create();
$page->seo->update([
'title' => 'My page title',
]);

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta property="og:title" content="My page title - A &amp; B">', false);
});
8 changes: 8 additions & 0 deletions tests/Feature/Tags/TitleTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@
get(route('seo.test-page', ['page' => $page]))
->assertSee('<title>1 | Laravel SEO</title>', false);
});

it('will escape the title', function () {
config()->set('seo.title.infer_title_from_url', true);
config()->set('seo.title.suffix', ' - A & B');

get(route('seo.test-plain'))
->assertSee('<title>Test Plain - A &amp; B</title>', false);
});
14 changes: 14 additions & 0 deletions tests/Feature/Tags/TwitterCardSummaryTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@
get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta name="twitter:title" content="My OG title | Laravel SEO">', false);
});

it('will escape the title', function () {
config()->set('seo.title.suffix', ' - A & B');

$page = Page::create();
$page->seo->update([
'title' => 'My page title',
]);

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta name="twitter:title" content="My page title - A &amp; B">', false);
});

0 comments on commit 1aa28ef

Please sign in to comment.