Skip to content

Instantly share code, notes, and snippets.

@Mykola-Veryha
Last active January 26, 2020 15:40
Show Gist options
  • Save Mykola-Veryha/6573f915c8d3702a265c75e04883f823 to your computer and use it in GitHub Desktop.
Save Mykola-Veryha/6573f915c8d3702a265c75e04883f823 to your computer and use it in GitHub Desktop.
EditorXssFilter with styles. Drupal 8.
<?php
namespace Drupal\MODULE_NAME\EditorXssFilter;
use Drupal\Component\Utility\Html;
use Drupal\editor\EditorXssFilter\Standard;
/**
* Defines the standard text editor with styles XSS filter.
*/
class StandardWithStyles extends Standard {
/**
* Processes a string of HTML attributes.
*
* @param string $attributes
* The html attribute to process.
*
* @return array
* Cleaned up version of the HTML attributes.
*/
protected static function attributes($attributes) {
/** @var array $attributes_array */
$attributes_array = parent::attributes($attributes);
if (preg_match('/^([-a-zA-Z][-a-zA-Z0-9]*)/', $attributes, $match)) {
$attribute_name = strtolower($match[1]);
if ($attribute_name === 'style') {
$html_dom = Html::load("<div " . $attributes . "></div>");
$div_tags = $html_dom->getElementsByTagName('div');
/** @var \DOMElement $div_tag */
foreach ($div_tags as $div_tag) {
if ($div_tag->hasAttribute('style')){
$attributes_array[] = 'style="' . $div_tag->getAttribute('style') . '"';
}
}
}
}
return $attributes_array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment