Skip to content

Commit

Permalink
fix(html): leading space for leading interpolation in textarea is sen…
Browse files Browse the repository at this point in the history
…sitive (#5468)
  • Loading branch information
ikatyang committed Nov 15, 2018
1 parent c00dcb9 commit 4f63be0
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 72 deletions.
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 @@ -827,8 +820,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

0 comments on commit 4f63be0

Please sign in to comment.