You are here

public function NoReferrerFilter::process in No Referrer 8

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

src/Plugin/Filter/NoReferrerFilter.php, line 76

Class

NoReferrerFilter
Provides a filter to apply the noreferrer attribute.

Namespace

Drupal\noreferrer\Plugin\Filter

Code

public function process($text, $langcode) {
  $modified = FALSE;
  $result = new FilterProcessResult($text);
  $html_dom = Html::load($text);
  $links = $html_dom
    ->getElementsByTagName('a');
  $config = $this->configFactory
    ->get('noreferrer.settings');
  $noopener = $config
    ->get('noopener');
  $noreferrer = $config
    ->get('noreferrer');
  foreach ($links as $link) {
    $types = [];
    if ($noopener && $link
      ->getAttribute('target') !== '') {
      $types[] = 'noopener';
    }
    if ($noreferrer && ($href = $link
      ->getAttribute('href')) && UrlHelper::isExternal($href) && !noreferrer_is_whitelisted($href)) {
      $types[] = 'noreferrer';
    }
    if ($types) {
      $rel = $link
        ->getAttribute('rel');
      foreach ($types as $type) {
        $rel .= $rel ? " {$type}" : $type;
      }
      $link
        ->setAttribute('rel', $rel);
      $modified = TRUE;
    }
  }
  if ($modified) {
    $result
      ->setProcessedText(Html::serialize($html_dom));
  }
  return $result;
}