Closed grych closed 6 years ago
A change here would be a quick fix.
defp build_attrs({attr, value}, attrs), do: ~s(#{attrs} #{attr}="#{value}")
could become
defp build_attrs({attr, value}, attrs), do: ~s(#{attrs} #{attr}="#{String.replace(value, "\"", "'")}")
Nope, it will change the attribute value, if you have one "two" three it will become one 'two' three, which is a different string.
I was thinking rather about something like:
if String.contains?(value, "\"") do
~s(#{attrs} #{attr}='#{value}')
else
~s(#{attrs} #{attr}="#{value}")
end
Good point. I can't think of a better solution.
It should do the trick, as no other escaping single or double quotes is permitted in the attribute value, as well as the value containing both single and double quote in the same time is not allowed.
@grych good catch! I think your solution should work good. Please go ahead with a PR. Thank you!
@philss will do!
Fixed in #182. Please let me know when it comes into the release, as I need to update deps for Drab. Thanks!
When parsing, Floki loses the information if the attribute was introduced as single or double quoted text. This is generating an issue when rendering attributes with double quotes inside.
Observe the example:
Attribute is retrieved correctly, as a proper Elixir string. But let's try to re-render the parsed HTML:
This is a different html than it was in the beginning. And the attribute value has changed:
This behaviour causes this issue: grych/drab#117. Notice that using quotes inside the attribute value is correct.
I understand that Floki does not store the information about single/double quotes used in the attribute, but maybe the simple workaround exists, like if there is a double quote inside the attribute value, render it with single quote?