48

How can I disable CKEditor to get me every time  , when i don't want them? I'm using CKEditor with jQuery adapter.

I don't want to have any   tags.

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
V.Rashkov
  • 1,527
  • 4
  • 18
  • 31
  • 1
    How will it know which ones you want and which ones you don't want? Do you want to remove them *all*, or reduce multiples, or something else? How are they getting there in the first place? – Wesley Murch Mar 16 '12 at 17:14
  • I just don't want them to be added.. all of them – V.Rashkov Mar 16 '12 at 17:17
  • OK, just wanted to clarify. So when a user presses the space bar, it will never add more than one space? In other words, you don't want to have to do this on the server side, correct? I suppose the best way would be to turn them all into regular spaces (just in case some are separating two words), but I'm not sure how to do it with ckeditor specifically. – Wesley Murch Mar 16 '12 at 17:20
  • When i load some html, go to the wysywyg part of the editor, then get back to the html part of the editor - it's adding nbsp tags, to lot's of places, i know why it adds them, I've searched about the problem, but no solution worked – V.Rashkov Mar 16 '12 at 17:22
  • 4
    It also adds   in empty table cells which breaks some email designs. – aaandre Sep 13 '12 at 17:11

10 Answers10

61

After some research I might shed some light on this issue - unfortunately there is no out-of-the-box solution.

In the CKEditor there are four ways a no-break space can occur (anybody know more?):

  1. Automatic filling of empty blocks. This can be disabled in the config:

    config.fillEmptyBlocks = false;
    
  2. Automatic insertion when pressing TAB-key. This can be disabled in the config:

    config.tabSpaces = 0;
    
  3. Converting double spaces to SPACE+NBSP. This is a browser behavior and will thus not be fixed by the CKEditor team. It could be fixed serverside or by a clientside javascript onunload. Maybe this php is a start:

    preg_replace('/\s \s/ig', ' ', $text);
    
  4. By copy & paste. If you paste a UTF-8 no-break space or double-spaces CKEditor will convert it automatically. The only solution I see here is doing a regex as above. config.forcePasteAsPlainText = true; doesn't help.

Summary: To get rid of all no-break spaces you need to write an additional function that cleans user input.

Comments and further suggestions are greatly appreciated! (I'm using ckeditor 3.6.4)

dhaupin
  • 1,613
  • 2
  • 21
  • 24
Tapper
  • 1,393
  • 17
  • 28
  • 3
    config.fillEmptyBlocks = false; takes care of   auto-inserted into empty table cells. Thank you! – aaandre Sep 13 '12 at 17:59
  • 8
    `config.fillEmptyBlocks = false;` is Not stopping the problem for me – dsdsdsdsd Dec 19 '13 at 09:19
  • A [Webkit bug](https://bugs.webkit.org/show_bug.cgi?id=123163) when formatting or deleting mid-word, see [this CK-ticket](http://dev.ckeditor.com/ticket/9929#comment:15) for clear example-descriptions. – Yahoo Serious May 03 '16 at 09:30
  • 2
    I think perhaps your regex should be `/\s* \s*/ig` to replace more than 1 instance within a string and also to account for more scenarios of mixing space(s) with ` `. So, something like `watermelon  flavored  drink` (which can happen often) would get replaced, whereas with your regex it would not. Also the * should be used in front of each `\s` since html only renders one space anyways. – dhaupin Dec 12 '16 at 15:11
  • for my CKEditors, 'config.fillEmptyBlocks = false;' did not work but 'config.basicEntities = false;' did – Brendan Metcalfe Mar 09 '18 at 21:13
11

There is another way that a non breaking space character can occur. By simply entering a space at the end of a sentence.

CKEditor escapes basic HTML entities along with latin and greek entities.

Add these config options to prevent this (you can also add them in your config file):

CKEDITOR.on( 'instanceCreated', function( event ) {
 editor.on( 'configLoaded', function() {

  editor.config.basicEntities = false;
  editor.config.entities_greek = false; 
  editor.config.entities_latin = false; 
  editor.config.entities_additional = '';

 });
});

These options will prevent CKEditor from escaping nbsp gt lt amp ' " an other latin and greek characters.

Sources: http://docs.ckeditor.com/#!/api/CKEDITOR.config http://docs.ckeditor.com/source/plugin48.html#CKEDITOR-config-cfg-basicEntities

gabriel
  • 379
  • 4
  • 8
  • 6
    config.basicEntities = false will prevent typed HTML from being escaped and so people will simply be able to type in HTML that would otherwise be filtered / rendered harmless. (That's implicit as you said it stops gt and lt being escaped, and really you shouldn't trust any user-entered data, not even if it came via ckEditor, but thought it worth mentioning.) – Nick Rice Nov 07 '14 at 04:30
11

Try:

config.basicEntities = false;

for me fixed the problem.

Deyvid.
  • 211
  • 4
  • 10
  • @gabriel already included this in his answer 17 months before you added this. A comment (and upvote) on his answer would have been better because repeating it is redundant and wastes readers' time, not to mention that it makes it look like you didn't check other answers. – Nick Rice Nov 07 '14 at 03:55
  • 2
    See also my comment on basicEntities=false letting through arbitrary HTML on @gabriel's answer. – Nick Rice Nov 07 '14 at 04:32
  • 2
    i believe that his answer was straight to the point, and this line alone solved the problem without needless distractions – Emad Ha Mar 12 '15 at 01:12
4

in config.js:

CKEDITOR.editorConfig = function( config ) {
  config.enterMode = CKEDITOR.ENTER_BR; // <p></p> to <br />
  config.entities = false;
  config.basicEntities = false;
};

It work for me, after you can print text with php: html_entity_decode( $someText );

kampageddon
  • 1,409
  • 15
  • 13
3

I noticed some text editing operations, like deleting a character (by hitting Backspace button) are spliting edited text node into two. Hitting Space Bar at the end of such newly created text node is always resulting into &nbsp; instead of normal space. I am calling normalize() http://www.w3schools.com/jsref/met_node_normalize.asp to changed element after change:

CKEDITOR.on('instanceReady', function (ck) {
    ck.editor.on("change", function (e) {
        var sel = ck.editor.getSelection();
        if (sel) {
            var selected = sel.getStartElement();
            if (selected && selected.$)
                sel.getStartElement().$.normalize();
        }
    });
 });
joso
  • 31
  • 1
2

This is a bad solution

config.basicEntities = false;
  • Because it does not allow you to insert the JS code in the form as text. Like that <script type="text/javascript" src="/scripts/redactor/ckeditor/ckeditor.js"></script>
  • In addition, empty blocks like <p>&nbsp;</p> сan not specify an indent in the text since the character &nbsp; will be deleted (config.fillEmptyBlocks = true;)

This is the right solution

$text = preg_replace("#([^>])&nbsp;#ui", "$1 ", $text);
  • This is a PHP function that replaces all the characters &nbsp; on a space, except those that are inside the tag like <p>&nbsp;</p>
  • The function code is not the most elegant, you can suggest your own version.
0

I had the same problems creating some tables. What I saw was that if i created the tables with the css rule align="left" the <p>&nbsp;</p> are added, but if i changed the css rule to align="center" i could edit the paragraphs out and they were not added again.

0

I had already had to play around with config.js, so in order to fix '?' showing up in safari I ended up with 3 lines in config.js

config.fillEmptyBlocks = function (element) {
return true; // DON'T DO ANYTHING!!!!!};
config.entities = false;
config.basicEntities = false;
pat capozzi
  • 1,412
  • 1
  • 20
  • 16
-3

If you're using PHP you can use the following :

preg_replace("/[\<]p[\>][\s]+&nbsp;[\<][\/]p[\>]/" , " " , $pre_comment);

This will remove : "<p> &nbsp;</p>"

Enjoy :)

Maxwell

  • 10
    I don't want to downvote this answer because you're new here but please keep in mind... `1.` question is already answered correctly! (when you don't hold better answer don't answer at all, especially on old questions) `2.` this pattern contains unnecessary `[]` brackets (all of them), unnecessary escape sequences `\>`, and most probably `\<` and unnecessary `+` after `\s` (pattern `~

    \s 

    ~` looks shorter right?). `3.` if you want to be sure there's one regular space inside `P` tags, replacing `~

    \s* \s*

    ~i` case-insensitively with SPACE is better suggestion (and shorter).
    – Wh1T3h4Ck5 Sep 25 '12 at 10:04
-3

Add this to your config.js

config.enterMode = CKEDITOR.ENTER_BR,
Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
xiltepin
  • 80
  • 1
  • 7