BUGFIX: HTMLText.Summary() will return nothing if there's an image at the beginning. Regex to strip out the image and unit tests (fixes #6436, thanks webbower)

This commit is contained in:
Julian Seidenberg 2011-03-22 10:29:48 +13:00 committed by Ingo Schommer
parent 341245dd4a
commit f94be66cb2
2 changed files with 10 additions and 3 deletions

View File

@ -77,9 +77,13 @@ class HTMLText extends Text {
* that does very badly on broken HTML*/
if (!$str) {
/* See if we can pull a paragraph out*/
if (preg_match('{<p(\s[^<>]*)?>(.*[A-Za-z]+.*)</p>}', $this->value, $matches)) $str = $matches[2];
// Strip out any images in case there's one at the beginning. Not doing this will return a blank paragraph
$str = preg_replace('{^\s*(<.+?>)*<img[^>]*>}', '', $this->value);
if (preg_match('{<p(\s[^<>]*)?>(.*[A-Za-z]+.*)</p>}', $str, $matches)) $str = $matches[2];
/* If _that_ failed, just use the whole text */
else $str = $this->value;
if (!$str) $str = $this->value;
/* Now pull out all the html-alike stuff */
$str = preg_replace('{</?[a-zA-Z]+[^<>]*>}', '', $str); /* Take out anything that is obviously a tag */

View File

@ -27,7 +27,10 @@ class HTMLTextTest extends SapphireTest {
'<h1>Should not take header</h1><p>Should take paragraph</p>' => 'Should take paragraph',
'<p>Should strip <b>tags, but leave</b> text</p>' => 'Should strip tags, but leave text',
'<p>Unclosed tags <br>should not phase it</p>' => 'Unclosed tags should not phase it',
'<p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph'
'<p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph',
'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph',
' <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph',
'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>' => 'example text words hello',
);
foreach($cases as $originalValue => $expectedValue) {