Class yii\gii\components\DiffRendererHtmlInline

Inheritanceyii\gii\components\DiffRendererHtmlInline » Diff_Renderer_Html_Array
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-gii/blob/master/src/components/DiffRendererHtmlInline.php

Renders diff to HTML. Output adjusted to be copy-paste friendly.

Public Methods

Hide inherited methods

Method Description Defined By
render() Render a and return diff with changes between the two sequences displayed inline (under each other) yii\gii\components\DiffRendererHtmlInline

Method Details

Hide inherited methods

render() public method

Render a and return diff with changes between the two sequences displayed inline (under each other)

public string render ( )
return string

The generated inline diff.

                public function render()
{
    $changes = parent::render();
    $html = '';
    if (empty($changes)) {
        return $html;
    }
    $html .= <<<HTML
le class="Differences DifferencesInline">
<thead>
    <tr>
        <th>Old</th>
        <th>New</th>
        <th>Differences</th>
    </tr>
</thead>
;
    foreach ($changes as $i => $blocks) {
        // If this is a separate block, we're condensing code so output ...,
        // indicating a significant portion of the code has been collapsed as
        // it is the same
        if ($i > 0) {
            $html .= <<<HTML
<tbody class="Skipped">
    <th data-line-number="&hellip;"></th>
    <th data-line-number="&hellip;"></th>
    <td>&nbsp;</td>
</tbody>
;
        }
        foreach ($blocks as $change) {
            $tag = ucfirst($change['tag']);
            $html .= <<<HTML
<tbody class="Change{$tag}">
;
            // Equal changes should be shown on both sides of the diff
            if ($change['tag'] === 'equal') {
                foreach ($change['base']['lines'] as $no => $line) {
                    $fromLine = $change['base']['offset'] + $no + 1;
                    $toLine = $change['changed']['offset'] + $no + 1;
                    $html .= <<<HTML
    <tr>
        <th data-line-number="{$fromLine}"></th>
        <th data-line-number="{$toLine}"></th>
        <td class="Left">{$line}</td>
    </tr>
;
                }
            }
            // Added lines only on the right side
            elseif ($change['tag'] === 'insert') {
                foreach ($change['changed']['lines'] as $no => $line) {
                    $toLine = $change['changed']['offset'] + $no + 1;
                    $html .= <<<HTML
    <tr>
        <th data-line-number="&nbsp;"></th>
        <th data-line-number="{$toLine}"></th>
        <td class="Right"><ins>{$line}</ins>&nbsp;</td>
    </tr>
;
                }
            }
            // Show deleted lines only on the left side
            elseif ($change['tag'] === 'delete') {
                foreach ($change['base']['lines'] as $no => $line) {
                    $fromLine = $change['base']['offset'] + $no + 1;
                    $html .= <<<HTML
    <tr>
        <th data-line-number="{$fromLine}"></th>
        <th data-line-number="&nbsp;"></th>
        <td class="Left"><del>{$line}</del>&nbsp;</td>
    </tr>
;
                }
            }
            // Show modified lines on both sides
            elseif ($change['tag'] === 'replace') {
                foreach ($change['base']['lines'] as $no => $line) {
                    $fromLine = $change['base']['offset'] + $no + 1;
                    $html .= <<<HTML
    <tr>
        <th data-line-number="{$fromLine}"></th>
        <th data-line-number="&nbsp;"></th>
        <td class="Left"><span>{$line}</span></td>
    </tr>
;
                }
                foreach ($change['changed']['lines'] as $no => $line) {
                    $toLine = $change['changed']['offset'] + $no + 1;
                    $html .= <<<HTML
    <tr>
        <th data-line-number="{$toLine}"></th>
        <th data-line-number="&nbsp;"></th>
        <td class="Right"><span>{$line}</span></td>
    </tr>
;
                }
            }
            $html .= <<<HTML
</tbody>
;
        }
    }
    $html .= <<<HTML
ble>
;
    return $html;
}