Skip to content

Commit

Permalink
Fix for image not rendering 'id' attribute - #956
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 11, 2016
1 parent f99f42a commit deda94a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
* Improved `authorize` Twig extension to accept a nested array of authorizations [#948](https://github.com/getgrav/grav/issues/948)
* Don't add timestamps on remote assets as it can cause conflicts
* Grav now looks at types from `media.yaml` when retrieving page mime types [#966](https://github.com/getgrav/grav/issues/966)
* Added support for dumping exceptions in the Debugger
1. [](#bugfix)
* Fixed `Folder::delete` method to recursively remove files and folders and causing Upgrade to fail.
* Fix [#952](https://github.com/getgrav/grav/issues/952) hypenize the session name.
* If no parent is set and siblings collection is called, return a new and empty collection [grav-plugin-sitemap/issues/22](https://github.com/getgrav/grav-plugin-sitemap/issues/22)
* Prevent exception being thrown when calling the Collator constructor failed in a Windows environment with the Intl PHP Extension enabled [#961](https://github.com/getgrav/grav/issues/961)
* Fix for markdown images not properly rendering `id` attribute [#956](https://github.com/getgrav/grav/issues/956)

# v1.1.1
## 07/16/2016
Expand Down
3 changes: 2 additions & 1 deletion system/src/Grav/Common/Markdown/ParsedownGravTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ protected function inlineImage($excerpt)
$alt = $excerpt['element']['attributes']['alt'] ?: '';
$title = $excerpt['element']['attributes']['title'] ?: '';
$class = isset($excerpt['element']['attributes']['class']) ? $excerpt['element']['attributes']['class'] : '';
$id = isset($excerpt['element']['attributes']['id']) ? $excerpt['element']['attributes']['id'] : '';

//get the url and parse it
$url = parse_url(htmlspecialchars_decode($excerpt['element']['attributes']['src']));
Expand Down Expand Up @@ -264,7 +265,7 @@ protected function inlineImage($excerpt)
$medium->urlHash($url['fragment']);
}

$excerpt['element'] = $medium->parseDownElement($title, $alt, $class, true);
$excerpt['element'] = $medium->parseDownElement($title, $alt, $class, $id, true);

} else {
// not a current page media file, see if it needs converting to relative
Expand Down
5 changes: 3 additions & 2 deletions system/src/Grav/Common/Page/Medium/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ public function __construct(array $attributes, Medium $medium)
* @param string $title
* @param string $alt
* @param string $class
* @param string $id
* @param boolean $reset
* @return array
*/
public function parsedownElement($title = null, $alt = null, $class = null, $reset = true)
public function parsedownElement($title = null, $alt = null, $class = null, $id = null, $reset = true)
{
$innerElement = $this->source->parsedownElement($title, $alt, $class, $reset);
$innerElement = $this->source->parsedownElement($title, $alt, $class, $id, $reset);

return [
'name' => 'a',
Expand Down
12 changes: 11 additions & 1 deletion system/src/Grav/Common/Page/Medium/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ public function urlHash($hash = null, $withHash = true)
* @param string $title
* @param string $alt
* @param string $class
* @param string $id
* @param boolean $reset
* @return array
*/
public function parsedownElement($title = null, $alt = null, $class = null, $reset = true)
public function parsedownElement($title = null, $alt = null, $class = null, $id = null, $reset = true)
{
$attributes = $this->attributes;

Expand Down Expand Up @@ -241,6 +242,14 @@ public function parsedownElement($title = null, $alt = null, $class = null, $res
}
}

if (empty($attributes['id'])) {
if (!empty($id)) {
$attributes['id'] = $id;
} elseif (!empty($this->items['id'])) {
$attributes['id'] = $this->items['id'];
}
}

switch ($this->mode) {
case 'text':
$element = $this->textParsedownElement($attributes, false);
Expand Down Expand Up @@ -417,6 +426,7 @@ public function classes()
*/
public function id($id)
{
xdebug_break();
if (is_string($id)) {
$this->attributes['id'] = trim($id);
}
Expand Down
6 changes: 4 additions & 2 deletions system/src/Grav/Common/Page/Medium/ParsedownHtmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ trait ParsedownHtmlTrait
* Return HTML markup from the medium.
*
* @param string $title
* @param string $alt
* @param string $class
* @param string $id
* @param bool $reset
* @return string
*/
public function html($title = null, $alt = null, $class = null, $reset = true)
public function html($title = null, $alt = null, $class = null, $id = null, $reset = true)
{
$element = $this->parsedownElement($title, $alt, $class, $reset);
$element = $this->parsedownElement($title, $alt, $class, $id, $reset);

if (!$this->parsedown) {
$this->parsedown = new Parsedown(null, null);
Expand Down
3 changes: 2 additions & 1 deletion system/src/Grav/Common/Page/Medium/RenderableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public function html($title = null, $alt = null, $class = null, $reset = true);
* @param string $title
* @param string $alt
* @param string $class
* @param string $id
* @param bool $reset
* @return string
*/
public function parsedownElement($title = null, $alt = null, $class = null, $reset = true);
public function parsedownElement($title = null, $alt = null, $class = null, $id = null, $reset = true);
}
11 changes: 6 additions & 5 deletions system/src/Grav/Common/Page/Medium/ThumbnailImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public function srcset($reset = true)
/**
* Get an element (is array) that can be rendered by the Parsedown engine
*
* @param string $title
* @param string $alt
* @param string $class
* @param string $title
* @param string $alt
* @param string $class
* @param string $id
* @param boolean $reset
* @return array
*/
public function parsedownElement($title = null, $alt = null, $class = null, $reset = true)
public function parsedownElement($title = null, $alt = null, $class = null, $id = null, $reset = true)
{
return $this->bubble('parsedownElement', [$title, $alt, $class, $reset]);
return $this->bubble('parsedownElement', [$title, $alt, $class, $id, $reset]);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Grav/Common/Markdown/ParsedownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ public function testImagesSubDirAbsoluteUrls()
$this->parsedown->text('![](/home-missing-image.jpg)'));
}

public function testImagesAttributes()
{
$this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();

$this->assertSame('<p><img title="My Title" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg "My Title")'));
$this->assertSame('<p><img class="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg?classes=foo)'));
$this->assertSame('<p><img class="foo bar" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg?classes=foo,bar)'));
$this->assertSame('<p><img id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![](sample-image.jpg?id=foo)'));
$this->assertSame('<p><img alt="Alt Text" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![Alt Text](sample-image.jpg?id=foo)'));
$this->assertSame('<p><img alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![Alt Text](sample-image.jpg?class=bar&id=foo)'));
$this->assertSame('<p><img title="My Title" alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
$this->parsedown->text('![Alt Text](sample-image.jpg?class=bar&id=foo "My Title")'));
}


public function testRootImages()
{
$this->uri->initializeWithURL('http://testing.dev/')->init();
Expand Down

0 comments on commit deda94a

Please sign in to comment.