-
Notifications
You must be signed in to change notification settings - Fork 641
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
Twig attr
filter encoding href
and src
attributes
#4984
Comments
In your example, the correct behavior should be to encode the However if it already is encoded as |
Ah! You're absolutely correct. And the double |
@brandonkelly i'm not sure if this is related, a separate issue or me not understanding something but seeing some inconsistencies between rendering HTML chars when run using the <input {{ attr({
type: "submit",
value: "Go →",
class: "btn btn-green cursor-pointer"
}) }}>
<input type="submit" value="Go →" class="btn btn-green cursor-pointer"> I would expect these two submit buttons should render exactly the same, but they don't? |
@samhibberd the <input {{ attr({
type: "submit",
value: "Go →",
class: "btn btn-green cursor-pointer"
})|raw }}> or just use the tag() function: {{ tag('input', {
type: "submit",
value: "Go →",
class: "btn btn-green cursor-pointer"
}) }} |
Hey @brandonkelly, tried that and the input tag, same deal for me, I must be missing something really obvious :( Tested the following in a fresh template, with no other content, and only the first raw html version outputs correctly: <input type="submit" value="Go →">
<input {{ attr({
type: "submit",
value: "Go →",
}) }}>
<input {{ attr({
type: "submit",
value: "Go →",
})|raw }}>
{{ tag('input', {
type: "submit",
value: "Go →",
}) }} |
Sorry, this is actually expected behavior. I forgot that <input {{ attr({
type: "submit",
value: "Go →",
}) }}> |
Cool. Thanks @brandonkelly |
Description
The new
attr
filter has the (in my opinion) unfortunate side effect of HTML encoding all attribute values for the tag that the filter operates on (as in, all of them – not only the attributes you're adding or modifying using the filter).I know this is due to Yii's
BaseHtml::renderTagAttributes()
method running all the values throughhtmlspecialchars()
, so I don't know if a fix on Craft's end is feasible. But, this unexpected behaviour can definitely lead to some gotcha moments, especially for attributes likesrc
andhref
, where e.g. ampersands in query strings being encoded to&
can be an issue (I just realized some of my Imgix URLs were failing due to this).Steps to reproduce
Renders
<img class="foo" src="image.jpg?width=100&height=100" />
Additional info
The text was updated successfully, but these errors were encountered: