fix(html): treat CRLF as LF (#5393)

master
Ika 2018-11-09 09:56:46 +08:00 committed by GitHub
parent 423ddf9abd
commit 12a8fa3a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 15 deletions

View File

@ -281,6 +281,7 @@ function locEnd(node) {
function createParser({ recognizeSelfClosing }) {
return {
preprocess: text => text.replace(/\r\n?/g, "\n"),
parse: (text, parsers, options) =>
_parse(text, options, recognizeSelfClosing),
hasPragma,

View File

@ -446,20 +446,25 @@ function printChildren(path, options, print) {
return print(childPath);
}
const child = childPath.getValue();
return concat([
printOpeningTagPrefix(child),
options.originalText.slice(
options.locStart(child) +
(child.prev && needsToBorrowNextOpeningTagStartMarker(child.prev)
? printOpeningTagStartMarker(child).length
: 0),
options.locEnd(child) -
(child.next && needsToBorrowPrevClosingTagEndMarker(child.next)
? printClosingTagEndMarker(child).length
: 0),
return concat(
[].concat(
printOpeningTagPrefix(child),
replaceNewlines(
options.originalText.slice(
options.locStart(child) +
(child.prev && needsToBorrowNextOpeningTagStartMarker(child.prev)
? printOpeningTagStartMarker(child).length
: 0),
options.locEnd(child) -
(child.next && needsToBorrowPrevClosingTagEndMarker(child.next)
? printClosingTagEndMarker(child).length
: 0)
),
literalline
),
printClosingTagSuffix(child)
)
]);
);
}
function printBetweenLine(prevNode, nextNode) {
@ -548,9 +553,14 @@ function printOpeningTag(path, options, print) {
return path.map(attrPath => {
const attr = attrPath.getValue();
return hasPrettierIgnoreAttribute(attr)
? options.originalText.slice(
options.locStart(attr),
options.locEnd(attr)
? concat(
replaceNewlines(
options.originalText.slice(
options.locStart(attr),
options.locEnd(attr)
),
literalline
)
)
: print(attrPath);
}, "attrs");

View File

@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`html parser should handle CRLF correctly 1`] = `"\\"<!--\\\\r\\\\n test\\\\r\\\\n test\\\\r\\\\n-->\\\\r\\\\n\\""`;
exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = `
"Expression expected. (9:7)
7 | );

View File

@ -25,3 +25,11 @@ label:
prettier.format(input, { parser: "typescript" })
).toThrowErrorMatchingSnapshot();
});
test("html parser should handle CRLF correctly", () => {
const input = "<!--\r\n test\r\n test\r\n-->";
expect(
// use JSON.stringify to observe CRLF
JSON.stringify(prettier.format(input, { parser: "html" }))
).toMatchSnapshot();
});