You are here

public function NoopenerFilter::processAttributes in Noopener Filter 8

Implement processAttributes().

1 call to NoopenerFilter::processAttributes()
NoopenerFilter::process in src/Plugin/Filter/NoopenerFilter.php
Performs the filter processing.

File

src/Plugin/Filter/NoopenerFilter.php, line 23

Class

NoopenerFilter
Define class NoopenerFilter.

Namespace

Drupal\noopener_filter\Plugin\Filter

Code

public function processAttributes($text) {
  $html_dom = Html::load($text);
  $links = $html_dom
    ->getElementsByTagName('a');
  foreach ($links as $link) {
    if (!empty($link
      ->getAttribute('target')) && $link
      ->getAttribute('target') === '_blank') {
      if (!empty($link
        ->getAttribute('rel'))) {
        $link
          ->setAttribute('rel', 'noopener ' . $link
          ->getAttribute('rel'));
      }
      else {
        $link
          ->setAttribute('rel', 'noopener');
      }
    }
  }
  $text = Html::serialize($html_dom);
  return trim($text);
}