1<?php
2
3namespace Drupal\Component\Diff\Engine;
4
5/**
6 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
7 */
8
9/**
10 * @todo document
11 * @private
12 * @subpackage DifferenceEngine
13 */
14class HWLDFWordAccumulator {
15
16  /**
17   * An iso-8859-x non-breaking space.
18   */
19  const NBSP = '&#160;';
20
21  protected $lines = [];
22
23  protected $line = '';
24
25  protected $group = '';
26
27  protected $tag = '';
28
29  protected function _flushGroup($new_tag) {
30    if ($this->group !== '') {
31      if ($this->tag == 'mark') {
32        $this->line = $this->line . '<span class="diffchange">' . $this->group . '</span>';
33      }
34      else {
35        $this->line = $this->line . $this->group;
36      }
37    }
38    $this->group = '';
39    $this->tag = $new_tag;
40  }
41
42  protected function _flushLine($new_tag) {
43    $this->_flushGroup($new_tag);
44    if ($this->line != '') {
45      array_push($this->lines, $this->line);
46    }
47    else {
48      // make empty lines visible by inserting an NBSP
49      array_push($this->lines, $this::NBSP);
50    }
51    $this->line = '';
52  }
53
54  public function addWords($words, $tag = '') {
55    if ($tag != $this->tag) {
56      $this->_flushGroup($tag);
57    }
58    foreach ($words as $word) {
59      // new-line should only come as first char of word.
60      if ($word == '') {
61        continue;
62      }
63      if ($word[0] == "\n") {
64        $this->_flushLine($tag);
65        $word = mb_substr($word, 1);
66      }
67      assert(!strstr($word, "\n"));
68      $this->group .= $word;
69    }
70  }
71
72  public function getLines() {
73    $this->_flushLine('~done');
74    return $this->lines;
75  }
76
77}
78