Skip to content

Commit 995a6b9

Browse files
Allow BOM prefixes before bare documents
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 28ac74e commit 995a6b9

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/functions/private/Test-YamlDocumentPrefix.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ function Test-YamlDocumentPrefix {
4545
$Index = $lineEnd + 1
4646
continue
4747
}
48-
if (-not $line.StartsWith('---', [System.StringComparison]::Ordinal)) {
49-
return $false
48+
if ($line.StartsWith('---', [System.StringComparison]::Ordinal) -and
49+
($line.Length -eq 3 -or (Test-YamlWhiteSpace -Character $line[3]))) {
50+
return $true
5051
}
51-
return $line.Length -eq 3 -or
52-
(Test-YamlWhiteSpace -Character $line[3])
52+
return -not $RequireDocumentStart -and -not $directiveSeen
5353
}
5454
return -not $directiveSeen
5555
}

tests/ConvertFrom-Yaml.Tests.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,29 @@ folded: >
228228
$trailingPrefix = @(
229229
"---`nvalue`n...`n${bom}# trailing prefix" | ConvertFrom-Yaml
230230
)
231+
$barePrefixDocument = @(
232+
"# prefix`n${bom}value" | ConvertFrom-Yaml
233+
)
234+
$bareBoundaryDocuments = @(
235+
"---`none`n...`n${bom}two" | ConvertFrom-Yaml
236+
)
237+
$commentedBareBoundaryDocuments = @(
238+
"---`none`n...`n${bom}# comment`ntwo" | ConvertFrom-Yaml
239+
)
231240

232241
$documents | Should -Be @('one', 'two')
233242
$implicitBoundaryDocuments | Should -Be @('one', 'two')
234243
$commentThenBom | Should -Be @('value')
235244
$alternatingPrefixes | Should -Be @('value')
236245
$trailingPrefix | Should -Be @('value')
246+
$barePrefixDocument | Should -Be @('value')
247+
$bareBoundaryDocuments | Should -Be @('one', 'two')
248+
$commentedBareBoundaryDocuments | Should -Be @('one', 'two')
237249
("${bom}---`nvalue" | ConvertFrom-Yaml) | Should -Be 'value'
238250
("foo${bom}bar" | Test-Yaml) | Should -BeFalse
239251
("---`n${bom}value" | Test-Yaml) | Should -BeFalse
240-
("---`none`n...`n${bom}# comment`ntwo" | Test-Yaml) | Should -BeFalse
252+
("---`none`n${bom}two" | Test-Yaml) | Should -BeFalse
253+
("${bom}%YAML 1.2`nvalue" | Test-Yaml) | Should -BeFalse
241254
('"foo' + $bom + 'bar"' | ConvertFrom-Yaml) | Should -Be "foo${bom}bar"
242255
("'foo${bom}bar'" | ConvertFrom-Yaml) | Should -Be "foo${bom}bar"
243256
("`"a`n${bom}%foo`nb`"" | ConvertFrom-Yaml) |

0 commit comments

Comments
 (0)