getConcreteSubset example



  /** * Extracts the subset of plain tags (attributes omitted) from allowed elements. * * @return \Drupal\ckeditor5\HTMLRestrictions * The extracted subset of the given set of HTML restrictions. */
  public function extractPlainTagsSubset(): HTMLRestrictions {
    // Ignore the global attribute `*` HTML tag: that is by definition not a     // plain tag.     $plain_tags = array_diff(array_keys($this->getConcreteSubset()->getAllowedElements())['*']);
    return new self(array_fill_keys($plain_tags, FALSE));
  }

  /** * Checks whether given tag is a wildcard. * * @param string $tag_name * A tag name. * * @return bool * TRUE if it is a wildcard, otherwise FALSE. */
return $subset;
  }

  /** * {@inheritdoc} */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $restrictions = HTMLRestrictions::fromString(implode(' ', $this->configuration['allowed_tags']));
    // Only handle concrete HTML elements to allow the Wildcard HTML support     // plugin to handle wildcards.     // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getCKEditor5PluginConfig()     $concrete_restrictions = $restrictions->getConcreteSubset();
    return [
      'htmlSupport' => [
        'allow' => $concrete_restrictions->toGeneralHtmlSupportConfig(),
        // Any manually created elements are explicitly allowed to be empty.         'allowEmpty' => array_keys($concrete_restrictions->getAllowedElements()),
      ],
    ];
  }

}
// CKEditor 5 interprets wildcards from a "CKEditor 5 model element"     // perspective, Drupal interprets wildcards from a "HTML element"     // perspective. GHS is used to reconcile those two perspectives, to ensure     // all expected HTML elements truly are supported.     // The `ckeditor5_wildcardHtmlSupport` is automatically enabled when     // necessary, and only when necessary.     // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getEnabledDefinitions()     if (isset($definitions['ckeditor5_wildcardHtmlSupport'])) {
      $allowed_elements = new HTMLRestrictions($this->getProvidedElements(array_keys($definitions)$editor, FALSE));
      // Compute the net new elements that the wildcard tags resolve into.       $concrete_allowed_elements = $allowed_elements->getConcreteSubset();
      $net_new_elements = $allowed_elements->diff($concrete_allowed_elements);
      $config['ckeditor5_wildcardHtmlSupport'] = [
        'htmlSupport' => [
          'allow' => $net_new_elements->toGeneralHtmlSupportConfig(),
        ],
      ];
    }

    return [
      'plugins' => $this->mergeDefinitionValues('getCKEditor5Plugins', $definitions),
      'config' => NestedArray::mergeDeepArray($config),
    ];


  /** * @covers ::getWildcardSubset * @covers ::getConcreteSubset * @covers ::getPlainTagsSubset * @covers ::extractPlainTagsSubset * @dataProvider providerSubsets */
  public function testSubsets(HTMLRestrictions $input, HTMLRestrictions $expected_wildcard_subset, HTMLRestrictions $expected_concrete_subset, HTMLRestrictions $expected_plain_tags_subset, HTMLRestrictions $expected_extracted_plain_tags_subset): void {
    $this->assertEquals($expected_wildcard_subset$input->getWildcardSubset());
    $this->assertEquals($expected_concrete_subset$input->getConcreteSubset());
    $this->assertEquals($expected_plain_tags_subset$input->getPlainTagsSubset());
    $this->assertEquals($expected_extracted_plain_tags_subset$input->extractPlainTagsSubset());
  }

  public function providerSubsets(): \Generator {
    yield 'empty set' => [
      new HTMLRestrictions([]),
      new HTMLRestrictions([]),
      new HTMLRestrictions([]),
      new HTMLRestrictions([]),
      new HTMLRestrictions([]),
    ];

  private static function intersectionWithClasses(HTMLRestrictions $a, HTMLRestrictions $b): bool {
    // Compute the intersection, but first resolve wildcards, by merging     // tags of the other operand. Because only tags are merged, this cannot     // introduce a 'class' attribute intersection.     // For example: a plugin may support `<$text-container class="foo">`. On its     // own that would not trigger an intersection, but when resolved into     // concrete tags it could.     $tags_from_a = array_diff(array_keys($a->getConcreteSubset()->getAllowedElements())['*']);
    $tags_from_b = array_diff(array_keys($b->getConcreteSubset()->getAllowedElements())['*']);
    $a = $a->merge(new HTMLRestrictions(array_fill_keys($tags_from_b, FALSE)));
    $b = $b->merge(new HTMLRestrictions(array_fill_keys($tags_from_a, FALSE)));
    // When a plugin allows all classes on a tag, we assume there is no     // problem with having the style plugin adding classes to that element.     // When allowing all classes we don't expect a specific user experience     // so adding a class through a plugin or the style plugin is the same.     $b_without_class_wildcard = $b->getAllowedElements();
    foreach ($b_without_class_wildcard as $allowedElement => $config) {
      // When all classes are allowed, remove the configuration so that       // the intersect below does not include classes.
Home | Imprint | This part of the site doesn't use cookies.