diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs index 5c64af3bb..c8824317e 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs @@ -27,8 +27,6 @@ namespace Squidex.Domain.Apps.Core.HandleRules public class RuleEventFormatter { private const string Fallback = "null"; - private const string ScriptSuffix = ")"; - private const string ScriptPrefix = "Script("; private static readonly Regex RegexPatternOld = new Regex(@"^(?[^_]*)_(?[^\s]*)", RegexOptions.Compiled); private static readonly Regex RegexPatternNew = new Regex(@"^\{(?[^_]*)_(?[^\s]*)\}", RegexOptions.Compiled); private readonly List<(char[] Pattern, Func Replacer)> patterns = new List<(char[] Pattern, Func Replacer)>(); @@ -85,13 +83,8 @@ namespace Squidex.Domain.Apps.Core.HandleRules return text; } - var trimmed = text.Trim(); - - if (trimmed.StartsWith(ScriptPrefix, StringComparison.OrdinalIgnoreCase) && - trimmed.EndsWith(ScriptSuffix, StringComparison.OrdinalIgnoreCase)) + if (TryGetScript(text.Trim(), out var script)) { - var script = trimmed.Substring(ScriptPrefix.Length, trimmed.Length - ScriptPrefix.Length - ScriptSuffix.Length); - var context = new ScriptContext { ["event"] = @event @@ -383,5 +376,23 @@ namespace Squidex.Domain.Apps.Core.HandleRules return current?.ToString(); } + + private static bool TryGetScript(string text, out string script) + { + const string ScriptSuffix = ")"; + const string ScriptPrefix = "Script("; + + script = null!; + + var comparer = StringComparison.OrdinalIgnoreCase; + + if (text.StartsWith(ScriptPrefix, comparer) && text.EndsWith(ScriptSuffix, comparer)) + { + script = text.Substring(ScriptPrefix.Length, text.Length - ScriptPrefix.Length - ScriptSuffix.Length); + return true; + } + + return false; + } } }