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 fold literal encoding with trailing line break #91

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ String? _tryYamlEncodeSingleQuoted(String string) {
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;

Expand Down Expand Up @@ -164,7 +164,7 @@ String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
/// 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.
Expand Down
5 changes: 5 additions & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ final _testStrings = [
'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',
Expand Down