Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Filters/StringFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected override void OnParametersSet()
new SelectedItem("Contains", Localizer["Contains"].Value),
new SelectedItem("Equal", Localizer["Equal"].Value),
new SelectedItem("NotEqual", Localizer["NotEqual"].Value),
new SelectedItem("NotContains", Localizer["NotContains"].Value)
new SelectedItem("NotContains", Localizer["NotContains"].Value),
new SelectedItem("Regex", Localizer["Regex"].Value)
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ protected override void OnParametersSet()
new SelectedItem("Equal", Localizer["Equal"].Value),
new SelectedItem("NotEqual", Localizer["NotEqual"].Value ),
new SelectedItem("Contains", Localizer["Contains"].Value ),
new SelectedItem("NotContains", Localizer["NotContains"].Value )
new SelectedItem("NotContains", Localizer["NotContains"].Value ),
new SelectedItem("Regex", Localizer["Regex"].Value )
];
}

Expand Down
7 changes: 7 additions & 0 deletions src/BootstrapBlazor/Enums/FilterAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public enum FilterAction
[Description("不包含")]
NotContains,

/// <summary>
/// <para lang="zh">正则表达式匹配</para>
/// <para lang="en">Regular expression match</para>
/// </summary>
[Description("正则匹配")]
Regex,

/// <summary>
/// <para lang="zh">自定义条件</para>
/// <para lang="en">Custom Predicate</para>
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapBlazor/Extensions/LambdaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;

namespace System.Linq;

Expand Down Expand Up @@ -241,6 +242,7 @@ private static Expression GetExpression(this FilterKeyValueAction filter, Expres
FilterAction.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
FilterAction.Contains => left.Contains(right, comparison),
FilterAction.NotContains => Expression.Not(left.Contains(right, comparison)),
FilterAction.Regex => left.RegexMatch(right),
_ => filter.FieldValue switch
{
LambdaExpression t => Expression.Invoke(t, left),
Comment on lines +245 to 248

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Regex matching does not guard against null values, unlike the Contains branch.

The ContainsWidthComparison helper wraps its call in a null-check (left != null && ...), but the new FilterAction.Regex path calls left.RegexMatch(right) without any guard. If left is null or not a string, this can throw at runtime. Please add consistent null-handling (and any needed string conversion) in the regex branch to match the other string-based filters.

Expand All @@ -267,6 +269,12 @@ private static BinaryExpression ContainsWidthComparison(this Expression left, Ex
return Expression.AndAlso(Expression.NotEqual(left, Expression.Constant(null)), Expression.Call(left, method, right, comparisonConstant));
}

private static MethodCallExpression RegexMatch(this Expression left, Expression right)
{
var method = typeof(Regex).GetMethod("IsMatch", [typeof(string), typeof(string)])!;
return Expression.Call(method, left, right);
}

#region Count
/// <summary>
/// <para lang="zh">Count 方法内部使用 Lambda 表达式做通用适配 可接受 IEnumerable 与 Array 子类</para>
Expand Down
2 changes: 2 additions & 0 deletions src/BootstrapBlazor/Locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
"NotEqual": "NotEqual",
"Contains": "Contains",
"NotContains": "NotContains",
"Regex": "Regex",
"EnumFilter.AllText": "All",
"NotSupportedColumnFilterMessage": "<p>Unsupported filter type, Please customize the filter use <code>FilterTemplate</code></p><div>Please refer <a href=\"https://www.blazor.zone/table/filter#CustomFilter\" target=\"_blank\">CustomFilter</a></div>",
"MultiFilterSearchPlaceHolderText": "Please enter ...",
Expand Down Expand Up @@ -377,6 +378,7 @@
"NotEqual": "NotEqual",
"Contains": "Contains",
"NotContains": "NotContains",
"Regex": "Regex",
"GroupText": "Group",
"ItemText": "Item"
},
Expand Down
2 changes: 2 additions & 0 deletions src/BootstrapBlazor/Locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
"NotEqual": "不等于",
"Contains": "包含",
"NotContains": "不包含",
"Regex": "正则匹配",
"EnumFilter.AllText": "全选",
"NotSupportedColumnFilterMessage": "<p>不支持的类型,请使用 <code>FilterTemplate</code> 自定义过滤组件</p><div>请参考文档 <a href=\"https://www.blazor.zone/table/filter#CustomFilter\" target=\"_blank\">CustomFilter</a></div>",
"MultiFilterSearchPlaceHolderText": "请输入 ...",
Expand Down Expand Up @@ -377,6 +378,7 @@
"NotEqual": "不等于",
"Contains": "包含",
"NotContains": "不包含",
"Regex": "正则匹配",
"GroupText": "组合条件",
"ItemText": "单行条件"
},
Expand Down