5

PROBLEM: I use Drupal with a rich-editor, and the rich editor likes to clobber my text by stripping out the indentation and formatting. This would be unacceptable in a desktop editor, but people seem to tolerate this with in-browser wyswigs.

QUESTION: How do I turn this off. I've searched around and I have yet to discover the best practice way of telling the rich-editor to preserve my indentation and text formatting.

dreftymac
  • 31,404
  • 26
  • 119
  • 182

5 Answers5

3

add this configuration to the tinymce init script

tinymce.init({
    protect: [/[\n\f\r\t\v]/g]
});
mohsinulhaq
  • 1,059
  • 1
  • 18
  • 31
2

This may not be exactly what you are looking for, but the drupal htmlpurifier module can tidy up the HTML and make your HTML standard compliant. It may remove the indentation but it is very useful.

Another simpler module is htmltidy, not sure if it can preserve indentation.

1

I was searching for this same answer for TinyMCE in general, and even as of May 2015, the author(s) of TinyMCE consider whitespace stripping a feature. It cannot be disabled, even though the docs indicate the configuration option preformatted will preserve whitespace like the <pre> tag. I have yet to get it to work, or anything else, and I believe the documentation must describe a feature no longer in the code.

http://community.tinymce.com/forum/viewtopic.php?id=26021

The thread repeatedly suggests that code in a WYSIWYG is fair game for total reformatting, and a server-side reformatting is best. Nevermind that the goal is to have minimal reformatting, so a server-side tool is just as destructive to the document structure. This is a WONTFIX according to the TinyMCE developer(s), so I doubt this is going to change in the foreseeable future.

jimp
  • 16,999
  • 3
  • 27
  • 36
  • Version 3.4.5 lists `whitespace_elements` in the changelog, but I haven't found documentation for it and it doesn't appear to work either. Not sure. It's definitely in the code, but it initializes to `pre script noscript style textarea` and it's unclear to me if the user config can override it (I have tried, but perhaps I'm doing something wrong). – jimp Dec 08 '15 at 23:00
1

The reason that this is happening is the rich-editor is actually an xhtml editor. In xhtml, most tags will (with the exception of the PRE tag), by default, merge multiple whitespace characters to a single space.

To overcome this in tinyMCE, you will need to create a stylesheet containing:

* {white-space: pre;}

And reference the stylesheet as content_css when initializing the editor. Be aware that while you will be preserving your format, it will not be WYSIWYG any more, due to the nature of HTMLs whitespacing munging.

Another option is to wrap your code in a PRE element, but there is no button to do this by default in tinyMCE (or so I believe).

EDIT:

As mentioned by mikl, tinyMCE also has a configuration option for preformatted

Matt
  • 2,757
  • 19
  • 23
1

There's a “preformatted” setting in the TinyMCE prefrerences (in this case, via the WYSIWYG module, but it's there in the TinyMCE module as well). It should do the trick.

Screenshot of setting http://www.quicksnapper.com/files/3603/2949292644A2CF2A72BC21_m.png

mikl
  • 23,749
  • 20
  • 68
  • 89
  • 3
    That's definitely a pretty screenshot. Unfortunately, it does not work, at least not for me. As soon as the "rich editor" invokes on the browser textarea, the markup inside the GUI control is instantly stripped of spaces and rendered as the dreaded "single line of HTML with no whitespace preservation at all". – dreftymac Jun 08 '09 at 16:10
  • 1
    +1 I can't believe ive overlooked that preformatted configuration option before! Thank you for that gem! – Matt Jun 08 '09 at 17:50