4

I'm using CKEditor 4.2 with D7 and the editor is replacing every space with an HTML non-breaking space. For instance, if I type: This is a paragraph and then either save or view source, I'll get the following:

<p>This&nbsp;is&nbsp;a&nbsp;paragraph.</p>

I don't have any HTML correctors/prettifiers running as filters and I'm banging my head against the wall trying to figure out what's causing this.

Oh, I also have Convert all applicable characters to HTML entities. unchecked, so that's not it.

Has anyone seen this before? Any solutions out there?

  • As far as I know by default it does no such thing, so the question is - how did it started? – Mołot Dec 16 '13 at 7:43
  • Great question. I have not added any plugins to the editor or changed the configuration significantly. I did tweak ckeditor.config.js to set the height, but that's it. This did just start happening, so I'll back that out, but I have a hard time believeing that's it. – DeepFriedBits Dec 16 '13 at 7:54
  • 1
    Backed out my changes to no avail. This is frustrating. – DeepFriedBits Dec 16 '13 at 8:08
5

I solve this with a text filter

/**
 * Implements hook_filter_info().
 */
function MYMODULE_filter_info() {
  $filters = array();

  $filters['kill_nbsp'] = array(
    'title' => t('Kill Non-Breaking Spaces'),
    'description' => t('Remove non-breaking spaces.'),
    'process callback' => '_MYMODULE_filters_kill_nbsp',
    'tips callback' => '_MYMODULE_filters_kill_nbsp_tips',
  );

  return $filters;
}

/**
 * Filter callbacks.
 */
function _MYMODULE_filters_kill_nbsp($text, $filter) {
  return preg_replace('/&nbsp;/', ' ', $text);
}

/**
 * Filter tips.
 */
function _MYMODULE_filters_kill_nbsp_tips($filter, $format, $long = FALSE) {
  if (!$long) {
    // This string will be shown in the content add/edit form.
    return t('Don\'t use non-breaking spaces.  Ever.');
  }
  else {
    // And this one on the "Filter Tips" page.
    return t('Non-breaking spaces are evil and will be eradicated.');
  }
}

The code simply searches for non-breaking spaces and replaces them with normal spaces.

Stuff this in a custom module, and then assign the filter to the input formats that have WYSIWYG enabled. This does assume that you never want actual non-breaking spaces in your output, which is good for my sites.

  • As it turns out, I had broken CKEditor trying to tweak its event model, so that was causing my spaces issue, but your answer works. Thank you. – DeepFriedBits Dec 17 '13 at 21:01
  • Would this code work for Drupal 8? – MrPaulDriver Jun 24 at 18:37
  • @MrPaulDriver Copy/paste into Drupal 8, no. Filters are now plugins, drupal.org/docs/8/api/filter-api/overview – mpdonadio Jun 24 at 22:08
0

Try this it will remove leading white spaces for ckeditor

var val = CKEDITOR.instances.[name_of_field].getData();//get data

val = $(val).text().trim();//remove white spaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.