Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified tokenizer to not break on on ]]] at the end of a style #1027

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}