Skip to content

Commit

Permalink
Escape any Markup when displaying selected prompt items
Browse files Browse the repository at this point in the history
If the item contained escaped markup, after the call to RemoveMarkup
the string will contain unescaped markup (that the user explicitly had
escaped before) for those cases we need to escape all remaining markup.
  • Loading branch information
nils-a authored and patriksvensson committed Nov 2, 2021
1 parent b738187 commit ba4b7b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Spectre.Console/Prompts/MultiSelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
if (current)
{
text = text.RemoveMarkup();
text = text.RemoveMarkup().EscapeMarkup();
}

var checkbox = item.Node.IsSelected
Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
var text = (Converter ?? TypeConverterHelper.ConvertToString)?.Invoke(item.Node.Data) ?? item.Node.Data.ToString() ?? "?";
if (current)
{
text = text.RemoveMarkup();
text = text.RemoveMarkup().EscapeMarkup();
}

grid.AddRow(new Markup(indent + prompt + " " + text, style));
Expand Down
30 changes: 30 additions & 0 deletions test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Shouldly;
using Spectre.Console.Testing;
using Xunit;

namespace Spectre.Console.Tests.Unit
{
public sealed class SelectionPromptTests
{
[Fact]
[GitHubIssue(608)]
public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
var input = "[red]This text will never be red[/]".EscapeMarkup();

// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.AddChoices(input);
prompt.Show(console);

// Then
console.Output.ShouldContain(@"[red]This text will never be red[/]");
}
}
}

0 comments on commit ba4b7b9

Please sign in to comment.