Skip to content

Commit 3fdeb30

Browse files
authored
Merge pull request #778 from rutger-dijkstra/fix-block-level-quoted-strings
Quoted scalars are flow scalars only if flowLevel > 0 +semver:fix
2 parents ae590e9 + 0bb66ea commit 3fdeb30

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

YamlDotNet.Test/Core/ScannerTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,93 @@ public void Plain_Scalar_inside_flow_does_not_allow_braces()
443443
Error("Invalid key indicator format."));
444444
}
445445

446+
[Fact]
447+
public void Keys_can_start_with_colons_in_nested_block()
448+
{
449+
AssertSequenceOfTokensFrom(Yaml.ScannerForText("root:\n :first: 1\n :second: 2"),
450+
StreamStart,
451+
BlockMappingStart,
452+
Key,
453+
PlainScalar("root"),
454+
Value,
455+
BlockMappingStart,
456+
Key,
457+
PlainScalar(":first"),
458+
Value,
459+
PlainScalar("1"),
460+
Key,
461+
PlainScalar(":second"),
462+
Value,
463+
PlainScalar("2"),
464+
BlockEnd,
465+
BlockEnd,
466+
StreamEnd);
467+
}
468+
469+
[Fact]
470+
public void Keys_can_start_with_colons_after_quoted_values()
471+
{
472+
AssertSequenceOfTokensFrom(Yaml.ScannerForText(":first: '1'\n:second: 2"),
473+
StreamStart,
474+
BlockMappingStart,
475+
Key,
476+
PlainScalar(":first"),
477+
Value,
478+
SingleQuotedScalar("1"),
479+
Key,
480+
PlainScalar(":second"),
481+
Value,
482+
PlainScalar("2"),
483+
BlockEnd,
484+
StreamEnd);
485+
}
486+
487+
[Fact]
488+
public void Keys_can_start_with_colons_after_single_quoted_values_in_nested_block()
489+
{
490+
AssertSequenceOfTokensFrom(Yaml.ScannerForText("xyz:\n :hello: 'world'\n :goodbye: world"),
491+
StreamStart,
492+
BlockMappingStart,
493+
Key,
494+
PlainScalar("xyz"),
495+
Value,
496+
BlockMappingStart,
497+
Key,
498+
PlainScalar(":hello"),
499+
Value,
500+
SingleQuotedScalar("world"),
501+
Key,
502+
PlainScalar(":goodbye"),
503+
Value,
504+
PlainScalar("world"),
505+
BlockEnd,
506+
BlockEnd,
507+
StreamEnd);
508+
}
509+
510+
[Fact]
511+
public void Keys_can_start_with_colons_after_double_quoted_values_in_nested_block()
512+
{
513+
AssertSequenceOfTokensFrom(Yaml.ScannerForText("xyz:\n :hello: \"world\"\n :goodbye: world"),
514+
StreamStart,
515+
BlockMappingStart,
516+
Key,
517+
PlainScalar("xyz"),
518+
Value,
519+
BlockMappingStart,
520+
Key,
521+
PlainScalar(":hello"),
522+
Value,
523+
DoubleQuotedScalar("world"),
524+
Key,
525+
PlainScalar(":goodbye"),
526+
Value,
527+
PlainScalar("world"),
528+
BlockEnd,
529+
BlockEnd,
530+
StreamEnd);
531+
}
532+
446533
private void AssertPartialSequenceOfTokensFrom(Scanner scanner, params Token[] tokens)
447534
{
448535
var tokenNumber = 1;

YamlDotNet/Core/Scanner.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,15 @@ private void FetchNextToken()
479479

480480
if (analyzer.Check('\''))
481481
{
482-
FetchFlowScalar(true);
482+
FetchQuotedScalar(true);
483483
return;
484484
}
485485

486486
// Is it a double-quoted scalar?
487487

488488
if (analyzer.Check('"'))
489489
{
490-
FetchFlowScalar(false);
490+
FetchQuotedScalar(false);
491491
return;
492492
}
493493

@@ -1770,7 +1770,7 @@ private int ScanBlockScalarBreaks(int currentIndent, StringBuilder breaks, bool
17701770
/// Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
17711771
/// </summary>
17721772

1773-
private void FetchFlowScalar(bool isSingleQuoted)
1773+
private void FetchQuotedScalar(bool isSingleQuoted)
17741774
{
17751775
// A plain scalar could be a simple key.
17761776

@@ -1782,7 +1782,7 @@ private void FetchFlowScalar(bool isSingleQuoted)
17821782

17831783
// Indicates the adjacent flow scalar that a prior flow scalar has been fetched.
17841784

1785-
flowScalarFetched = true;
1785+
flowScalarFetched = flowLevel > 0;
17861786

17871787
// Create the SCALAR token and append it to the queue.
17881788

0 commit comments

Comments
 (0)