mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
26d70d6fca
The issue was raised in #7628, where an anchor tag was being changed from <a name="anchor"></a> to <a name="anchor"/> by SS_HTMLValue, when HtmlEditorField::saveInto() parses the HTML fragments. This is because SS_HTMLValue uses DOMDocument::saveXML(), which is fine for saving an XML document, but not suitable for HTML. This fix changes that to use DOMDocument::saveHTML() instead. Note that we can't use the parameter to saveHTML() for selecting a single node only, as that's only supported in PHP 5.3.6+, SilverStripe 3.0 supports PHP 5.3.2 as a minimum. The workaround for this shortcoming is to replace unncessary output by DOMDocument with a regular expression.
52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* @package framework
|
|
* @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></p><div class="example"></div>',
|
|
'<html><html><body><falsetag "attribute=""attribute""">' => '<falsetag></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'
|
|
);
|
|
}
|
|
}
|
|
|
|
public function testMixedNewlines() {
|
|
$value = new SS_HTMLValue();
|
|
$value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>");
|
|
$this->assertEquals(
|
|
"<p>paragraph</p>\n<ul><li>1</li>\n</ul>",
|
|
$value->getContent(),
|
|
'Newlines get converted'
|
|
);
|
|
}
|
|
|
|
}
|