Problem/Motivation

Cannot set the width or height of table cells because CKeditor uses inline CSS styles, and Drupal strips these styles.

Steps to reproduce

  1. Using the CKeditor interface, create a table with two columns inside a formatted text field, such as node' body field.
    The text format used for this field should be cores' bundled 'Basic HTML', or any custom text format using ' Limit allowed HTML tags and correct faulty HTML' filter.
  2. Right click one of the cells, go to Cell -> Cell Properties [Figure].
  3. Set the width and or height field to any integer value, then press OK in the modal [figure].
  4. Take note: use the CKeditor 'Source' function to inspect the code. The width or height will be set using inline css (<td style="width: 123px">)
  5. Save the entity.

Result: the inline CSS will be stripped by Drupal.

Proposed resolution

Use width or height properties of the table cell tag? eg: <td width="123px"> Deprecated in HTML5: Specification

Allow inline CSS for instances where CKeditor uses it.

Remaining tasks

User interface changes

API changes

CommentFileSizeAuthor
#21 table-styles.gif1.29 MBidebr
#2 2785483_1.jpg150.35 KBdpi
#2 2785483_2.jpg88.76 KBdpi

Comments

dpi created an issue. See original summary.

dpi’s picture

Issue summary: View changes
StatusFileSize
new88.76 KB
new150.35 KB

Added screens

dpi’s picture

Issue summary: View changes

width/height attributes on <td> is deprecated

wim leers’s picture

Title: Cannot set cell width or height because inline styles are stripped » [upstream] Cannot set cell width or height because inline styles are stripped
Assigned: Unassigned » mlewand
Issue tags: +Needs upstream bugfix

Thanks for the bug report! It looks like CKEditor's Table plugin is not fully integrated with its Advanced Content Filter. The Basic HTML text format doesn't allow those attributes, so the CKEditor Table plugin shouldn't show those in its dialogs.

Assigning to the CKEditor team.

Tade0’s picture

Trying to reproduce this bug I created my own text format(since Basic HTML doesn't have tables, and Full HTML does not reproduce it) which differed from Basic HTML only by having the table plugin.

I received a notification, that based on the plugin set I had chosen a few entries in the Allowed HTML tags were added. This configuration reproduced the bug, but when I modified the list of allowed tags so that it included <td style> everything worked fine.

How are these additional automatic entries determined?

wim leers’s picture

#5: it looks at the requiredContent of a CKEditor plugin, and then whitelists that automatically.

And that's the whole point: if only <td> is whitelisted, then it'll mean that allowedContent will only allow <td>, not any classes, not any styles, not any attributes at all. Then the CKEditor Table plugin should respect that, but it does not.

wim leers’s picture

wim leers’s picture

See #1894644-2: Unidirectional editor configuration -> filter settings syncing for the history of how this was built, in tandem with the CKEditor team :)

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

saikatewu@gmail.com’s picture

Does anyone have any solution for this? I need to set width for td.

@Tade0 I added style to allowed tags attribute list for td. After that Ckeditor doesn't strip it out if you switch between source and preview mode. But after saving the content and editing it strips it out.

wim leers’s picture

@saikatewu Perhaps the upstream bug has been fixed. Did you whitelist the necessary attributes in the "Allowed HTML tags" filter on the Drupal side?

saikatewu@gmail.com’s picture

Yes, I did. Still same issue.

karenann’s picture

I am having the same issue.

Having updated to 8.3.4 and whitelisted "style" in the "Limit allowed HTML tags and correct faulty HTML", I do observe that the style is no longer stripped when I toggle from source to wysiwyg, which was an issue before.

However, when I save, the style attribute is still stripped.

karenann’s picture

As an addendum, I changed the allowance in "Limit allowed HTML tags and correct faulty HTML" to <td border cellpadding cellspacing colspan rowspan style> and confirmed that only "style" is still stripped. The other 5 attributes maintain integrity.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Status: Active » Postponed
mlewand’s picture

Fix issue landed to our code base, and is scheduled to be release with version 4.8.0 🎉.

wim leers’s picture

Great news! 👍 ❤️

manauwarsheikh’s picture

Awesome!

Would love to see :)

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idebr’s picture

Title: [upstream] Cannot set cell width or height because inline styles are stripped » [upstream?] Cannot set cell width or height because inline styles are stripped
Status: Postponed » Active
StatusFileSize
new1.29 MB

This issue was reportedly fixed in CKEditor 4.8.0 in https://github.com/ckeditor/ckeditor-dev/issues/1014

CKEditor 4.8.0 was added to Drupal in #2926932: Update CKEditor library to 4.8.0

However, style properties on table cells (th, td) are still not preserved when saving. Should investigate if this is because of the implementation in Drupal or in CKEditor.

Screencapture:

Filter settings 'Allowed HTML tags':
<a href hreflang id name data-entity-type data-entity-uuid data-entity-substitution> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <br> <span> <img src alt height width data-entity-type data-entity-uuid data-entity-substitution data-align data-caption> <p> <table><summary> <caption> <tbody> <thead> <tfoot> <th colspan rowspan style> <td colspan rowspan style> <tr> <p class></p>

idebr’s picture

Title: [upstream?] Cannot set cell width or height because inline styles are stripped » [upstream] Cannot set cell width or height because inline styles are stripped

It turns out this issue consists of bugs in both Drupal and CKEditor:

  1. Drupal tells the CKEditor the style attribute is allowed in allowedContent, while these are always filtered when the 'Limit allowed HTML tags and correct faulty HTML' filter is enabled.
  2. If Drupal tells CKEditor the style attribute is now allowed, several table 'Cell properties' can still be added through the UI in the CKEditor such as 'Vertical-align' and 'Background color'.

I will file a new issue for #1, so the scope of this issue is contained to #2.

idebr’s picture

johnpitcairn’s picture

I think the scope of this issue is actually #1, no?

Looks like \Drupal\filter\Plugin\Filter\FilterHtml will always strip out style attributes, possibly as a result of passing the filtered text through Xss::filter().

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xem8vfdh’s picture

This problem also affects floating the cell content... really all cell property settings, I think.

permanaj’s picture

How to allow td and th tag to have style attributes in Xss::filter?

I'm thinking to copy the Limit allowed HTML tags and correct faulty HTML filter into my custom module and modify to allow td and th to have style tag. But I'm not sure what to pass to Xss::filter.

I tried to add

$restrictions['allowed']['td'] = [
      'style' => true,
      'on*' => false,
      'lang' => true,
      'dir' => ['ltr' => true, 'rtl' => true],
    ];
    $restrictions['allowed']['th'] = [
      'style' => true,
      'on*' => false,
      'lang' => true,
      'dir' => ['ltr' => true, 'rtl' => true],
    ];

below $restrictions['allowed']['*'] = [ .. but doesn't seems to work.

rgpublic’s picture

@permanaj: If you allow style attributes you allow XSS attacks. You can switch off the XSS filter completely then, because it will become almost useless. See e.g.: https://stackoverflow.com/questions/4546591/xss-attacks-and-style-attrib...

dsnopek’s picture

CKEditor has a way of saying which style properties are allowed:

https://ckeditor.com/docs/ckeditor4/latest/guide/dev_allowed_content_rul...

It just seems that Drupal only allows us to say attributes. If we could do something like td{width,height} (notice the curly braces rather than square brackets) then it should only allow those particular CSS properties.

We'd also need to be able to tell the Drupal input filter that those style attributes are allowed as well.

rgpublic’s picture

@dsnopek: Yeah, I think your solution is the way to go. Of course it's quite difficult to fully and securely support all CSS. But I think the naive approach (explode by ";" and ":") should already be sufficient. We then get a key-value array we can filter by the allowed keys mentioned in the curly braces. Values should strictly be alphanumerical only. No parentheses, no calc(), no nothing. Anything else will be deleted - even if we allowed the key in the first place. The final result will be concatenated together. I think this method will be quite safe because you cannot do monkey-business with it.

recrit’s picture

The issue with allowing style tags has been noted above for XSS reasons. If you were to allow style tags within CKEditor and then saved the node, then the style attributes could still be stripped when editing again since Standard::filterXss() would be called if you were using the filter_html filter ("Limit allowed HTML tags and correct faulty HTML").

adamfranco’s picture

While I appreciate the difficulty of exhaustively filtering all possibly-safe CSS attributes, there is a significant subset of simple properties and values (e.g. width: 25%) that could be safely allowed without risk of XSS. I've added #3109650: Refactor Xss::attributes() to allow filtering of style attribute values to refactor the filtering code enough to allow filtering to be easier to implement as right now the attribute filtering is tough to extend. My proof-of-concept may be sufficient to block XSS attacks, but certainly it could be expanded upon to white-list just certain properties and a limited set of values for those properties.

xem8vfdh’s picture

thanks @adamfranco!

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jptillman’s picture

I'm seeing the originally reported behavior (style attributes getting stripped from table cells), as well, right now (D8.8.5) and I don't even have "Limit allowed HTML tags and correct faulty HTML" enabled.

Has anyone really figured out what the true cause(s) of this is/are? I can't identify what still remains as the source of the problem by reading through all these comments in this issue.

I'd really appreciate some guidance on how to actually add size attributes to a table cell via WYSIWYG and NOT have them get stripped by something.

johnpitcairn’s picture

@jptillman: at present, you can't using stock Drupal, because Drupal strips all style attributes for security, to prevent XSS attacks. See #3109650: Refactor Xss::attributes() to allow filtering of style attribute values for a possible patch and custom module solution.

xem8vfdh’s picture

sounds like there should be official drupal approved css classes to handle this

fabianderijk’s picture

I've tried the patch as mentioned in #36. This works in ckeditor, but my style attributes are still stripped from the output unfortunately.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Project: Drupal core » CKEditor 4 - WYSIWYG HTML editor
Version: 9.4.x-dev » 1.0.x-dev
Component: ckeditor.module » Code

CKEditor has been removed from core, CKEditor 4 is removed from Drupal Core in 10.0.0