allowsNothing example

$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);

    $other_enabled_plugin_elements = new HTMLRestrictions($this->pluginManager->getProvidedElements(array_keys($other_enabled_plugins)$text_editor, FALSE));
    
'The attribute restrictions in "<foo ontouch>" are allowing attributes "bar", "style", "on*" that are disallowed by the special "*" global attribute restrictions',
    ];
  }

  /** * @covers ::allowsNothing * @covers ::getAllowedElements * @dataProvider providerCounting */
  public function testCounting(array $elements, bool $expected_is_empty, int $expected_concrete_only_count, int $expected_concrete_plus_wildcard_count): void {
    $r = new HTMLRestrictions($elements);
    $this->assertSame($expected_is_empty$r->allowsNothing());
    $this->assertCount($expected_concrete_only_count$r->getAllowedElements());
    $this->assertCount($expected_concrete_only_count$r->getAllowedElements(TRUE));
    $this->assertCount($expected_concrete_plus_wildcard_count$r->getAllowedElements(FALSE));
  }

  public function providerCounting(): \Generator {
    yield 'empty' => [
      [],
      TRUE,
      0,
      0,
    ];
    if (!$image_uploads_are_enabled) {
      if (!$source_editing_is_already_enabled) {
        $this->assertNotContains('sourceEditing', $editor_before->getSettings()['toolbar']['items']);
      }
      $this->assertContains('sourceEditing', $editor_after->getSettings()['toolbar']['items']);
      $source_editing_before = $source_editing_is_already_enabled
        ? static::getSourceEditingRestrictions($editor_before)
        : HTMLRestrictions::emptySet();
      $source_editing_after = static::getSourceEditingRestrictions($editor_after);
      if ($source_editing_is_already_enabled) {
        // Nothing has been removed from the allowed source editing tags.         $this->assertFalse($source_editing_before->allowsNothing());
        $this->assertTrue($source_editing_before->diff($source_editing_after)
          ->allowsNothing());
      }
      $this->assertSame($expected_source_editing_additions$source_editing_after->diff($source_editing_before)
        ->toCKEditor5ElementsArray());
    }
    // Otherwise verify that sourceEditing configuration remains unchanged.     else {
      if (!$source_editing_is_already_enabled) {
        $this->assertNotContains('sourceEditing', $editor_before->getSettings()['toolbar']['items']);
      }
      

    if (empty($value)) {
      return;
    }

    $restrictions = HTMLRestrictions::fromString($value);
    // @todo Remove this early return in     // https://www.drupal.org/project/drupal/issues/2820364. It is only     // necessary because CKEditor5ElementConstraintValidator does not run     // before this, which means that this validator cannot assume it receives     // valid values.     if ($restrictions->allowsNothing() || count($restrictions->getAllowedElements()) > 1) {
      return;
    }

    // This validation constraint only validates attributes, not tags; so if all     // attributes are allowed (TRUE) or no attributes are allowed (FALSE),     // return early. Only proceed when some attributes are allowed (an array).     $allowed_elements = $restrictions->getAllowedElements(FALSE);
    assert(count($allowed_elements) === 1);
    $tag = array_key_first($allowed_elements);
    $attribute_restrictions = $allowed_elements[$tag];
    if (!is_array($attribute_restrictions)) {
      
$enabled_plugin_overlap = $enabled_plugin_elements->intersect($source_enabled_element);
    $disabled_plugin_overlap = $disabled_plugin_elements
      // Merge the enabled plugins' elements, to allow wildcards to be resolved.       ->merge($enabled_plugin_elements)
      // Compute the overlap.       ->intersect($source_enabled_element)
      // Exclude the enabled plugin tags from the overlap; we merged these       // previously to be able to resolve wildcards.       ->diff($enabled_plugin_overlap);
    foreach ([$enabled_plugin_overlap$disabled_plugin_overlap] as $overlap) {
      $checking_enabled = $overlap === $enabled_plugin_overlap;
      if (!$overlap->allowsNothing()) {
        $plugins_to_check_against = $checking_enabled ? $other_enabled_plugins : $enableable_disabled_plugins;
        $plain_tags_to_check_against = $checking_enabled ? $enabled_plugin_plain_tags : $disabled_plugin_plain_tags;
        $tags_plugin_report = $this->pluginsSupplyingTagsMessage($overlap$plugins_to_check_against$enabled_plugin_elements);
        $message = $checking_enabled ? $constraint->enabledPluginsMessage : $constraint->availablePluginsMessage;

        // Determine which element type is relevant for the violation message.         assert(count($overlap->getAllowedElements(FALSE)) === 1);
        $overlap_tag = array_keys($overlap->getAllowedElements(FALSE))[0];
        $is_attr_overlap = self::tagHasAttributeRestrictions($overlap$overlap_tag);

        // If the entirety (so not just the tag but also the attributes, and not

  private function checkHtmlRestrictionsAreCompatible(FilterFormatInterface $text_format, FundamentalCompatibilityConstraint $constraint): void {
    $html_restrictions = HTMLRestrictions::fromTextFormat($text_format);
    if ($html_restrictions->isUnrestricted()) {
      return;
    }

    $fundamental = new HTMLRestrictions($this->pluginManager->getProvidedElements(self::FUNDAMENTAL_CKEDITOR5_PLUGINS));
    if (!$fundamental->diff($html_restrictions)->allowsNothing()) {
      $offending_filter = static::findHtmlRestrictorFilterNotAllowingTags($text_format$fundamental);
      $this->context->buildViolation($constraint->nonAllowedElementsMessage)
        ->setParameter('%filter_label', (string) $offending_filter->getLabel())
        ->setParameter('%filter_plugin_id', $offending_filter->getPluginId())
        ->addViolation();
    }
  }

  /** * Checks the HTML restrictions match the enabled CKEditor 5 plugins' output. * * @param \Drupal\editor\EditorInterface $text_editor * The text editor to validate. * @param \Drupal\ckeditor5\Plugin\Validation\Constraint\FundamentalCompatibilityConstraint $constraint * The constraint to validate. */
// If the text format has HTML restrictions, ensure that a strict superset     // is allowed after switching to CKEditor 5.     $html_restrictions = $text_format->getHtmlRestrictions();
    if (is_array($html_restrictions) && array_key_exists('allowed', $html_restrictions)) {
      $allowed_tags = HTMLRestrictions::fromTextFormat($text_format);
      $enabled_plugins = array_keys($this->manager->getEnabledDefinitions($updated_text_editor));
      $updated_allowed_tags = new HTMLRestrictions($this->manager->getProvidedElements($enabled_plugins$updated_text_editor));
      $unsupported_tags_attributes = $allowed_tags->diff($updated_allowed_tags);
      $superset_tags_attributes = $updated_allowed_tags->diff($allowed_tags);
      $this->assertSame($expected_superset$superset_tags_attributes->toFilterHtmlAllowedTagsString());
      $this->assertTrue($unsupported_tags_attributes->allowsNothing(), "The following tags/attributes are not allowed in the updated text format:" . implode(' ', $unsupported_tags_attributes->toCKEditor5ElementsArray()));

      // Update the text format like ckeditor5_form_filter_format_form_alter()       // would.       $updated_text_format = clone $text_format;
      $filter_html_config = $text_format->filters('filter_html')->getConfiguration();
      $filter_html_config['settings']['allowed_html'] = $updated_allowed_tags->toFilterHtmlAllowedTagsString();
      $updated_text_format->setFilterConfig('filter_html', $filter_html_config);
    }
    else {
      // No update.       $updated_text_format = $text_format;
    }
    // configuration it does support any HTML tags.     // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getProvidedElements()     elseif ($definition['id'] === 'ckeditor5_sourceEditing') {
      assert($definition['drupal']['elements'] === []);
    }
    elseif ($definition['drupal']['elements'] !== FALSE && !(is_array($definition['drupal']['elements']) && !empty($definition['drupal']['elements']) && Inspector::assertAllStrings($definition['drupal']['elements']))) {
      throw new InvalidPluginDefinitionException($idsprintf('The "%s" CKEditor 5 plugin definition has a "drupal.elements" value that is neither a list of HTML tags/attributes nor false.', $id));
    }
    elseif (is_array($definition['drupal']['elements'])) {
      foreach ($definition['drupal']['elements'] as $index => $element) {
        $parsed = HTMLRestrictions::fromString($element);
        if ($parsed->allowsNothing()) {
          throw new InvalidPluginDefinitionException($idsprintf('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));
        }
        if (count($parsed->getAllowedElements()) > 1) {
          throw new InvalidPluginDefinitionException($idsprintf('The "%s" CKEditor 5 plugin definition has a value at "drupal.elements.%d": multiple tags listed, should be one: "%s".', $id$index$element));
        }
      }
    }

    if (isset($definition['drupal']['class']) && !class_exists($definition['drupal']['class'])) {
      throw new InvalidPluginDefinitionException($idsprintf('The CKEditor 5 "%s" provides a plugin class: "%s", but it does not exist.', $id$definition['drupal']['class']));
    }
    

  public function validate($element$constraint) {
    if (!$constraint instanceof CKEditor5ElementConstraint) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\CKEditor5Element');
    }

    $parsed = HTMLRestrictions::fromString($element);
    if ($parsed->allowsNothing() || count($parsed->getAllowedElements()) > 1 || $element !== $parsed->toCKEditor5ElementsArray()[0]) {
      $this->context->buildViolation($constraint->message)
        ->setParameter('%provided_element', $element)
        ->addViolation();
    }

    // The optional "requiredAttributes" constraint property allows more     // detailed validation.     if (isset($constraint->requiredAttributes)) {
      $allowed_elements = $parsed->getAllowedElements();
      $tag = array_keys($allowed_elements)[0];
      $attribute_restrictions = $allowed_elements[$tag];
      
if ($enabling_message_content) {
        $this->logger->info('The CKEditor 5 migration enabled the following plugins to support tags that are allowed by the %text_format text format: %enabling_message_content. The text format must be saved to make these changes active.',
          [
            '%text_format' => $editor->getFilterFormat()->get('name'),
            '%enabling_message_content' => $enabling_message_content,
          ]
        );
      }

      // Warn user about unsupported tags.       if (!$unsupported->allowsNothing()) {
        $this->addTagsToSourceEditing($editor$unsupported);
        $source_editing_additions = $source_editing_additions->merge($unsupported);
        $this->logger->info("The following tags were permitted by the %text_format text format's filter configuration, but no plugin was available that supports them. To ensure the tags remain supported by this text format, the following were added to the Source Editing plugin's <em>Manually editable HTML tags</em>: @unsupported_string. The text format must be saved to make these changes active.", [
          '%text_format' => $editor->getFilterFormat()->get('name'),
          '@unsupported_string' => $unsupported->toFilterHtmlAllowedTagsString(),
        ]);
      }

      if ($enabled_for_attributes_message_content) {
        $this->logger->info('The CKEditor 5 migration process enabled the following plugins to support specific attributes that are allowed by the %text_format text format: %enabled_for_attributes_message_content.',
          [
            


    // Evaluate `plugins` condition.     foreach ($definitions_with_plugins_condition as $plugin_id => $definition) {
      if (!empty(array_diff($definition->getConditions()['plugins']array_keys($definitions)))) {
        unset($definitions[$plugin_id]);
      }
    }

    if (!isset($definitions['ckeditor5_arbitraryHtmlSupport'])) {
      $restrictions = new HTMLRestrictions($this->getProvidedElements(array_keys($definitions)$editor, FALSE));
      if ($restrictions->getWildcardSubset()->allowsNothing()) {
        // This is only reached if arbitrary HTML is not enabled. If wildcard         // tags (such as $text-container) are present, they need to         // be resolved via the wildcardHtmlSupport plugin.         // @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getCKEditor5PluginConfig()         unset($definitions['ckeditor5_wildcardHtmlSupport']);
      }
    }
    // When arbitrary HTML is already supported, there is no need to support     // wildcard tags.     else {
      unset($definitions['ckeditor5_wildcardHtmlSupport']);
    }
Home | Imprint | This part of the site doesn't use cookies.