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

Commit 407a451

Browse files
author
Wanderson
committed
Render HTML5 meta charset at first position
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.
1 parent d4b8bf0 commit 407a451

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Helper/HeadMeta.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,18 @@ public function toString($indent = null)
172172
$items = [];
173173
$this->getContainer()->ksort();
174174

175+
$isHtml5 = $this->view->plugin('doctype')->isHtml5();
176+
175177
try {
176178
foreach ($this as $item) {
177-
$items[] = $this->itemToString($item);
179+
$content = $this->itemToString($item);
180+
181+
if ($isHtml5 && $item->type == 'charset') {
182+
array_unshift($items, $content);
183+
continue;
184+
}
185+
186+
$items[] = $content;
178187
}
179188
} catch (Exception\InvalidArgumentException $e) {
180189
trigger_error($e->getMessage(), E_USER_WARNING);

test/Helper/HeadMetaTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,21 @@ public function testCharset()
403403
$view->plugin('headMeta')->toString());
404404
}
405405

406+
public function testCharsetPosition()
407+
{
408+
$view = new View();
409+
$view->plugin('doctype')->__invoke('HTML5');
410+
411+
$view->plugin('headMeta')
412+
->setProperty('description', 'foobar')
413+
->setCharset('utf-8');
414+
415+
$this->assertEquals(
416+
'<meta charset="utf-8">' . PHP_EOL
417+
. '<meta property="description" content="foobar">',
418+
$view->plugin('headMeta')->toString());
419+
}
420+
406421
/**
407422
* @group ZF-9743
408423
*/

0 commit comments

Comments
 (0)