You are here

protected function NbspCleanerFilter::swapNbspHtml in CKEditor Non-breaking space Plugin ( ) 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Filter/NbspCleanerFilter.php \Drupal\nbsp\Plugin\Filter\NbspCleanerFilter::swapNbspHtml()

Replace <span class="nbsp"> tags with their respected HTML.

Parameters

string $text: The HTML string to replace <span class="nbsp"> tags.

Return value

string The HTML with all the <span class="nbsp"> tags replaced with their respected html.

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

File

src/Plugin/Filter/NbspCleanerFilter.php, line 45

Class

NbspCleanerFilter
NBSP Cleaner Filter class. Implements process() method only.

Namespace

Drupal\nbsp\Plugin\Filter

Code

protected function swapNbspHtml($text) {
  $document = Html::load($text);
  $xpath = new \DOMXPath($document);
  foreach ($xpath
    ->query('//span[@class="nbsp"]') as $node) {
    if (!empty($node) && !empty($node->nodeValue)) {

      // PHP DOM removing the tag (not content)
      $node->parentNode
        ->replaceChild(new \DOMText($node->nodeValue), $node);
    }
  }
  return Html::serialize($document);
}