HWLDFWordAccumulator.php 1.6 KB

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