Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WordAccumulator
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 flushGroup
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 flushLine
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 addWords
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 getLines
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * You may copy this code freely under the conditions of the GPL.
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 * @ingroup DifferenceEngine
9 * @defgroup DifferenceEngine DifferenceEngine
10 */
11
12namespace Wikimedia\Diff;
13
14/**
15 * Stores, escapes and formats the results of word-level diff
16 */
17class WordAccumulator {
18    /** @var string */
19    public $insClass = ' class="diffchange diffchange-inline"';
20    /** @var string */
21    public $delClass = ' class="diffchange diffchange-inline"';
22
23    /** @var array */
24    private $lines = [];
25    /** @var string */
26    private $line = '';
27    /** @var string */
28    private $group = '';
29    /** @var string */
30    private $tag = '';
31
32    /**
33     * @param string $new_tag
34     */
35    private function flushGroup( $new_tag ) {
36        if ( $this->group !== '' ) {
37            $encGroup = htmlspecialchars( $this->group, ENT_NOQUOTES );
38            if ( $this->tag == 'ins' ) {
39                $this->line .= "<ins{$this->insClass}>$encGroup</ins>";
40            } elseif ( $this->tag == 'del' ) {
41                $this->line .= "<del{$this->delClass}>$encGroup</del>";
42            } else {
43                $this->line .= $encGroup;
44            }
45        }
46        $this->group = '';
47        $this->tag = $new_tag;
48    }
49
50    /**
51     * @param string $new_tag
52     */
53    private function flushLine( $new_tag ) {
54        $this->flushGroup( $new_tag );
55        if ( $this->line != '' ) {
56            $this->lines[] = $this->line;
57        } else {
58            # make empty lines visible by inserting an NBSP
59            $this->lines[] = "\u{00A0}";
60        }
61        $this->line = '';
62    }
63
64    /**
65     * @param string[] $words
66     * @param string $tag
67     */
68    public function addWords( $words, $tag = '' ) {
69        if ( $tag != $this->tag ) {
70            $this->flushGroup( $tag );
71        }
72
73        foreach ( $words as $word ) {
74            // new-line should only come as first char of word.
75            if ( $word == '' ) {
76                continue;
77            }
78            if ( $word[0] == "\n" ) {
79                $this->flushLine( $tag );
80                $word = substr( $word, 1 );
81            }
82            // FIXME: Don't use assert()
83            // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
84            assert( !str_contains( $word, "\n" ) );
85            $this->group .= $word;
86        }
87    }
88
89    /**
90     * @return string[]
91     */
92    public function getLines() {
93        $this->flushLine( '~done' );
94
95        return $this->lines;
96    }
97}
98
99/** @deprecated class alias since 1.41 */
100class_alias( WordAccumulator::class, 'MediaWiki\\Diff\\WordAccumulator' );