开发者

Drupal WYSIWYG removing &nbsp automatically

When pasting content from WORD a lot of the markup is being cleared up by using "Force cleanup on standard paste" using Ti开发者_C百科nyMCE and WYSIWYG module. It seems however to leave in the following code:

<p>&nbsp;</p>

Is there any way so that I can filter this out?


The empty p tags were formerly placed to denote a change of line, however drupal wysiwyg automatically adds a non-breaking space tag which thereby fails to display properly and thus be replaced with the character replacement "?"

Working Solution:

$to_filter = array('bloc_1_delta', 'bloc_2_delta', 'bloc_3_delta', 'bloc_4_delta');
if (in_array($vars['block']->delta, $to_filter)) {
    $vars['block']->content = str_replace("<p></p>", "<br/>", mb_cοnvert_encoding($vars['block']->content, 'HTML-ENTITIES', 'UTF-8'));
}


Are you sure that it's caused by TinyMCE and WYSIWYG? It can also be the combination of a setting in WYSIWYG (when "Remove linebreaks" is off) and your input filter settings ("Line break converter" is on).


As far as i know does tinymce have a clean code function. When this one is called it should remove the empty paragraphs.

But you should have to try this...


This happens in almost every js WYSIWYG. Altering the "paste from word" behavior specifically would involve patching or extending the timymce/wysiwyg javascript, but if you don't care where empty <p> tags come from and want to get rid of every one that is submitted, you have some options.

[edited my answer after I had to do this recently again myself]

If you need a lot of html correction/rewriting you probably should look at the HTML Purifier module. It has an advanced option to remove empty and nbsp filled HTML elements, along with correction and xss filtering. But it's slow.

There's the Empty Paragraph Killer module, but it doesn't work for D7 right now so you can skip that.

The most direct solution is to write your own input filter in a custom module. There's an example filter module here you can copy and learn from. Or you can just copy mine:

/* Implements hook_filter_info(). */
function YOURMODULE_filter_info() {

  $filters['kill_empty'] = array(
    'title' => t('Kill Empty Paragraphs'),
    'description' => t('Remove paragraphs that contain only whitespace (including line breaks and &amp;nbsp;\'s) that are often inserted by editors using WYSIWYGs.'),
    'process callback'  => '_YOURMODULE_kill_empty',
    'tips callback' => '_YOURMODULE_kill_empty_tips',
  );
  return $filters;
}

/* Process callbacks, where the work is done. */
function _YOURMODULE_kill_empty($text, $filter) {
  // Remove all <p> tags containing only nbsp's, white space, or nothing.
  return preg_replace('/<p[^>]*>(&nbsp;|\s)*<\/p>/', '', $text);
}

/* Tips for the content editor, if you want them. I usually take these out. */
function _YOURMODULE_kill_empty_tips($filter, $format, $long = FALSE) {
  if (!$long) {
    // This string will be shown in the content add/edit form.
    return t('Use one [enter] to create a new paragraph. More than one will be ignored.');
  }
  else {
    // And this one on the "Filter Tips" page.
    return t('To maintain consistancy in content display, empty paragraphs inserted by WYSIWYG editors will be removed.');
  }
}

Put all of that in a custom module, go to configuration and edit your text formats, and activate the "Kill Empty Paragraphs" filter. That should be it.

Last note is that if you really want them gone, like not-in-the-database, will-never-come-back gone, you should be able to use this filter with the Sanitizable module to remove all of the empty <p>s on submit. The usual warnings about messing with content before it goes into the database apply.


You don't always need the above hacks. as marcvangend mentioned but in plainer english you can change it in settings.

  1. Go to drupal administer
  2. Site configuration
  3. Input formats
  4. configure the input format of your choice
  5. unselect "Line break converter"


A quick and easy way is just to put something like this in your theme's template.php:

<?php
function THEMENAME_preprocess_node(&$vars, $hook) {
    $vars['content'] = str_replace('<p>&nbsp;</p>','', $vars['content']);
}

That just does a quick find and replace to replace the empty paragraph with nothing. It's sort of a hack, but it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜