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 | use SilverStripe\Core\Convert; |
||
10 | |||
11 | /** |
||
12 | * @package framework |
||
13 | * @subpackage tests |
||
14 | */ |
||
15 | class HTML5ValueTest extends SapphireTest |
||
16 | { |
||
17 | public function testInvalidHTMLParsing() |
||
18 | { |
||
19 | $value = new HTML5Value(); |
||
20 | |||
21 | $invalid = [ |
||
22 | '<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p><p></p>', |
||
23 | '<meta content="text/html"></meta>' => '<meta content="text/html">', |
||
24 | '<p><div class="example"></div></p>' => '<p></p><div class="example"></div><p></p>' |
||
25 | ]; |
||
26 | |||
27 | foreach ($invalid as $input => $expected) { |
||
28 | $value->setContent($input); |
||
29 | $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | public function testUtf8Saving() |
||
34 | { |
||
35 | $value = new HTML5Value(); |
||
36 | |||
37 | $value->setContent('<p>ö ß ā い 家</p>'); |
||
38 | $this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent()); |
||
39 | } |
||
40 | |||
41 | public function testWhitespaceHandling() |
||
42 | { |
||
43 | $value = new HTML5Value(); |
||
44 | |||
45 | $value->setContent('<p></p> <p></p>'); |
||
46 | $this->assertEquals('<p></p> <p></p>', $value->getContent()); |
||
47 | } |
||
48 | |||
49 | public function testInvalidHTMLTagNames() |
||
50 | { |
||
51 | $value = new HTML5Value(); |
||
52 | |||
53 | $invalid = [ |
||
54 | '<p><div><a href="test-link"></p></div>', |
||
55 | '<html><div><a href="test-link"></a></a></html_>' |
||
56 | ]; |
||
57 | |||
58 | foreach ($invalid as $input) { |
||
59 | $value->setContent($input); |
||
60 | |||
61 | $this->assertEquals( |
||
62 | 'test-link', |
||
63 | $value->getElementsByTagName('a')->item(0)->getAttribute('href'), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
64 | 'Link data can be extraced from malformed HTML' |
||
65 | ); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function testMixedNewlines() |
||
70 | { |
||
71 | $value = new HTML5Value(); |
||
72 | |||
73 | $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>"); |
||
74 | $this->assertEquals( |
||
75 | "<p>paragraph</p>\n<ul><li>1</li>\n</ul>", |
||
76 | $value->getContent(), |
||
77 | 'Newlines get converted' |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | public function testShortcodeValue() |
||
82 | { |
||
83 | ShortcodeParser::get('default')->register( |
||
84 | 'test_shortcode', |
||
85 | function () { |
||
86 | return 'bit of test shortcode output'; |
||
87 | } |
||
88 | ); |
||
89 | $content = DBHTMLText::create('Test', ['shortcodes' => true]) |
||
90 | ->setValue('<p>Some content with a [test_shortcode] and a <br /> followed by an <hr> in it.</p>') |
||
91 | ->forTemplate(); |
||
92 | $this->assertContains( |
||
93 | // hr is flow content, not phrasing content, so must be corrected to be outside the p tag. |
||
94 | '<p>Some content with a bit of test shortcode output and a <br> followed by an </p><hr> in it.', |
||
95 | $content |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | public function testEntities() |
||
100 | { |
||
101 | $content = '<a href="http://domain.test/path?two&vars">ampersand & test & link</a>'; |
||
102 | $output = new HTML5Value($content); |
||
103 | $output = $output->getContent(); |
||
104 | $this->assertEquals( |
||
105 | '<a href="http://domain.test/path?two&vars">ampersand & test & link</a>', |
||
106 | $output |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | public function testShortcodeEntities() |
||
111 | { |
||
112 | ShortcodeParser::get('default')->register( |
||
113 | 'sitetree_link_test', |
||
114 | // A mildly stubbed copy from SilverStripe\CMS\Model\SiteTree::link_shortcode_handler |
||
115 | function ($arguments, $content = null, $parser = null) { |
||
116 | $link = Convert::raw2att('https://google.com/search?q=unit&test'); |
||
117 | if ($content) { |
||
118 | $link = sprintf('<a href="%s">%s</a>', $link, $parser->parse($content)); |
||
119 | } |
||
120 | return $link; |
||
121 | } |
||
122 | ); |
||
123 | $content = [ |
||
124 | '[sitetree_link_test,id=2]' => 'https://google.com/search?q=unit&test', |
||
125 | // the random [ triggers the shortcode parser, which seems to be where problems arise. |
||
126 | '<a href="https://google.com/search?q=unit&test"> [ non shortcode link</a>' => |
||
127 | '<a href="https://google.com/search?q=unit&test"> [ non shortcode link</a>', |
||
128 | '[sitetree_link_test,id=1]test link[/sitetree_link_test]' => |
||
129 | '<a href="https://google.com/search?q=unit&test">test link</a>' |
||
130 | ]; |
||
131 | foreach ($content as $input => $expected) { |
||
132 | $output = DBHTMLText::create('Test', ['shortcodes' => true]) |
||
133 | ->setValue($input) |
||
134 | ->forTemplate(); |
||
135 | $this->assertEquals($expected, $output); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 |