class WordLevelDiff

Same name in other branches
  1. 9 core/lib/Drupal/Component/Diff/WordLevelDiff.php \Drupal\Component\Diff\WordLevelDiff
  2. 8.9.x core/lib/Drupal/Component/Diff/WordLevelDiff.php \Drupal\Component\Diff\WordLevelDiff
  3. 10 core/lib/Drupal/Component/Diff/WordLevelDiff.php \Drupal\Component\Diff\WordLevelDiff

@todo document @private @subpackage DifferenceEngine

Hierarchy

  • class \Drupal\Component\Diff\Diff
    • class \Drupal\Component\Diff\MappedDiff extends \Drupal\Component\Diff\Diff
      • class \Drupal\Component\Diff\WordLevelDiff extends \Drupal\Component\Diff\MappedDiff

Expanded class hierarchy of WordLevelDiff

1 file declares its use of WordLevelDiff
DiffFormatter.php in core/lib/Drupal/Core/Diff/DiffFormatter.php

File

core/lib/Drupal/Component/Diff/WordLevelDiff.php, line 12

Namespace

Drupal\Component\Diff
View source
class WordLevelDiff extends MappedDiff {
    const MAX_LINE_LENGTH = 10000;
    public function __construct($orig_lines, $closing_lines) {
        [
            $orig_words,
            $orig_stripped,
        ] = $this->_split($orig_lines);
        [
            $closing_words,
            $closing_stripped,
        ] = $this->_split($closing_lines);
        parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
    }
    protected function _split($lines) {
        $words = [];
        $stripped = [];
        $first = TRUE;
        foreach ($lines as $line) {
            // If the line is too long, just pretend the entire line is one big word
            // This prevents resource exhaustion problems
            if ($first) {
                $first = FALSE;
            }
            else {
                $words[] = "\n";
                $stripped[] = "\n";
            }
            if (mb_strlen($line) > $this::MAX_LINE_LENGTH) {
                $words[] = $line;
                $stripped[] = $line;
            }
            else {
                if (preg_match_all('/ ( [^\\S\\n]+ | [0-9_A-Za-z\\x80-\\xff]+ | . ) (?: (?!< \\n) [^\\S\\n])? /xs', $line, $m)) {
                    $words = array_merge($words, $m[0]);
                    $stripped = array_merge($stripped, $m[1]);
                }
            }
        }
        return [
            $words,
            $stripped,
        ];
    }
    public function orig() {
        $orig = new HWLDFWordAccumulator();
        foreach ($this->edits as $edit) {
            if ($edit->type == 'copy') {
                $orig->addWords($edit->orig);
            }
            elseif ($edit->orig) {
                $orig->addWords($edit->orig, 'mark');
            }
        }
        $lines = $orig->getLines();
        return $lines;
    }
    public function closing() {
        $closing = new HWLDFWordAccumulator();
        foreach ($this->edits as $edit) {
            if ($edit->type == 'copy') {
                $closing->addWords($edit->closing);
            }
            elseif ($edit->closing) {
                $closing->addWords($edit->closing, 'mark');
            }
        }
        $lines = $closing->getLines();
        return $lines;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Diff::$edits protected property The list of differences as an array of diff operations.
Diff::getEdits public function Gets the list of differences as an array of diff operations.
WordLevelDiff::closing public function
WordLevelDiff::MAX_LINE_LENGTH constant
WordLevelDiff::orig public function
WordLevelDiff::_split protected function
WordLevelDiff::__construct public function Overrides MappedDiff::__construct

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.