`data-ccp-props` 是 microsoft office(尤其是 word 和 office 365)复制粘贴到富文本编辑器时自动生成的私有元数据属性,用于保留格式信息,但会导致 html 校验失败或编辑器异常。
在实际 Web 开发与内容管理过程中,你可能偶然发现类似如下 HTML 片段:
这类 data-ccp-props 属性并非标准 HTML 属性,也不属于任何公开规范,而是 Microsoft Office 套件(包括 Word、Outlook、Office 365)在「复制 → 粘贴至支持富文本的 Web 编辑器」流程中注入的内部标记。其作用是向目标编辑器(如 TinyMCE、CKEditor、Quill 或自研 WYSIWYG 编辑器)传递排版上下文,例如段落样式 ID、字体缩放比例、兼容性标志等。
⚠️ 注意:这些属性通常伴随大量其他非标准类名与内联样式,例如:
它们虽不影响浏览器渲染,但会带来三类典型问题:
在富文本编辑器初始化时,监听 paste 事件并移除敏感属性:
editor.on('paste', function (evt) {
const fragment = evt.data && evt.data.dataValue;
if (!fragment) return;
const tempDiv = document.createElement('div');
tempDiv.innerHTML = fragment;
// 移除所有 data-ccp-* 属性
tempDiv.querySelectorAll('[data-ccp-props], [data-ccp-parastyle], [data-contrast]').forEach(el => {
el.removeAttribute('data-ccp-props');
el.removeAttribute('data-ccp-parastyle');
el.removeAttribute('data-contrast');
});
// 可选:清理 Office 特有 class(保留语义类)
tempDiv.querySelectorAll('*').forEach(el => {
const classes = Array.from(el.classList).filter(cls =>
!/^SCXW\d+|BCX\d+$/.test(cls)
);
el.className = classes.join(' ');
});
evt.data.dataValue = tempDiv.innerHTML;
});使用 HTML sanitizer 库(如 DOMPurify 或服务端 sanitize-html)配置白名单:
const sanitizeHtml = require('sanitize-html');
const clean = sanitizeHtml(dirtyHtml, {
allowedAttributes: {
'*': ['class', 'style', 'id', 'lang', 'title', 'aria-*'],
'a': ['href', 'target', 'rel'],
'img': ['src', 'alt', 'width', 'height']
},
// 显式禁止 data-ccp-* 类型属性
allowedIframeHostnames: [],
transformTags: {
'span': (tagName, attribs) => {
// 删除 data-ccp-* 所有变体
Object.keys(attribs).forEach(key => {
if (key.startsWith('data-ccp-') || key === 'data-contrast') {
delete attribs[key];
}
});
ret
urn { tagName, attribs };
}
}
});data-ccp-props 是 Office 生态遗留的“格式胶水”,对现代 Web 构建并无实际价值,反而引入技术债务。应从前端拦截 + 服务端净化双通道治理,并辅以团队协作规范,从根本上减少污染源。保持 HTML 干净、语义清晰、可维护性强,才是可持续内容交付的基础。