Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Double encoding image shortcode attributes #567

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
return in_array($k, $whitelist) && (strlen(trim($v ?? '')) || $k === 'alt');
}, ARRAY_FILTER_USE_BOTH);

$markup = HTML::createTag('img', $attrs);
$markup = self::createImageTag($attrs);

// cache it for future reference
if ($fileFound) {
Expand All @@ -131,6 +131,25 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
return $markup;
}

/**
* Construct and return HTML image tag.
*/
public static function createImageTag(array $attributes) : string
{
$preparedAttributes = '';
foreach ($attributes as $attributeKey => $attributeValue) {
if (strlen($attributeValue ?? '') > 0 || $attributeKey === 'alt') {
$preparedAttributes .= sprintf(
' %s="%s"',
$attributeKey,
htmlspecialchars($attributeValue ?? '', ENT_QUOTES, 'UTF-8', false)
);
}
}

return "<img{$preparedAttributes} />";
}

/**
* Regenerates "[image id=n]" shortcode with new src attribute prior to being edited within the CMS.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/php/Shortcodes/ImageShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,40 @@ public function testWhiteIsConfigurable()
))
);
}

public function gettersAndSettersProvider(): array
{
return [
'image without special characters' => [
'<img src="http://example.com/image.jpg" alt="My alt text" title="My Title" width="300" height="200" class="leftAlone ss-htmleditorfield-file image" />',
[
'src' => 'http://example.com/image.jpg',
'alt' => 'My alt text',
'title' => 'My Title',
'width' => '300',
'height' => '200',
'class' => 'leftAlone ss-htmleditorfield-file image',
],
],
'image with special characters' => [
'<img src="http://example.com/image.jpg" alt="My alt text &amp; special character" title="My Title &amp; special character" width="300" height="200" class="leftAlone ss-htmleditorfield-file image" />',
[
'src' => 'http://example.com/image.jpg',
'alt' => 'My alt text &amp; special character',
'title' => 'My Title & special character',
'width' => '300',
'height' => '200',
'class' => 'leftAlone ss-htmleditorfield-file image',
]
]
];
}

/**
* @dataProvider gettersAndSettersProvider
*/
public function testCreateImageTag(string $expected, array $attributes)
{
$this->assertEquals($expected, ImageShortcodeProvider::createImageTag($attributes));
}
}