diff --git a/app/Utils/TextFormatter.php b/app/Utils/TextFormatter.php index bfde505..90d889c 100644 --- a/app/Utils/TextFormatter.php +++ b/app/Utils/TextFormatter.php @@ -6,9 +6,9 @@ class TextFormatter { /** * Formata conteúdo de texto para exibição HTML segura. - * - Escapa caracteres especiais HTML (segurança XSS) - * - Converte URLs em links clicáveis - * - Converte quebras de linha em
+ * - Detecta se o conteúdo já é HTML (do Quill editor) ou texto puro + * - Para HTML: aplica purificação básica mantendo tags permitidas + * - Para texto puro: escapa e converte URLs/quebras de linha * * @param string|null $text Texto a ser formatado * @return string Texto formatado para HTML @@ -19,6 +19,14 @@ class TextFormatter return ''; } + // Detecta se o conteúdo já é HTML (contém tags HTML comuns do Quill) + if (self::isHtmlContent($text)) { + // Conteúdo HTML do Quill - purificar e retornar + return self::purifyHtml($text); + } + + // Conteúdo texto puro (dados antigos) - aplicar formatação completa + // 1. Escapa caracteres especiais HTML (segurança contra XSS) $text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); @@ -37,6 +45,47 @@ class TextFormatter return $text; } + /** + * Verifica se o conteúdo parece ser HTML (do Quill editor) + * + * @param string $text Texto a verificar + * @return bool True se parece ser HTML + */ + private static function isHtmlContent(string $text): bool + { + // Verifica se contém tags HTML comuns do Quill + return preg_match('/<(p|div|br|strong|em|u|a|ul|ol|li|h[1-6]|span|blockquote)[^>]*>/i', $text) === 1; + } + + /** + * Purifica HTML mantendo apenas tags seguras + * + * @param string $html HTML a ser purificado + * @return string HTML purificado + */ + private static function purifyHtml(string $html): string + { + // Tags permitidas do Quill editor + $allowedTags = '