You are here

protected function TruncateHTML::domNodeTruncateChars in Smart Trim 8

Truncates a DOMNode by character count.

Parameters

\DOMNode $domnode: Object to be truncated.

1 call to TruncateHTML::domNodeTruncateChars()
TruncateHTML::truncateChars in src/Truncate/TruncateHTML.php
Truncates HTML text by characters.

File

src/Truncate/TruncateHTML.php, line 165
Contains trim functionality.

Class

TruncateHTML
Class TruncateHTML.

Namespace

Drupal\smart_trim\Truncate

Code

protected function domNodeTruncateChars(\DOMNode $domnode) {
  foreach ($domnode->childNodes as $node) {
    if ($this->foundBreakpoint == TRUE) {
      return;
    }
    if ($node
      ->hasChildNodes()) {
      $this
        ->domNodeTruncateChars($node);
    }
    else {
      $text = html_entity_decode($node->nodeValue, ENT_QUOTES, 'UTF-8');
      $length = mb_strlen($text);
      if ($this->charCount + $length >= $this->limit) {

        // We have found our end point.
        $node->nodeValue = Unicode::truncate($text, $this->limit - $this->charCount, TRUE);
        $this
          ->removeTrailingPunctuation($node);
        $this
          ->removeProceedingNodes($node);
        $this
          ->insertEllipsis($node);
        $this->foundBreakpoint = TRUE;
        return;
      }
      else {
        $this->charCount += $length;
      }
    }
  }
}