93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import { setCoreRuntime } from '@/core/runtime'
|
|
import { cleanSvg, composeCleaningPreview } from '@/editor/core/cleaning'
|
|
import { applyEdit, dependentsOf } from '@/editor/core/editOps'
|
|
import { testCoreRuntime } from '../helpers/core'
|
|
|
|
const DIRTY =
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><style>.a{fill:red}</style>' +
|
|
'<defs><linearGradient id="g"><stop offset="0"/></linearGradient></defs>' +
|
|
'<rect id="r" class="a" width="4" height="4" fill="url(#g)"/></svg>'
|
|
|
|
beforeAll(async () => {
|
|
setCoreRuntime(await testCoreRuntime())
|
|
})
|
|
|
|
function cleaned() {
|
|
const out = cleanSvg(new TextEncoder().encode(DIRTY))
|
|
if (!out.ok || out.value.stored === null || out.value.sha256 === null) {
|
|
throw new Error('not cleanable')
|
|
}
|
|
return {
|
|
cleaning: out.value,
|
|
drawing: { stored: out.value.stored, sha256: out.value.sha256 },
|
|
}
|
|
}
|
|
|
|
describe('cleanSvg', () => {
|
|
it('reports a removal and a stored form without the handler', () => {
|
|
const { cleaning } = cleaned()
|
|
expect(cleaning.outcome).toBe('cleanable')
|
|
expect(cleaning.style_removed).toBe(true)
|
|
expect(cleaning.stored).not.toContain('<style')
|
|
})
|
|
|
|
it('rejects bytes that are not an SVG', () => {
|
|
const out = cleanSvg(new TextEncoder().encode('nope'))
|
|
expect(out).toMatchObject({ ok: true, value: { outcome: 'rejected' } })
|
|
const active = cleanSvg(
|
|
new TextEncoder().encode(
|
|
'<svg xmlns="http://www.w3.org/2000/svg"><rect onclick="x()"/></svg>',
|
|
),
|
|
)
|
|
expect(active).toMatchObject({
|
|
ok: true,
|
|
value: { reason: 'svg_active_content' },
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('composeCleaningPreview', () => {
|
|
it('renders the stored drawing in a scope unique per preview and never the original', () => {
|
|
const { drawing } = cleaned()
|
|
const a = composeCleaningPreview(drawing)
|
|
const b = composeCleaningPreview(drawing)
|
|
if (!a.ok || !b.ok) throw new Error('compose failed')
|
|
expect(a.value.scope).toMatch(/^fv\.view-[a-z2-7]{10}\.$/)
|
|
expect(a.value.scope).not.toBe(b.value.scope)
|
|
const svg = a.value.preview.canvas(a.value.viewId)
|
|
if (!svg.ok) throw new Error(svg.kind)
|
|
expect(svg.value).toContain(`id="${a.value.scope}r"`)
|
|
expect(svg.value).not.toContain('<style')
|
|
a.value.preview.free()
|
|
b.value.preview.free()
|
|
expect(a.value.preview.canvas(a.value.viewId)).toMatchObject({
|
|
ok: false,
|
|
kind: 'core_unavailable',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('edit wrappers', () => {
|
|
it('refuses a remove of an unknown element with the core kind', () => {
|
|
const text = JSON.stringify(
|
|
(() => {
|
|
const p = composeCleaningPreview(cleaned().drawing)
|
|
if (!p.ok) throw new Error(p.kind)
|
|
const document = JSON.parse(p.value.preview.document)
|
|
p.value.preview.free()
|
|
return document
|
|
})(),
|
|
)
|
|
expect(
|
|
applyEdit(text, {
|
|
op: 'remove',
|
|
key: { kind: 'annotation', id: 'anno-none' },
|
|
}).ok,
|
|
).toBe(false)
|
|
expect(dependentsOf(text, { kind: 'view', id: 'view-main' })).toMatchObject(
|
|
{ ok: true },
|
|
)
|
|
})
|
|
})
|