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

Strings with tabs and newlines can't be serialized as block quotes #682

Open
beckjake opened this issue Sep 14, 2022 · 1 comment · May be fixed by #683
Open

Strings with tabs and newlines can't be serialized as block quotes #682

beckjake opened this issue Sep 14, 2022 · 1 comment · May be fixed by #683

Comments

@beckjake
Copy link

beckjake commented Sep 14, 2022

The dumper code (in chooseScalarStyle) checks in a couple places to see if any characters in the string to be written are non-printable, and if so only allows double-quoted output. This is mostly fine, except tabs are explicitly excluded from the "printable" set (there's a comment implying it's intentional). That means that any block quote with tabs is unconditionally rendered as a single-line double-quoted string.

For example, loading+dumping this:

value: |-
  a
  	b

results in this:

value: "a\n\tb"

obviously this example is trivial and no big deal, but with a very large string that has embedded tabs (for example, an example program embedded in a value of a yaml block), you can imagine it gets pretty ugly.

For my needs it'd be enough to change the isPrintable checks in chooseScalarStyle to also accept tabs (0x09), but I think there's a pretty reasonable case to be made for carriage returns and non-breaking spaces too. It turns out I also need carriage returns and newlines, so I've patched that as well in my PR.

@beckjake beckjake linked a pull request Sep 14, 2022 that will close this issue
@beckjake beckjake changed the title Strings with tabs can't be serialized as block quotes Strings with tabs and newlines can't be serialized as block quotes Sep 15, 2022
@KhodeN
Copy link

KhodeN commented Dec 13, 2022

until the PR accepted patch-package helps me with this patch

diff --git a/node_modules/js-yaml/dist/js-yaml.mjs b/node_modules/js-yaml/dist/js-yaml.mjs
index be71cad..77a71f2 100644
--- a/node_modules/js-yaml/dist/js-yaml.mjs
+++ b/node_modules/js-yaml/dist/js-yaml.mjs
@@ -3017,7 +3017,8 @@ function isPrintable(c) {
   return  (0x00020 <= c && c <= 0x00007E)
       || ((0x000A1 <= c && c <= 0x00D7FF) && c !== 0x2028 && c !== 0x2029)
       || ((0x0E000 <= c && c <= 0x00FFFD) && c !== CHAR_BOM)
-      ||  (0x10000 <= c && c <= 0x10FFFF);
+      ||  (0x10000 <= c && c <= 0x10FFFF)
+      || c === CHAR_TAB;
 }
 
 // [34] ns-char ::= nb-char - s-white

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants