ckeditor how to allow for .insertHtml(“<customTag myAttr='value'”></customTag>")

﹥>﹥吖頭↗ 提交于 2019-11-28 05:42:27

问题


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

Throws an error, TypeError: Cannot read property 'isBlock' of undefined

If I try .insertHtml("<span>hello</span>") it works just fine.

How can I change ckeditor to allow me to specify my own custom html tags via .insertHtml()? I'd love to just change it to be something like <span class='custom'... or something like that, but I'm having to deal with legacy CMS articles. Using latest ckeditor. Thanks.


回答1:


  1. You need to modify CKEDITOR.dtd object so editor will know this tag and correctly parse HTML and process 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.
    
  2. You need to allow for this tag and its styles/attrs/classes in Advanced Content Filter:

    editor.filter.allow( 'customtag[myattr]', 'myfeature' );
    

Unfortunately, due to some caching, in certain situations you cannot modify DTD object after CKEditor is loaded - you need to modify it when it is created. So to do that:

  1. Clone the CKEditor repository or CKEditor presets repository.

  2. Modify core/dtd.js code.

  3. And build your minified package following instructions in README.md - the only requirements are Java (sorry - Google Closure Compiler :P) and Bash.

PS. That error should not be thrown when unknown element is inserted, so I reported http://dev.ckeditor.com/ticket/10339 and to solve this inconvenience http://dev.ckeditor.com/ticket/10340.




回答2:


I worked around this issue with a combination of createFromHtml() and insertElement()

CKEDITOR.replace('summary', { ... });

var editor = CKEDITOR.instances.summary; 
editor.on('key', function(ev) {
    if (ev.data.keyCode == 9) { // TAB
        var tabHtml = '<span style="white-space:pre">&#09;</span>';
        var tabElement = CKEDITOR.dom.element.createFromHtml(tabHtml, editor.document);
        editor.insertElement(tabElement);
    }
}


来源:https://stackoverflow.com/questions/16066556/ckeditor-how-to-allow-for-inserthtmlcustomtag-myattr-value-customtag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!