Wednesday, June 21, 2017

How to force CKEditor to preserve <br> tags

Leave a Comment

I am using the latest version of CKEditor (4.7 to date) with the standard package, and I want to be able to force it to preserve line break elements (<br>).

I have attempted to use the following config, without success:

CKEDITOR.replace('ck', {     allowedContent: true,     enterMode: CKEDITOR.ENTER_BR }); 

As you can see in this jsfiddle, when you open Source mode, <br> tags have been replaced with a &nbsp;.

How do you achieve that?

2 Answers

Answers 1

A workaround (or at least partial workaround) was given on this CKEditor ticket, which forces the CKEditor to preserve <br> tags:

editor.on( 'pluginsLoaded', function( evt ){     evt.editor.dataProcessor.dataFilter.addRules({         elements :{             br : function( element ) {                           //if next element is BR or <!--cke_br_comment-->, ignore it.                 if( element && element.next && ( element.next.name == 'br' || element.next.value == 'cke_br_comment' ) ){                     return;                 }else {                     var comment = new CKEDITOR.htmlParser.comment( 'cke_br_comment' );                     comment.insertAfter( element );                  }             }         }     });  evt.editor.dataProcessor.htmlFilter.addRules({     comment : function( value, node ) {         if( value.indexOf('cke_br_comment') >= 0 ) {             return false;         }     } }); 

Updated fiddle here.

Answers 2

The developers of CKeditor have reportedly told that br to nbsp auto conversion is not an issue but CKeditors ways of normalizing things.

It wont create any problem for you. So, you need not worry about your br tags being converted to nbsp Go through the following link for more.

If you wish to remove the &nbsp from the source code, One way if to include the following :

 basicEntities: false,  entities_additional: 'lt,gt,amp,apos,quot' 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment