Skip to content

Commit

Permalink
modified tokenizer to not break on on ]]] at the end of a style
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-a committed Oct 23, 2022
1 parent 9df6ed2 commit f97d9b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/Spectre.Console/Internal/Text/Markup/MarkupTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,55 @@ private bool ReadMarkup()
var encounteredClosing = false;
while (!_reader.Eof)
{
var currentStylePartCanContainMarkup =
builder.ToString()
.Split(' ')
.Last()
.StartsWith("link=", StringComparison.OrdinalIgnoreCase);
current = _reader.Peek();

if (current == ']' && !encounteredOpening)
if (currentStylePartCanContainMarkup)
{
if (encounteredClosing)
switch (current)
{
builder.Append(_reader.Read());
encounteredClosing = false;
continue;
case ']' when !encounteredOpening:
if (encounteredClosing)
{
builder.Append(_reader.Read());
encounteredClosing = false;
continue;
}

_reader.Read();
encounteredClosing = true;
continue;

case '[' when !encounteredClosing:
if (encounteredOpening)
{
builder.Append(_reader.Read());
encounteredOpening = false;
continue;
}

_reader.Read();
encounteredOpening = true;
continue;
}

_reader.Read();
encounteredClosing = true;
continue;
}

if (current == '[' && !encounteredClosing)
else
{
if (encounteredOpening)
switch (current)
{
builder.Append(_reader.Read());
encounteredOpening = false;
continue;
case ']':
_reader.Read();
encounteredClosing = true;
break;
case '[':
_reader.Read();
encounteredOpening = true;
break;
}

_reader.Read();
encounteredOpening = true;
continue;
}

if (encounteredClosing)
Expand Down
20 changes: 20 additions & 0 deletions test/Spectre.Console.Tests/Unit/AnsiConsoleTests.Markup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,25 @@ public void Should_Not_Get_Confused_When_Mixing_Escaped_And_Unescaped()
// Then
console.Output.ShouldBe("[grey][white]");
}

[Theory]
[InlineData("[white][[[/][white]]][/]", "[]")]
[InlineData("[white][[[/]", "[")]
[InlineData("[white]]][/]", "]")]
[InlineData("[black on white link=https://www.gooole.com/q=]]]Search for a bracket[/]", "Search for a bracket")]
[InlineData("[link=https://www.gooole.com/q=]] black on white]Search for a bracket[/]", "Search for a bracket")]
[InlineData("[link]https://www.gooole.com/q=]][/]", "https://www.gooole.com/q=]")]
public void Should_Not_Fail_As_In_GH1024(string markup, string expected)
{
// Given
var console = new TestConsole();
console.EmitAnsiSequences = false;

// When
console.Markup(markup);

// Then
console.Output.ShouldBe(expected);
}
}
}

0 comments on commit f97d9b2

Please sign in to comment.