g-plane / markup_fmt

Configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks and Vento formatter with dprint integration.
https://dprint.dev/plugins/markup_fmt/
MIT License
100 stars 9 forks source link

Fix invalid html when formatting attribute value containing single and double quotes #36

Closed UnknownPlatypus closed 3 months ago

UnknownPlatypus commented 3 months ago

markup_fmt parses just fine and attribute containing single & double quote (often happens for any kind of interpolation inside the value) but then try to switch the quote style when formatting resulting in cropping the attribute value.

For ex here, data-content is cropped to '{% if status == "active" %}I':

-<div data-content="{% if status == "active" %}I'd like it{% endif %}"></div>
+<div data-content='{% if status == "active" %}I'd like it{% endif %}'></div>

I'm not very happy with my fix, format_attr_value has a lot of fields now but at least it work. Feel free to solve this another way, you'll probably have better ideas.

g-plane commented 3 months ago

Sorry I'm busy on something else so your PRs will be reviewed later.

UnknownPlatypus commented 3 months ago

No worries, take you time. Thanks for the tool, I'm enjoying a lot poking around with it

g-plane commented 3 months ago

You're right. This solution isn't elegant enough, but after thinking I found it may be hard to avoid storing original quotes. However, if we decide to store original quotes to AST, the rest of implementation can be better. Should I create another PR or continue working on this PR?

UnknownPlatypus commented 3 months ago

Feel free to open a new PR, I feel like you'll have some smart ideas to solve this.

I also don't see a way to avoid storing the original quote in the ast, when the printer discover there are both quote styles in the same string, it must be aware of the original one to avoid breakage.

UnknownPlatypus commented 3 months ago

Superseded by #37