ckeditor如何允许.insertHtml("<customTag myAttr ='value'"> </ customTag>")

Ian*_*vis 3 ckeditor

var currentDialog = CKEDITOR.dialog.getCurrent();
currentDialog._.editor.insertHtml("<customTag myAttr='var'></customTag>");
Run Code Online (Sandbox Code Playgroud)

引发错误, TypeError: Cannot read property 'isBlock' of undefined

如果我尝试.insertHtml("<span>hello</span>")它就可以了.

如何更改ckeditor以允许我通过指定我自己的自定义html标签.insertHtml()?我喜欢把它改成类似的<span class='custom'...东西,但是我不得不处理遗留的CMS文章.使用最新的ckeditor.谢谢.

Rei*_*mar 5

  1. 您需要修改CKEDITOR.dtd对象,以便编辑器知道此标记并正确解析HTML并处理DOM:

    CKEDITOR.dtd.customtag = { em:1 };        // List of tag names it can contain.
    CKEDITOR.dtd.$block.customtag = 1;        // Choose $block or $inline.
    CKEDITOR.dtd.body.customtag = 1;          // Body may contain customtag.
    
    Run Code Online (Sandbox Code Playgroud)
  2. 您需要在高级内容过滤器中允许此标记及其样式/ attrs /类:

    editor.filter.allow( 'customtag[myattr]', 'myfeature' );
    
    Run Code Online (Sandbox Code Playgroud)

不幸的是,由于某些缓存,在某些情况下,您无法在加载CKEditor后修改DTD对象 - 您需要在创建它时对其进行修改.所以要做到这一点:

  1. 克隆CKEditor存储库CKEditor预设存储库.

  2. 修改core/dtd.js代码.

  3. 并按照说明构建您的缩小包README.md- 唯一的要求是Java(抱歉 - Google Closure Compiler:P)和Bash.

PS.插入未知元素时不应抛出该错误,因此我报告了http://dev.ckeditor.com/ticket/10339并解决了这个不便之处http://dev.ckeditor.com/ticket/10340.