Skip to content

Commit

Permalink
Merge pull request #64 from goetas/doctype
Browse files Browse the repository at this point in the history
HTML5 doctype is case insensitive
  • Loading branch information
goetas authored Oct 21, 2018
2 parents fb367c7 + 35e36d6 commit 3671ee1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/SourceAdapter/HTML5Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class HTML5Adapter implements SourceAdapter
public function load($source)
{
$html5 = $this->getHtml5();
$f = $html5->loadHTMLFragment($source);
$dom = new \DOMDocument('1.0', 'UTF-8');
if ('' !== trim($source)) {
$dom->appendChild($dom->importNode($f, true));
}

if (stripos(rtrim($source), '<!DOCTYPE html>') === 0) {
$dom = $html5->loadHTML($source);
} else {
$f = $html5->loadHTMLFragment($source);
$dom = new \DOMDocument('1.0', 'UTF-8');
if ('' !== trim($source)) {
$dom->appendChild($dom->importNode($f, true));
}
}
return new Template($dom, $this->collectMetadata($dom, $source));
}

Expand All @@ -51,7 +55,7 @@ protected function collectMetadata(\DOMDocument $dom, $source)
$metadata = array();

$metadata['doctype'] = !!$dom->doctype;
$metadata['fragment'] = strpos(rtrim($source), '<!DOCTYPE html>') !== 0;
$metadata['fragment'] = stripos(rtrim($source), '<!DOCTYPE html>') !== 0;

return $metadata;
}
Expand Down
11 changes: 7 additions & 4 deletions tests/Tests/CoreNodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ public function getDataFormTemplates()
public function testVisitNodeTemplates($source, $expected)
{
$compiled = $this->twital->compile($this->sourceAdapter, $source);
$cleanup = function ($str) {
return preg_replace("/\s+/", "", $str);
};
$this->assertEquals($cleanup($expected), $cleanup($compiled));
$this->assertEquals($this->cleanup($expected), $this->cleanup($compiled));
}

abstract protected function getSourceAdapter();

protected function cleanup($str)
{
$str = str_ireplace('<!DOCTYPE html>', '<!DOCTYPE html>', $str);
return preg_replace("/\s+/", "", $str);
}
}
8 changes: 7 additions & 1 deletion tests/Tests/Html5CoreNodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Html5CoreNodesTest extends CoreNodesTest
{
public function getDataFormTemplates()
{
$all = glob(__DIR__."/templates/*.xml");
$all = glob(__DIR__."/templates/*.{htm,xml}", GLOB_BRACE);
$data = array();
foreach ($all as $file) {
$source = file_get_contents($file);
Expand All @@ -32,4 +32,10 @@ protected function getSourceAdapter()
{
return new HTML5Adapter();
}

protected function cleanup($str)
{
$str = str_ireplace('<!DOCTYPE html>', '<!DOCTYPE html>', $str);
return parent::cleanup($str);
}
}
2 changes: 2 additions & 0 deletions tests/Tests/templates/doctype-02.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<html><body>foo</body></html>
3 changes: 3 additions & 0 deletions tests/Tests/templates/doctype-02.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<html><body>foo</body>
</html>

0 comments on commit 3671ee1

Please sign in to comment.