From 29e33475621fd9e4be20ecdea45d017bbd4f65f8 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 25 Sep 2014 16:04:48 +1200 Subject: [PATCH] API Convert::html2raw no longer wraps text automatically BUG Convert::html2raw now correctly decodes single quotes --- core/Convert.php | 17 ++++++++------- docs/en/changelogs/3.2.0.md | 1 + tests/core/ConvertTest.php | 43 +++++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/core/Convert.php b/core/Convert.php index d4e303805..c472f7c57 100644 --- a/core/Convert.php +++ b/core/Convert.php @@ -286,11 +286,11 @@ class Convert { /** * Simple conversion of HTML to plaintext. * - * @param $data string - * @param $preserveLinks boolean - * @param $wordwrap array + * @param string $data Input data + * @param bool $preserveLinks + * @param int $wordwrap */ - public static function html2raw($data, $preserveLinks = false, $wordWrap = 60, $config = null) { + public static function html2raw($data, $preserveLinks = false, $wordWrap = 0, $config = null) { $defaultConfig = array( 'PreserveLinks' => false, 'ReplaceBoldAsterisk' => true, @@ -343,9 +343,7 @@ class Convert { $data = preg_replace('/<\/p>/i', "\n\n", $data ); // Replace HTML entities - //$data = preg_replace("/&#([0-9]+);/e", 'chr(\1)', $data); - //$data = str_replace(array("<",">","&"," "), array("<", ">", "&", " "), $data); - $data = html_entity_decode($data, ENT_COMPAT , 'UTF-8'); + $data = html_entity_decode($data, ENT_QUOTES, 'UTF-8'); // Remove all tags (but optionally keep links) // strip_tags seemed to be restricting the length of the output @@ -355,7 +353,10 @@ class Convert { } else { $data = strip_tags($data, ''); } - return trim(wordwrap(trim($data), $wordWrap)); + + // Wrap + if($wordWrap) $data = wordwrap(trim($data), $wordWrap); + return trim($data); } /** diff --git a/docs/en/changelogs/3.2.0.md b/docs/en/changelogs/3.2.0.md index e2524a987..a59680bc4 100644 --- a/docs/en/changelogs/3.2.0.md +++ b/docs/en/changelogs/3.2.0.md @@ -13,6 +13,7 @@ * `ClassInfo::dataClassesFor` now returns classes which should have tables, regardless of whether those tables actually exist. * `SS_Filterable`, `SS_Limitable` and `SS_Sortable` now explicitly extend `SS_List` + * `Convert::html2raw` no longer wraps text by default and can decode single quotes. #### Deprecated classes/methods removed diff --git a/tests/core/ConvertTest.php b/tests/core/ConvertTest.php index a9c684b14..07f56c239 100644 --- a/tests/core/ConvertTest.php +++ b/tests/core/ConvertTest.php @@ -38,34 +38,63 @@ class ConvertTest extends SapphireTest { public function testHtml2raw() { $val1 = 'This has a strong tag.'; - $this->assertEquals('This has a *strong tag*.', Convert::xml2raw($val1), + $this->assertEquals('This has a *strong tag*.', Convert::html2raw($val1), 'Strong tags are replaced with asterisks'); $val1 = 'This has a b tag with attributes.'; - $this->assertEquals('This has a *b tag with attributes*.', Convert::xml2raw($val1), + $this->assertEquals('This has a *b tag with attributes*.', Convert::html2raw($val1), 'B tags with attributes are replaced with asterisks'); $val2 = 'This has a strong tag with attributes.'; - $this->assertEquals('This has a *strong tag with attributes*.', Convert::xml2raw($val2), + $this->assertEquals('This has a *strong tag with attributes*.', Convert::html2raw($val2), 'Strong tags with attributes are replaced with asterisks'); $val3 = ''; - $this->assertEquals('', Convert::xml2raw($val3), + $this->assertEquals('', Convert::html2raw($val3), 'Script tags are completely removed'); $val4 = ''; - $this->assertEquals('', Convert::xml2raw($val4), + $this->assertEquals('', Convert::html2raw($val4), 'Style tags are completely removed'); $val5 = ''; - $this->assertEquals('', Convert::xml2raw($val5), + $this->assertEquals('', Convert::html2raw($val5), 'Multiline script tags are completely removed'); $val6 = ''; - $this->assertEquals('', Convert::xml2raw($val6), + $this->assertEquals('', Convert::html2raw($val6), 'Multiline style tags are completely removed'); + + $val7 = '

That's absolutely correct

'; + $this->assertEquals( + "That's absolutely correct", + Convert::html2raw($val7), + "Single quotes are decoded correctly" + ); + + $val8 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor '. + 'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud '. + 'exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute '. + 'irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla '. + 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia '. + 'deserunt mollit anim id est laborum.'; + $this->assertEquals($val8, Convert::html2raw($val8), 'Test long text is unwrapped'); + $this->assertEquals(<<