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

Prettier changes meaning of HTML+JS code when using tags that look like self-closing #16205

Open
nicolo-ribaudo opened this issue Apr 4, 2024 · 3 comments
Labels
lang:html Issues affecting HTML (and SVG but not JSX) type:bug Issues identifying ugly output, or a defect in the program

Comments

@nicolo-ribaudo
Copy link
Contributor

nicolo-ribaudo commented Apr 4, 2024

Prettier 3.2.5
Playground link

# Options (if any):
--parser=html

Input:

<!DOCTYPE html>
<html>
<head>
<script type="module" />
alert(`<script>`.length);
</script>
</head>
<body>
</body>
</html>

Output:

<!doctype html>
<html>
  <head>
    <script type="module" />
    alert(`
    <script>
      `.length);
    </script>
  </head>
  <body></body>
</html>

Expected output:

<!doctype html>
<html>
  <head>
    <script type="module" />
      alert(`<script>`.length);
    </script>
  </head>
  <body></body>
</html>

or

<!doctype html>
<html>
  <head>
    <script type="module">
      alert(`<script>`.length);
    </script>
  </head>
  <body></body>
</html>

or throw an error

Why?

The original code alerts 8, while the formatted code alerts 20. When parsing an HTML file, for elements that cannot be self-closed (like <script>) Prettier should not consider them as self-closed if they end with />.

This is related to #5246 and #15336, but those issue are about equivalent ways that Prettier could format code to, while this issue is about a parsing bug where Prettier changes the behavior of code.

@fisker fisker added type:bug Issues identifying ugly output, or a defect in the program lang:html Issues affecting HTML (and SVG but not JSX) labels Apr 4, 2024
@Aspyryan

This comment was marked as off-topic.

@nicolo-ribaudo

This comment was marked as off-topic.

@fisker

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:html Issues affecting HTML (and SVG but not JSX) type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

3 participants