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