Skip to content

Commit 9719932

Browse files
authored
Fix fold literal encoding with trailing line break (dart-archive/yaml_edit#91)
* Return null if string cannot be encoded as literal/folded * Add test variants for trailing line breaks
1 parent 4a635ee commit 9719932

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pkgs/yaml_edit/lib/src/strings.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ String? _tryYamlEncodeSingleQuoted(String string) {
9696
String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
9797
// A string that starts with space or newline followed by space can't be
9898
// encoded in folded mode.
99-
if (string.isEmpty || string.trimLeft().length != string.length) return null;
99+
if (string.isEmpty || string.trim().length != string.length) return null;
100100

101101
if (_hasUnprintableCharacters(string)) return null;
102102

@@ -164,7 +164,7 @@ String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
164164
/// See: https://yaml.org/spec/1.2.2/#812-literal-style
165165
String? _tryYamlEncodeLiteral(
166166
String string, int indentSize, String lineEnding) {
167-
if (string.isEmpty || string.trimLeft().length != string.length) return null;
167+
if (string.isEmpty || string.trim().length != string.length) return null;
168168

169169
// A string that starts with space or newline followed by space can't be
170170
// encoded in literal mode.

pkgs/yaml_edit/test/string_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ final _testStrings = [
1111
'whitespace\n after line breaks',
1212
'whitespace\n \nbetween line breaks',
1313
'\n line break at the start',
14+
'whitespace and line breaks at end 1\n ',
15+
'whitespace and line breaks at end 2 \n \n',
16+
'whitespace and line breaks at end 3 \n\n',
17+
'whitespace and line breaks at end 4 \n\n ',
18+
'\n\nline with multiple trailing line break \n\n\n\n\n',
1419
'word',
1520
'foo bar',
1621
'foo\nbar',

0 commit comments

Comments
 (0)