bram209 / leptosfmt

A formatter for the leptos view! macro
Apache License 2.0
271 stars 29 forks source link

Add possibility to have non void self closing HTML tags #123

Closed V4ldum closed 4 months ago

V4ldum commented 5 months ago

resolves #44

I added a parameter to enable/disable the feature. It is false by default but I can make it true. Please tell me if I missed or should improve some things !

bram209 commented 4 months ago

Hi, thank you for your contribution!

I would like to do this slightly differently. When I created the formatter, leptos was still using syn-rsx for it's html-like syntax. The problem with syn-rsx is that it would not capture the closing tags in its AST.

However, as the author of syn-rsx has unfortunately passed away, leptos is now using its improved fork rstml. This new fork does capture closing tags, so now it is possible to preserve the original closing tags by default.

I propose to add a configuration option named preserve_void_element_closing_tag that is by default set to false. Keep in mind that this goes directly against the HTML specification:

A void element is an element in HTML that cannot have any child nodes (i.e., nested elements or text nodes). Void elements only have a start tag; end tags must not be specified for void elements.

In HTML, a void element must not have an end tag. For example, <input type="text"></input> is invalid HTML.

bram209 commented 4 months ago

Ah wait, this is about non void elements... in that case the current approach is fine, except that I would like to preserve the closing tag (self-closing or not) based on the source. With the current PR it will format <div></div> to <div />.

Let's add the following option:

/// Preferred style for closing tags (self-closing or not) when a non-void element has no children
closing_tag_style: ClosingTagStyle

// ...

enum ClosingTagStyle {
  /// Preserve the original closing tag style (self-closing or a separate closing tag)
  Preserve,
  /// Self closing tag for elements with no children: `<div></div>` formats to `<div />`
  SelfClosing,
  /// Separate closing tag for elements with no children: `<div />` formats to `<div></div>`
  NonSelfClosing,
}
V4ldum commented 4 months ago

I think I fixed all the issues reported (except the macro, cf the discussion on that topic). I took the liberty to add some tests for the non closing style too, but commented those out while the macro topic is not resolved

V4ldum commented 4 months ago

Everything should be good now, thank you very much for the feedback !