Throw if markup contains unescaped close tag

This commit is contained in:
Patrik Svensson 2020-08-26 21:15:52 +02:00 committed by Patrik Svensson
parent 31f117aed0
commit decb887b0a
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,40 @@
using System;
using Shouldly;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
public sealed class MarkupTests
{
[Theory]
[InlineData("Hello [[ World ]")]
[InlineData("Hello [[ World ] !")]
public void Should_Throw_If_Closing_Tag_Is_Not_Properly_Escaped(string input)
{
// Given
var fixture = new PlainConsole();
// When
var result = Record.Exception(() => new Markup(input));
// Then
result.ShouldNotBeNull();
result.ShouldBeOfType<InvalidOperationException>();
result.Message.ShouldBe("Encountered unescaped ']' token at position 16");
}
[Fact]
public void Should_Escape_Markup_Blocks_As_Expected()
{
// Given
var fixture = new PlainConsole();
var markup = new Markup("Hello [[ World ]] !");
// When
fixture.Render(markup);
// Then
fixture.Output.ShouldBe("Hello [ World ] !");
}
}
}

View File

@ -91,6 +91,8 @@ namespace Spectre.Console.Internal
{
var position = _reader.Position;
var builder = new StringBuilder();
var encounteredClosing = false;
while (!_reader.Eof)
{
current = _reader.Peek();
@ -98,10 +100,34 @@ namespace Spectre.Console.Internal
{
break;
}
else if (current == ']')
{
if (encounteredClosing)
{
_reader.Read();
encounteredClosing = false;
continue;
}
encounteredClosing = true;
}
else
{
if (encounteredClosing)
{
throw new InvalidOperationException(
$"Encountered unescaped ']' token at position {_reader.Position}");
}
}
builder.Append(_reader.Read());
}
if (encounteredClosing)
{
throw new InvalidOperationException($"Encountered unescaped ']' token at position {_reader.Position}");
}
Current = new MarkupToken(MarkupTokenKind.Text, builder.ToString(), position);
return true;
}