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

BUG Properly handle empty image attributes #474

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static function regenerate_shortcode($args, $content, $parser, $shortcode
// Rebuild shortcode
$parts = [];
foreach ($args as $name => $value) {
$htmlValue = Convert::raw2att($value ?: $name);
$htmlValue = Convert::raw2att($value);
$parts[] = sprintf('%s="%s"', $name, $htmlValue);
}
return sprintf("[%s %s]", $shortcode, implode(' ', $parts));
Expand Down
33 changes: 33 additions & 0 deletions tests/php/Shortcodes/ImageShortcodeProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,39 @@ public function testShortcodeHandlerUsesShortcodeProperties()
);
}

public function testShorcodeRegenrator()
{
$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$parser = new ShortcodeParser();
$parser->register('image', [ImageShortcodeProvider::class, 'regenerate_shortcode']);

$this->assertEquals(
sprintf(
'[image id="%d" alt="My alt text" title="My Title & special character" src="%s"]',
$image->ID,
$image->Link()
),
$parser->parse(sprintf(
'[image id="%d" alt="My alt text" title="My Title & special character"]',
$image->ID
)),
'Shortcode regeneration properly reads attributes'
);

$this->assertEquals(
sprintf(
'[image id="%d" alt="" title="" src="%s"]',
$image->ID,
$image->Link()
),
$parser->parse(sprintf(
'[image id="%d" alt="" title=""]',
$image->ID
)),
"Shortcode regeneration properly handles empty attributes"
);
}

public function testShortcodeHandlerAddsDefaultAttributes()
{
$image = $this->objFromFixture(Image::class, 'imageWithoutTitle');
Expand Down