Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Adds doctype and encoding by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jensscherbl committed Feb 27, 2023
1 parent a1ae2a6 commit ca49b6f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -19,12 +19,5 @@
"ext-libxml": "*",
"getkirby/cms": "^3.9",
"getkirby/composer-installer": "^1.2"
},
"autoload":
{
"psr-4":
{
"Kenshō\\XHTML\\": "./src/"
}
}
}
38 changes: 17 additions & 21 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down

0 comments on commit ca49b6f

Please sign in to comment.