[6.x] Inline styles (alignment, font color, background color) are stripped on save
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
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
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():
allowAttribute('style', …)is called up to three times (['div', 'span'], then['ol']whennumberedListis in the toolbar, then['ul']whenbulletedListis). Symfony'sHtmlSanitizerConfig::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,styleis only allowed on<ul>.styleis never allowed on block elements (p,h1–h6, …), so thealignmenttoolbar item can't persisttext-aligneven without the list buttons.
Steps to reproduce
- Create a CKEditor field with
alignment,fontColor,bulletedListandnumberedListin the toolbar, with "Sanitize HTML" on and the default sanitizer. - Center a paragraph and give some text a font color.
- 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.xbranch) - 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from craftcms/ckeditor
-
repo:ckeditor
Difficulty 1/5 Under an hour Newbie friendliness 78/100
craftcms/ckeditor#682 · 1 comment ·
Maintainers usually reply within 1 day
-
repo:ckeditor
Difficulty 3/5 1-2 days Newbie friendliness 52/100
craftcms/ckeditor#678 · 1 comment ·
Maintainers usually reply within 1 day
-
bug repo:ckeditor
Difficulty 3/5 1-2 days Newbie friendliness 40/100
craftcms/ckeditor#665 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
[6.x]: Esc in a fullscreen CKEditor field closes Preview instead of leaving fullscreenPossibly taken @i-just claimed this 15 days ago. Openbug repo:ckeditor
Difficulty 3/5 1-2 days Newbie friendliness 58/100
craftcms/ckeditor#651 · 3 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
bug repo:ckeditor
Difficulty 3/5 1-2 days Newbie friendliness 54/100
craftcms/ckeditor#646 · 2 comments · 1 assignee ·
Maintainers usually reply within 1 day
All issues in craftcms/ckeditor
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Codeinwp/optimole-wp#1259 ·
Maintainers usually reply within 1 day
-
component/core domain/framework
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100