Fix fold literal encoding with trailing line break (dart-lang/yaml_edit#91)
* Return null if string cannot be encoded as literal/folded
* Add test variants for trailing line breaks
diff --git a/pkgs/yaml_edit/lib/src/strings.dart b/pkgs/yaml_edit/lib/src/strings.dart
index dcb1b72..1b85641 100644
--- a/pkgs/yaml_edit/lib/src/strings.dart
+++ b/pkgs/yaml_edit/lib/src/strings.dart
@@ -96,7 +96,7 @@
String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
// A string that starts with space or newline followed by space can't be
// encoded in folded mode.
- if (string.isEmpty || string.trimLeft().length != string.length) return null;
+ if (string.isEmpty || string.trim().length != string.length) return null;
if (_hasUnprintableCharacters(string)) return null;
@@ -164,7 +164,7 @@
/// See: https://yaml.org/spec/1.2.2/#812-literal-style
String? _tryYamlEncodeLiteral(
String string, int indentSize, String lineEnding) {
- if (string.isEmpty || string.trimLeft().length != string.length) return null;
+ if (string.isEmpty || string.trim().length != string.length) return null;
// A string that starts with space or newline followed by space can't be
// encoded in literal mode.
diff --git a/pkgs/yaml_edit/test/string_test.dart b/pkgs/yaml_edit/test/string_test.dart
index b92c20a..5ea7e73 100644
--- a/pkgs/yaml_edit/test/string_test.dart
+++ b/pkgs/yaml_edit/test/string_test.dart
@@ -11,6 +11,11 @@
'whitespace\n after line breaks',
'whitespace\n \nbetween line breaks',
'\n line break at the start',
+ 'whitespace and line breaks at end 1\n ',
+ 'whitespace and line breaks at end 2 \n \n',
+ 'whitespace and line breaks at end 3 \n\n',
+ 'whitespace and line breaks at end 4 \n\n ',
+ '\n\nline with multiple trailing line break \n\n\n\n\n',
'word',
'foo bar',
'foo\nbar',