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

Long strings that start with spaces aren't rendered properly (and lose newlines) #55

Closed
rix0rrr opened this issue Dec 10, 2018 · 3 comments
Labels
bug Something isn't working

Comments

@rix0rrr
Copy link

rix0rrr commented Dec 10, 2018

I'm not well-versed enough in the YAML spec to tell you what the proper terms are to use in this bug report. It seems to be in "block mode" (?) rendering of long strings starting with spaces.

They don't need to contain newlines to trigger the issue, but if they do the newlines are lost.

The issue is in rendering, not parsing, as all YAML engines seem to parse the string the same (which is different from the original string).

Summary

If the renderer switches to some particular rendering mode marked by ">-2" (seems to be triggered by the string starting with a space) then if word wrap occurs the word wrap is interpreted as a literal newline. Actual newlines that are in this block will be lost upon parsing as well.

A string that looks like this:

" very long string that will cause wrapping. \n a newline"

Will render like this:

field: >-2
    very long string that will cause
   wrapping.
   a newline

And when parsed back will be parsed as:

" very long string that will cause \n wrapping. a newline"

So the wrap is interpreted as a newline and the actual newline is lost.

Repro

Here is the reproduction (still occurs with version 1.0.3).

const YAML = require('yaml');
const assert = require('assert');

const obj = {
    Field: ' very long line that starts with a space. very long line that starts with a space.\nstart on a new line'
};

const yamlified = YAML.stringify(obj);
console.log(yamlified);
const parsed = YAML.parse(yamlified);

assert.deepEqual(obj, parsed);

Output:

Field: >-2
   very long line that starts with a space. very long line that starts with a
  space.
  start on a new line

Assertion fails:

AssertionError [ERR_ASSERTION]: { Field: ' very long line that starts with a space. very long line that starts with a space.\nstart on a new line' } deepEqual { Field: ' very long line that starts with a space. very long line that starts with a\nspace. start on a new line' }

NOTE: We're normally using this library in yaml-1.1 mode (because we're generating output for AWS CloudFormation), but this issue also crops up in YAML 1.2 mode as the repro shows.

@rix0rrr
Copy link
Author

rix0rrr commented Dec 11, 2018

Thank you!

@eemeli
Copy link
Owner

eemeli commented Dec 11, 2018

The bug was that the check for more-indented lines (which should not be folded) in a folded block scalar was not triggering on the first content line. Thank you for catching it!

tripodsan added a commit to tripodsan/yaml that referenced this issue Feb 28, 2019
@tiye
Copy link

tiye commented Oct 10, 2023

any options to remove those newlines in a single string. some parser in other language(Go I guess) might parse it differently:

> y.stringify({a: ["[] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99"]}, {compat: 'failsafe'})
'a:\n' +
  '  - "[] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26\n' +
  '    27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51\n' +
  '    52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76\n' +
  '    77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99"\n'

updated:

maybe a very great lineWidth?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants