|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\HTML5\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
6
|
|
|
use SilverStripe\HTML5\HTML5Value; |
|
7
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
|
8
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @package framework |
|
12
|
|
|
* @subpackage tests |
|
13
|
|
|
*/ |
|
14
|
|
|
class HTML5ValueTest extends SapphireTest |
|
15
|
|
|
{ |
|
16
|
|
|
public function testInvalidHTMLParsing() |
|
17
|
|
|
{ |
|
18
|
|
|
$value = new HTML5Value(); |
|
19
|
|
|
|
|
20
|
|
|
$invalid = [ |
|
21
|
|
|
'<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p>', |
|
22
|
|
|
'<p><div class="example"></div></p>' => '<p></p><div class="example"></div>' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
foreach ($invalid as $input => $expected) { |
|
26
|
|
|
$value->setContent($input); |
|
27
|
|
|
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testUtf8Saving() |
|
32
|
|
|
{ |
|
33
|
|
|
$value = new HTML5Value(); |
|
34
|
|
|
|
|
35
|
|
|
$value->setContent('<p>ö ß ā い 家</p>'); |
|
36
|
|
|
$this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testWhitespaceHandling() |
|
40
|
|
|
{ |
|
41
|
|
|
$value = new HTML5Value(); |
|
42
|
|
|
|
|
43
|
|
|
$value->setContent('<p></p> <p></p>'); |
|
44
|
|
|
$this->assertEquals('<p></p> <p></p>', $value->getContent()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testInvalidHTMLTagNames() |
|
48
|
|
|
{ |
|
49
|
|
|
$value = new HTML5Value(); |
|
50
|
|
|
|
|
51
|
|
|
$invalid = [ |
|
52
|
|
|
'<p><div><a href="test-link"></p></div>', |
|
53
|
|
|
'<html><div><a href="test-link"></a></a></html_>' |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($invalid as $input) { |
|
57
|
|
|
$value->setContent($input); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals( |
|
60
|
|
|
'test-link', |
|
61
|
|
|
$value->getElementsByTagName('a')->item(0)->getAttribute('href'), |
|
|
|
|
|
|
62
|
|
|
'Link data can be extraced from malformed HTML' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testMixedNewlines() |
|
68
|
|
|
{ |
|
69
|
|
|
$value = new HTML5Value(); |
|
70
|
|
|
|
|
71
|
|
|
$value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>"); |
|
72
|
|
|
$this->assertEquals( |
|
73
|
|
|
"<p>paragraph</p>\n<ul><li>1</li>\n</ul>", |
|
74
|
|
|
$value->getContent(), |
|
75
|
|
|
'Newlines get converted' |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testShortcodeValue() |
|
80
|
|
|
{ |
|
81
|
|
|
ShortcodeParser::get('default')->register( |
|
82
|
|
|
'test_shortcode', |
|
83
|
|
|
function () { |
|
84
|
|
|
return 'bit of test shortcode output'; |
|
85
|
|
|
} |
|
86
|
|
|
); |
|
87
|
|
|
$content = DBHTMLText::create('Test', ['shortcodes'=>true]) |
|
|
|
|
|
|
88
|
|
|
->setValue('<p>Some content with a [test_shortcode] in it.</p>') |
|
89
|
|
|
->forTemplate(); |
|
90
|
|
|
$this->assertContains( |
|
91
|
|
|
'<p>Some content with a bit of test shortcode output in it.</p>', |
|
92
|
|
|
$content |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|