Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Render HTML5 meta charset at first position
Browse files Browse the repository at this point in the history
W3C Markup Validation Service currently checks if a "meta charset" is
configured at first position, because a HTML5 renderer needs to know
what charset to use before the content.
  • Loading branch information
Wanderson committed Nov 16, 2016
1 parent d4b8bf0 commit 407a451
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Helper/HeadMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,18 @@ public function toString($indent = null)
$items = [];
$this->getContainer()->ksort();

$isHtml5 = $this->view->plugin('doctype')->isHtml5();

try {
foreach ($this as $item) {
$items[] = $this->itemToString($item);
$content = $this->itemToString($item);

if ($isHtml5 && $item->type == 'charset') {
array_unshift($items, $content);
continue;
}

$items[] = $content;
}
} catch (Exception\InvalidArgumentException $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
Expand Down
15 changes: 15 additions & 0 deletions test/Helper/HeadMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,21 @@ public function testCharset()
$view->plugin('headMeta')->toString());
}

public function testCharsetPosition()
{
$view = new View();
$view->plugin('doctype')->__invoke('HTML5');

$view->plugin('headMeta')
->setProperty('description', 'foobar')
->setCharset('utf-8');

$this->assertEquals(
'<meta charset="utf-8">' . PHP_EOL
. '<meta property="description" content="foobar">',
$view->plugin('headMeta')->toString());
}

/**
* @group ZF-9743
*/
Expand Down

0 comments on commit 407a451

Please sign in to comment.