CKEditor - 在不创建编辑器实例的情况下过滤html

时间:2013-06-17 16:39:08

标签: filter ckeditor

有没有办法做CKEditor.filter('some html');之类的事情? 如何使用默认规则创建filter实例然后使用它?

1 个答案:

答案 0 :(得分:2)

这很简单。您所要做的就是合并CKEDITOR.htmlParserCKEDITOR.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"已消失。