mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Suppressed errors in SS_HTMLValue->setContent() so it can handle malformed HTML.
MINOR: Added tests for saving and managing invalid HTML with SS_HTMLValue. From: Andrew Short <andrewjshort@gmail.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88773 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
d4acf7c938
commit
77dc826278
@ -45,17 +45,10 @@ class SS_HTMLValue extends ViewableData {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function setContent($content) {
|
public function setContent($content) {
|
||||||
//This is a patch to prevent invalid HTML returning warnings.
|
return @$this->getDocument()->loadHTML(
|
||||||
//Error messages are disabled, and then re-enabled
|
|
||||||
$old_level=error_reporting();
|
|
||||||
error_reporting(0);
|
|
||||||
$value=$this->getDocument()->loadHTML(
|
|
||||||
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
|
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
|
||||||
"<body>$content</body></html>");
|
"<body>$content</body></html>"
|
||||||
error_reporting($old_level);
|
);
|
||||||
return $value;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
41
tests/integration/HTMLValueTest.php
Executable file
41
tests/integration/HTMLValueTest.php
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class SS_HTMLValueTest extends SapphireTest {
|
||||||
|
|
||||||
|
public function testInvalidHTMLSaving() {
|
||||||
|
$value = new SS_HTMLValue();
|
||||||
|
$invalid = array (
|
||||||
|
'<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p>',
|
||||||
|
'<p><div class="example"></div></p>' => '<p/><div class="example"/>',
|
||||||
|
'<html><html><body><falsetag "attribute=""attribute""">' => '<falsetag/>',
|
||||||
|
'<body<body<body>/bodu>/body>' => '/bodu>/body>'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($invalid as $input => $expected) {
|
||||||
|
$value->setContent($input);
|
||||||
|
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be saved');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvalidHTMLTagNames() {
|
||||||
|
$value = new SS_HTMLValue();
|
||||||
|
$invalid = array (
|
||||||
|
'<p><div><a href="test-link"></p></div>',
|
||||||
|
'<html><div><a href="test-link"></a></a></html_>',
|
||||||
|
'""\'\'\'"""\'""<<<>/</<htmlbody><a href="test-link"<<>'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($invalid as $input) {
|
||||||
|
$value->setContent($input);
|
||||||
|
$this->assertEquals (
|
||||||
|
'test-link',
|
||||||
|
$value->getElementsByTagName('a')->item(0)->getAttribute('href'),
|
||||||
|
'Link data can be extraced from malformed HTML'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user