Merge pull request #13269 from peppy/fix-relative-link-resolution

Fix relative URLs having a null argument after resolution to `LinkDetails`
This commit is contained in:
Dan Balasescu 2021-06-02 15:43:05 +09:00 committed by GitHub
commit 61ce599898
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 8 deletions

View File

@ -489,5 +489,23 @@ namespace osu.Game.Tests.Chat
Assert.AreEqual(result.Links[2].Url, "\uD83D\uDE00");
Assert.AreEqual(result.Links[3].Url, "\uD83D\uDE20");
}
[Test]
public void TestAbsoluteExternalLinks()
{
LinkDetails result = MessageFormatter.GetLinkDetails("https://google.com");
Assert.AreEqual(LinkAction.External, result.Action);
Assert.AreEqual("https://google.com", result.Argument);
}
[Test]
public void TestRelativeExternalLinks()
{
LinkDetails result = MessageFormatter.GetLinkDetails("/relative");
Assert.AreEqual(LinkAction.External, result.Action);
Assert.AreEqual("/relative", result.Argument);
}
}
}

View File

@ -43,7 +43,7 @@ namespace osu.Game.Graphics.Containers
AddText(text[previousLinkEnd..link.Index]);
string displayText = text.Substring(link.Index, link.Length);
string linkArgument = link.Argument ?? link.Url;
string linkArgument = link.Argument;
string tooltip = displayText == link.Url ? null : link.Url;
AddLink(displayText, link.Action, linkArgument, tooltip);
@ -57,7 +57,7 @@ namespace osu.Game.Graphics.Containers
createLink(AddText(text, creationParameters), new LinkDetails(LinkAction.External, url), url);
public void AddLink(string text, Action action, string tooltipText = null, Action<SpriteText> creationParameters = null)
=> createLink(AddText(text, creationParameters), new LinkDetails(LinkAction.Custom, null), tooltipText, action);
=> createLink(AddText(text, creationParameters), new LinkDetails(LinkAction.Custom, string.Empty), tooltipText, action);
public void AddLink(string text, LinkAction action, string argument, string tooltipText = null, Action<SpriteText> creationParameters = null)
=> createLink(AddText(text, creationParameters), new LinkDetails(action, argument), tooltipText);
@ -70,7 +70,7 @@ namespace osu.Game.Graphics.Containers
createLink(spriteText.Yield(), new LinkDetails(action, argument), tooltipText);
}
public void AddLink(IEnumerable<SpriteText> text, LinkAction action = LinkAction.External, string linkArgument = null, string tooltipText = null)
public void AddLink(IEnumerable<SpriteText> text, LinkAction action, string linkArgument, string tooltipText = null)
{
foreach (var t in text)
AddArbitraryDrawable(t);

View File

@ -6,6 +6,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
#nullable enable
namespace osu.Game.Online.Chat
{
public static class MessageFormatter
@ -61,7 +63,7 @@ namespace osu.Game.Online.Chat
private static string websiteRootUrl = "osu.ppy.sh";
private static void handleMatches(Regex regex, string display, string link, MessageFormatterResult result, int startIndex = 0, LinkAction? linkActionOverride = null, char[] escapeChars = null)
private static void handleMatches(Regex regex, string display, string link, MessageFormatterResult result, int startIndex = 0, LinkAction? linkActionOverride = null, char[]? escapeChars = null)
{
int captureOffset = 0;
@ -173,12 +175,12 @@ namespace osu.Game.Online.Chat
}
}
return new LinkDetails(LinkAction.External, url);
break;
case "osu":
// every internal link also needs some kind of argument
if (args.Length < 3)
return new LinkDetails(LinkAction.External, null);
break;
LinkAction linkType;
@ -220,7 +222,7 @@ namespace osu.Game.Online.Chat
return new LinkDetails(LinkAction.JoinMultiplayerMatch, args[1]);
}
return new LinkDetails(LinkAction.External, null);
return new LinkDetails(LinkAction.External, url);
}
private static MessageFormatterResult format(string toFormat, int startIndex = 0, int space = 3)

View File

@ -277,7 +277,7 @@ namespace osu.Game
{
case LinkAction.OpenBeatmap:
// TODO: proper query params handling
if (link.Argument != null && int.TryParse(link.Argument.Contains('?') ? link.Argument.Split('?')[0] : link.Argument, out int beatmapId))
if (int.TryParse(link.Argument.Contains('?') ? link.Argument.Split('?')[0] : link.Argument, out int beatmapId))
ShowBeatmap(beatmapId);
break;