From d452be5cb0d30ac68aa15401fdfff4816ead0573 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 27 Oct 2025 08:46:30 +0800 Subject: [PATCH] Don't force handlebars elements to break in `htmlWhitespaceSensitivity: "ignore"` mode (#18133) --- changelog_unreleased/handlebars/18133.md | 14 +++++ src/language-handlebars/printer-glimmer.js | 62 ++++++------------- .../__snapshots__/format.test.js.snap | 9 +-- 3 files changed, 37 insertions(+), 48 deletions(-) create mode 100644 changelog_unreleased/handlebars/18133.md diff --git a/changelog_unreleased/handlebars/18133.md b/changelog_unreleased/handlebars/18133.md new file mode 100644 index 000000000..943cbdc12 --- /dev/null +++ b/changelog_unreleased/handlebars/18133.md @@ -0,0 +1,14 @@ +#### Don't force elements to break with `--html-whitespace-sensitivity=ignore` (#18133 by @fisker) + + +```hbs +{{! Input }} +
+ +{{! Prettier stable (--html-whitespace-ensitivity=ignore) }} +
+
+ +{{! Prettier main (--html-whitespace-ensitivity=ignore) }} +
+``` diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 4c5a94365..073c54c01 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -40,40 +40,36 @@ function print(path, options, print) { return group(path.map(print, "body")); case "ElementNode": { - const startingTag = group(printStartingTag(path, print)); + const isWhitespaceSensitive = + options.htmlWhitespaceSensitivity !== "ignore"; - const escapeNextElementNode = - options.htmlWhitespaceSensitivity === "ignore" && - path.next?.type === "ElementNode" + const startingTag = [ + !isWhitespaceSensitive && path.previous?.type === "ElementNode" ? softline - : ""; + : "", + group([printStartingTag(path, print)]), + ]; if (isVoidElement(node)) { - return [startingTag, escapeNextElementNode]; + return [startingTag]; } const endingTag = [""]; - if (node.children.length === 0) { - return [startingTag, indent(endingTag), escapeNextElementNode]; + if ( + node.children.length === 0 || + (!isWhitespaceSensitive && + node.children.every((node) => isWhitespaceNode(node))) + ) { + return [startingTag, endingTag]; + } + const parts = path.map(print, "children"); + + if (!isWhitespaceSensitive) { + return [startingTag, indent([softline, ...parts]), softline, endingTag]; } - if (options.htmlWhitespaceSensitivity === "ignore") { - return [ - startingTag, - indent(printChildren(path, options, print)), - hardline, - endingTag, - escapeNextElementNode, - ]; - } - - return [ - startingTag, - indent(group(printChildren(path, options, print))), - endingTag, - escapeNextElementNode, - ]; + return [startingTag, indent(group(parts)), endingTag]; } case "BlockStatement": @@ -433,24 +429,6 @@ function printStartingTag(path, print) { return ["<", node.tag, indent(attributes), printStartingTagEndMarker(node)]; } -function printChildren(path, options, print) { - const { node } = path; - const isEmpty = node.children.every((node) => isWhitespaceNode(node)); - if (options.htmlWhitespaceSensitivity === "ignore" && isEmpty) { - return ""; - } - - return path.map(({ isFirst }) => { - const printedChild = print(); - - if (isFirst && options.htmlWhitespaceSensitivity === "ignore") { - return [softline, printedChild]; - } - - return printedChild; - }, "children"); -} - function printStartingTagEndMarker(node) { if (isVoidElement(node)) { return ifBreak([softline, "/>"], [" />", softline]); diff --git a/tests/format/handlebars/html-whitespace-sensitivity/__snapshots__/format.test.js.snap b/tests/format/handlebars/html-whitespace-sensitivity/__snapshots__/format.test.js.snap index a7ae19aff..dbbcd5b4a 100644 --- a/tests/format/handlebars/html-whitespace-sensitivity/__snapshots__/format.test.js.snap +++ b/tests/format/handlebars/html-whitespace-sensitivity/__snapshots__/format.test.js.snap @@ -209,11 +209,9 @@ expressions. sometimes{{nogaps}}areimportant some {{text}} -
-
+
-
-
+
This is your name: {{name}}. @@ -303,8 +301,7 @@ expressions. sometimes{{nogaps}}areimportant - - +
================================================================================