[6.x] Inline styles (alignment, font color, background color) are stripped on save

Abierto Apto para principiantes
#676 2 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@i-just ya está trabajando en esto.

Desde el 2/12/2026.

  • #679 de @i-just — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
php
Área
backend, content

Línea de trabajo

Empieza en Field::createHtmlSanitizer() en la clase de campo de CKEditor, donde las llamadas a allowAttribute('style', ...) se hacen una vez por cada botón de lista y cada llamada elimina style de los elementos fuera de su lista. Reproduce el caso ejecutando el sanitizador sobre <p style="text-align:center">a</p> y <span style="color:#ff0000;">x</span>. Está terminado cuando esos estilos en línea se conservan, style está permitido en p, h1-h6, div, span, ol, ul y li en una única llamada, y el comportamiento del sanitizador está cubierto por una prueba.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

repo:ckeditor
Description

In 6.0.0-beta.2, anything the editor stores as an inline style attribute is removed when the field is saved. Text alignment, font color and font background color all revert after saving.

Two causes in Field::createHtmlSanitizer():

  1. allowAttribute('style', …) is called up to three times (['div', 'span'], then ['ol'] when numberedList is in the toolbar, then ['ul'] when bulletedList is). Symfony's HtmlSanitizerConfig::allowAttribute() removes the attribute from every element not in the list it is given, so each call undoes the previous one. With both list buttons enabled, style is only allowed on <ul>.
  2. style is never allowed on block elements (p, h1–h6, …), so the alignment toolbar item can't persist text-align even without the list buttons.
Steps to reproduce
  1. Create a CKEditor field with alignment, fontColor, bulletedList and numberedList in the toolbar, with "Sanitize HTML" on and the default sanitizer.
  2. Center a paragraph and give some text a font color.
  3. Save the entry.
Expected behavior

The alignment and color are kept, as in 5.x.

Actual behavior

Both are gone after saving. Running the field's sanitizer directly:

<p style="text-align:center">a</p>     => <p>a</p>
<span style="color:#ff0000;">x</span>  => <span>x</span>
Workaround

Listening for HtmlSanitizerConfigResolving and re-allowing style on the affected elements in a single call:

Event::listen(function (HtmlSanitizerConfigResolving $event): void {
    $event->config = $event->config->allowAttribute('style', [
        'div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'li',
    ]);
});
Versions
  • craftcms/ckeditor 6.0.0-beta.2 (same code on the current 6.x branch)
  • craftcms/cms 6.0.0-beta.2
  • symfony/html-sanitizer 8.1.12
  • PHP 8.5.5
Lenguaje dominante
PHP
Estrellas
47
Forks
35
Merge medio
1 d 2 h
PR fusionados (30 d)
25

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de craftcms/ckeditor

Todos los issues de craftcms/ckeditor

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.