Commit cd4d856a authored by Ben Mullins's avatar Ben Mullins
Browse files

Issue #3228334 by Wim Leers, larowlan, lauriii, DieterHolvoet,...

Issue #3228334 by Wim Leers, larowlan, lauriii, DieterHolvoet, beatrizrodrigues: Refactor HTMLRestrictionsUtilities to a HtmlRestrictions value object
parent 3603f878
Loading
Loading
Loading
Loading
+852

File added.

Preview size limit exceeded, changes collapsed.

−284

File deleted.

+4 −5
Changes for core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/SourceEditing.php: 4 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@

namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;

use Drupal\ckeditor5\HTMLRestrictionsUtilities;
use Drupal\ckeditor5\HTMLRestrictions;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableTrait;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface;
@@ -43,7 +43,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
    // Match the config schema structure at ckeditor5.plugin.ckeditor5_heading.
    $form_value = $form_state->getValue('allowed_tags');
    if (!is_array($form_value)) {
      $config_value = HTMLRestrictionsUtilities::allowedElementsStringToPluginElementsArray($form_value);
      $config_value = HTMLRestrictions::fromString($form_value)->toCKEditor5ElementsArray();
      $form_state->setValue('allowed_tags', $config_value);
    }
  }
@@ -75,11 +75,10 @@ public function getElementsSubset(): array {
   * {@inheritdoc}
   */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $allowed = HTMLRestrictionsUtilities::allowedElementsStringToHtmlSupportConfig(implode('', $this->configuration['allowed_tags']));

    $restrictions = HTMLRestrictions::fromString(implode(' ', $this->configuration['allowed_tags']));
    return [
      'htmlSupport' => [
        'allow' => $allowed,
        'allow' => $restrictions->toGeneralHtmlSupportConfig(),
      ],
    ];
  }
+4 −4
Changes for core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@

namespace Drupal\ckeditor5\Plugin;

use Drupal\ckeditor5\HTMLRestrictionsUtilities;
use Drupal\ckeditor5\HTMLRestrictions;
use Drupal\Component\Assertion\Inspector;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
@@ -140,11 +140,11 @@ private function validateDrupalAspects(string $id, array $definition): void {
        if ($definition['id'] === 'ckeditor5_sourceEditing') {
          continue;
        }
        $parsed_elements = HTMLRestrictionsUtilities::allowedElementsStringToPluginElementsArray($element);
        if (count($parsed_elements) === 0) {
        $parsed = HTMLRestrictions::fromString($element);
        if ($parsed->isEmpty()) {
          throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a value at "drupal.elements.%d" that is not an HTML tag with optional attributes: "%s". Expected structure: "<tag allowedAttribute="allowedValue1 allowedValue2">".', $id, $index, $element));
        }
        elseif (count($parsed_elements) > 1) {
        if (count($parsed->getAllowedElements()) > 1) {
          throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a value at "drupal.elements.%d": multiple tags listed, should be one: "%s".', $id, $index, $element));
        }
      }
+6 −30
Changes for core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php: 6 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
namespace Drupal\ckeditor5\Plugin;

use Drupal\ckeditor5\Annotation\CKEditor5Plugin;
use Drupal\ckeditor5\HTMLRestrictionsUtilities;
use Drupal\ckeditor5\HTMLRestrictions;
use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
use Drupal\Component\Assertion\Inspector;
use Drupal\Component\Utility\NestedArray;
@@ -263,13 +263,12 @@ public function getCKEditor5PluginConfig(EditorInterface $editor): array {
  /**
   * {@inheritdoc}
   */
  public function getProvidedElements(array $plugin_ids = [], EditorInterface $editor = NULL, bool $retain_wildcard = FALSE): array {
  public function getProvidedElements(array $plugin_ids = [], EditorInterface $editor = NULL): array {
    $plugins = $this->getDefinitions();
    if (!empty($plugin_ids)) {
      $plugins = array_intersect_key($plugins, array_flip($plugin_ids));
    }
    $elements = [];
    $processed_elements = [];
    $elements = HTMLRestrictions::emptySet();

    foreach ($plugins as $id => $definition) {
      // Some CKEditor 5 plugins only provide functionality, not additional
@@ -308,35 +307,12 @@ public function getProvidedElements(array $plugin_ids = [], EditorInterface $edi
      }
      assert(Inspector::assertAllStrings($defined_elements));
      foreach ($defined_elements as $element) {
        if (in_array($element, $processed_elements)) {
          continue;
        }
        $processed_elements[] = $element;
        $additional_elements = HTMLRestrictionsUtilities::allowedElementsStringToHtmlFilterArray($element);
        $elements = array_merge_recursive($elements, $additional_elements);
      }
    }

    foreach ($elements as $tag_name => $tag_config) {
      if (substr($tag_name, 0, 1) === '$') {
        $wildcard_tags = HTMLRestrictionsUtilities::getWildcardTags($tag_name);
        foreach ($wildcard_tags as $wildcard_tag) {
          if (isset($elements[$wildcard_tag])) {
            foreach ($tag_config as $attribute_name => $attribute_value) {
              if (is_array($attribute_value)) {
                $attribute_value = array_keys($attribute_value);
              }
              HTMLRestrictionsUtilities::addAllowedAttributeToElements($elements, $wildcard_tag, $attribute_name, $attribute_value);
            }
          }
        }
        if (!$retain_wildcard) {
          unset($elements[$tag_name]);
        }
        $additional_elements = HTMLRestrictions::fromString($element);
        $elements = $elements->merge($additional_elements);
      }
    }

    return HTMLRestrictionsUtilities::cleanAllowedHtmlArray($elements);
    return $elements->getAllowedElements();
  }

  /**
Loading