Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...