Skip to content

Commit

Permalink
feat!: replace custom HTML5DOMDocument with Kirby's Dom class
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jul 19, 2024
1 parent 0685b12 commit 0e4404d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 116 deletions.
74 changes: 0 additions & 74 deletions classes/JohannSchopplich/HTML5DOMDocument.php

This file was deleted.

13 changes: 6 additions & 7 deletions extensions/hooks.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

use Highlight\Highlighter;
use JohannSchopplich\HTML5DOMDocument;
use Kirby\Cms\App;
use Kirby\Toolkit\Dom;

return [
'kirbytext:after' => function (string|null $text) {
$kirby = App::instance();

// Parse KirbyText input as HTML document
$dom = new HTML5DOMDocument();
$dom->loadHTML($text);
$dom = new Dom(htmlspecialchars_decode(htmlentities($text, ENT_COMPAT, 'UTF-8'), ENT_QUOTES));

// Retrieve all `pre` elements inside newly created HTML document
$preNodes = $dom->getElementsByTagName('pre');
$preNodes = $dom->document()->getElementsByTagName('pre');

// Bail if no `pre` elements have been found
if ($preNodes->length === 0) {
Expand Down Expand Up @@ -74,13 +73,13 @@
}

// Append highlighted wrapped in `code` block to parent `pre`
$codeNode = $dom->createDocumentFragment();
$codeNode->appendXML('<code data-language="' . $language . '">' . $highlightedCode->value . '</code>');
$codeNode = $dom->document()->createElement('code', $highlightedCode->value);
$codeNode->setAttribute('data-language', $language);
$preNode->appendChild($codeNode);
}

// Save all changes
$text = $dom->saveHTML(null, true);
$text = html_entity_decode($dom->toString());
return $text;
}
];
32 changes: 10 additions & 22 deletions tests/HighlightAdapterTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php

declare(strict_types = 1);

use Kirby\Cms\App;
use PHPUnit\Framework\TestCase;

class HighlightAdapterTest extends TestCase
{
protected $kirby;
protected App $kirby;
protected function tearDown(): void
{
restore_error_handler();
restore_exception_handler();
}

public function setUp(): void
{
$this->kirby = new \Kirby\Cms\App([]);
$this->kirby = new App([]);
}

public function testKirbyTextExplicitHighlighting()
Expand Down Expand Up @@ -89,26 +97,6 @@ public function testKirbyTextAutoHighlighting()
$this->assertEquals($expectedHtml, $app->kirbytext($text));
}

public function testUmlautsInNormalKirbyText()
{
$text = 'Ä, ö, ü';
$expectedHtml = '<p>Ä, ö, ü</p>';

$this->assertEquals($expectedHtml, $this->kirby->kirbytext($text));
}

public function testUmlautsInHighlightedKirbyText()
{
$text = <<<'EOD'
```
Ä, ö, ü
```
EOD;
$expectedHtml = '<pre><code>&Auml;, &ouml;, &uuml;</code></pre>';

$this->assertEquals($expectedHtml, $this->kirby->kirbytext($text));
}

public function testCodeBlockHighlighting()
{
$code = <<<'EOD'
Expand Down
9 changes: 9 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

error_reporting(E_ALL);

ini_set('memory_limit', '512M');
ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');

require_once __DIR__ . '/../vendor/autoload.php';
1 change: 0 additions & 1 deletion vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
'Highlight\\RegExMatch' => $vendorDir . '/scrivo/highlight.php/Highlight/RegExMatch.php',
'Highlight\\RegExUtils' => $vendorDir . '/scrivo/highlight.php/Highlight/RegExUtils.php',
'Highlight\\Terminators' => $vendorDir . '/scrivo/highlight.php/Highlight/Terminators.php',
'JohannSchopplich\\HTML5DOMDocument' => $baseDir . '/classes/JohannSchopplich/HTML5DOMDocument.php',
'Kirby\\ComposerInstaller\\CmsInstaller' => $vendorDir . '/getkirby/composer-installer/src/ComposerInstaller/CmsInstaller.php',
'Kirby\\ComposerInstaller\\Installer' => $vendorDir . '/getkirby/composer-installer/src/ComposerInstaller/Installer.php',
'Kirby\\ComposerInstaller\\Plugin' => $vendorDir . '/getkirby/composer-installer/src/ComposerInstaller/Plugin.php',
Expand Down
1 change: 0 additions & 1 deletion vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@

return array(
'Kirby\\' => array($vendorDir . '/getkirby/composer-installer/src'),
'JohannSchopplich\\' => array($baseDir . '/classes/JohannSchopplich'),
);
9 changes: 0 additions & 9 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ class ComposerStaticInit7cf55cdc9b66480b507233b054eae43f
array (
'Kirby\\' => 6,
),
'J' =>
array (
'JohannSchopplich\\' => 17,
),
);

public static $prefixDirsPsr4 = array (
'Kirby\\' =>
array (
0 => __DIR__ . '/..' . '/getkirby/composer-installer/src',
),
'JohannSchopplich\\' =>
array (
0 => __DIR__ . '/../..' . '/classes/JohannSchopplich',
),
);

public static $prefixesPsr0 = array (
Expand Down Expand Up @@ -59,7 +51,6 @@ class ComposerStaticInit7cf55cdc9b66480b507233b054eae43f
'Highlight\\RegExMatch' => __DIR__ . '/..' . '/scrivo/highlight.php/Highlight/RegExMatch.php',
'Highlight\\RegExUtils' => __DIR__ . '/..' . '/scrivo/highlight.php/Highlight/RegExUtils.php',
'Highlight\\Terminators' => __DIR__ . '/..' . '/scrivo/highlight.php/Highlight/Terminators.php',
'JohannSchopplich\\HTML5DOMDocument' => __DIR__ . '/../..' . '/classes/JohannSchopplich/HTML5DOMDocument.php',
'Kirby\\ComposerInstaller\\CmsInstaller' => __DIR__ . '/..' . '/getkirby/composer-installer/src/ComposerInstaller/CmsInstaller.php',
'Kirby\\ComposerInstaller\\Installer' => __DIR__ . '/..' . '/getkirby/composer-installer/src/ComposerInstaller/Installer.php',
'Kirby\\ComposerInstaller\\Plugin' => __DIR__ . '/..' . '/getkirby/composer-installer/src/ComposerInstaller/Plugin.php',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'johannschopplich/kirby-highlighter',
'pretty_version' => '3.1.0',
'version' => '3.1.0.0',
'reference' => NULL,
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -22,7 +22,7 @@
'johannschopplich/kirby-highlighter' => array(
'pretty_version' => '3.1.0',
'version' => '3.1.0.0',
'reference' => NULL,
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit 0e4404d

Please sign in to comment.