Skip to content

Commit

Permalink
Fix rendering when tag value is empty
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
pedroborges committed Jul 7, 2018
1 parent 6fa0b35 commit 7553471
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
76 changes: 42 additions & 34 deletions src/MetaTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ public function __construct($indentation = null, $order = null)
*/
public function link($key, $value)
{
$attributes = ['rel' => $key];
if (! empty($value)) {
$attributes = ['rel' => $key];

if (is_array($value)) {
foreach ($value as $key => $v) {
$attributes[$key] = $v;
if (is_array($value)) {
foreach ($value as $key => $v) {
$attributes[$key] = $v;
}
} else {
$attributes['href'] = $value;
}
} else {
$attributes['href'] = $value;
}

$tag = $this->createTag('link', $attributes);
$tag = $this->createTag('link', $attributes);

$this->addToTagsGroup('link', $key, $tag);
$this->addToTagsGroup('link', $key, $tag);

return $tag;
return $tag;
}
}

/**
Expand All @@ -70,21 +72,23 @@ public function link($key, $value)
*/
public function meta($key, $value)
{
$attributes = ['name' => $key];
if (! empty($value)) {
$attributes = ['name' => $key];

if (is_array($value)) {
foreach ($value as $key => $v) {
$attributes[$key] = $v;
if (is_array($value)) {
foreach ($value as $key => $v) {
$attributes[$key] = $v;
}
} else {
$attributes['content'] = $value;
}
} else {
$attributes['content'] = $value;
}

$tag = $this->createTag('meta', $attributes);
$tag = $this->createTag('meta', $attributes);

$this->addToTagsGroup('meta', $key, $tag);
$this->addToTagsGroup('meta', $key, $tag);

return $tag;
return $tag;
}
}

/**
Expand All @@ -98,15 +102,17 @@ public function meta($key, $value)
*/
public function og($key, $value, $prefixed = true)
{
$key = $prefixed ? "og:{$key}" : $key;
$tag = $this->createTag('meta', [
'property' => $key,
'content' => $value
]);
if (! empty($value)) {
$key = $prefixed ? "og:{$key}" : $key;
$tag = $this->createTag('meta', [
'property' => $key,
'content' => $value
]);

$this->addToTagsGroup('og', $key, $tag);
$this->addToTagsGroup('og', $key, $tag);

return $tag;
return $tag;
}
}

/**
Expand Down Expand Up @@ -162,15 +168,17 @@ public function title($value)
*/
public function twitter($key, $value, $prefixed = true)
{
$key = $prefixed ? "twitter:{$key}" : $key;
$tag = $this->createTag('meta', [
'name' => $key,
'content' => $value
]);
if (! empty($value)) {
$key = $prefixed ? "twitter:{$key}" : $key;
$tag = $this->createTag('meta', [
'name' => $key,
'content' => $value
]);

$this->addToTagsGroup('twitter', $key, $tag);
$this->addToTagsGroup('twitter', $key, $tag);

return $tag;
return $tag;
}
}

/**
Expand Down
19 changes: 15 additions & 4 deletions tests/MetaTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public function testArrayOfTags()

public function testLinkTag()
{
$tag = $this->head->link('canonical', 'https://pedroborg.es');
$tag = $this->head->link('canonical', 'https://pedroborg.es');
$alternate = $this->head->link('alternate', [
'hreflang' => 'pt-br',
'href' => 'https://br.pedroborg.es'
]);
$empty = $this->head->link('dns-prefetch', '');

$this->assertEquals(
'<link rel="canonical" href="https://pedroborg.es">',
Expand All @@ -57,12 +58,15 @@ public function testLinkTag()
'<link rel="alternate" hreflang="pt-br" href="https://br.pedroborg.es">',
$alternate
);

$this->assertEquals('', $empty);
}

public function testMetaTag()
{
$tag = $this->head->meta('description', 'Meta tag test');
$tag = $this->head->meta('description', 'Meta tag test');
$encoded = $this->head->meta('description', '"Meta tag" test');
$empty = $this->head->meta('description', '');

$this->assertEquals(
'<meta name="description" content="Meta tag test">',
Expand All @@ -73,12 +77,15 @@ public function testMetaTag()
'<meta name="description" content="&quot;Meta tag&quot; test">',
$encoded
);

$this->assertEquals('', $empty);
}

public function testOpenGraphTag()
{
$tag = $this->head->og('title', 'Open Graph test');
$tag = $this->head->og('title', 'Open Graph test');
$preffixed = $this->head->og('og:title', 'Open Graph test', false);
$empty = $this->head->og('og:title', '', false);

$this->assertEquals(
'<meta property="og:title" content="Open Graph test">',
Expand All @@ -89,15 +96,19 @@ public function testOpenGraphTag()
'<meta property="og:title" content="Open Graph test">',
$preffixed
);

$this->assertEquals('', $empty);
}

public function testTwitterCardTag()
{
$tag = $this->head->twitter('card', 'summary');
$tag = $this->head->twitter('card', 'summary');
$preffixed = $this->head->twitter('twitter:card', 'summary', false);
$empty = $this->head->twitter('twitter:image', '', false);

$this->assertEquals('<meta name="twitter:card" content="summary">', $tag);
$this->assertEquals('<meta name="twitter:card" content="summary">', $preffixed);
$this->assertEquals('', $empty);
}

public function testLinkedDataTag()
Expand Down

0 comments on commit 7553471

Please sign in to comment.