fix(filters): preserve regex meaning when changing operators

Swap wrapped and raw regex sources only when both compile identically after saving. Leave literal, glob, multiline and Unicode-sensitive comparisons unchanged rather than inventing non-equivalent conversions.
This commit is contained in:
翅膀 committed 2026-10-17 07:47:03 +08:00
1 parent 2717ab49dc
commit 54b2944dd4
5 files changed
+129 -8

No files matched your search

+11 -3
View File
@@ -5,6 +5,7 @@ import { FILTER_SCOPE_EVENTS, blankNode, isBlankNode, nodeKey } from "~/composab
import { FILTER_TYPES, FILTER_OPS } from "~/types";
import type { FilterOp, FilterType } from "~/types";
import {
convertValuesForOp,
fieldPathOptions,
isArrayFieldPath,
validateFilterValue,
@@ -58,17 +59,24 @@ function validateValue(value: string): string | null {
return validateFilterValue(props.node.leaf.type, props.node.leaf.op, value);
}
/** Switch the operator, swapping `/x/` ↔ `x` when moving to or from `regex`. */
function changeOp(next: FilterOp): void {
const { leaf } = props.node;
leaf.values = convertValuesForOp(leaf.values, leaf.op, next);
leaf.op = next;
}
function setType(value: string): void {
if ((FILTER_TYPES as readonly string[]).includes(value)) {
props.node.leaf.type = value as FilterType;
// Keyword filters have no operator; a hidden `exists` would drop their values.
if (value === "keyword") props.node.leaf.op = "eq";
if (value === "keyword") changeOp("eq");
}
}
function setOp(value: string): void {
if (FILTER_OPS.includes(value as FilterOp)) {
props.node.leaf.op = value as FilterOp;
changeOp(value as FilterOp);
// `exists` hides the values input: its unadded text must not block saving.
if (!showValues.value) props.node.leaf.draft = "";
}
@@ -78,7 +86,7 @@ function setPath(value: string | string[]): void {
props.node.leaf.path = typeof value === "string" ? value : "";
// Paths ending on an array compare the whole array; `contains` matches an element.
if (isArrayFieldPath(props.node.leaf.path) && props.node.leaf.op === "eq") {
props.node.leaf.op = "contains";
changeOp("contains");
}
}
+32
View File
@@ -1198,6 +1198,38 @@ function typeValueOptions(
}
}
// ---------------------------------------------------------------------------
// Operator changes
// ---------------------------------------------------------------------------
/** Operators whose values may be a wrapped `/regex/` (server `matchField`). */
function readsWrappedRegex(op: FilterOp): boolean {
return op === "eq" || op === "ne" || op === "in";
}
/**
* Values rewritten for a new operator where the rewrite matches exactly the
* same: a wrapped `/x/` of the default operators and the raw `x` of `regex`
* compile to the same server regex, so they are swapped (`/^v\d+/` ↔
* `^v\d+`). Nothing else is rewritten — literal, glob and substring
* comparisons differ from regexes on line breaks and Unicode case folding —
* so other values stay as typed, and validation flags those that can never
* match. Saving trims every value, so a source that is empty or starts/ends
* with whitespace (`//`, `/ main /`) cannot stand alone and stays wrapped.
*/
export function convertValuesForOp(values: string[], from: FilterOp, to: FilterOp): string[] {
if (readsWrappedRegex(from) && to === "regex") {
return values.map((v) => {
const value = v.trim();
const source = value.slice(1, -1);
return isWrappedRegex(value) && source && source === source.trim() ? source : v;
});
}
if (from === "regex" && readsWrappedRegex(to))
return values.map((v) => (v.trim() ? `/${v.trim()}/` : v));
return values;
}
// ---------------------------------------------------------------------------
// Validation (mirrors server/lib/events/filter-ast.ts)
// ---------------------------------------------------------------------------