3 namespace Drupal\Core\Diff;
5 use Drupal\Component\Diff\DiffFormatter as DiffFormatterBase;
6 use Drupal\Component\Diff\WordLevelDiff;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Core\Config\ConfigFactoryInterface;
11 * Diff formatter which uses returns output that can be rendered to a table.
13 class DiffFormatter extends DiffFormatterBase {
16 * The diff represented as an array of rows.
23 * Creates a DiffFormatter to render diffs in a table.
25 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
28 public function __construct(ConfigFactoryInterface $config_factory) {
29 $config = $config_factory->get('system.diff');
30 $this->leading_context_lines = $config->get('context.lines_leading');
31 $this->trailing_context_lines = $config->get('context.lines_trailing');
37 protected function _start_diff() {
44 protected function _end_diff() {
51 protected function _block_header($xbeg, $xlen, $ybeg, $ylen) {
54 'data' => $xbeg + $this->line_stats['offset']['x'],
58 'data' => $ybeg + $this->line_stats['offset']['y'],
67 protected function _start_block($header) {
68 if ($this->show_header) {
69 $this->rows[] = $header;
76 protected function _lines($lines, $prefix=' ', $color='white') {
80 * Creates an added line.
83 * An HTML-escaped line.
86 * An array representing a table row.
88 protected function addedLine($line) {
92 'class' => 'diff-marker',
95 'data' => ['#markup' => $line],
96 'class' => 'diff-context diff-addedline',
102 * Creates a deleted line.
104 * @param string $line
105 * An HTML-escaped line.
108 * An array representing a table row.
110 protected function deletedLine($line) {
114 'class' => 'diff-marker',
117 'data' => ['#markup' => $line],
118 'class' => 'diff-context diff-deletedline',
124 * Creates a context line.
126 * @param string $line
127 * An HTML-escaped line.
130 * An array representing a table row.
132 protected function contextLine($line) {
136 'data' => ['#markup' => $line],
137 'class' => 'diff-context',
143 * Creates an empty line.
146 * An array representing a table row.
148 protected function emptyLine() {
158 protected function _added($lines) {
159 foreach ($lines as $line) {
160 $this->rows[] = array_merge($this->emptyLine(), $this->addedLine(Html::escape($line)));
167 protected function _deleted($lines) {
168 foreach ($lines as $line) {
169 $this->rows[] = array_merge($this->deletedLine(Html::escape($line)), $this->emptyLine());
176 protected function _context($lines) {
177 foreach ($lines as $line) {
178 $this->rows[] = array_merge($this->contextLine(Html::escape($line)), $this->contextLine(Html::escape($line)));
185 protected function _changed($orig, $closing) {
186 $orig = array_map('\Drupal\Component\Utility\Html::escape', $orig);
187 $closing = array_map('\Drupal\Component\Utility\Html::escape', $closing);
188 $diff = new WordLevelDiff($orig, $closing);
189 $del = $diff->orig();
190 $add = $diff->closing();
192 // Notice that WordLevelDiff returns HTML-escaped output. Hence, we will be
193 // calling addedLine/deletedLine without HTML-escaping.
194 while ($line = array_shift($del)) {
195 $aline = array_shift( $add );
196 $this->rows[] = array_merge($this->deletedLine($line), isset($aline) ? $this->addedLine($aline) : $this->emptyLine());
200 foreach ($add as $line) {
201 $this->rows[] = array_merge($this->emptyLine(), $this->addedLine($line));