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): treat CRLF as LF #5393

Merged
merged 2 commits into from Nov 9, 2018
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
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 @@ -442,20 +442,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 @@ -544,9 +549,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();
});