From 52d485c435cdaa621ef54324a516a74336478c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Fri, 28 Jun 2013 13:10:10 +0200 Subject: [PATCH] MDL-35206 Fix the links list enumeration in the html2text library --- lib/html2text.php | 2 +- lib/html2text_readme.txt | 1 + lib/tests/html2text_test.php | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/html2text.php b/lib/html2text.php index afedac5fdf7e5..b78b0ae1b9b27 100644 --- a/lib/html2text.php +++ b/lib/html2text.php @@ -584,7 +584,7 @@ function _build_link_list( $link, $display ) $index = count($this->_link_list); } - return $display . ' [' . ($index+1) . ']'; + return $display . ' [' . ($index) . ']'; } /** diff --git a/lib/html2text_readme.txt b/lib/html2text_readme.txt index 75ddfbefa0efe..c92fe84b0b33c 100644 --- a/lib/html2text_readme.txt +++ b/lib/html2text_readme.txt @@ -12,6 +12,7 @@ Modifications 3. Use textlib, not crappy functions that break UTF-8, in the _strtoupper method. (Tim Hunt 2010-11-02) 4. Make sure html2text does not destroy '0'. (Tim Hunt 2011-09-21) 5. define missing mail charset +6. Fixed the links list enumeration (MDL-35206). Imported from: https://github.com/moodle/custom-html2text/tree/MOODLE_5886_1 diff --git a/lib/tests/html2text_test.php b/lib/tests/html2text_test.php index c4f54ff21f85e..2791dd0879090 100644 --- a/lib/tests/html2text_test.php +++ b/lib/tests/html2text_test.php @@ -73,6 +73,31 @@ public function test_zero() { $this->assertSame('0', html_to_text('0')); } + /** + * Test the links list enumeration. + */ + public function test_build_link_list() { + + // Note the trailing whitespace left intentionally in the text. + $text = 'Total of + 27 issues and some other +have been fixed last week'; + + // Do not collect links. + $result = html_to_text($text, 5000, false); + $this->assertSame('Total of 27 ISSUES and some other have been fixed LAST WEEK', $result); + + // Collect and enumerate links. + $result = html_to_text($text, 5000, true); + $this->assertSame(0, strpos($result, 'Total of 27 ISSUES [1] and some [2] other have been fixed LAST WEEK [3]')); + $this->assertSame(false, strpos($result, '[0]')); + $this->assertSame(1, preg_match('|^'.preg_quote('[1] http://tr.mdl.org/sh.jspa?r=1&j=p+%3D+%22I+d%22+%3D').'$|m', $result)); + $this->assertSame(1, preg_match('|^'.preg_quote('[2] http://another.url/?f=a&b=2').'$|m', $result)); + $this->assertSame(1, preg_match('|^'.preg_quote('[3] http://third.url/view.php').'$|m', $result)); + $this->assertSame(false, strpos($result, '[4]')); + } + // ======= Standard html2text conversion features ======= /**