Merge pull request #8729 from emteknetnz/bugfix/htmlvalue-getcontent

Ensure document is not falsey before attempting to clone it
This commit is contained in:
Maxime Rainville 2019-01-29 09:11:04 +13:00 committed by GitHub
commit a589bcb092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -35,7 +35,11 @@ abstract class HTMLValue extends ViewableData
*/
public function getContent()
{
$doc = clone $this->getDocument();
$document = $this->getDocument();
if (!$document) {
return '';
}
$doc = clone $document;
$xp = new DOMXPath($doc);
// If there's no body, the content is empty string

View File

@ -80,4 +80,19 @@ class HTML4ValueTest extends SapphireTest
$value->setContent('<a href="&quot;"></a>');
$this->assertEquals('<a href="&quot;"></a>', $value->getContent(), "'\"' character is escaped");
}
public function testGetContent()
{
$value = new HTML4Value();
$value->setContent('<p>This is valid</p>');
$this->assertEquals('<p>This is valid</p>', $value->getContent(), "Valid content is returned");
$value->setContent('<p?< This is not really valid but it will get parsed into something valid');
// can sometimes get a this state where HTMLValue->valid is false
// for instance if a content editor saves something really weird in a LiteralField
// we can manually get to this state via ->setInvalid()
$value->setInvalid();
$this->assertEquals('', $value->getContent(), "Blank string is returned when invalid");
}
}