Scenario:
- The code in RTE:
<p>Lorem ipsum dolor sit amet<strong class="blue">!</strong></p>
config.sourceopt.formatHtml is more than 0
Current output: Lorem ipsum dolor sit amet !.
Desired output: Lorem ipsum dolor sit amet! (without extra space before !).
It is a know problem of the Dindent library.
Untill they fix the bug, you can use an XCLASS to remove the extra space:
Create file: /your_extension/Classes/Xclass/Service/CleanHtmlService.php with the following code:
<?php
declare(strict_types=1);
namespace YourVendor\YourExtension\Xclass\Service;
use HTML\Sourceopt\Service\CleanHtmlService as SourceoptCleanHtmlService;
class CleanHtmlService extends SourceoptCleanHtmlService
{
private const BOUNDARY_MARKER = "\u{E000}";
private const INLINE_TAGS = 'a|abbr|acronym|b|bdo|big|cite|code|dfn|em|i|kbd|samp|small|span|strong|sub|sup|tt|var';
public function clean(string $html, array $config = [], string $doctype = ''): string
{
$formatHtmlEnabled = isset($config['formatHtml']) && (bool) $config['formatHtml'];
if ($formatHtmlEnabled) {
$html = $this->protectInlineBoundaries($html);
}
$html = parent::clean($html, $config, $doctype);
if ($formatHtmlEnabled) {
$html = $this->restoreInlineBoundaries($html);
}
return $html;
}
private function protectInlineBoundaries(string $html): string
{
$html = (string) preg_replace(
'/(?<=[^\s>])(?=<(?:' . self::INLINE_TAGS . ')(?:\s[^>]*)?>)/u',
self::BOUNDARY_MARKER,
$html
);
return (string) preg_replace(
'/<\/(?:' . self::INLINE_TAGS . ')>(?=[^\s<])/u',
'$0' . self::BOUNDARY_MARKER,
$html
);
}
private function restoreInlineBoundaries(string $html): string
{
return (string) preg_replace('/\s*' . self::BOUNDARY_MARKER . '\s*/u', '', $html);
}
}
Register the XCLASS in /your_extension/ext_localconf.php:
if (class_exists(\HTML\Sourceopt\Service\CleanHtmlService::class)) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\HTML\Sourceopt\Service\CleanHtmlService::class] = [
'className' => \YourVendor\YourExtension\Xclass\Service\CleanHtmlService::class,
];
}
Replace YourVendor\YourExtension with your extension and vendor and replace your_extension with your extension folder.
Scenario:
<p>Lorem ipsum dolor sit amet<strong class="blue">!</strong></p>config.sourceopt.formatHtmlis more than 0Current output:
Lorem ipsum dolor sit amet !.Desired output:
Lorem ipsum dolor sit amet!(without extra space before !).It is a know problem of the Dindent library.
Untill they fix the bug, you can use an XCLASS to remove the extra space:
Create file:
/your_extension/Classes/Xclass/Service/CleanHtmlService.phpwith the following code:Register the XCLASS in
/your_extension/ext_localconf.php:Replace
YourVendor\YourExtensionwith your extension and vendor and replace your_extension with your extension folder.