2009-10-13 03:44:41 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\View\Tests\Parsers;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\View\Parsers\HTML4Value;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class HTML4ValueTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testInvalidHTMLSaving()
|
|
|
|
{
|
|
|
|
$value = new HTML4Value();
|
2013-03-11 21:06:20 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$invalid = [
|
|
|
|
'<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>'
|
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($invalid as $input => $expected) {
|
|
|
|
$value->setContent($input);
|
|
|
|
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be saved');
|
|
|
|
}
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testUtf8Saving()
|
|
|
|
{
|
|
|
|
$value = new HTML4Value();
|
2013-03-11 21:06:20 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$value->setContent('<p>ö ß ā い 家</p>');
|
|
|
|
$this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent());
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testInvalidHTMLTagNames()
|
|
|
|
{
|
|
|
|
$value = new HTML4Value();
|
2013-03-11 21:06:20 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$invalid = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'<p><div><a href="test-link"></p></div>',
|
|
|
|
'<html><div><a href="test-link"></a></a></html_>',
|
|
|
|
'""\'\'\'"""\'""<<<>/</<htmlbody><a href="test-link"<<>'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testMixedNewlines()
|
|
|
|
{
|
|
|
|
$value = new HTML4Value();
|
2013-03-11 21:06:20 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$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'
|
|
|
|
);
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAttributeEscaping()
|
|
|
|
{
|
|
|
|
$value = new HTML4Value();
|
2013-04-17 23:13:24 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$value->setContent('<a href="[]"></a>');
|
|
|
|
$this->assertEquals('<a href="[]"></a>', $value->getContent(), "'[' character isn't escaped");
|
2013-04-17 23:13:24 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$value->setContent('<a href="""></a>');
|
|
|
|
$this->assertEquals('<a href="""></a>', $value->getContent(), "'\"' character is escaped");
|
|
|
|
}
|
2019-01-25 03:55:03 +01:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|