You are here

protected function SpamspanDomTrait::processAsDom in SpamSpan filter 8

Same name and namespace in other branches
  1. 8.2 src/SpamspanDomTrait.php \Drupal\spamspan\SpamspanDomTrait::processAsDom()

Replaces email addresses using DOM and regex.

Parameters

string $text: Input text.

bool $altered: Set to true if any replacements happen.

Return value

string Output text.

1 call to SpamspanDomTrait::processAsDom()
FilterSpamspan::process in src/Plugin/Filter/FilterSpamspan.php
Performs the filter processing.

File

src/SpamspanDomTrait.php, line 30

Class

SpamspanDomTrait
Trait SpamspanDomTrait.

Namespace

Drupal\spamspan

Code

protected function processAsDom($text, &$altered) {
  $document = $this
    ->loadHtmlDocument($text);

  // Process mailto <a> tags.
  foreach ($document
    ->getElementsByTagName('a') as $atag) {
    $href = trim($atag
      ->getAttribute('href'));
    if (strpos($href, 'mailto:') === 0) {
      $atag
        ->setAttribute('href', $href);
      $text = $this
        ->replaceMailtoLinks($document
        ->saveHTML($atag), $altered);
      $this
        ->replaceDomNode($atag, $text);
    }
  }

  // Parse all text nodes.
  $xpath = new \DomXPath($document);
  foreach ($xpath
    ->query('//text()') as $text_node) {
    $text = $text_node->nodeValue;
    $node_altered = FALSE;
    if (!empty($this->settings['spamspan_use_form'])) {
      $text = $this
        ->replaceEmailAddressesWithOptions($text, $node_altered);
    }
    $text = $this
      ->replaceBareEmailAddresses($text, $node_altered);
    if ($node_altered) {
      $this
        ->replaceDomNode($text_node, $text);
      $altered = TRUE;
    }
  }
  return $this
    ->toStringHtmlDocument($document);
}