1

On a Drupal 8 installation, with CKEditor enabled for Full HTML format, when I paste text from Word with cmd+V, the source code includes format that makes the code dirty.

Example:

<p><span style="font-size:12pt"><span style="line-height:115%"><span style="font-family:Helvetica">Lorem ipsum dolor.</span></span></span></p>

How can I force it to clean the code from Word?

3 Answers 3

0

Two options:

  • there is a special "Paste from Word"-button in CKeditor, add it to your toolbar
  • limit allowed HTML tags, it will strip unwanted tags attributes when saving the node (by default spans are allowed, but style attributes will be removed by default)

Both can be done in Configuration > Content authoring > Text formats and editors

2
  • 1
    "limit allowed HTML tags" worked for me, thanks @Hudri. "Paste from Word"-button dowsn't work in some browsers, for example in Chrome for Mac shows the error: "Your browser doesn't allow you to paste this way. Press ⌘+V to paste."
    – jmzea
    Oct 20, 2017 at 8:16
  • Yes, I do get the same message when pasting in chrome for windows, but it did work in IE
    – Hudri
    Oct 20, 2017 at 9:00
2

For reference, there was a backwards-compatibility break in CKEditor v4.6 - it removed some of its own paste-from-Word button's cleaning. See this discussion on the Drupal side: https://www.drupal.org/project/drupal/issues/2871539

Note comment #6 there offers another workaround - responding to the afterPasteFromWord CKEditor event in some custom Javascript and filtering exactly as desired.

2

This answer is only for CKEditor in Drupal. I can recommend two modules to clean up pasted text (not only from Word) into the CKEditor:

CKEditor Paste Filter. This module works out of the box without further settings.

Paste Format (filter, clean-up pasted text). This module has no stable release (Feb 2022) for Drupal 8/9, but the linked port to Drupal 8/9 works quite well. Nevertheless, I prefer it to the first module, because it works by using a "normal" textformat like "Plain Text". Because of that you have the opportunity to further tweek and restrict the pasted text. I am using a custom textformat "Pasteformat" with "Convert line breaks into HTML" and "Limit allowed HTML tags and correct faulty HTML" enabled for the following tags:

<a href title> <i> <b> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6><br><p> <table> <th> <tr> <td> <thead> <tbody> <tfoot>

For "Paste Format" i also recommend the installation of the following modules and enabling them in your textformat for pasting text:

No Non-breaking Space Filter to remove &nbsp; (has to be the first filter in the textformat).

Filter Empty Tags to remove empty tags and lines (last filter in the textformat).

After all i can safely give the website to customers which want to paste text from word into the editor – without the risk of breaking design and typography. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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