Skip to content

Commit

Permalink
fix(html): treat CRLF as LF (#5393)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 9, 2018
1 parent 423ddf9 commit 12a8fa3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/language-html/parser-html.js
Expand Up @@ -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,
Expand Down
40 changes: 25 additions & 15 deletions src/language-html/printer-html.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions tests_integration/__tests__/__snapshots__/format.js.snap
@@ -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 | );
Expand Down
8 changes: 8 additions & 0 deletions tests_integration/__tests__/format.js
Expand Up @@ -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();
});

0 comments on commit 12a8fa3

Please sign in to comment.