Skip to content

Commit

Permalink
🐛 fix(Breadcrumbs): invalid regex pattern (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Aug 23, 2024
1 parent 4d5f1a9 commit b21c38d
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace Masa.Stack.Components.Layouts
{
public partial class Breadcrumbs : MasaComponentBase
{
[Parameter, EditorRequired]
public List<Nav> FlattenedNavs { get; set; } = new();
[Inject] private ILogger<Breadcrumbs> Logger { get; set; } = null!;

[Parameter, EditorRequired] public List<Nav> FlattenedNavs { get; set; } = new();

private List<Nav>? _previousFlattenedNavs;
private string? _prevLocation;
Expand Down Expand Up @@ -45,22 +46,30 @@ private void UpdateItems()
var absolutePath = new Uri(NavigationManager.Uri).AbsolutePath;
var matchedNavs = FlattenedNavs.Where(n =>
{
if (string.IsNullOrWhiteSpace(n.Url) || n.HasChildren)
try
{
return false;
}
if (string.IsNullOrWhiteSpace(n.Url) || n.HasChildren)
{
return false;
}

if (!string.IsNullOrWhiteSpace(n.MatchPattern))
{
return Regex.IsMatch(absolutePath, n.MatchPattern, RegexOptions.IgnoreCase);
}
if (!string.IsNullOrWhiteSpace(n.MatchPattern))
{
return Regex.IsMatch(absolutePath, n.MatchPattern, RegexOptions.IgnoreCase);
}

if (n.Exact)
{
return n.Url.Equals(absolutePath, StringComparison.OrdinalIgnoreCase);
}

if (n.Exact)
return Regex.IsMatch(absolutePath, $"^/?{n.Url}($|/[^/]?)", RegexOptions.IgnoreCase);
}
catch (RegexParseException e)
{
return n.Url.Equals(absolutePath, StringComparison.OrdinalIgnoreCase);
Logger.LogError(e, "Invalid regular expression pattern: {MatchPattern}", n.MatchPattern);
return false;
}

return Regex.IsMatch(absolutePath, $"^/?{n.Url}($|/[^/]?)", RegexOptions.IgnoreCase);
}).ToList();

if (matchedNavs.Count == 0)
Expand Down Expand Up @@ -197,4 +206,4 @@ protected override ValueTask DisposeAsyncCore()
return base.DisposeAsyncCore();
}
}
}
}

0 comments on commit b21c38d

Please sign in to comment.