fix(toml): handle CRLF as newline in parsing multiline string (#3920)

This commit is contained in:
NAKAI Tsuyoshi 2023-12-12 09:41:28 +09:00 committed by GitHub
parent b460c4a017
commit aff48b2deb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -481,8 +481,11 @@ export function MultilineBasicString(
return failure();
}
if (scanner.char() === "\n") {
// The first newline is trimmed
// The first newline (LF) is trimmed
scanner.next();
} else if (scanner.slice(0, 2) === "\r\n") {
// The first newline (CRLF) is trimmed
scanner.next(2);
}
const acc: string[] = [];
while (scanner.slice(0, 3) !== '"""' && !scanner.eof()) {
@ -491,6 +494,10 @@ export function MultilineBasicString(
scanner.next();
scanner.nextUntilChar({ comment: false });
continue;
} else if (scanner.slice(0, 3) === "\\\r\n") {
scanner.next();
scanner.nextUntilChar({ comment: false });
continue;
}
const escapedChar = EscapeSequence(scanner);
if (escapedChar.ok) {
@ -525,8 +532,11 @@ export function MultilineLiteralString(
return failure();
}
if (scanner.char() === "\n") {
// The first newline is trimmed
// The first newline (LF) is trimmed
scanner.next();
} else if (scanner.slice(0, 2) === "\r\n") {
// The first newline (CRLF) is trimmed
scanner.next(2);
}
const acc: string[] = [];
while (scanner.slice(0, 3) !== "'''" && !scanner.eof()) {

View File

@ -99,6 +99,27 @@ Violets are\\tblue"""`),
},
});
Deno.test({
name: "[TOML parser] multi-line basic string (CRLF)",
fn() {
const parse = ParserFactory(MultilineBasicString);
assertEquals(
parse(`"""\r
Roses are red\r
Violets are\\tblue"""`),
"Roses are red\r\nViolets are\tblue",
);
assertEquals(
parse(`"""\\\r
The quick brown \\\r
fox jumps over \\\r
the lazy dog.\\\r
"""`),
"The quick brown fox jumps over the lazy dog.",
);
},
});
Deno.test({
name: "[TOML parser] multi-line literal string",
fn() {
@ -112,6 +133,19 @@ Violets are\\tblue'''`),
},
});
Deno.test({
name: "[TOML parser] multi-line literal string (CRLF)",
fn() {
const parse = ParserFactory(MultilineLiteralString);
assertEquals(
parse(`'''\r
Roses are red\r
Violets are\\tblue'''`),
"Roses are red\r\nViolets are\\tblue",
);
},
});
Deno.test({
name: "[TOML parser] symbols",
fn() {