mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUG Fixing regression in 26d70d6fca with formatted output in SS_HTMLValue
If formatOutput is set to TRUE, then the regexes in getContent() will not match the newlines, and the output will include html, body and meta tags. Introduce a few new tests to ensure the output is correct, and fix the regex.
This commit is contained in:
parent
574c53d5ba
commit
a171c7e4b0
@ -17,14 +17,30 @@ class SS_HTMLValue extends ViewableData {
|
||||
* @param string $content
|
||||
*/
|
||||
public function __construct($content = null) {
|
||||
$this->document = new DOMDocument('1.0', 'UTF-8');
|
||||
$this->document->scrictErrorChecking = false;
|
||||
|
||||
$this->setDocument(new DOMDocument('1.0', 'UTF-8'));
|
||||
$this->setScrictErrorChecking(false);
|
||||
$this->setOutputFormatting(false);
|
||||
$this->setContent($content);
|
||||
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Should strict error checking be used?
|
||||
* @param boolean $bool
|
||||
*/
|
||||
public function setScrictErrorChecking($bool) {
|
||||
$this->getDocument()->scrictErrorChecking = $bool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the output be formatted?
|
||||
* @param boolean $bool
|
||||
*/
|
||||
public function setOutputFormatting($bool) {
|
||||
$this->getDocument()->formatOutput = $bool;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@ -35,16 +51,15 @@ class SS_HTMLValue extends ViewableData {
|
||||
return trim(
|
||||
preg_replace(
|
||||
array(
|
||||
'/^<!DOCTYPE.+?>/i',
|
||||
'/(.*)<body>/i',
|
||||
'/<\/body>(.*)/i',
|
||||
'/(.*)<body>/is',
|
||||
'/<\/body>(.*)/is',
|
||||
),
|
||||
'',
|
||||
urldecode($this->getDocument()->saveHTML())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return bool
|
||||
@ -59,14 +74,21 @@ class SS_HTMLValue extends ViewableData {
|
||||
"<body>$content</body></html>"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return DOMDocument
|
||||
*/
|
||||
public function getDocument() {
|
||||
return $this->document;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DOMDocument $document
|
||||
*/
|
||||
public function setDocument($document) {
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple convenience wrapper around DOMDocument::getElementsByTagName().
|
||||
*
|
||||
|
@ -6,9 +6,10 @@
|
||||
class SS_HTMLValueTest extends SapphireTest {
|
||||
|
||||
public function testInvalidHTMLSaving() {
|
||||
$value = new SS_HTMLValue();
|
||||
$value = new SS_HTMLValue();
|
||||
$invalid = array (
|
||||
'<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p>',
|
||||
'<meta content="text/html"></meta>' => '<meta content="text/html">',
|
||||
'<p><div class="example"></div></p>' => '<p></p><div class="example"></div>',
|
||||
'<html><html><body><falsetag "attribute=""attribute""">' => '<falsetag></falsetag>',
|
||||
'<body<body<body>/bodu>/body>' => '/bodu>/body>'
|
||||
@ -19,9 +20,22 @@ class SS_HTMLValueTest extends SapphireTest {
|
||||
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be saved');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function testUtf8Saving() {
|
||||
$value = new SS_HTMLValue();
|
||||
$value->setContent('<p>ö ß ā い 家</p>');
|
||||
$this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent());
|
||||
}
|
||||
|
||||
public function testOutputFormatting() {
|
||||
$value = new SS_HTMLValue();
|
||||
$value->setOutputFormatting(true);
|
||||
$value->setContent('<meta content="text/html">');
|
||||
$this->assertEquals('<meta content="text/html">', $value->getContent(), 'Formatted output works');
|
||||
}
|
||||
|
||||
public function testInvalidHTMLTagNames() {
|
||||
$value = new SS_HTMLValue();
|
||||
$value = new SS_HTMLValue();
|
||||
$invalid = array(
|
||||
'<p><div><a href="test-link"></p></div>',
|
||||
'<html><div><a href="test-link"></a></a></html_>',
|
||||
@ -30,7 +44,7 @@ class SS_HTMLValueTest extends SapphireTest {
|
||||
|
||||
foreach($invalid as $input) {
|
||||
$value->setContent($input);
|
||||
$this->assertEquals (
|
||||
$this->assertEquals(
|
||||
'test-link',
|
||||
$value->getElementsByTagName('a')->item(0)->getAttribute('href'),
|
||||
'Link data can be extraced from malformed HTML'
|
||||
@ -47,5 +61,5 @@ class SS_HTMLValueTest extends SapphireTest {
|
||||
'Newlines get converted'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user