diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 3a8dd4dda83f..dc4af692d3f2 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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) + + + ```html + + Prix·:·32·€ + + + Prix : 32 € + + + Prix·:·32·€ + ``` diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 896b54a3715a..20f4bc1b1405 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -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) { diff --git a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap index ac9e673c06e5..4e8a8a5339be 100644 --- a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap @@ -203,6 +203,8 @@ printWidth: 80 Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error. Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error. + +Prix : 32 € =====================================output===================================== @@ -214,6 +216,8 @@ printWidth: 80 Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error. + +Prix : 32 € ================================================================================ `; diff --git a/tests/html_whitespace/non-breaking-whitespace.html b/tests/html_whitespace/non-breaking-whitespace.html index 371f0eeae4b8..55a8b9477611 100644 --- a/tests/html_whitespace/non-breaking-whitespace.html +++ b/tests/html_whitespace/non-breaking-whitespace.html @@ -2,3 +2,5 @@ Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error. Nihil aut odit omnis. Quam maxime est molestiae. Maxime dolorem dolores voluptas quaerat ut qui sunt vitae error. + +Prix : 32 €