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

Unescape slashes in output #223

Merged
merged 3 commits into from
Jan 20, 2025
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
2 changes: 1 addition & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function nonceAttr(): string

public function toScript(): string
{
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
2 changes: 1 addition & 1 deletion src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ public function nonceAttr(): string

public function toScript(): string
{
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
2 changes: 1 addition & 1 deletion src/MultiTypedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ public function nonceAttr(): string

public function toScript(): string
{
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
6 changes: 3 additions & 3 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
$type->setProperty('foo', 'bar');

$expected = '<script type="application/ld+json">'.
'{"@context":"https:\/\/schema.org","@type":"DummyType","foo":"bar"}'.
'{"@context":"https://schema.org","@type":"DummyType","foo":"bar"}'.
'</script>';

expect($type->toScript())->toBe($expected);
Expand All @@ -170,7 +170,7 @@
$type->setNonce('baz');

$expected = '<script type="application/ld+json" nonce="baz">'.
'{"@context":"https:\/\/schema.org","@type":"DummyType","foo":"bar"}'.
'{"@context":"https://schema.org","@type":"DummyType","foo":"bar"}'.
'</script>';

expect($type->toScript())->toBe($expected);
Expand Down Expand Up @@ -273,7 +273,7 @@ public function __toString()
$type->setProperty('foo', 'bar');

$expected = '<script type="application/ld+json">'.
'{"@context":"https:\/\/schema.org","@type":"DummyType","foo":"bar"}'.
'{"@context":"https://schema.org","@type":"DummyType","foo":"bar"}'.
'</script>';

expect((string) $type)->toBe($expected);
Expand Down
4 changes: 2 additions & 2 deletions tests/GeneratedTypeSmokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
->email('[email protected]');

$expected = '<script type="application/ld+json">'.
'{"@context":"https:\/\/schema.org","@type":"LocalBusiness","name":"Spatie","email":"[email protected]"}'.
'{"@context":"https://schema.org","@type":"LocalBusiness","name":"Spatie","email":"[email protected]"}'.
'</script>';

expect($localBusiness->toScript())->toBe($expected);
Expand All @@ -32,7 +32,7 @@
->contactPoint(Schema::contactPoint()->areaServed('Worldwide'));

$expected = '<script type="application/ld+json">'.
'{"@context":"https:\/\/schema.org","@type":"LocalBusiness","name":"Spatie",'.
'{"@context":"https://schema.org","@type":"LocalBusiness","name":"Spatie",'.
'"contactPoint":{"@type":"ContactPoint","areaServed":"Worldwide"}}'.
'</script>';

Expand Down
22 changes: 11 additions & 11 deletions tests/GraphIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$graph->get(Organization::class, Graph::IDENTIFIER_DEFAULT)->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
);
});

Expand All @@ -23,7 +23,7 @@
$graph->getOrCreate(Organization::class, Graph::IDENTIFIER_DEFAULT)->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
);
});

Expand All @@ -33,7 +33,7 @@
$graph->getOrCreate(Organization::class, Graph::IDENTIFIER_DEFAULT)->name('My Company')->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
);
});

Expand All @@ -44,7 +44,7 @@
$graph->hide(Organization::class, 'astrotomic');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"}]}</script>'
);
});

Expand All @@ -56,7 +56,7 @@
$graph->product()->brand($graph->organization('spatie'));

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -69,7 +69,7 @@
$graph->show(Organization::class, null);

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Organization","name":"astrotomic"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Organization","name":"astrotomic"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -82,7 +82,7 @@
$graph->show(Organization::class, 'spatie');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -95,7 +95,7 @@
$graph->hide(Organization::class, 'astrotomic');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -107,7 +107,7 @@
$graph->show(Organization::class, 'spatie');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -118,7 +118,7 @@
$graph->product()->brand($graph->organization('spatie'));

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand All @@ -130,7 +130,7 @@
$graph->product()->brand($graph->organization('spatie'));

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"spatie"},{"@type":"Product","brand":{"@type":"Organization","name":"spatie"}}]}</script>'
);
});

Expand Down
30 changes: 15 additions & 15 deletions tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
it('can render empty', function () {
$graph = new Graph();

expect((string) $graph)->toBe('<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[]}</script>');
expect((string) $graph)->toBe('<script type="application/ld+json">{"@context":"https://schema.org","@graph":[]}</script>');
});

it('can render single item', function () {
$graph = new Graph();
$graph->add(Schema::organization()->name('My Company'));

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
);
});

Expand All @@ -32,7 +32,7 @@
$graph->add(Schema::product()->name('My Product'));

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company"},{"@type":"Product","name":"My Product"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company"},{"@type":"Product","name":"My Product"}]}</script>'
);
});

Expand All @@ -42,7 +42,7 @@
$graph->get(Organization::class)->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
);
});

Expand All @@ -52,7 +52,7 @@
$graph->getOrCreate(Organization::class)->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"}]}</script>'
);
});

Expand All @@ -62,7 +62,7 @@
$graph->getOrCreate(Organization::class)->name('My Company')->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
);
});

Expand All @@ -73,7 +73,7 @@
$graph->getOrCreate(Organization::class)->name('My Company')->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
);
});

Expand All @@ -86,7 +86,7 @@
$graph->show(Organization::class);

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","email":"[email protected]"},{"@type":"Product","brand":{"@type":"Organization","name":"My Company","email":"[email protected]"}}]}</script>'
);
});

Expand Down Expand Up @@ -130,7 +130,7 @@

expect($organization)->toBeInstanceOf(Organization::class);
expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
);
});

Expand All @@ -143,7 +143,7 @@
expect($graph)->toBeInstanceOf(Graph::class);
expect($graph->organization())->toBeInstanceOf(Organization::class);
expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company"}]}</script>'
);
});

Expand All @@ -159,7 +159,7 @@
expect($graph)->toBeInstanceOf(Graph::class);
expect($graph->organization())->toBeInstanceOf(Organization::class);
expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","name":"My Company","description":"This is my awesome Company."}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Organization","name":"My Company","description":"This is my awesome Company."}]}</script>'
);
});

Expand All @@ -179,7 +179,7 @@
expect($graph->organization())->toBeInstanceOf(Organization::class);
expect($graph->product())->toBeInstanceOf(Product::class);
expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"Product","name":"My Product","brand":{"@type":"Organization","name":"My Company"}}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Product","name":"My Product","brand":{"@type":"Organization","name":"My Company"}}]}</script>'
);
});

Expand Down Expand Up @@ -217,7 +217,7 @@
->email('[email protected]');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","author":{"@id":"https:\/\/example.com\/#organization"},"publisher":{"@id":"https:\/\/example.com\/#organization"},"@id":"https:\/\/example.com\/blog\/my-post\/#blogPosting"},{"@type":"Organization","name":"organization","url":"https:\/\/example.com","email":"[email protected]","@id":"https:\/\/example.com\/#organization"}]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"BlogPosting","author":{"@id":"https://example.com/#organization"},"publisher":{"@id":"https://example.com/#organization"},"@id":"https://example.com/blog/my-post/#blogPosting"},{"@type":"Organization","name":"organization","url":"https://example.com","email":"[email protected]","@id":"https://example.com/#organization"}]}</script>'
);
});

Expand All @@ -227,7 +227,7 @@
expect($graph->getContext())->toBe('https://foobar.com');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/foobar.com","@graph":[]}</script>'
'<script type="application/ld+json">{"@context":"https://foobar.com","@graph":[]}</script>'
);
});

Expand All @@ -243,6 +243,6 @@
]);

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":{"@vocab":"https:\/\/schema.org\/","@base":"https:\/\/domain.com\/"},"@graph":[]}</script>'
'<script type="application/ld+json">{"@context":{"@vocab":"https://schema.org/","@base":"https://domain.com/"},"@graph":[]}</script>'
);
});
6 changes: 3 additions & 3 deletions tests/MultiTypedEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$mte = new MultiTypedEntity();

expect((string) $mte)->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":[]}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@type":[]}</script>'
);
});

Expand All @@ -19,7 +19,7 @@
$mte->hotelRoom()->name('The Presidential Suite');

expect($mte->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":["HotelRoom"],"name":"The Presidential Suite"}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@type":["HotelRoom"],"name":"The Presidential Suite"}</script>'
);
});

Expand All @@ -41,6 +41,6 @@
});

expect($mte->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":["HotelRoom","Product"],"name":"The Presidential Suite","offers":{"@type":"Offer","name":"One Night","price":100000,"priceCurrency":"USD"},"aggregateRating":{"@type":"AggregateRating","bestRating":5,"worstRating":4}}</script>'
'<script type="application/ld+json">{"@context":"https://schema.org","@type":["HotelRoom","Product"],"name":"The Presidential Suite","offers":{"@type":"Offer","name":"One Night","price":100000,"priceCurrency":"USD"},"aggregateRating":{"@type":"AggregateRating","bestRating":5,"worstRating":4}}</script>'
);
});
Loading