admin管理员组

文章数量:1401968

There is a problem with CKEditor when the user inserts empty tags:

(in HTML source mode)

<a href="fds" class="doIt"></a>

When he presses on the source and then back again, the tag 'A' is gone. I made a little research, and I found that this happens since there is a very plicated function in ckeditor.js that checks whether there is a value inside the element. If there's none the CKEditor drops the tag.

There is a problem with CKEditor when the user inserts empty tags:

(in HTML source mode)

<a href="fds" class="doIt"></a>

When he presses on the source and then back again, the tag 'A' is gone. I made a little research, and I found that this happens since there is a very plicated function in ckeditor.js that checks whether there is a value inside the element. If there's none the CKEditor drops the tag.

Share Improve this question edited Feb 27, 2020 at 18:01 Nmk 1,3192 gold badges14 silver badges28 bronze badges asked Aug 2, 2012 at 16:54 RoyRoy 2433 silver badges10 bronze badges 2
  • Have you tried removing the function? – Waleed Khan Commented Aug 2, 2012 at 19:09
  • This creates havoc with my code, since some of my so-called empty div's actually have background-images, but CKEditor doesn't care, and removes them nonetheless... – Stefan Commented Nov 28, 2018 at 8:46
Add a ment  | 

4 Answers 4

Reset to default 1

I know this is old, but in CKEDITOR 4.x you can do:

CKEDITOR.dtd.$removeEmpty.ElementHere = 0;

Replace ElementHere with whatever tag you want. Like allowing empty i tags for icons:

CKEDITOR.dtd.$removeEmpty.i = 0;

If it doesn't affect anything try adding &nbsp; in your A tag. ckeditor validates, sanitizes, and cleans html. Obviously it doesn't like empty anchor tags.

I found the solution for this specific problem i ran into with the tag

The original answer i got from CKEditor strips <i> Tag

The fix or tweak (you name it) for it is to set the following into the ckeditors config.js:

// ALLOW <i></i>
config.protectedSource.push( /<i[\s\S]*?\>/g ); //allows beginning <i> tag
config.protectedSource.push( /<\/i[\s\S]*?\>/g ); //allows ending </i> tag

Try adding this line into your config.js file:

CKEDITOR.config.allowedContent = true;

Hope this helps

本文标签: javascriptCkeditor removes empty tagsStack Overflow