diff --git a/README.md b/README.md index 904a423..32892d5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Kirby XHTML -Ensures well-formed XML and XHTML output for [Kirby][1] templates and strips whitespace between nodes. +Ensures well-formed and XHTML output for [Kirby][1] templates and removes whitespace between nodes. [1]: https://getkirby.com diff --git a/composer.json b/composer.json index 88b7547..072e71b 100755 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "name": "kenshodigital/kirby-xhtml", - "description": "Ensures well-formed XML and XHTML output for Kirby templates.", + "description": "Ensures well-formed XHTML output for Kirby templates.", "type": "kirby-plugin", - "version": "1.0.0", + "version": "2.0.0", "homepage": "https://github.com/kenshodigital/kirby-xhtml", "license": "MIT", "authors": @@ -19,12 +19,5 @@ "ext-libxml": "*", "getkirby/cms": "^3.9", "getkirby/composer-installer": "^1.2" - }, - "autoload": - { - "psr-4": - { - "Kenshō\\XHTML\\": "./src/" - } } } diff --git a/index.php b/index.php index 2781796..6a614a2 100755 --- a/index.php +++ b/index.php @@ -6,19 +6,10 @@ namespace Kenshō\XHTML; -use DOMDocument; +use DOMImplementation; use Kirby\Cms\App; -const XML = [ - 'htm', - 'html', - 'rss', - 'xht', - 'xhtml', - 'xml', - 'xsl', -]; -const XHTML = [ +const HTML = [ 'htm', 'html', 'xht', @@ -27,19 +18,24 @@ App::plugin('kensho/xhtml', [ 'hooks' => [ /** - * Ensures well-formed XML and XHTML - * output and strips whitespace between - * nodes. + * Ensures well-formed XHTML output and + * removes whitespace between nodes. */ 'page.render:after' => function (string $contentType, array $data, string $html): string { - if (\in_array($contentType, XML)) { - $document = new DOMDocument; - $document->preserveWhiteSpace = FALSE; - $document->loadXML($html); + if (\in_array($contentType, HTML)) { + $dom = new DOMImplementation; + $doctype = $dom->createDocumentType('html'); + $document = $dom->createDocument(null, '', $doctype); + $document->xmlVersion = '1.0'; + $document->encoding = 'utf-8'; + $document->preserveWhiteSpace = false; + $fragment = $document->createDocumentFragment(); + + $fragment->appendXML($html); + $document->appendChild($fragment); + + App::instance()->response()->type('application/xhtml+xml'); - if (\in_array($contentType, XHTML)) { - App::instance()->response()->type('application/xhtml+xml'); - } return $document->saveXML(); } return $html;