mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
341245dd4a
commit
f94be66cb2
@ -77,9 +77,13 @@ class HTMLText extends Text {
|
|||||||
* that does very badly on broken HTML*/
|
* that does very badly on broken HTML*/
|
||||||
if (!$str) {
|
if (!$str) {
|
||||||
/* See if we can pull a paragraph out*/
|
/* 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 */
|
/* 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 */
|
/* Now pull out all the html-alike stuff */
|
||||||
$str = preg_replace('{</?[a-zA-Z]+[^<>]*>}', '', $str); /* Take out anything that is obviously a tag */
|
$str = preg_replace('{</?[a-zA-Z]+[^<>]*>}', '', $str); /* Take out anything that is obviously a tag */
|
||||||
|
@ -27,7 +27,10 @@ class HTMLTextTest extends SapphireTest {
|
|||||||
'<h1>Should not take header</h1><p>Should take paragraph</p>' => 'Should take paragraph',
|
'<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>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>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) {
|
foreach($cases as $originalValue => $expectedValue) {
|
||||||
|
Loading…
Reference in New Issue
Block a user