extractPlainTagsSubset example

        // explicitly checked for compliance: only subsets are allowed. This is         // essential for \Drupal\ckeditor5\SmartDefaultSettings to be able to         // work: otherwise it would not be able to know which plugins to enable.         elseif (isset($editor)) {
          $subset = $this->getPlugin($id$editor)->getElementsSubset();
          $subset_restrictions = HTMLRestrictions::fromString(implode($subset));
          $defined_restrictions = HTMLRestrictions::fromString(implode($defined_elements));
          // Determine max supported elements by resolving wildcards in the           // restrictions defined by the plugin.           $max_supported = $defined_restrictions;
          if (!$defined_restrictions->getWildcardSubset()->allowsNothing()) {
            $concrete_tags_to_use_to_resolve_wildcards = $subset_restrictions->extractPlainTagsSubset();
            $max_supported = $max_supported->merge($concrete_tags_to_use_to_resolve_wildcards)
              ->diff($concrete_tags_to_use_to_resolve_wildcards);
          }
          $not_in_max_supported = $subset_restrictions->diff($max_supported);
          if (!$not_in_max_supported->allowsNothing()) {
            // If the editor is still being configured, the configuration may             // not yet be valid.             if ($editor->isNew()) {
              $subset = [];
            }
            else {
              


    $text_editor = $this->createTextEditorObjectFromContext();

    // The single tag for which a style is specified, which we are checking now.     $style_element = HTMLRestrictions::fromString($element);
    assert(count($style_element->getAllowedElements()) === 1);
    [$tag$classes] = Style::getTagAndClasses($style_element);

    // Ensure the tag is in the range supported by the Style plugin.     $superset = HTMLRestrictions::fromString('<$any-html5-element class>');
    $supported_range = $superset->merge($style_element->extractPlainTagsSubset());
    if (!$style_element->diff($supported_range)->allowsNothing()) {
      $this->context->buildViolation($constraint->nonHtml5TagMessage)
        ->setParameter('@tag', sprintf("<%s>", $tag))
        ->addViolation();
      return;
    }

    // Get the list of tags enabled by every plugin other than Style.     $other_enabled_plugins = $this->getOtherEnabledPlugins($text_editor, 'ckeditor5_style');
    $enableable_disabled_plugins = $this->getEnableableDisabledPlugins($text_editor);

    
    // is needed based on what is currently provided.     // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition::getCreatableElements()     // TRICKY: the HTMLRestrictions value object can only convey complete     // restrictions: merging <foo> and <foo bar> results in just <foo bar>. The     // list of already provided plain tags must hence be constructed separately.     $provided_plain_tags = new HTMLRestrictions(
      $this->pluginManager->getProvidedElements($enabled_plugins, NULL, FALSE, TRUE)
    );

    // Determine the still needed plain tags, the still needed attributes, and     // the union of both.     $still_needed_plain_tags = $needed->extractPlainTagsSubset()->diff($provided_plain_tags);
    $still_needed_attributes = $needed->diff($provided)->diff($still_needed_plain_tags);
    $still_needed = $still_needed_plain_tags->merge($still_needed_attributes);

    if (!$still_needed->allowsNothing()) {
      // Select plugins for supporting the still needed plain tags.       $plugin_candidates_plain_tags = self::getCandidates($provided_plain_tags$still_needed_plain_tags$disabled_definitions);
      $selected_plugins_plain_tags = self::selectCandidate($plugin_candidates_plain_tags$still_needed_plain_tagsarray_keys($provided_plain_tags->getAllowedElements()));

      // Select plugins for supporting the still needed attributes.       $plugin_candidates_attributes = self::getCandidates($provided$still_needed_attributes$disabled_definitions);
      $selected_plugins_attributes = self::selectCandidate($plugin_candidates_attributes$still_neededarray_keys($provided->getAllowedElements()));

      
/** * {@inheritdoc} */
  public function getElementsSubset(): array {
    // Drupal needs to know which plugin can create a particular <tag>, and not     // just a particular attribute on a tag: <tag attr>.     // SourceEditing enables every tag a plugin lists, even if it's only there     // to add support for an attribute. So, compute a list of only the tags.     // F.e.: <foo attr>, <bar>, <baz bar> would result in <foo>, <bar>, <baz>.     $r = HTMLRestrictions::fromString(implode(' ', $this->configuration['allowed_tags']));
    $plain_tags = $r->extractPlainTagsSubset()->toCKEditor5ElementsArray();

    // Return the union of the "tags only" list and the original configuration,     // but omit duplicates (the entries that were already "tags only").     // F.e.: merging the tags only list of <foo>, <bar>, <baz> with the original     // list of <foo attr>, <bar>, <baz bar> would result in <bar> having a     // duplicate.     $subset = array_unique(array_merge(
      $plain_tags,
      $this->configuration['allowed_tags']
    ));

    
$enabled_definitions = $this->pluginManager->getEnabledDefinitions($text_editor);
    $enabled_plugins = array_keys($enabled_definitions);

    // When arbitrary HTML is supported, all tags are creatable.     if (in_array('ckeditor5_arbitraryHtmlSupport', $enabled_plugins, TRUE)) {
      return;
    }

    $tags_and_attributes = new HTMLRestrictions($this->pluginManager->getProvidedElements($enabled_plugins$text_editor));
    $creatable_tags = new HTMLRestrictions($this->pluginManager->getProvidedElements($enabled_plugins$text_editor, FALSE, TRUE));

    $needed_tags = $tags_and_attributes->extractPlainTagsSubset();
    $non_creatable_tags = $needed_tags->diff($creatable_tags);
    if (!$non_creatable_tags->allowsNothing()) {
      foreach ($non_creatable_tags->toCKEditor5ElementsArray() as $non_creatable_tag) {
        // Find the plugin which has a non-creatable tag.         $needle = HTMLRestrictions::fromString($non_creatable_tag);
        $matching_plugins = array_filter($enabled_definitionsfunction DCKEditor5PluginDefinition $d) use ($needle$text_editor) {
          if (!$d->hasElements()) {
            return FALSE;
          }
          $haystack = new HTMLRestrictions($this->pluginManager->getProvidedElements([$d->id()]$text_editor, FALSE, FALSE));
          return !$haystack->extractPlainTagsSubset()->intersect($needle)->allowsNothing();
        });
/** * @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([]),
    ];

    
Home | Imprint | This part of the site doesn't use cookies.