-
Notifications
You must be signed in to change notification settings - Fork 83
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
Prismic\Dom\RichText <img> width & height attributes #204
Conversation
@@ -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 .'">'; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Hey @husseinalhammad Thank you for your contribution to the package 👍🏼
@angeloashmore How would you proceed with this? How is this handled in the Javascript Packages? |
Good point. Regardless of whether the Prismic API always returns these properties, one might want to use $html = RichText::asHtml([
(object) [
'type' => 'paragraph',
'text' => 'Some text',
'spans' => [],
],
(object) [
'type' => 'image',
'url' => 'https://example.com/image.png',
]
]); So I think it's a good idea to only add these tag attributes if the data is present.
If this change is added in a future release, the release notes can warn developers of this change. I don't see this as a blocker. Do you prefer to make this behaviour optional and not add the tag attributes by default? |
@husseinalhammad Thanks for your reply! Yes, I would definitely like for you to add the null checks on the dimension attributes. I'm not sure if "warning the developers" is enough and if this can be added in a minor release. Changing the width and height of an image could severely break some designs. Furthermore, I fixed the CI in #207. Once this gets approved, the actions should also automatically test this PR. |
@c0nst4ntin The JavaScript SDK excludes the I don't know why we originally omitted In this case, it might be safest to keep the serializer as it is today without the As an aside, the React, Vue, Svelte, and Next.js SDKs have a dedicated |
@angeloashmore So I take it, your comment is aligned with my concerns of breaking people's website designs by introducing this behavior? How do we proceed in this case? Do we go ahead and close this issue? As far as I know, there is not a way to pass a custom serializer to the RichText Dom Class. Extending it is also not easily possible, as the methods are private instead of protected. |
Correct, your concern is valid since this change could unexpectedly break projects. I think either of the following options are reasonable:
Of the two, I lean more toward Option 2, but only if you think a breaking change won't cause unnecessary distress. With Prismic's official SDKs, we tend to be more conservative and avoid breaking changes as much as possible, offering simple workarounds or examples as needed (i.e. Option 1). Ultimately, the decision is up to you, so go with whatever you are more comfortable with. 🙂 |
@husseinalhammad Thank you very much for your efforts, but as discussed with @angeloashmore, this would be considered a breaking change. As of right now, I don't see this change alone justifying a major version. However, if I were to ever start working on a new version, this could definitely be implemented. Have you tried using the custom serializer? Maybe that is a way for you to use your changes in your own project? I will proceed to close this pull request for now. However, I still really value the time and effort you put into this, and I might revisit the topic at a later point in time. If you have any further troubles or want to suggest something different, feel free to open a new issue or pull request and discuss things with me and the team. |
No problem at all. Thank you both for your time. I have been using the custom serializer option before submitting this PR. It works as expected. |
This PR adds the
width
andheight
attributes to the<img>
tag returned byRichText::asHtml()
. This prevents a layout shift when the image is downloaded and rendered, and is considered best practice.