Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "chrisboulton/php-diff",
"type": "library",
"type": "library",
"description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).",
"authors": [
{
"name": "Chris Boulton",
"email": "@chrisboulton"
}
],
"autoload": {
"psr-0": {
"Diff": "lib/"
}
}
}
"autoload": {
"psr-0": {
"Diff": "lib/"
}
},
"require-dev": {
"phpunit/phpunit": "~5.5"
}
}
7 changes: 4 additions & 3 deletions lib/Diff/Renderer/Html/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,20 @@ protected function formatLines($lines)
$lines = array_map(array($this, 'ExpandTabs'), $lines);
$lines = array_map(array($this, 'HtmlSafe'), $lines);
foreach($lines as &$line) {
$line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line);
$line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
}
return $lines;
}

/**
* Replace a string containing spaces with a HTML representation using  .
*
* @param string $spaces The string of spaces.
* @param string[] $matches Array with preg matches.
* @return string The HTML representation of the string.
*/
function fixSpaces($spaces='')
private function fixSpaces(array $matches)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is used only by the callback above, so it can be private

{
$spaces = $matches[1];
$count = strlen($spaces);
if($count == 0) {
return '';
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
66 changes: 66 additions & 0 deletions tests/Diff/Renderer/Html/ArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Diff\Renderer\Html;

class ArrayTest extends \PHPUnit_Framework_TestCase
{
public function testRenderSimpleDelete()
{
$htmlRenderer = new \Diff_Renderer_Html_Array();
$htmlRenderer->diff = new \Diff(
array('a'),
array()
);

$result = $htmlRenderer->render();

static::assertEquals(array(
array(
array(
'tag' => 'delete',
'base' => array(
'offset' => 0,
'lines' => array(
'a'
)
),
'changed' => array(
'offset' => 0,
'lines' => array()
)
)
)
), $result);
}

public function testRenderFixesSpaces()
{
$htmlRenderer = new \Diff_Renderer_Html_Array();
$htmlRenderer->diff = new \Diff(
array(' a'),
array('a')
);

$result = $htmlRenderer->render();

static::assertEquals(array(
array(
array(
'tag' => 'replace',
'base' => array(
'offset' => 0,
'lines' => array(
'<del>&nbsp; &nbsp;</del>a',
)
),
'changed' => array(
'offset' => 0,
'lines' => array(
'<ins></ins>a'
)
)
)
)
), $result);
}
}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';