Commit 5d632f06 authored by alexpott's avatar alexpott

Issue #2773085 by Sam152, Wim Leers: CKEditor doesn't show elements in the...

Issue #2773085 by Sam152, Wim Leers: CKEditor doesn't show elements in the styles dropdown if an input filter alters them
parent 8c22ddf5
......@@ -5,6 +5,7 @@
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginContextualInterface;
use Drupal\ckeditor\CKEditorPluginManager;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -369,7 +370,7 @@ protected function generateFormatTagsSetting(Editor $editor) {
foreach ($possible_format_tags as $tag) {
$input = '<' . $tag . '>TEST</' . $tag . '>';
$output = trim(check_markup($input, $editor->id()));
if ($input == $output) {
if (Html::load($output)->getElementsByTagName($tag)->length !== 0) {
$format_tags[] = $tag;
}
}
......
......@@ -5,3 +5,12 @@ ckeditor.plugin.llama_contextual_and_button:
ultra_llama_mode:
type: boolean
label: 'Ultra llama mode'
filter_settings.test_attribute_filter:
type: filter
label: 'Test Attribute Filter'
mapping:
tags:
type: sequence
sequence:
type: string
<?php
namespace Drupal\ckeditor_test\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
/**
* A filter that adds a test attribute to any configured HTML tags.
*
* @Filter(
* id = "test_attribute_filter",
* title = @Translation("Test Attribute Filter"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
* settings = {
* "tags" = {},
* },
* weight = -10
* )
*/
class TestAttributeFilter extends FilterBase {
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$document = Html::load($text);
foreach ($this->settings['tags'] as $tag) {
$tag_elements = $document->getElementsByTagName($tag);
foreach ($tag_elements as $tag_element) {
$tag_element->setAttribute('test_attribute', 'test attribute value');
}
}
return new FilterProcessResult(Html::serialize($document));
}
}
<?php
namespace Drupal\Tests\ckeditor\Kernel\Plugin\CKEditorPlugin;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\ckeditor\Plugin\CKEditorPlugin\Internal
*
* @group ckeditor
*/
class InternalTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'ckeditor',
'ckeditor_test',
'filter',
'editor',
];
/**
* A testing text format.
*
* @var \Drupal\filter\Entity\FilterFormat
*/
protected $format;
/**
* A testing text editor.
*
* @var \Drupal\editor\Entity\Editor
*/
protected $editor;
/**
* The CKEditor plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $ckeditorPluginManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('editor');
$this->installEntitySchema('filter_format');
$this->format = FilterFormat::create([
'format' => 'test_format',
'name' => $this->randomMachineName(),
]);
$this->format->save();
$this->editor = Editor::create([
'editor' => 'ckeditor',
'format' => 'test_format',
'settings' => [
'toolbar' => [
'rows' => [
[
[
'name' => 'Enabled Buttons',
'items' => [
'Format',
],
],
],
],
],
],
]);
$this->editor->save();
$this->ckeditorPluginManager = $this->container->get('plugin.manager.ckeditor.plugin');
}
/**
* Test the format tags settings.
*
* @dataProvider formatTagsSettingsTestCases
*/
public function testFormatTagsSettings($filter_plugins, $expected_format_tags) {
foreach ($filter_plugins as $filter_plugin_id => $filter_plugin_settings) {
$this->format->setFilterConfig($filter_plugin_id, $filter_plugin_settings);
}
$this->format->save();
$internal_plugin = $this->ckeditorPluginManager->createInstance('internal', []);
$plugin_config = $internal_plugin->getConfig($this->editor);
$this->assertEquals($expected_format_tags, explode(';', $plugin_config['format_tags']));
}
/**
* A data provider for testFormatTagsSettings.
*/
public function formatTagsSettingsTestCases() {
$all_tags = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre'];
return [
'No filter plugins enabled (all tags allowed)' => [
[],
$all_tags,
],
'HTML filter plugin enabled (some tags filtered out)' => [
[
'filter_html' => [
'status' => 1,
'settings' => [
'allowed_html' => '<h1> <h2>',
'filter_html_help' => 1,
'filter_html_nofollow' => 0,
],
],
],
['p', 'h1', 'h2'],
],
'Test attribute filter enabled (all tags allowed)' => [
[
'test_attribute_filter' => [
'status' => 1,
'settings' => [
'tags' => ['h1', 'h2'],
],
],
],
$all_tags,
],
];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment