mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 22:41:09 +02:00
fix(calc): use safe evaluate engine in ListView, add error logging
The workbench ListView was using Function() (effectively eval) with aggressive sanitizing that stripped valid characters, causing every calculation to fail with a generic "Fehler". Now uses the same safe parser as the standard calculator page. Also adds console.error logging and shows result before persisting to DB. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a91a6076cc
commit
4616798d34
2 changed files with 18 additions and 22 deletions
|
|
@ -5,6 +5,7 @@
|
|||
<script lang="ts">
|
||||
import { useLiveQueryWithDefault } from '@mana/local-store/svelte';
|
||||
import { db } from '$lib/data/database';
|
||||
import { evaluate as evalExpression, formatResult } from './engine/evaluate';
|
||||
import type { LocalCalculation } from './types';
|
||||
|
||||
const calcQuery = useLiveQueryWithDefault(async () => {
|
||||
|
|
@ -32,37 +33,31 @@
|
|||
return;
|
||||
}
|
||||
try {
|
||||
const sanitized = expression.replace(/[^0-9+\-*/().%\s]/g, '');
|
||||
const evalResult = Function('"use strict"; return (' + sanitized + ')')();
|
||||
if (evalResult === undefined || evalResult === null || isNaN(evalResult)) {
|
||||
result = expression;
|
||||
hasError = false;
|
||||
} else {
|
||||
result = String(evalResult);
|
||||
hasError = false;
|
||||
}
|
||||
const num = evalExpression(expression);
|
||||
result = formatResult(num);
|
||||
hasError = false;
|
||||
} catch {
|
||||
// Incomplete expression — don't show error while typing
|
||||
hasError = false;
|
||||
}
|
||||
});
|
||||
|
||||
function evaluate() {
|
||||
function calc() {
|
||||
if (!expression.trim()) return;
|
||||
try {
|
||||
const sanitized = expression.replace(/[^0-9+\-*/().%\s]/g, '');
|
||||
const evalResult = Function('"use strict"; return (' + sanitized + ')')();
|
||||
result = String(evalResult);
|
||||
const num = evalExpression(expression);
|
||||
result = formatResult(num);
|
||||
hasError = false;
|
||||
} catch {
|
||||
result = 'Fehler';
|
||||
} catch (e) {
|
||||
console.error('[calc] ListView error:', e);
|
||||
result = e instanceof Error ? e.message : 'Fehler';
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
|
||||
function press(key: string) {
|
||||
if (key === '=') {
|
||||
evaluate();
|
||||
calc();
|
||||
} else if (key === 'C') {
|
||||
expression = '';
|
||||
result = '0';
|
||||
|
|
@ -77,7 +72,7 @@
|
|||
function handleKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
evaluate();
|
||||
calc();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -86,18 +86,19 @@
|
|||
const result = evaluate(expression);
|
||||
const formatted = formatResult(result);
|
||||
|
||||
display = formatted;
|
||||
hasResult = true;
|
||||
error = '';
|
||||
|
||||
await calculationsStore.addCalculation({
|
||||
mode: 'standard',
|
||||
expression: expression,
|
||||
result: formatted,
|
||||
skin: activeSkin,
|
||||
});
|
||||
|
||||
display = formatted;
|
||||
hasResult = true;
|
||||
error = '';
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Fehler';
|
||||
console.error('[calc] Fehler bei Berechnung:', e);
|
||||
error = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue