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

Đang mở Phù hợp với người mới
#676 2 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@i-just đang làm issue này rồi.

Từ ngày 2/12/2026.

  • #679 của @i-just — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
backend, content

Hướng nghiên cứu

Bắt đầu từ Field::createHtmlSanitizer() trong lớp field của CKEditor, nơi các lệnh gọi allowAttribute('style', ...) được thực hiện một lần cho mỗi nút danh sách, và mỗi lệnh gọi xóa style khỏi các phần tử nằm ngoài danh sách của nó. Tái hiện bằng cách chạy sanitizer trên <p style="text-align:center">a</p> và <span style="color:#ff0000;">x</span>. Hoàn thành khi các inline style đó được giữ lại, style được cho phép trên p, h1-h6, div, span, ol, ul và li trong một lệnh gọi duy nhất, và hành vi của sanitizer được bao phủ bởi một bài kiểm thử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
PHP
Star
47
Fork
35
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
25

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của craftcms/ckeditor

Tất cả issue của craftcms/ckeditor

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.