From f1b1b16b7d3f15e9d450e1a95f8e88ee47febfde Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Wed, 29 Apr 2020 20:34:10 +0200
Subject: [PATCH] fix: SF Bug 190: highlight_lines_extra eats end-of-line
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.
---
Removed the changes to phpunit.xml from the original patch,
since these caused the tests to fail in Fedora.
- Artur Iwicki <fedora@svgames.pl>
src/geshi.php | 2 +-
tests/NewlineTest.php | 56 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 59 insertions(+), 2 deletions(-)
create mode 100644 tests/NewlineTest.php
diff --git a/src/geshi.php b/src/geshi.php
index cf5e1632..8c3f0a31 100644
--- a/src/geshi.php
+++ b/src/geshi.php
@@ -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]);
diff --git a/tests/NewlineTest.php b/tests/NewlineTest.php
new file mode 100644
index 00000000..593168b8
--- /dev/null
+++ b/tests/NewlineTest.php
@@ -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");
+ }
+}