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

Prismic\Dom\RichText <img> width & height attributes #204

Closed
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
2 changes: 1 addition & 1 deletion src/Prismic/Dom/RichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static function serialize($element, $content, $linkResolver, $htmlSerial
case 'o-list-item':
return nl2br('<li' . $classCode . '>' . $content . '</li>');
case 'image':
$img = '<img src="' . $element->url . '" alt="' . htmlentities($element->alt) . '">';
$img = '<img src="' . $element->url . '" alt="' . htmlentities($element->alt ?? '') . '" width="'. $element->dimensions->width .'" height="'. $element->dimensions->height .'">';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be sure that dimensions and the properties width and height always exists?

If so, then everything is good. Otherwise we could use:

$element->dimensions?->width ?? ''

In case this get's changed we should ensure that there is also a testcase for this new scenario

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dimensions.width and dimensions.height properties should always exist in API responses. The null fallback is still a good idea just in case someone is working on a very old repository that might be running on an older version of the API.


$link = property_exists($element, 'linkTo') ? Link::asUrl($element->linkTo, $linkResolver) : null;

Expand Down
12 changes: 6 additions & 6 deletions tests/Prismic/Dom/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public function testAsHtml()
'<li>Ringo</li>' .
'</ol>' .
'<p class="block-img">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</p>' .
'<p class="block-img">' .
'<a href="https://prismic.io" target="_blank">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</a>' .
'</p>' .
'<p class="block-img">' .
'<a href="https://prismic.io">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</a>' .
'</p>' .
'<div data-oembed="https://www.youtube.com/watch?v=joA7VpZLQaQ" data-oembed-type="video" data-oembed-provider="youtube">' .
Expand Down Expand Up @@ -113,16 +113,16 @@ public function testHtmlSerializer()
'<li>Ringo</li>' .
'</ol>' .
'<p class="block-img">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</p>' .
'<p class="block-img">' .
'<a href="https://prismic.io" target="_blank">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</a>' .
'</p>' .
'<p class="block-img">' .
'<a href="https://prismic.io">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text">' .
'<img src="https://prismic-io.s3.amazonaws.com/levi-templeting/357366ce9af5fd05dcd0a76e6ee267fc46c08f6a_mi0003995354.jpg" alt="Alt text" width="400" height="400">' .
'</a>' .
'</p>' .
'<div data-oembed="https://www.youtube.com/watch?v=joA7VpZLQaQ" data-oembed-type="video" data-oembed-provider="youtube">' .
Expand Down