API Convert::html2raw no longer wraps text automatically

BUG Convert::html2raw now correctly decodes single quotes
This commit is contained in:
Damian Mooyman 2014-09-25 16:04:48 +12:00
parent bbd4e8b8c1
commit 29e3347562
3 changed files with 46 additions and 15 deletions

View File

@ -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("&lt;","&gt;","&amp;","&nbsp;"), 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, '<a>');
}
return trim(wordwrap(trim($data), $wordWrap));
// Wrap
if($wordWrap) $data = wordwrap(trim($data), $wordWrap);
return trim($data);
}
/**

View File

@ -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

View File

@ -38,34 +38,63 @@ class ConvertTest extends SapphireTest {
public function testHtml2raw() {
$val1 = 'This has a <strong>strong tag</strong>.';
$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 class="test" style="font-weight: bold">b tag with attributes</b>.';
$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 class="test" style="font-weight: bold">strong tag with attributes</STRONG>.';
$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 = '<script type="text/javascript">Some really nasty javascript here</script>';
$this->assertEquals('', Convert::xml2raw($val3),
$this->assertEquals('', Convert::html2raw($val3),
'Script tags are completely removed');
$val4 = '<style type="text/css">Some really nasty CSS here</style>';
$this->assertEquals('', Convert::xml2raw($val4),
$this->assertEquals('', Convert::html2raw($val4),
'Style tags are completely removed');
$val5 = '<script type="text/javascript">Some really nasty
multiline javascript here</script>';
$this->assertEquals('', Convert::xml2raw($val5),
$this->assertEquals('', Convert::html2raw($val5),
'Multiline script tags are completely removed');
$val6 = '<style type="text/css">Some really nasty
multiline CSS here</style>';
$this->assertEquals('', Convert::xml2raw($val6),
$this->assertEquals('', Convert::html2raw($val6),
'Multiline style tags are completely removed');
$val7 = '<p>That&#39;s absolutely correct</p>';
$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(<<<PHP
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.
PHP
,
Convert::html2raw($val8, false, 60),
'Test long text is wrapped'
);
}
/**