Enclosed Value

a' => '

Enclosed Value

a

', '' => '', '

' => '

', '' => '', '/bodu>/body>' => '/bodu>/body>' ]; foreach ($invalid as $input => $expected) { $value->setContent($input); $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); } } public function testUtf8Saving() { $value = new HTMLValue(); $value->setContent('

ö ß ā い 家

'); $this->assertEquals('

ö ß ā い 家

', $value->getContent()); } public function testWhitespaceHandling() { $value = new HTMLValue(); $value->setContent('

'); $this->assertEquals('

', $value->getContent()); } public function testInvalidHTMLTagNames() { $value = new HTMLValue(); $invalid = [ '

', '
' ]; 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 HTMLValue(); $value->setContent("

paragraph

\n
  • 1
  • \r\n
"); $this->assertEquals( "

paragraph

\n
  • 1
  • \n
", $value->getContent(), 'Newlines get converted' ); } public function testAttributeEscaping() { $value = new HTMLValue(); $value->setContent(''); $this->assertEquals('', $value->getContent(), "'[' character isn't escaped"); $value->setContent(''); $this->assertEquals('', $value->getContent(), "'\"' character is escaped"); } public function testShortcodeValue() { ShortcodeParser::get('default')->register( 'test_shortcode', function () { return 'bit of test shortcode output'; } ); $content = DBHTMLText::create('Test', ['shortcodes' => true]) ->setValue('

Some content with a [test_shortcode] and a
followed by an


in it.

') ->forTemplate(); $this->assertStringContainsString( // hr is flow content, not phrasing content, so must be corrected to be outside the p tag. '

Some content with a bit of test shortcode output and a
followed by an


in it.', $content ); } public function testEntities() { $content = 'ampersand & test & link'; $output = new HTMLValue($content); $output = $output->getContent(); $this->assertEquals( 'ampersand & test & link', $output ); } public function testShortcodeEntities() { ShortcodeParser::get('default')->register( 'sitetree_link_test', // A mildly stubbed copy from SilverStripe\CMS\Model\SiteTree::link_shortcode_handler function ($arguments, $content = null, $parser = null) { $link = Convert::raw2att('https://google.com/search?q=unit&test'); if ($content) { $link = sprintf('%s', $link, $parser->parse($content)); } return $link; } ); $content = [ '[sitetree_link_test,id=2]' => 'https://google.com/search?q=unit&test', // the random [ triggers the shortcode parser, which seems to be where problems arise. ' [ non shortcode link' => ' [ non shortcode link', '[sitetree_link_test,id=1]test link[/sitetree_link_test]' => 'test link' ]; foreach ($content as $input => $expected) { $output = DBHTMLText::create('Test', ['shortcodes' => true]) ->setValue($input) ->forTemplate(); $this->assertEquals($expected, $output); } } public function testValidHTMLInNoscriptTags() { $value = new HTMLValue(); $noscripts = [ '', '', '', ]; foreach ($noscripts as $noscript) { $value->setContent($noscript); $this->assertEquals($noscript, $value->getContent(), 'Child tags are left untouched in noscript tags.'); } } }