Skip to content

Commit 464a5b8

Browse files
committed
Fix flow scalar trailing whitespace folding, close #307
1 parent 1fda4f7 commit 464a5b8

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323

2424
### Fixed
2525
- Fix parsing of properties on the first implicit block mapping key, #62.
26+
- Fix trailing whitespace handling when folding flow scalar lines, #307.
2627
- Reject top-level block scalars without content indentation, #280.
2728
- Ensure numbers survive round-trip, #737.
2829
- Fix test coverage for issue #221.

lib/loader.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,9 @@ function readSingleQuotedScalar (state, nodeIndent) {
680680
throwError(state, 'unexpected end of the document within a single quoted scalar')
681681
} else {
682682
state.position++
683-
captureEnd = state.position
683+
if (!isWhiteSpace(ch)) {
684+
captureEnd = state.position
685+
}
684686
}
685687
}
686688

@@ -749,7 +751,9 @@ function readDoubleQuotedScalar (state, nodeIndent) {
749751
throwError(state, 'unexpected end of the document within a double quoted scalar')
750752
} else {
751753
state.position++
752-
captureEnd = state.position
754+
if (!isWhiteSpace(ch)) {
755+
captureEnd = state.position
756+
}
753757
}
754758
}
755759

test/core/issues/0307.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const { it } = require('node:test')
4+
5+
const assert = require('assert')
6+
const yaml = require('js-yaml')
7+
8+
it('Trims trailing whitespace when folding flow scalar lines', function () {
9+
// https://github.com/nodeca/js-yaml/issues/307
10+
// https://yaml.org/spec/1.2.0/#id2787745
11+
assert.strictEqual(
12+
yaml.load('"folded \nto a space,\t\n \nto a line feed, or \t\\\n \\ \tnon-content"'),
13+
'folded to a space,\nto a line feed, or \t \tnon-content'
14+
)
15+
16+
assert.strictEqual(
17+
yaml.load("'folded \nto a space,\t\n \nto a line feed'"),
18+
'folded to a space,\nto a line feed'
19+
)
20+
})
21+
22+
it('Preserves trailing whitespace in the final line of quoted scalars', function () {
23+
assert.strictEqual(yaml.load('"line \t"'), 'line \t')
24+
assert.strictEqual(yaml.load("'line \t'"), 'line \t')
25+
})

0 commit comments

Comments
 (0)