diff --git a/src/View/Shortcodes/EmbedShortcodeProvider.php b/src/View/Shortcodes/EmbedShortcodeProvider.php index 8dec5b8b3..032fd7cd0 100644 --- a/src/View/Shortcodes/EmbedShortcodeProvider.php +++ b/src/View/Shortcodes/EmbedShortcodeProvider.php @@ -5,6 +5,9 @@ namespace SilverStripe\View\Shortcodes; use Embed\Http\DispatcherInterface; use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; +use SilverStripe\ORM\ArrayList; +use SilverStripe\ORM\FieldType\DBField; +use SilverStripe\View\ArrayData; use SilverStripe\View\Embed\Embeddable; use SilverStripe\View\Embed\EmbedResource; use SilverStripe\View\HTML; @@ -138,16 +141,13 @@ class EmbedShortcodeProvider implements ShortcodeHandler $arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;'; } - // Convert caption to

- if (!empty($arguments['caption'])) { - $xmlCaption = Convert::raw2xml($arguments['caption']); - $content .= "\n

{$xmlCaption}

"; - } - unset($arguments['width']); - unset($arguments['height']); - unset($arguments['url']); - unset($arguments['caption']); - return HTML::createTag('div', $arguments, $content); + $data = [ + 'Arguments' => $arguments, + 'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']), + 'Content' => DBField::create_field('HTMLFragment', $content) + ]; + + return ArrayData::create($data)->renderWith(self::class . '_video')->forTemplate(); } /** @@ -160,13 +160,14 @@ class EmbedShortcodeProvider implements ShortcodeHandler */ protected static function linkEmbed($arguments, $href, $title) { - $title = !empty($arguments['caption']) ? ($arguments['caption']) : $title; - unset($arguments['caption']); - unset($arguments['width']); - unset($arguments['height']); - unset($arguments['url']); - $arguments['href'] = $href; - return HTML::createTag('a', $arguments, Convert::raw2xml($title)); + $data = [ + 'Arguments' => $arguments, + 'Attributes' => static::buildAttributeListFromArguments($arguments, ['width', 'height', 'url', 'caption']), + 'Href' => $href, + 'Title' => !empty($arguments['caption']) ? ($arguments['caption']) : $title + ]; + + return ArrayData::create($data)->renderWith(self::class . '_link')->forTemplate(); } /** @@ -178,8 +179,37 @@ class EmbedShortcodeProvider implements ShortcodeHandler */ protected static function photoEmbed($arguments, $src) { - $arguments['src'] = $src; - unset($arguments['url']); - return HTML::createTag('img', $arguments); + $data = [ + 'Arguments' => $arguments, + 'Attributes' => static::buildAttributeListFromArguments($arguments, ['url']), + 'Src' => $src + ]; + + return ArrayData::create($data)->renderWith(self::class . '_photo')->forTemplate(); + } + + /** + * Build a list of HTML attributes from embed arguments - used to preserve backward compatibility + * + * @deprecated 4.5.0 Use {$Arguments.name} directly in shortcode templates to access argument values + * @param array $arguments List of embed arguments + * @param array $exclude List of attribute names to exclude from the resulting list + * @return ArrayList + */ + private static function buildAttributeListFromArguments(array $arguments, array $exclude = []): ArrayList + { + $attributes = ArrayList::create(); + foreach ($arguments as $key => $value) { + if (in_array($key, $exclude)) { + continue; + } + + $attributes->push(ArrayData::create([ + 'Name' => $key, + 'Value' => Convert::raw2att($value) + ])); + } + + return $attributes; } } diff --git a/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_link.ss b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_link.ss new file mode 100644 index 000000000..7d7db878c --- /dev/null +++ b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_link.ss @@ -0,0 +1,6 @@ + {$Name}="{$Value}"<% end_loop %> +> + {$Title} + diff --git a/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_photo.ss b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_photo.ss new file mode 100644 index 000000000..ac5cce976 --- /dev/null +++ b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_photo.ss @@ -0,0 +1,4 @@ + {$Name}="{$Value}"<% end_loop %> +/> diff --git a/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_video.ss b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_video.ss new file mode 100644 index 000000000..c526f1640 --- /dev/null +++ b/templates/SilverStripe/View/Shortcodes/EmbedShortcodeProvider_video.ss @@ -0,0 +1,8 @@ +
{$Name}="{$Value}"<% end_loop %> +> + {$Content} + <% if $Arguments.caption %> +

{$Arguments.caption}

+ <% end_if %> +
diff --git a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php index 9826261b3..58ea889ed 100644 --- a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php +++ b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php @@ -25,6 +25,11 @@ class EmbedShortcodeProviderTest extends SapphireTest */ protected static $test_soundcloud = 'http://soundcloud.com/napalmrecords/delain-suckerpunch'; + public function assertEqualIgnoringWhitespace($a, $b, $message = '') + { + $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message); + } + public function testYoutube() { /** @var string $result */ @@ -51,7 +56,7 @@ class EmbedShortcodeProviderTest extends SapphireTest 'height' => 270, ] ); - $this->assertEquals( + $this->assertEqualIgnoringWhitespace( <<

A nice video

@@ -81,7 +86,7 @@ EOS 'author_url' => 'http://soundcloud.com/napalmrecords', ] ); - $this->assertEquals( + $this->assertEqualIgnoringWhitespace( << EOS