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

fix(html): do not format non-normal whitespaces as normal whitespaces #5797

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -41,3 +41,24 @@ Examples:
```

-->

- HTML: Do not format non-normal whitespace as normal whitespace ([#5797] by [@ikatyang])

Previously, only non-breaking whitespaces (U+00A0) are marked as non-normal whitespace,
which means other non-normal whitespaces such as non-breaking narrow whitespaces (U+202F)
could be formatted as normal whitespaces, which breaks the output. We now follow the spec to
exclude all non-[ASCII whitespace](https://infra.spec.whatwg.org/#ascii-whitespace) from whitespace normalization.

(`·` represents a non-breaking narrow whitespace)

<!-- prettier-ignore -->
```html
<!-- Input -->
Prix·:·32·€

<!-- Output (Prettier stable) -->
Prix : 32 €

<!-- Output (Prettier master) -->
Prix·:·32·€
```
4 changes: 2 additions & 2 deletions src/language-html/printer-html.js
Expand Up @@ -900,8 +900,8 @@ function getTextValueParts(node, value = node.value) {
dedentString(value.replace(/^\s*?\n|\n\s*?$/g, "")),
hardline
)
: // non-breaking whitespace: 0xA0
join(line, value.split(/[^\S\xA0]+/)).parts;
: // https://infra.spec.whatwg.org/#ascii-whitespace
join(line, value.split(/[\t\n\f\r ]+/)).parts;
}

function printEmbeddedAttributeValue(node, originalTextToDoc, options) {
Expand Down
4 changes: 4 additions & 0 deletions tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -203,6 +203,8 @@ printWidth: 80
<span>Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error.</span>
<!-- non-breaking whitespaces -->
<span>Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error.</span>
<!-- non-breaking narrow whitespaces -->
<span>Prix : 32 €</span>
=====================================output=====================================
<!-- normal whitespaces -->
Expand All @@ -214,6 +216,8 @@ printWidth: 80
<span
>Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error.</span
>
<!-- non-breaking narrow whitespaces -->
<span>Prix : 32 €</span>
================================================================================
`;
Expand Down
2 changes: 2 additions & 0 deletions tests/html_whitespace/non-breaking-whitespace.html
Expand Up @@ -2,3 +2,5 @@
<span>Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error.</span>
<!-- non-breaking whitespaces -->
<span>Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error.</span>
<!-- non-breaking narrow whitespaces -->
<span>Prix : 32 €</span>