You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止CKEditor 5自动移除Font Awesome空<i>标签?

解决CKEditor 5移除Font Awesome空标签的问题

你的核心问题是CKEditor 5默认会清理空的行内元素,且之前的htmlSupport配置写法有误。以下是可行的解决方案:

1. 修正htmlSupport配置

将原有错误的正则匹配改为正确的标签规则,并明确允许空标签存在:

htmlSupport: {
    allow: [
        {
            name: 'i',
            classes: ['fa-thin', 'fa-regular', 'fa-light', 'fa-solid', 'fa-brands'],
            // 允许<i>标签为空内容
            empty: true
        }
    ]
}
  • 注意:htmlSupport.allow中的name只需写标签名(i),不需要用正则匹配整个标签结构,之前的正则写法是无效的。
  • empty: true是关键,它告诉编辑器不要移除这类空标签。

2. 补充extraAllowedContent配置(双重保险)

如果上述配置仍不生效,可以加上传统的内容过滤规则作为补充:

extraAllowedContent: 'i[class](fa-thin,fa-regular,fa-light,fa-solid,fa-brands);'

3. 禁用自动格式化的空元素清理(可选)

若编辑器的自动格式化插件仍在清理空标签,可禁用对应功能:

autoformat: {
    removeEmptyElements: false
}

完整的编辑器初始化示例:

ClassicEditor
    .create(document.querySelector('#editor'), {
        htmlSupport: {
            allow: [
                {
                    name: 'i',
                    classes: ['fa-thin', 'fa-regular', 'fa-light', 'fa-solid', 'fa-brands'],
                    empty: true
                }
            ]
        },
        extraAllowedContent: 'i[class](fa-thin,fa-regular,fa-light,fa-solid,fa-brands);',
        autoformat: {
            removeEmptyElements: false
        }
    })
    .catch(error => {
        console.error(error);
    });

内容的提问来源于stack exchange,提问作者WeepingBoil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:40:28