diff --git a/src/__tests__/unit/figure-svg.test.ts b/src/__tests__/unit/figure-svg.test.ts index 784b2913..6aadf9dd 100644 --- a/src/__tests__/unit/figure-svg.test.ts +++ b/src/__tests__/unit/figure-svg.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { figureSvgToDataUri, MAX_FIGURE_SVG_LENGTH, sanitizeFigureSvg } from '@/lib/figure-svg'; +import { figureSvgToDataUri, MAX_FIGURE_SVG_LENGTH, repairDuplicateSvgAttributes, sanitizeFigureSvg } from '@/lib/figure-svg'; import { parseFigure } from '@/lib/ai/schema'; const triangle = 'A'; @@ -70,3 +70,32 @@ describe('parseFigure', () => { expect(parseFigure(null, null)).toEqual({ hasFigure: false, figureSvg: null }); }); }); + +describe('repairDuplicateSvgAttributes', () => { + const isValidXml = (svg: string) => new DOMParser().parseFromString(svg, 'image/svg+xml').getElementsByTagName('parsererror').length === 0; + + it('把重复写了 x2/y2 的 改成折线,箭头与描边属性保留', () => { + const svg = '' + + '' + + '' + + ''; + expect(isValidXml(svg)).toBe(false); + + const repaired = repairDuplicateSvgAttributes(svg); + expect(isValidXml(repaired)).toBe(true); + expect(repaired).toContain(''); + expect(repaired).toContain(''); + expect(repaired).toContain(''); + }); + + it('其他元素的重复属性只保留第一个,正常 SVG 原样返回', () => { + expect(repairDuplicateSvgAttributes('')).toBe(''); + expect(repairDuplicateSvgAttributes(triangle)).toBe(triangle); + }); + + it('净化与显示时都会修复,已保存的旧配图也能显示', () => { + const broken = ''; + expect(sanitizeFigureSvg(broken)).toContain(''); + expect(decodeURIComponent(figureSvgToDataUri(broken))).toContain(' 元素,包含 x - 白色背景、黑色线条(stroke="#000",线宽 1.5~2),需要区分的区域可用浅灰填充;不要使用彩色装饰。 - 按原图比例和相对位置绘制,保留原题印刷标注:顶点字母、数字、长度、角度、单位、箭头和虚线等,文字用 (font-size 14~18);必须过滤掉人在图上手写的草稿、演算、思考批注、圈画和涂改痕迹。 - 只使用基本图形元素:line、polyline、polygon、rect、circle、ellipse、path、text、g、defs、marker。 +- 必须是合法 XML:同一元素的属性不能重复; 只画一条线段,折线用 。 - 严禁使用 、