feat(anatomy): parse SVG convention into Bone map with validation

This commit is contained in:
Bastien Chanot committed 2026-05-18 21:01:23 +02:00
1 parent 3dc7153361
commit ac50ac5c2c
2 files changed
+156 -29

No files matched your search

+71 -29
View File
@@ -1,6 +1,7 @@
import { SvgConventionError } from './types'
import type { Bone, BoneCategory } from './types'
const VALID_CATEGORIES: BoneCategory[] = [
const VALID_CATEGORIES: readonly BoneCategory[] = [
'articulation',
'charniere',
'spine-zone',
@@ -10,33 +11,74 @@ const VALID_CATEGORIES: BoneCategory[] = [
'cadran-crane',
]
/**
* Parses an inline SVG document and returns a map of boneId → Bone.
* Throws SvgConventionError if a required attribute is missing or invalid.
*/
export function parseSvgBones(_svgRoot: SVGSVGElement): Map<string, Bone> {
throw new Error('not implemented')
}
/**
* Walks up from event.target to find the nearest element with data-bone-id.
* Returns null if no bone element is found.
*/
export function getBoneIdFromEvent(_event: Event): string | null {
throw new Error('not implemented')
}
/**
* Validates that all required data attributes are present on an element.
* Throws SvgConventionError on the first missing attribute.
*/
export function validateBoneElement(_el: Element): void {
throw new Error('not implemented')
}
/**
* Returns true if the category string is a valid BoneCategory.
*/
export function isValidCategory(value: string): value is BoneCategory {
return (VALID_CATEGORIES as string[]).includes(value)
return (VALID_CATEGORIES as readonly string[]).includes(value)
}
function classList(el: Element): string[] {
return (el.getAttribute('class') ?? '').split(/\s+/).filter(Boolean)
}
export function validateBoneElement(el: Element): void {
const boneId = el.getAttribute('data-bone-id') ?? ''
for (const attr of ['data-bone-fr', 'data-bone-en', 'data-category']) {
if (!el.getAttribute(attr)) {
throw new SvgConventionError(
`Element data-bone-id="${boneId}" missing attribute ${attr}`,
boneId,
attr,
)
}
}
const cat = el.getAttribute('data-category') ?? ''
if (!isValidCategory(cat)) {
throw new SvgConventionError(
`Element data-bone-id="${boneId}" has unknown data-category="${cat}"`,
boneId,
'data-category',
)
}
}
export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
const map = new Map<string, Bone>()
const all = svgRoot.querySelectorAll<Element>('[data-bone-id]')
all.forEach((el) => {
validateBoneElement(el)
const id = el.getAttribute('data-bone-id') as string
const fr = el.getAttribute('data-bone-fr') as string
const en = el.getAttribute('data-bone-en') as string
const category = el.getAttribute('data-category') as BoneCategory
const opensDetail = el.getAttribute('data-opens-detail') ?? undefined
const classes = classList(el)
const isHitbox = classes.includes('hitbox')
const existing = map.get(id)
if (existing) {
if (isHitbox) existing.hitboxEls.push(el)
else existing.visibleEls.push(el)
if (opensDetail && !existing.opensDetail) existing.opensDetail = opensDetail
} else {
map.set(id, {
id,
fr,
en,
category,
opensDetail,
hitboxEls: isHitbox ? [el] : [],
visibleEls: isHitbox ? [] : [el],
})
}
})
return map
}
export function getBoneIdFromEvent(event: Event): string | null {
let node = event.target as Element | null
while (node && node.nodeType === 1) {
const id = node.getAttribute?.('data-bone-id')
if (id) return id
node = node.parentElement
}
return null
}
+85
View File
@@ -0,0 +1,85 @@
import { describe, it, expect } from 'vitest'
import {
parseSvgBones,
getBoneIdFromEvent,
isValidCategory,
} from '@/anatomy/svgConvention'
import { SvgConventionError } from '@/anatomy/types'
function svgFromString(s: string): SVGSVGElement {
const doc = new DOMParser().parseFromString(s, 'image/svg+xml')
return doc.documentElement as unknown as SVGSVGElement
}
const FIXTURE = `
<svg xmlns="http://www.w3.org/2000/svg">
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
data-category="articulation" class="bone osteo articulation hitbox bone-epaule" />
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
data-category="articulation" class="bone osteo articulation visible bone-epaule" />
<polygon data-bone-id="cervical-vertebrae-spine-zone" data-bone-fr="Vertèbres cervicales"
data-bone-en="Cervical Vertebrae" data-category="spine-zone"
class="bone osteo spine-zone bone-cervical-vertebrae" />
<polygon data-bone-id="zone-head" data-bone-fr="Crane" data-bone-en="Head"
data-category="head" data-opens-detail="head" />
</svg>`
describe('parseSvgBones', () => {
it('groups hitbox + visible by data-bone-id', () => {
const map = parseSvgBones(svgFromString(FIXTURE))
const epaule = map.get('epaule')
expect(epaule).toBeDefined()
expect(epaule?.fr).toBe('Épaule')
expect(epaule?.category).toBe('articulation')
expect(epaule?.hitboxEls.length).toBe(1)
expect(epaule?.visibleEls.length).toBe(1)
})
it('captures opensDetail when present', () => {
const map = parseSvgBones(svgFromString(FIXTURE))
expect(map.get('zone-head')?.opensDetail).toBe('head')
})
it('records spine-zone single-element bones', () => {
const map = parseSvgBones(svgFromString(FIXTURE))
const bone = map.get('cervical-vertebrae-spine-zone')
expect(bone?.category).toBe('spine-zone')
})
})
describe('parseSvgBones validation', () => {
it('throws on missing data-bone-fr', () => {
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
<circle data-bone-id="x" data-bone-en="X" data-category="os" />
</svg>`
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
})
it('throws on unknown category', () => {
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
<circle data-bone-id="x" data-bone-fr="X" data-bone-en="X" data-category="bogus" />
</svg>`
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
})
})
describe('isValidCategory', () => {
it('accepts documented categories', () => {
expect(isValidCategory('articulation')).toBe(true)
expect(isValidCategory('spine-zone')).toBe(true)
expect(isValidCategory('cadran-crane')).toBe(true)
})
it('rejects unknown values', () => {
expect(isValidCategory('nope')).toBe(false)
})
})
describe('getBoneIdFromEvent', () => {
it('returns null when no bone in ancestry', () => {
const div = document.createElement('div')
const evt = new Event('click')
Object.defineProperty(evt, 'target', { value: div })
expect(getBoneIdFromEvent(evt)).toBeNull()
})
})