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): leading space for leading interpolation in textarea is sensitive #5468

Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 3 additions & 10 deletions src/language-html/preprocess.js
Expand Up @@ -262,22 +262,15 @@ function extractWhitespaces(ast /*, options*/) {
const isIndentationSensitive = isIndentationSensitiveNode(node);

return node.clone({
isWhitespaceSensitive,
isIndentationSensitive,
children: node.children
// extract whitespace nodes
.reduce((newChildren, child) => {
if (child.type !== "text") {
if (child.type !== "text" || isWhitespaceSensitive) {
return newChildren.concat(child);
}

if (isWhitespaceSensitive) {
return newChildren.concat(
Object.assign({}, child, {
isWhitespaceSensitive,
isIndentationSensitive
})
);
}

const localChildren = [];

const [, leadingSpaces, text, trailingSpaces] = child.value.match(
Expand Down
41 changes: 17 additions & 24 deletions src/language-html/printer-html.js
Expand Up @@ -7,6 +7,7 @@ const {
} = require("../doc");
const {
breakParent,
dedentToRoot,
fill,
group,
hardline,
Expand All @@ -29,7 +30,6 @@ const {
getPrettierIgnoreAttributeCommentData,
hasPrettierIgnore,
inferScriptParser,
isPreLikeNode,
isScriptLikeTag,
normalizeParts,
preferHardlineAsLeadingSpaces,
Expand Down Expand Up @@ -199,19 +199,13 @@ function genericPrint(path, options, print) {
concat([
shouldHugContent
? ifBreak(softline, "", { groupId: attrGroupId })
: node.firstChild.type === "text" &&
node.firstChild.isWhitespaceSensitive &&
node.firstChild.isIndentationSensitive
? (node.children.length === 1 &&
node.firstChild.type === "text" &&
node.firstChild.value.indexOf("\n") === -1) ||
node.firstChild.sourceSpan.start.line ===
node.lastChild.sourceSpan.end.line
? ""
: literalline
: node.firstChild.hasLeadingSpaces &&
node.firstChild.isLeadingSpaceSensitive
? line
: node.firstChild.type === "text" &&
node.isWhitespaceSensitive &&
node.isIndentationSensitive
? dedentToRoot(softline)
: softline,
printChildren(path, options, print)
])
Expand All @@ -228,17 +222,16 @@ function genericPrint(path, options, print) {
: node.lastChild.hasTrailingSpaces &&
node.lastChild.isTrailingSpaceSensitive
? line
: node.type === "element" &&
isPreLikeNode(node) &&
node.lastChild.type === "text" &&
(node.lastChild.value.indexOf("\n") === -1 ||
new RegExp(
`\\n\\s{${options.tabWidth *
countParents(
path,
n => n.parent && n.parent.type !== "root"
)}}$`
).test(node.lastChild.value))
: node.lastChild.type === "text" &&
node.isWhitespaceSensitive &&
node.isIndentationSensitive &&
new RegExp(
`\\n\\s{${options.tabWidth *
countParents(
path,
n => n.parent && n.parent.type !== "root"
)}}$`
).test(node.lastChild.value)
? /**
* <div>
* <pre>
Expand Down Expand Up @@ -825,8 +818,8 @@ function printClosingTagEndMarker(node) {
}

function getTextValueParts(node, value = node.value) {
return node.isWhitespaceSensitive
? node.isIndentationSensitive
return node.parent.isWhitespaceSensitive
? node.parent.isIndentationSensitive
? replaceNewlines(value, literalline)
: replaceNewlines(
dedentString(value.replace(/^\s*?\n|\n\s*?$/g, "")),
Expand Down
78 changes: 43 additions & 35 deletions src/language-html/utils.js
Expand Up @@ -139,51 +139,59 @@ function isIndentationSensitiveNode(node) {
}

function isLeadingSpaceSensitiveNode(node) {
if (isFrontMatterNode(node)) {
return false;
}

if (
(node.type === "text" || node.type === "interpolation") &&
node.prev &&
(node.prev.type === "text" || node.prev.type === "interpolation")
) {
return true;
}

if (!node.parent || node.parent.cssDisplay === "none") {
return false;
}
const isLeadingSpaceSensitive = _isLeadingSpaceSensitiveNode();

if (
isLeadingSpaceSensitive &&
!node.prev &&
node.parent.type === "element" &&
node.parent &&
node.parent.tagDefinition &&
node.parent.tagDefinition.ignoreFirstLf
) {
return false;
return node.type === "interpolation";
}

if (isPreLikeNode(node.parent)) {
return true;
}
return isLeadingSpaceSensitive;

if (
!node.prev &&
(node.parent.type === "root" ||
isScriptLikeTag(node.parent) ||
!isFirstChildLeadingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
) {
return false;
}
function _isLeadingSpaceSensitiveNode() {
if (isFrontMatterNode(node)) {
return false;
}

if (
node.prev &&
!isNextLeadingSpaceSensitiveCssDisplay(node.prev.cssDisplay)
) {
return false;
}
if (
(node.type === "text" || node.type === "interpolation") &&
node.prev &&
(node.prev.type === "text" || node.prev.type === "interpolation")
) {
return true;
}

return true;
if (!node.parent || node.parent.cssDisplay === "none") {
return false;
}

if (isPreLikeNode(node.parent)) {
return true;
}

if (
!node.prev &&
(node.parent.type === "root" ||
isScriptLikeTag(node.parent) ||
!isFirstChildLeadingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
) {
return false;
}

if (
node.prev &&
!isNextLeadingSpaceSensitiveCssDisplay(node.prev.cssDisplay)
) {
return false;
}

return true;
}
}

function isTrailingSpaceSensitiveNode(node) {
Expand Down