dart-lang / yaml_edit

A library for YAML manipulation with comment and whitespace preservation.
https://pub.dev/packages/yaml_edit
BSD 3-Clause "New" or "Revised" License
27 stars 16 forks source link

String value is escaped in an unwanted way #14

Open Schwusch opened 2 years ago

Schwusch commented 2 years ago

When updating a string value with forward slashes, the resulting string escapes the forward slashes unnecessarily:

final doc = YamlEditor('exampleKey: \${file(../../bar.yml)}');

doc.update(['exampleKey'], '\${file(../../foo.yml)}');

print(doc);
// exampleKey: ${file(..\/..\/bar.yml)}

My current workaround is to modify the resulting string:

doc.toString().replaceAll('\\/', '/')

It would be nice to customize that behaviour.

jonasfj commented 2 years ago

// exampleKey: ${file(..\/..\/bar.yml)}

Are you sure it's not double quoting it, as: // exampleKey: "${file(..\/..\/bar.yml)}"


On topic, I'm guessing we could make the strings prettier. Contributions are welcome, but I think it would be best to have:

Schwusch commented 2 years ago

Are you sure it's not double quoting it, as: // exampleKey: "${file(..\/..\/bar.yml)}"

Oh yeah, that's right. When skipping the { and } characters, everything worked as expected during experimentation, with no double quotes or escaping:

doc.update(['exampleKey'], '\$file(../../foo.yml)');
print(doc);
// exampleKey: $file(../../foo.yml)

So curly braces makes this function return true: https://github.com/dart-lang/yaml_edit/blob/4fadb43801b07f90b3f0c6065dbce4efc6d8d55e/lib/src/utils.dart#L15 and then that triggers escaping all code units in this map: https://github.com/dart-lang/yaml_edit/blob/4fadb43801b07f90b3f0c6065dbce4efc6d8d55e/lib/src/strings.dart#L286 But the forward slashes, in itself, does not trigger any character escaping.

I can make a PR if there is a consensus on the solution. Personally, I'd like to pass a parameter to skip all checks for dangerous strings and skip all character replacing somehow.