From ac50ac5c2ca1bb571e19e322b342f93560d108d4 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Mon, 18 May 2026 21:01:23 +0200 Subject: [PATCH] feat(anatomy): parse SVG convention into Bone map with validation --- src/anatomy/svgConvention.ts | 100 ++++++++++++++++------- tests/unit/anatomy.svgConvention.test.ts | 85 +++++++++++++++++++ 2 files changed, 156 insertions(+), 29 deletions(-) create mode 100644 tests/unit/anatomy.svgConvention.test.ts diff --git a/src/anatomy/svgConvention.ts b/src/anatomy/svgConvention.ts index 81009ce9..f753d813 100644 --- a/src/anatomy/svgConvention.ts +++ b/src/anatomy/svgConvention.ts @@ -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 { - 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 { + const map = new Map() + const all = svgRoot.querySelectorAll('[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 } diff --git a/tests/unit/anatomy.svgConvention.test.ts b/tests/unit/anatomy.svgConvention.test.ts new file mode 100644 index 00000000..baaf8542 --- /dev/null +++ b/tests/unit/anatomy.svgConvention.test.ts @@ -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 = ` + + + + + +` + +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 = ` + + ` + expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError) + }) + + it('throws on unknown category', () => { + const bad = ` + + ` + 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() + }) +})