Skip to content

Commit

Permalink
fix: KINT visual error when activating CSP
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 25, 2021
1 parent 7807496 commit 7062536
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
$parameters->set(Option::SKIP, [
__DIR__ . '/app/Views',
__DIR__ . '/system/Debug/Toolbar/Views/toolbar.tpl.php',
__DIR__ . '/system/Debug/Kint/RichRenderer.php',
__DIR__ . '/system/ThirdParty',
__DIR__ . '/tests/system/Config/fixtures',
__DIR__ . '/tests/_support',
Expand Down
4 changes: 3 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter;

use Closure;
use CodeIgniter\Debug\Kint\RichRenderer;
use CodeIgniter\Debug\Timer;
use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\FrameworkException;
Expand All @@ -33,7 +34,6 @@
use Exception;
use Kint;
use Kint\Renderer\CliRenderer;
use Kint\Renderer\RichRenderer;

/**
* This class is the core of the framework, and will analyse the
Expand Down Expand Up @@ -257,6 +257,8 @@ protected function initializeKint()
Kint::$plugins = $config->plugins;
}

Kint::$renderers[Kint::MODE_RICH] = RichRenderer::class;

RichRenderer::$theme = $config->richTheme;
RichRenderer::$folder = $config->richFolder;
RichRenderer::$sort = $config->richSort;
Expand Down
75 changes: 75 additions & 0 deletions system/Debug/Kint/RichRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Debug\Kint;

use Kint\Renderer\RichRenderer as KintRichRenderer;

/**
* Overrides RichRenderer::preRender() for CSP
*/
class RichRenderer extends KintRichRenderer
{
public function preRender()
{
$output = '';

if ($this->pre_render) {
foreach (self::$pre_render_sources as $type => $values) {
$contents = '';

foreach ($values as $v) {
$contents .= $v($this);
}

if (! \strlen($contents)) {
continue;
}

switch ($type) {
case 'script':
$output .= '<script {csp-script-nonce} class="kint-rich-script">' . $contents . '</script>';
break;

case 'style':
$output .= '<style {csp-style-nonce} class="kint-rich-style">' . $contents . '</style>';
break;

default:
$output .= $contents;
}
}

// Don't pre-render on every dump
if (! $this->force_pre_render) {
self::$needs_pre_render = false;
}
}

$output .= '<div class="kint-rich';

if ($this->use_folder) {
$output .= ' kint-file';

if (self::$needs_folder_render || $this->force_pre_render) {
$output = $this->renderFolder() . $output;

if (! $this->force_pre_render) {
self::$needs_folder_render = false;
}
}
}

$output .= '">';

return $output;
}
}

0 comments on commit 7062536

Please sign in to comment.