Skip to content

Commit fefb29f

Browse files
committed
fix(#262): ignore the CRLF immediately following a multiple basic string opening
Fix #262 Signed-off-by: Frost Ming <[email protected]>
1 parent a766d3a commit fefb29f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tests/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ def test_parser_should_raise_an_error_if_equal_not_found():
3939
parser = Parser(content)
4040
with pytest.raises(UnexpectedCharError):
4141
parser.parse()
42+
43+
44+
def test_parse_multiline_string_ignore_the_first_newline():
45+
content = 'a = """\nfoo\n"""'
46+
parser = Parser(content)
47+
assert parser.parse() == {"a": "foo\n"}
48+
49+
content = 'a = """\r\nfoo\n"""'
50+
parser = Parser(content)
51+
assert parser.parse() == {"a": "foo\n"}

tomlkit/parser.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,17 @@ def _parse_string(self, delim: StringType) -> String:
799799
value = ""
800800

801801
# A newline immediately following the opening delimiter will be trimmed.
802-
if delim.is_multiline() and self._current == "\n":
803-
# consume the newline, EOF here is an issue (middle of string)
804-
self.inc(exception=UnexpectedEofError)
802+
if delim.is_multiline():
803+
if self._current == "\n":
804+
# consume the newline, EOF here is an issue (middle of string)
805+
self.inc(exception=UnexpectedEofError)
806+
else:
807+
cur = self._current
808+
with self._state(restore=True):
809+
if self.inc():
810+
cur += self._current
811+
if cur == "\r\n":
812+
self.inc_n(2, exception=UnexpectedEofError)
805813

806814
escaped = False # whether the previous key was ESCAPE
807815
while True:

0 commit comments

Comments
 (0)