function SmartDefaultSettings::computeSurplusScore

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/src/SmartDefaultSettings.php \Drupal\ckeditor5\SmartDefaultSettings::computeSurplusScore()
  2. 11.x core/modules/ckeditor5/src/SmartDefaultSettings.php \Drupal\ckeditor5\SmartDefaultSettings::computeSurplusScore()

Computes a score for the given surplus compared to the given need.

Parameters

\Drupal\ckeditor5\HTMLRestrictions $surplus: A surplus compared to what is needed.

\Drupal\ckeditor5\HTMLRestrictions $needed: Exactly what is needed.

Return value

int A surplus score. Lower is better. Scores are a positive integer.

See also

https://www.drupal.org/project/drupal/issues/3231328#comment-14444987

1 call to SmartDefaultSettings::computeSurplusScore()
SmartDefaultSettings::getCandidates in core/modules/ckeditor5/src/SmartDefaultSettings.php
Finds candidates for the still needed restrictions among disabled plugins.

File

core/modules/ckeditor5/src/SmartDefaultSettings.php, line 552

Class

SmartDefaultSettings
Generates CKEditor 5 settings for existing text editors/formats.

Namespace

Drupal\ckeditor5

Code

private static function computeSurplusScore(HTMLRestrictions $surplus, HTMLRestrictions $needed) : int {
  // Compute a score for surplus elements, while taking into account how much
  // impact each surplus element has:
  $surplus_score = 0;
  foreach ($surplus->getAllowedElements() as $tag_name => $attributes_config) {
    // 10^6 per surplus tag.
    if (!isset($needed->getAllowedElements()[$tag_name])) {
      $surplus_score += pow(10, 6);
    }
    // 10^5 per surplus "any attributes allowed".
    if ($attributes_config === TRUE) {
      $surplus_score += pow(10, 5);
    }
    if (!is_array($attributes_config)) {
      continue;
    }
    foreach ($attributes_config as $attribute_name => $attribute_config) {
      // 10^4 per surplus wildcard attribute.
      if (str_contains($attribute_name, '*')) {
        $surplus_score += pow(10, 4);
      }
      else {
        $surplus_score += pow(10, 3);
      }
      // 10^2 per surplus "any attribute values allowed".
      if ($attribute_config === TRUE) {
        $surplus_score += pow(10, 2);
      }
      if (!is_array($attribute_config)) {
        continue;
      }
      foreach ($attribute_config as $allowed_attribute_value => $allowed_attribute_value_config) {
        // 10^1 per surplus wildcard attribute value.
        if (str_contains($allowed_attribute_value, '*')) {
          $surplus_score += pow(10, 1);
        }
        else {
          $surplus_score += pow(10, 0);
        }
      }
    }
  }
  return $surplus_score;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.