Don't force handlebars elements to break in htmlWhitespaceSensitivity: "ignore" mode (#18133)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-10-27 08:46:30 +08:00
1 parent 35dacd1aad
commit d452be5cb0
3 files changed
+37 -48

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
#### Don't force elements to break with `--html-whitespace-sensitivity=ignore` (#18133 by @fisker)
<!-- prettier-ignore -->
```hbs
{{! Input }}
<div> </div>
{{! Prettier stable (--html-whitespace-ensitivity=ignore) }}
<div>
</div>
{{! Prettier main (--html-whitespace-ensitivity=ignore) }}
<div></div>
```
+20 -42
View File
@@ -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 = ["</", node.tag, ">"];
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]);
@@ -209,11 +209,9 @@ expressions. sometimes{{nogaps}}areimportant
some {{text}}
</HelloWorld>
<div class="a list of classes">
</div>
<div class="a list of classes"></div>
<div class="a list of classes">
</div>
<div class="a list of classes"></div>
<span>
This is your name: {{name}}.
@@ -303,8 +301,7 @@ expressions. sometimes{{nogaps}}areimportant
<table>
<thead>
<tr>
</tr>
<tr></tr>
</thead>
</table>
================================================================================