Skip to content

Commit

Permalink
🐛 fix(Routable): invalid regex pattern (#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Aug 23, 2024
1 parent e73d943 commit 8c6ef3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Masa.Blazor/Core/MasaComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ForwardRef RefBack
private ElementReference? _prevRef;
private bool _elementReferenceChanged;

protected ILogger Logger => LoggerFactory.CreateLogger(GetType());
public ILogger Logger => LoggerFactory.CreateLogger(GetType());

#region Build class and style

Expand Down
16 changes: 13 additions & 3 deletions src/Masa.Blazor/Mixins/Routable/IRoutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Masa.Blazor;

public interface IRoutable
{
ILogger Logger { get; }

IDictionary<string, object> Attributes { get; }

bool Disabled { get; }
Expand Down Expand Up @@ -40,7 +42,7 @@ public interface IRoutable

public int Tabindex => Attributes.TryGetValue("tabindex", out var tabindex) ? Convert.ToInt32(tabindex) : 0;

public(string tag, Dictionary<string, object>) GenerateRouteLink()
public (string tag, Dictionary<string, object>) GenerateRouteLink()
{
string tag;
Dictionary<string, object> attrs = new(Attributes);
Expand Down Expand Up @@ -69,7 +71,15 @@ public bool MatchRoute()

var absolutePath = NavigationManager.GetAbsolutePath();

return MatchRoute(Href, absolutePath, Exact, MatchPattern);
try
{
return MatchRoute(Href, absolutePath, Exact, MatchPattern);
}
catch (RegexParseException)
{
Logger.LogError("Invalid regular expression pattern: {MatchPattern}", MatchPattern);
return false;
}
}

public static bool MatchRoute(string href, string absolutePath, bool exact, string? matchPattern)
Expand Down Expand Up @@ -100,4 +110,4 @@ private static string FormatUrl(string url)

return url;
}
}
}
5 changes: 4 additions & 1 deletion src/Masa.Blazor/Mixins/Routable/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public Router(IRoutable routable)
Exact = routable.Exact;
NavigationManager = routable.NavigationManager;
MatchPattern = routable.MatchPattern;
Logger = routable.Logger;
}

public IDictionary<string, object?> Attributes { get; set; }
Expand All @@ -35,4 +36,6 @@ public Router(IRoutable routable)
public string? MatchPattern { get; set; }

public NavigationManager NavigationManager { get; set; }
}

public ILogger Logger { get; set; }
}

0 comments on commit 8c6ef3b

Please sign in to comment.