Skip to content

Commit

Permalink
Fix unwanted line breaks in folded scalars.
Browse files Browse the repository at this point in the history
Related to issue nodeca#93
dervus committed Oct 5, 2013
1 parent 964bae2 commit e67a354
Showing 4 changed files with 60 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
@@ -745,7 +745,8 @@ function loadAll(input, output, options) {
chomping = CHOMPING_CLIP,
detectedIndent = false,
textIndent = nodeIndent,
emptyLines = -1;
emptyLines = -1,
atMoreIndented = false;

switch (character) {
case CHAR_VERTICAL_LINE:
@@ -831,22 +832,34 @@ function loadAll(input, output, options) {

detectedIndent = true;

// Folded style: use fancy rules to handle line breaks.
if (folding) {

// Lines starting with white space characters (more-indented lines) are not folded.
if (CHAR_SPACE === character || CHAR_TAB === character) {
atMoreIndented = true;
result += common.repeat('\n', emptyLines + 1);

// End of more-indented block.
} else if (atMoreIndented) {
atMoreIndented = false;
result += common.repeat('\n', emptyLines + 1);
emptyLines = 1;

// Just one line break - perceive as the same line.
} else if (0 === emptyLines) {
result += ' ';
emptyLines = 0;

// Several line breaks - perceive as different lines.
} else {
result += common.repeat('\n', emptyLines);
emptyLines = 0;
}

// Literal style: just add exact number of line breaks between content lines.
} else {
result += common.repeat('\n', emptyLines + 1);
emptyLines = 0;
}

emptyLines = 0;
captureStart = position;

do { character = input.charCodeAt(++position); }
1 change: 1 addition & 0 deletions test/issues.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ describe('Issues.', function () {
require('./issues/issue-64.js');
require('./issues/issue-85.js');
require('./issues/issue-92.js');
require('./issues/issue-93.js');
require('./issues/issue-parse-function-security.js');
require('./issues/issue-skip-invalid.js');
});
25 changes: 25 additions & 0 deletions test/issues/data/issue-93.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
first: >
a
b
c
d
e
f
second: >
a
b
c
d
e
f
third: >
a
b
c
d
e
f
16 changes: 16 additions & 0 deletions test/issues/issue-93.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
/*global it */


var assert = require('assert');

require('../../lib/js-yaml');


it('Unwanted line breaks in folded scalars', function () {
var data = require('./data/issue-93.yml');

assert.strictEqual(data.first, 'a b\n c\n d\ne f\n');
assert.strictEqual(data.second, 'a b\n c\n\n d\ne f\n');
assert.strictEqual(data.third, 'a b\n\n c\n d\ne f\n');
});

0 comments on commit e67a354

Please sign in to comment.