Skip to content
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

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

Merged
merged 9 commits into from
Jul 15, 2024
Merged

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

merged 9 commits into from
Jul 15, 2024

Conversation

V4ldum
Copy link
Contributor

@V4ldum V4ldum commented Jun 23, 2024

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
Copy link
Owner

bram209 commented Jul 8, 2024

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
Copy link
Owner

bram209 commented Jul 8, 2024

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,
}

formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
@V4ldum
Copy link
Contributor Author

V4ldum commented Jul 9, 2024

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

formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
formatter/src/formatter/element.rs Outdated Show resolved Hide resolved
@V4ldum
Copy link
Contributor Author

V4ldum commented Jul 13, 2024

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

@bram209 bram209 merged commit 148869b into bram209:main Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preference: Self Closing Tags
2 participants