Skip to content

Commit

Permalink
fix: SF Bug 190: highlight_lines_extra eats end-of-line
Browse files Browse the repository at this point in the history
This applies the proposed fix from https://sourceforge.net/p/geshi/bugs/190/

Because of the lack of comprehensive tests, it's unclear if this has any
side effects, but it does fix the reported problem.

A test case for this bug in particular has been added.
  • Loading branch information
splitbrain authored and BenBE committed Jun 22, 2020
1 parent fd22ab7 commit 3c12a79
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
convertWarningsToExceptions="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/geshi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4045,7 +4045,7 @@ protected function finalise(&$parsed_code) {
$parsed_code .= str_repeat('</span>', $close);
$close = 0;
}
elseif ($i + 1 < $n) {
if ($i + 1 < $n) {
$parsed_code .= "\n";
}
unset($code[$i]);
Expand Down
56 changes: 56 additions & 0 deletions tests/NewlineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Ensures the number of output lines are the same as the input
*/
class NewlineTest extends TestCase
{

/**
* No fancy settings
*/
public function testDefault()
{
$input = <<< END
void main () {
printf ("Hello World");
exit 0;
}
END;
$input_lines = count(explode("\n", $input));

$geshi = new GeSHi($input, 'c');
$geshi->highlight_lines_extra(2);
$output = $geshi->parse_code();
$output_lines = count(explode("\n", $output));

$this->assertEquals($input_lines, $output_lines, "number of line mismatch between input and output");
}

/**
* Highlighting a line
*
* checks for SF bug 190
*
* @link https://sourceforge.net/p/geshi/bugs/190/
*/
public function testHighlight()
{
$input = <<< END
void main () {
printf ("Hello World");
exit 0;
}
END;
$input_lines = count(explode("\n", $input));

$geshi = new GeSHi($input, 'c');
$geshi->highlight_lines_extra(2);
$output = $geshi->parse_code();
$output_lines = count(explode("\n", $output));

$this->assertEquals($input_lines, $output_lines, "number of line mismatch between input and output");
}
}

0 comments on commit 3c12a79

Please sign in to comment.