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

Open Beginner friendly
#676 2 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@i-just is already working on this.

Since Dec 2, 2026.

  • #679 by @i-just — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
backend, content

Research direction

Start in Field::createHtmlSanitizer() in the CKEditor field class, where the allowAttribute('style', ...) calls are made once per list button and each call removes style from elements outside its list. Reproduce by running the sanitizer on <p style="text-align:center">a</p> and <span style="color:#ff0000;">x</span>. Done when those inline styles survive, with style allowed on p, h1-h6, div, span, ol, ul and li in a single call, and the sanitizer behaviour is covered by a test.

Written by the indexing model from the issue text.

Description

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
Dominant language
PHP
Stars
47
Forks
35
Avg merge
1d 2h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from craftcms/ckeditor

All issues in craftcms/ckeditor

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.