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

Aperta Adatta ai principianti
#676 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@i-just ci sta già lavorando.

Dal 2/12/2026.

  • #679 di @i-just — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
backend, content

Direzione di ricerca

Inizia da Field::createHtmlSanitizer() nella classe di campo CKEditor, dove le chiamate allowAttribute('style', ...) vengono eseguite una volta per ogni pulsante di lista e ogni chiamata rimuove style dagli elementi al di fuori della sua lista. Riproduci il problema eseguendo il sanitizer su <p style="text-align:center">a</p> e <span style="color:#ff0000;">x</span>. È completato quando questi stili inline sopravvivono, style è consentito su p, h1-h6, div, span, ol, ul e li in un'unica chiamata, e il comportamento del sanitizer è coperto da un test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
PHP
Stelle
47
Fork
35
Merge medio
1g 2h
PR unite (30g)
25

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di craftcms/ckeditor

Tutte le issue di craftcms/ckeditor

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.