您好,欢迎访问一九零五行业门户网

TinyMCE 粘贴HTML代码,避免style属性被自动清除_html/css_WEB-ITnose

tinymce在粘贴含有style属性的html代码时,会自动清除style属性,设置 extended_valid_elements 也只能在firefox浏览器起作用,chrome无效。
extended_valid_elements: 'div[style|class|id]'
chrome下即使设置了 inline_styles: true, schema: 'html5', 也没有用
无奈去看tinymce源码,发现如果去掉 plugins 里的 paste 就不会被剔除样式,最终在 plugin/paste/plugin.js 里找到这样一段代码:
function addpreprocessfilter(filterfunc) { editor.on('beforepastepreprocess', function(e) { e.content = filterfunc(e.content); }); }
估计是在这里过滤的,找到 filterfunc这个函数:
// sniff browsers and apply fixes since we can't feature detect if (env.webkit) { addpreprocessfilter(removewebkitstyles); } if (env.ie) { addpreprocessfilter(removeexplorerbrelementsafterblocks); }
看到这里我震惊了,tinymce真的这么狗屎吗?就直接检测了webkit和ie,firefox被无视了吗?firefox下的style被保留是因为tinymce的开发人员根本没有考虑firefox浏览器,我还以为是chrome下出了什么bug,真够狗屎的。
在进去这个 removewebkitstyles函数里:
// filter away styles that isn't matching the target node var webkitstyles = editor.settings.paste_webkit_styles; if (editor.settings.paste_remove_styles_if_webkit === false || webkitstyles == all) { return content; }
很明显,只要设置一下这个属性就可以
paste_webkit_styles: true
看了tinymce的源代码,写的真叫一个烂,全局变量没有加任何标识区分本地变量,函数和语句混合在一起。
其它类似信息

推荐信息