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

Allow blockquoted tabs #683

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/dumper.js
Expand Up @@ -199,6 +199,12 @@ function isPrintable(c) {
|| (0x10000 <= c && c <= 0x10FFFF);
}

// Return true if the char is printable or a tab.
// derived from nb-char - #x85 - #xA0 - #x2028 - #x2029.
function isPrintableWithTabs(c) {
return isPrintable(c) || c === CHAR_TAB;
}

// [34] ns-char ::= nb-char - s-white
// [27] nb-char ::= c-printable - b-char - c-byte-order-mark
// [26] b-char ::= b-line-feed | b-carriage-return
Expand Down Expand Up @@ -332,7 +338,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
// Check for disallowed characters to rule out plain and single.
for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
char = codePointAt(string, i);
if (!isPrintable(char)) {
if (!isPrintableWithTabs(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char, prevChar, inblock);
Expand All @@ -352,7 +358,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
string[previousLineBreak + 1] !== ' ');
previousLineBreak = i;
}
} else if (!isPrintable(char)) {
} else if (!isPrintableWithTabs(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char, prevChar, inblock);
Expand Down
3 changes: 3 additions & 0 deletions test/issues/0682-1.yml
@@ -0,0 +1,3 @@
value: |-
a
b
7 changes: 7 additions & 0 deletions test/issues/0682-2.yml
@@ -0,0 +1,7 @@
value: |2

some text
less indented text
other: |2
some text
less indented text
33 changes: 33 additions & 0 deletions test/issues/0682.js
@@ -0,0 +1,33 @@
'use strict';

const assert = require('assert');
const fs = require('fs');
const yaml = require('../..');


it('Should serialize strings with tabs as blocks', function () {
const expected = fs.readFileSync(require('path').join(__dirname, '/0682-1.yml'), 'utf8');
const result = yaml.dump({ value: 'a\n\tb' });
assert.strictEqual(expected, result);
});

it('Should serialize strings with tabs and carriage returns as blocks and deserialize them', function () {
const data = {
value: '\r\n \tsome text\r\n more text but less indented \r\neven less indentation'
};
const result = yaml.dump(data);
const loaded = yaml.load(result);
assert.deepEqual(data, loaded);
});

it('Should serialize strings where the initial whitespace is less than the following', function () {
const data = {
value: ' \nsome text\n less indented text\n',
other: ' some text\nless indented text\n'
};
const result = yaml.dump(data);
const loaded = yaml.load(result);
assert.deepEqual(data, loaded);
const expected = fs.readFileSync(require('path').join(__dirname, '/0682-2.yml'), 'utf8');
assert.strictEqual(expected, result);
});
3 changes: 3 additions & 0 deletions test/issues/0682.yml
@@ -0,0 +1,3 @@
value: |-
a
b