fix(html): no need to strip newline for text in script-like tag (#5111)

The issue here is that the `stripTrailingHardline` does not work for js/ts, I fixed it and then I realized that we always print trailing newline in every language, so there's no need to strip newline for text in script-like tag.
master
Ika 2018-09-20 00:03:50 +08:00 committed by GitHub
parent ebedd5b74a
commit 4a084c82a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 24 deletions

View File

@ -184,16 +184,24 @@ function removeLines(doc) {
function stripTrailingHardline(doc) {
// HACK remove ending hardline, original PR: #1984
if (
doc.type === "concat" &&
doc.parts.length === 2 &&
doc.parts[1].type === "concat" &&
doc.parts[1].parts.length === 2 &&
doc.parts[1].parts[0].hard &&
doc.parts[1].parts[1].type === "break-parent"
) {
return doc.parts[0];
if (doc.type === "concat" && doc.parts.length !== 0) {
const lastPart = doc.parts[doc.parts.length - 1];
if (lastPart.type === "concat") {
if (
lastPart.parts.length === 2 &&
lastPart.parts[0].hard &&
lastPart.parts[1].type === "break-parent"
) {
return { type: "concat", parts: doc.parts.slice(0, -1) };
}
return {
type: "concat",
parts: doc.parts.slice(0, -1).concat(stripTrailingHardline(lastPart))
};
}
}
return doc;
}

View File

@ -3,7 +3,7 @@
const { hasNewlineInRange } = require("../common/util");
const {
builders: { hardline, concat, markAsRoot, literalline },
utils: { stripTrailingHardline, removeLines, mapDoc }
utils: { removeLines, mapDoc }
} = require("../doc");
function embed(path, print, textToDoc, options) {
@ -21,7 +21,7 @@ function embed(path, print, textToDoc, options) {
) {
const parser = options.parser === "flow" ? "flow" : "babylon";
const doc = textToDoc(getText(options, node), { parser });
return concat([hardline, stripTrailingHardline(doc)]);
return concat([hardline, doc]);
}
// Inline TypeScript
@ -35,13 +35,13 @@ function embed(path, print, textToDoc, options) {
{ parser: "typescript" },
options
);
return concat([hardline, stripTrailingHardline(doc)]);
return concat([hardline, doc]);
}
// Inline Styles
if (parent.type === "style") {
const doc = textToDoc(getText(options, node), { parser: "css" });
return concat([hardline, stripTrailingHardline(doc)]);
return concat([hardline, doc]);
}
break;

View File

@ -32,7 +32,8 @@ exports[`less.html - parse5-verify 1`] = `
#header {
color: @light-blue;
}</style>
}
</style>
<style lang="less">
@nice-blue: #5B83AD;
@ -40,7 +41,8 @@ exports[`less.html - parse5-verify 1`] = `
#header {
color: @light-blue;
}</style>
}
</style>
`;
@ -57,13 +59,15 @@ exports[`postcss.html - parse5-verify 1`] = `
body {
background: navy;
color: yellow;
}</style>
}
</style>
<style lang="postcss">
body {
background: navy;
color: yellow;
}</style>
}
</style>
`;
@ -95,7 +99,8 @@ $primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}</style>
}
</style>
<style lang="scss">
$font-stack: Helvetica, sans-serif;
@ -104,7 +109,8 @@ $primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}</style>
}
</style>
`;
@ -131,12 +137,14 @@ exports[`simple.html - parse5-verify 1`] = `
<style>
a {
color: red;
}</style>
}
</style>
<style>
body {
background: navy;
color: yellow;
}</style>
}
</style>
</head>
<body>
<h1>Sample styled page</h1>
@ -159,12 +167,14 @@ exports[`single-style.html - parse5-verify 1`] = `
<style>
a {
color: red;
}</style>
}
</style>
<style>
h1 {
font-size: 120%;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #333366;
}</style>
}
</style>
`;

View File

@ -20,7 +20,8 @@ exports[`html-with-css-style.html - parse5-verify 1`] = `
<style>
blink {
display: none;
}</style>
}
</style>
</head>
<body></body>
</html>