// Form control properties (E-SHEET W8). A worksheet form control (checkbox, // option button, spinner, …) points through a relationship at its ctrlProp part // (xl/ctrlProps/ctrlProp#.xml), a single element carrying the // control's `objectType` plus its state — `checked` for check/option buttons and // `val` for the spin / scroll / list controls. That is all the print model needs // to list the control with a type-appropriate affordance and its current value. import { XMLParser } from 'fast-xml-parser'; const decoder = new TextDecoder('utf-8'); const parser = new XMLParser({ // §4.1 of XML 1.0: a numeric character reference is not an entity — ` ` // IS a line feed and every parser must decode it. fast-xml-parser gates that // on `htmlEntities`, which defaults to false, so ` ` reached the page as // five literal characters (formats.xlsx writes "Hello, Calc!"). Named // HTML entities come along with the switch; in XML they are undefined anyway, // and reading ` ` as a space beats drawing it. Nested DOCTYPE entities // stay unexpanded either way — the parser never registers them (54764-2.xlsx). htmlEntities: true, ignoreAttributes: false, attributeNamePrefix: '@_', parseAttributeValue: false, removeNSPrefix: true, }); /** A form control's resolved state from its `ctrlProp` part (E-SHEET W8). */ export interface FormControlProps { /** * §18.18.18-ish `ST_ObjectType` — `CheckBox`, `Radio`, `Spin`, `Scroll`, `Drop`, * `List`, `Buttons`, `Label`, `GBox`, `Dialog`, `EditBox`, `Note` … (the * producer's spelling kept). */ readonly objectType?: string; /** Checked state for a check/option button. */ readonly checked?: boolean; /** Current value for a spin / scroll / list control. */ readonly value?: number; } /** * Parse a `xl/ctrlProps/ctrlProp#.xml` part (a single ``) into the * control's {@link FormControlProps} — its `objectType` plus the bit of state the * listing shows (`checked` for check/option buttons, `val` for spin/scroll/list). */ export function parseFormControlProps(data: Uint8Array): FormControlProps { const tree = parser.parse(decoder.decode(data)) as Record; const pr = tree['formControlPr']; const obj = pr && typeof pr === 'object' ? (pr as Record) : undefined; if (!obj) return {}; const objectType = strAttr(obj, 'objectType'); const checkedRaw = strAttr(obj, 'checked'); const valRaw = strAttr(obj, 'val'); const value = valRaw !== undefined && /^-?\d+$/.test(valRaw) ? Number(valRaw) : undefined; return { ...(objectType ? { objectType } : {}), ...(checkedRaw !== undefined ? { checked: checkedRaw === 'Checked' || checkedRaw === '1' } : {}), ...(value !== undefined ? { value } : {}), }; } function strAttr(obj: Record, key: string): string | undefined { const v = obj[`@_${key}`]; return typeof v === 'string' ? v : undefined; }