1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\HTML5\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\HTML5\HTML5Value; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package framework |
10
|
|
|
* @subpackage tests |
11
|
|
|
*/ |
12
|
|
|
class HTML5ValueTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
public function testInvalidHTMLParsing() |
15
|
|
|
{ |
16
|
|
|
$value = new HTML5Value(); |
17
|
|
|
|
18
|
|
|
$invalid = [ |
19
|
|
|
'<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p><p></p>', |
20
|
|
|
'<meta content="text/html"></meta>' => '<meta content="text/html">', |
21
|
|
|
'<p><div class="example"></div></p>' => '<p></p><div class="example"></div><p></p>' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
foreach ($invalid as $input => $expected) { |
25
|
|
|
$value->setContent($input); |
26
|
|
|
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testUtf8Saving() |
31
|
|
|
{ |
32
|
|
|
$value = new HTML5Value(); |
33
|
|
|
|
34
|
|
|
$value->setContent('<p>ö ß ā い 家</p>'); |
35
|
|
|
$this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testWhitespaceHandling() |
39
|
|
|
{ |
40
|
|
|
$value = new HTML5Value(); |
41
|
|
|
|
42
|
|
|
$value->setContent('<p></p> <p></p>'); |
43
|
|
|
$this->assertEquals('<p></p> <p></p>', $value->getContent()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testInvalidHTMLTagNames() |
47
|
|
|
{ |
48
|
|
|
$value = new HTML5Value(); |
49
|
|
|
|
50
|
|
|
$invalid = [ |
51
|
|
|
'<p><div><a href="test-link"></p></div>', |
52
|
|
|
'<html><div><a href="test-link"></a></a></html_>' |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
foreach ($invalid as $input) { |
56
|
|
|
$value->setContent($input); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( |
59
|
|
|
'test-link', |
60
|
|
|
$value->getElementsByTagName('a')->item(0)->getAttribute('href'), |
|
|
|
|
61
|
|
|
'Link data can be extraced from malformed HTML' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testMixedNewlines() |
67
|
|
|
{ |
68
|
|
|
$value = new HTML5Value(); |
69
|
|
|
|
70
|
|
|
$value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>"); |
71
|
|
|
$this->assertEquals( |
72
|
|
|
"<p>paragraph</p>\n<ul><li>1</li>\n</ul>", |
73
|
|
|
$value->getContent(), |
74
|
|
|
'Newlines get converted' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|