From 4358cf02cc9f0193129b14641b386b4d5717fac9 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Fri, 26 Jun 2026 08:13:43 +0200 Subject: [PATCH] fix(parsing): keep more-indented folded lines literal A folded block scalar folded every non-blank line into the surrounding text, including more-indented lines, which YAML keeps literal with their line breaks. Detect a more-indented line by its retained leading whitespace and keep the breaks adjacent to it instead of folding them. Plain and blank-line folding are unchanged. --- CHANGELOG.md | 2 ++ Sources/Parsing/TokenEventParser.Block.swift | 11 +++++++++++ Tests/BlockScalarChompingTests.swift | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21e16f4..4005e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 paths (which had duplicated the loop), and `blockScalarValue` applies literal versus folded line semantics and clip/strip/keep chomping correctly. A block scalar without blank lines is unchanged. +- Keep a more-indented line inside a folded `>` block literal, with its line + breaks preserved, rather than folding it into the surrounding text. ### Documentation diff --git a/Sources/Parsing/TokenEventParser.Block.swift b/Sources/Parsing/TokenEventParser.Block.swift index c8ea178..89192c1 100644 --- a/Sources/Parsing/TokenEventParser.Block.swift +++ b/Sources/Parsing/TokenEventParser.Block.swift @@ -274,6 +274,11 @@ extension PureYAML.Parsing.TokenEventParser { result += lines[index] index += 1 } + } else if isMoreIndentedBlockLine(lines[index]) || isMoreIndentedBlockLine(lines[index - 1]) { + // A line break adjacent to a more-indented line is kept literal, + // so the more-indented line and its breaks survive folding. + result += "\n" + lines[index] + index += 1 } else { result += " " + lines[index] index += 1 @@ -282,6 +287,12 @@ extension PureYAML.Parsing.TokenEventParser { return result + "\n" } + func isMoreIndentedBlockLine(_ line: String) -> Bool { + // In a block scalar the scanner strips content lines to the block's + // indentation, so only a more-indented line retains leading whitespace. + line.first == " " || line.first == "\t" + } + func chompedBlockScalar( _ body: String, chomping: PureYAML.Parsing.BlockScalarChomping, diff --git a/Tests/BlockScalarChompingTests.swift b/Tests/BlockScalarChompingTests.swift index d351630..d0b8c0a 100644 --- a/Tests/BlockScalarChompingTests.swift +++ b/Tests/BlockScalarChompingTests.swift @@ -36,6 +36,12 @@ struct BlockScalarChompingTests { #expect(try value("k: >\n a\n\n\n b\nnext: 1\n") == .string("a\n\nb\n")) } + @Test("A more-indented line inside a folded block stays literal") + func test_foldedMoreIndentedLineStaysLiteral() throws { + #expect(try value("k: >\n normal\n indented\n back\nnext: 1\n") == .string("normal\n indented\nback\n")) + #expect(try value("k: >\n a\n code\nnext: 1\n") == .string("a\n code\n")) + } + @Test("A block scalar with no blank lines is unaffected") func test_blockScalarWithoutBlankLinesUnchanged() throws { #expect(try value("k: |\n a\n b\nnext: 1\n") == .string("a\nb\n"))