Skip to content

Commit

Permalink
fix(html): do not format non-normal whitespaces as normal whitespaces (
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Jan 30, 2019
1 parent 1802ce1 commit 847a78e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
22 changes: 20 additions & 2 deletions CHANGELOG.unreleased.md
Expand Up @@ -47,5 +47,23 @@ Examples:
In Prettier 1.16.0 and 1.16.1, `--list-different` and `--check` logged every file in some CI environments, instead of just unformatted files.
This unwanted behavior is now fixed.

[#5804]: https://github.com/prettier/prettier/pull/5804
[@kachkaev]: https://github.com/kachkaev
- 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>

0 comments on commit 847a78e

Please sign in to comment.