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

Skin::renderHead - do not pass null to htmlspecialchars() #169

Merged
merged 3 commits into from
Nov 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions classes/Skin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class Skin
* @return self
* @throws Exception
*/
public static function factory(NanoApp $app, $skinName)
public static function factory(NanoApp $app, string $skinName): Skin
{
$className = sprintf('Skin%s', ucfirst($skinName));

Expand All @@ -72,7 +72,7 @@ public static function factory(NanoApp $app, $skinName)
* @param NanoApp $app
* @param string $skinName
*/
public function __construct(NanoApp $app, $skinName)
public function __construct(NanoApp $app, string $skinName)
{
$this->app = $app;
$this->skinName = $skinName;
Expand Down Expand Up @@ -134,7 +134,7 @@ public function getPageTitle()
/**
* Add <meta> tag entry
*/
public function addMeta($name, $value)
public function addMeta(string $name, ?string $value): void
{
$this->meta[] = [
'name' => $name,
Expand Down Expand Up @@ -306,7 +306,7 @@ protected function getAssetsUrls($type)
/**
* Renders set of <meta> elements to be used in page's head section
*/
public function renderHead($sep = "\n")
public function renderHead(string $sep = "\n"): string
{
// render <meta> elements
$elements = [];
Expand All @@ -315,7 +315,7 @@ public function renderHead($sep = "\n")
$node = '<meta';

foreach ($item as $name => $value) {
$value = htmlspecialchars($value);
$value = htmlspecialchars($value ?? '');
$node .= " {$name}=\"{$value}\"";
}

Expand All @@ -329,7 +329,7 @@ public function renderHead($sep = "\n")
$node = '<link';

foreach ($item as $name => $value) {
$value = htmlspecialchars($value);
$value = htmlspecialchars($value ?? '');
$node .= " {$name}=\"{$value}\"";
}

Expand Down
40 changes: 40 additions & 0 deletions tests/SkinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Nano\NanoBaseTest;

class TestSkin extends Skin
{
const NAME = 'test-skin';
}

class SkinTest extends NanoBaseTest
{
private TestSkin $skin;

public function setUp(): void
{
$app = Nano::app(__DIR__ . '/app');
$this->skin = new TestSkin($app, TestSkin::NAME);
}

public function testRenderHeadNullHandling()
{
$this->skin->addMeta('null', null);
$this->skin->addMeta('empty_string', '');
$this->skin->addMeta('a_string', 'Foo Bar');
$this->skin->addMeta('a_number', 42);

$this->skin->addLink('foo', null);
$this->skin->addLink('bar', 'test', ['type' => 'some/thing']);

$head = $this->skin->renderHead();

$this->assertStringContainsString('<meta name="null" value="">', $head, );
$this->assertStringContainsString('<meta name="empty_string" value="">', $head, );
$this->assertStringContainsString('<meta name="a_string" value="Foo Bar">', $head, );
$this->assertStringContainsString('<meta name="a_number" value="42">', $head, );

$this->assertStringContainsString('<link rel="foo" value="">', $head, );
$this->assertStringContainsString('<link rel="bar" value="test" type="some/thing">', $head, );
}
}