有没有办法做CKEditor.filter('some html');
之类的事情?
如何使用默认规则创建filter
实例然后使用它?
答案 0 :(得分:2)
这很简单。您所要做的就是合并CKEDITOR.htmlParser和CKEDITOR.filter:
function gimmeFilter( rules ) {
var filter = new CKEDITOR.filter( rules ),
writer = new CKEDITOR.htmlParser.basicWriter();
var fn = function( input, output, msg ) {
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( input );
writer.reset();
filter.applyTo( fragment );
fragment.writeHtml( writer );
return writer.getHtml();
};
fn.allow = function( rules ) {
filter.allow( rules );
};
return fn;
}
var f = gimmeFilter( {
'p b i': {
classes: 'foo',
styles: 'text-align'
}
} );
f( '<p style="text-align:right"><b class="boom" style="color: red">foo</b> <i class="foo">bar</i> <u>bum</u></p>' );
>>> "<p style="text-align:right"><b>foo</b> <i class="foo">bar</i> bum</p>"
看到<u>
代码,style="color: red"
和class="boom"
已消失。