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

オープン 初心者向け
#676 コメント 2 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@i-just がすでに取り組んでいます。

2026年12月2日 から。

  • #679 @i-just による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
php
領域
backend, content

調査の方向性

CKEditor のフィールドクラスにある Field::createHtmlSanitizer() から始めてください。ここでは allowAttribute('style', ...) の呼び出しがリストボタンごとに一度ずつ行われ、各呼び出しがそのリスト外の要素から style を削除しています。<p style="text-align:center">a</p> と <span style="color:#ff0000;">x</span> に対してサニタイザーを実行して再現してください。インラインスタイルが残り、単一の呼び出しで p、h1-h6、div、span、ol、ul、li に style が許可され、サニタイザーの動作がテストで網羅されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
PHP
スター
47
フォーク
35
平均マージ
1日 2時間
マージ済み PR(30日)
25

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

craftcms/ckeditor のほかの issue

craftcms/ckeditor の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。