Skip to content

Gajus/Dindent inserts extra space when an inline HTML tag like <strong> is placed directly against text #172

Description

@NitinParri

Scenario:

  1. The code in RTE: <p>Lorem ipsum dolor sit amet<strong class="blue">!</strong></p>
  2. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions