How to fix: CKEditor is removing style attributes. Drupal 8.

Mykola Veryha
1 min readJan 26, 2020

--

How it works

CKEditor can has filters for the html. We can use existing filters for example

\Drupal\filter\Plugin\Filter

or create own custom filter. This is an example:

Html content in CKEditor always is filtering in editor_filter_xss in editor.module by secure reasons.

So if we have a filter with FilterInterface::TYPE_HTML_RESTRICTOR type then our html text will be filtered in

\Drupal\editor\EditorXssFilter\Standard::filterXss

and then filterBadProtocol method will remove our style attributes.

\Drupal\Component\Utility\UrlHelper::filterBadProtocol in core/lib/Drupal/Component/Utility/Xss.php:279

How to change it

Replace the \Drupal\editor\EditorXssFilter\Standard on our custom class.

And create the custom class for XSS filtering

\Drupal\MODULE_NAME\EditorXssFilter\StandardWithStyles

Clear drupal cache for hook_editor_xss_filter_alter.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (2)

Write a response