mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Improved detection of empty HTMLText fields.
This commit is contained in:
parent
d8bfc0bb48
commit
1e1df8c43e
@ -136,8 +136,21 @@ class HTMLText extends Text {
|
|||||||
return ShortcodeParser::get_active()->parse($this->value);
|
return ShortcodeParser::get_active()->parse($this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the field has meaningful content.
|
||||||
|
* Excludes null content like <h1></h1>, <p></p> ,etc
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
public function exists() {
|
public function exists() {
|
||||||
return parent::exists() && $this->value != '<p></p>';
|
// If it's blank, it's blank
|
||||||
|
if(!parent::exists()) return false;
|
||||||
|
// If it's got a content tag
|
||||||
|
if(preg_match('/<(img|embed|object|iframe)[^>]*>/i', $this->value)) return true;
|
||||||
|
// If it's just one or two tags on its own (and not the above) it's empty. This might be <p></p> or <h1></h1> or whatever.
|
||||||
|
if(preg_match('/^[\\s]*(<[^>]+>[\\s]*){1,2}$/', $this->value)) return false;
|
||||||
|
// Otherwise its content is genuine content
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scaffoldFormField($title = null, $params = null) {
|
public function scaffoldFormField($title = null, $params = null) {
|
||||||
|
@ -139,4 +139,38 @@ class HTMLTextTest extends SapphireTest {
|
|||||||
$data = DBField::create_field('HTMLText', '"this is a test"');
|
$data = DBField::create_field('HTMLText', '"this is a test"');
|
||||||
$this->assertEquals($data->ATT(), '"this is a test"');
|
$this->assertEquals($data->ATT(), '"this is a test"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testExists() {
|
||||||
|
$h = new HTMLText;
|
||||||
|
$h->setValue("");
|
||||||
|
$this->assertFalse($h->exists());
|
||||||
|
$h->setValue("<p></p>");
|
||||||
|
$this->assertFalse($h->exists());
|
||||||
|
$h->setValue("<p> </p>");
|
||||||
|
$this->assertFalse($h->exists());
|
||||||
|
$h->setValue("<h2/>");
|
||||||
|
$this->assertFalse($h->exists());
|
||||||
|
$h->setValue("<h2></h2>");
|
||||||
|
$this->assertFalse($h->exists());
|
||||||
|
|
||||||
|
$h->setValue("something");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
$h->setValue("<img src=\"dummy.png\">");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
$h->setValue("<img src=\"dummy.png\"><img src=\"dummy.png\">");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
$h->setValue("<p><img src=\"dummy.png\"></p>");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
|
||||||
|
$h->setValue("<iframe src=\"http://www.google.com\"></iframe>");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
$h->setValue("<embed src=\"test.swf\">");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
$h->setValue("<object width=\"400\" height=\"400\" data=\"test.swf\"></object>");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
|
||||||
|
|
||||||
|
$h->setValue("<p>test</p>");
|
||||||
|
$this->assertTrue($h->exists());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user