@office-kit/xlsx 0.9.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cell-style-CN9JXOwm.mjs → cell-style-DuvS_Lsg.mjs} +2 -2
- package/dist/cell-style-DuvS_Lsg.mjs.map +1 -0
- package/dist/{differential-YEjxNScf.mjs → differential-DvKU8fJt.mjs} +2 -2
- package/dist/{differential-YEjxNScf.mjs.map → differential-DvKU8fJt.mjs.map} +1 -1
- package/dist/drawing/drawing-xml.d.ts +7 -0
- package/dist/drawing/drawing.d.ts +25 -0
- package/dist/drawing-BxzLuryn.mjs.map +1 -1
- package/dist/io/load.d.ts +7 -0
- package/dist/io.mjs +2 -2
- package/dist/{load-BA30X4-M.mjs → load-MyOIjtrU.mjs} +57 -13
- package/dist/load-MyOIjtrU.mjs.map +1 -0
- package/dist/{manifest-CVL1_t0O.mjs → manifest-Cm9K7D62.mjs} +5 -5
- package/dist/{manifest-CVL1_t0O.mjs.map → manifest-Cm9K7D62.mjs.map} +1 -1
- package/dist/node.mjs +1 -1
- package/dist/packaging.mjs +1 -1
- package/dist/{parser-nk0Lqmcb.mjs → parser-D3SLaOFe.mjs} +27 -4
- package/dist/parser-D3SLaOFe.mjs.map +1 -0
- package/dist/{save-C9ChJfWM.mjs → save-2moCT3Af.mjs} +68 -34
- package/dist/save-2moCT3Af.mjs.map +1 -0
- package/dist/schema.mjs +1 -1
- package/dist/{serialize-55EnT30e.mjs → serialize-BC2Wu3bR.mjs} +2 -2
- package/dist/{serialize-55EnT30e.mjs.map → serialize-BC2Wu3bR.mjs.map} +1 -1
- package/dist/{serializer-DQzTTNOS.mjs → serializer-BtO2TrI7.mjs} +21 -9
- package/dist/serializer-BtO2TrI7.mjs.map +1 -0
- package/dist/streaming/read-only.d.ts +2 -0
- package/dist/streaming.mjs +16 -17
- package/dist/streaming.mjs.map +1 -1
- package/dist/styles/stylesheet.d.ts +8 -0
- package/dist/styles.mjs +3 -3
- package/dist/{stylesheet-writer-DOGKBGEk.mjs → stylesheet-writer-BrOw4jMY.mjs} +87 -41
- package/dist/stylesheet-writer-BrOw4jMY.mjs.map +1 -0
- package/dist/{table-BifavDVW.mjs → table-1P8we2gS.mjs} +2 -2
- package/dist/{table-BifavDVW.mjs.map → table-1P8we2gS.mjs.map} +1 -1
- package/dist/{tree-Bbs1C8Rc.mjs → tree-BX-sRRVp.mjs} +3 -1
- package/dist/tree-BX-sRRVp.mjs.map +1 -0
- package/dist/{utf8-D91g1XTG.mjs → utf8-Tn3nzyik.mjs} +2 -2
- package/dist/{utf8-D91g1XTG.mjs.map → utf8-Tn3nzyik.mjs.map} +1 -1
- package/dist/workbook/shared-strings.d.ts +15 -5
- package/dist/workbook/views.d.ts +7 -0
- package/dist/workbook/workbook.d.ts +15 -0
- package/dist/{workbook-T1Y3lstQ.mjs → workbook-CARZS94U.mjs} +39 -14
- package/dist/workbook-CARZS94U.mjs.map +1 -0
- package/dist/workbook.mjs +1 -1
- package/dist/workbook.mjs.map +1 -1
- package/dist/worksheet/hyperlinks.d.ts +5 -0
- package/dist/worksheet/reader.d.ts +7 -2
- package/dist/worksheet/worksheet.d.ts +4 -0
- package/dist/{worksheet-C6CWgvP5.mjs → worksheet-C5Ensf_j.mjs} +167 -135
- package/dist/worksheet-C5Ensf_j.mjs.map +1 -0
- package/dist/worksheet.mjs +2 -2
- package/dist/xml/parser.d.ts +20 -0
- package/dist/xml/serializer.d.ts +8 -0
- package/dist/xml.mjs +4 -4
- package/package.json +1 -1
- package/dist/cell-style-CN9JXOwm.mjs.map +0 -1
- package/dist/load-BA30X4-M.mjs.map +0 -1
- package/dist/parser-nk0Lqmcb.mjs.map +0 -1
- package/dist/save-C9ChJfWM.mjs.map +0 -1
- package/dist/serializer-DQzTTNOS.mjs.map +0 -1
- package/dist/stylesheet-writer-DOGKBGEk.mjs.map +0 -1
- package/dist/tree-Bbs1C8Rc.mjs.map +0 -1
- package/dist/workbook-T1Y3lstQ.mjs.map +0 -1
- package/dist/worksheet-C6CWgvP5.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"save-2moCT3Af.mjs","names":["serializeXmlNode"],"sources":["../src/io/save.ts"],"sourcesContent":["// Public `saveWorkbook` entry point.\n//\n// **Stage 1 minimum**: emits the bare set of parts a Workbook needs to\n// round-trip through `loadWorkbook`:\n//\n// [Content_Types].xml\n// _rels/.rels\n// xl/workbook.xml\n// xl/_rels/workbook.xml.rels\n// xl/worksheets/sheetN.xml ...\n// xl/styles.xml\n// xl/sharedStrings.xml (only when sst is non-empty)\n//\n// docProps / theme / VBA / drawings / charts are reserved for later iterations\n// — load tolerates their absence.\n\nimport { escapeXmlAttr, escapeXmlText } from '../utils/escape';\nimport { chartToBytes } from '../chart/chart-xml';\nimport { chartExToBytes } from '../chart/cx/chartex-xml';\nimport { userShapesToBytes } from '../chart/user-shapes-xml';\nimport { chartsheetToBytes } from '../chartsheet/chartsheet-xml';\nimport type { Drawing, DrawingItem } from '../drawing/drawing';\nimport { drawingToBytes } from '../drawing/drawing-xml';\nimport { IMAGE_FORMAT_EXTENSION, IMAGE_FORMAT_MIME, type XlsxImageFormat } from '../drawing/image';\nimport type { XlsxSink } from '../io/sink';\nimport { OpenXmlIoError, OpenXmlSchemaError } from '../utils/exceptions';\nimport { corePropsToBytes } from '../packaging/core';\nimport { customPropsToBytes } from '../packaging/custom';\nimport { extendedPropsToBytes } from '../packaging/extended';\nimport { addDefault, addOverride, makeManifest, manifestToBytes } from '../packaging/manifest';\nimport { makeRelationships, type Relationships, relsToBytes } from '../packaging/relationships';\nimport { stylesheetToBytes } from '../styles/stylesheet-writer';\nimport { makeSharedStrings, sharedStringsToBytes } from '../workbook/shared-strings';\nimport { type Workbook, validateSheetTitle } from '../workbook/workbook';\nimport type { LegacyComment } from '../worksheet/comments';\nimport { commentsToBytes, placeholderVmlDrawing } from '../worksheet/comments-xml';\nimport type { TableDefinition } from '../worksheet/table';\nimport { tableToBytes } from '../worksheet/table-xml';\nimport { worksheetToBytes } from '../worksheet/writer';\nimport { serializeXml as serializeXmlNode } from '../xml/serializer';\nimport {\n ARC_APP,\n ARC_CONTENT_TYPES,\n ARC_CORE,\n ARC_CUSTOM,\n ARC_ROOT_RELS,\n ARC_SHARED_STRINGS,\n ARC_STYLE,\n ARC_THEME,\n ARC_WORKBOOK,\n ARC_WORKBOOK_RELS,\n CHARTEX_TYPE,\n CPROPS_TYPE,\n parseQName,\n PKG_REL_NS,\n REL_NS,\n SHARED_STRINGS_TYPE,\n SHEET_MAIN_NS,\n STYLES_TYPE,\n THEME_TYPE,\n WORKSHEET_TYPE,\n XLSX_TYPE,\n} from '../xml/namespaces';\nimport { createZipWriter } from '../zip/writer';\n\nconst CORE_PROPS_TYPE = 'application/vnd.openxmlformats-package.core-properties+xml';\nconst EXT_PROPS_TYPE = 'application/vnd.openxmlformats-officedocument.extended-properties+xml';\nconst CORE_PROPS_REL = `${PKG_REL_NS}/metadata/core-properties`;\nconst EXT_PROPS_REL = `${REL_NS}/extended-properties`;\nconst CUSTOM_PROPS_REL = `${REL_NS}/custom-properties`;\nconst THEME_REL = `${REL_NS}/theme`;\nconst TABLE_REL = `${REL_NS}/table`;\nconst TABLE_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml';\nconst COMMENTS_REL = `${REL_NS}/comments`;\nconst VML_DRAWING_REL = `${REL_NS}/vmlDrawing`;\nconst COMMENTS_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml';\nconst VML_DRAWING_TYPE = 'application/vnd.openxmlformats-officedocument.vmlDrawing';\nconst DRAWING_REL = `${REL_NS}/drawing`;\nconst DRAWING_TYPE = 'application/vnd.openxmlformats-officedocument.drawing+xml';\nconst CHART_REL = `${REL_NS}/chart`;\nconst CHARTEX_REL = 'http://schemas.microsoft.com/office/2014/relationships/chartEx';\nconst CHART_TYPE = 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml';\nconst IMAGE_REL = `${REL_NS}/image`;\nconst CHARTSHEET_REL = `${REL_NS}/chartsheet`;\nconst CHARTSHEET_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml';\nconst CHART_USER_SHAPES_REL = `${REL_NS}/chartUserShapes`;\n\nexport interface SaveOptions {\n /** Reserved — passes through to the underlying ZIP writer when implemented. */\n compressionLevel?: number;\n}\n\n/** Convenience: serialise a Workbook to an in-memory `Uint8Array` xlsx.\n *\n * Browser-safe: this path uses an in-memory `Uint8Array` sink and never\n * touches Node's `Buffer` global, so it works unchanged in browser bundles.\n */\nexport async function workbookToBytes(wb: Workbook, opts: SaveOptions = {}): Promise<Uint8Array> {\n const sink = toUint8ArraySink();\n await saveWorkbook(wb, sink, opts);\n return sink.result();\n}\n\nconst toUint8ArraySink = (): XlsxSink & { result(): Uint8Array } => {\n const chunks: Uint8Array[] = [];\n let finalised: Uint8Array | undefined;\n const finalise = (): Uint8Array => {\n if (finalised !== undefined) return finalised;\n let total = 0;\n for (const c of chunks) total += c.byteLength;\n const out = new Uint8Array(total);\n let off = 0;\n for (const c of chunks) {\n out.set(c, off);\n off += c.byteLength;\n }\n finalised = out;\n chunks.length = 0;\n return out;\n };\n return {\n toBytes() {\n return {\n write(chunk: Uint8Array): void {\n if (finalised !== undefined) {\n throw new OpenXmlIoError('workbookToBytes sink: write after finish');\n }\n if (!(chunk instanceof Uint8Array)) {\n throw new OpenXmlIoError('workbookToBytes sink: chunk is not a Uint8Array');\n }\n chunks.push(chunk);\n },\n async finish(): Promise<Uint8Array> {\n return finalise();\n },\n abort(): void {\n if (finalised !== undefined) return;\n finalised = new Uint8Array(0);\n chunks.length = 0;\n },\n };\n },\n result(): Uint8Array {\n return finalise();\n },\n };\n};\n\n/**\n * Validate every sheet title against Excel's character + length rules and\n * confirm titles are unique within the workbook. Catches bad state introduced\n * via direct mutation (`ws.title = ...`) that bypassed the public mutators.\n *\n * Without this gate, a workbook with an invalid sheet name would round-trip to\n * an xlsx Excel rejects with a generic \"file is corrupt\" dialog on open —\n * making it hard to diagnose. Raising at save time keeps the bad state\n * recoverable: callers can `renameSheet` and retry.\n */\nconst validateSheetTitles = (wb: Workbook): void => {\n const seen = new Map<string, number>();\n for (let i = 0; i < wb.sheets.length; i++) {\n const ref = wb.sheets[i];\n if (!ref) continue;\n const title = ref.sheet.title;\n const reason = validateSheetTitle(title);\n if (reason) {\n throw new OpenXmlSchemaError(`saveWorkbook: sheet[${i}] title \"${title}\": ${reason}`);\n }\n const lower = title.toLowerCase();\n const prior = seen.get(lower);\n if (prior !== undefined) {\n throw new OpenXmlSchemaError(\n `saveWorkbook: sheet[${i}] title \"${title}\" collides with sheet[${prior}]` +\n ` (Excel treats sheet names case-insensitively for uniqueness).`,\n );\n }\n seen.set(lower, i);\n }\n};\n\n/** Save a workbook through the given sink. Returns once `finalize()` resolves. */\nexport async function saveWorkbook(wb: Workbook, sink: XlsxSink, _opts: SaveOptions = {}): Promise<void> {\n validateSheetTitles(wb);\n const writer = createZipWriter(sink);\n try {\n await saveWorkbookImpl(wb, writer);\n } catch (err) {\n // Release the sink so streaming destinations (`toFile` / `toWritable`)\n // don't leave a half-written file looking valid. abort() is idempotent so\n // a successful finalize() above is a no-op here.\n writer.abort(err);\n throw err;\n }\n}\n\nasync function saveWorkbookImpl(wb: Workbook, writer: ReturnType<typeof createZipWriter>): Promise<void> {\n // ---- 1. assemble the per-sheet rels + serialise each worksheet ----------\n const sst = makeSharedStrings();\n interface SheetEmit {\n id: string;\n target: string;\n bytes: Uint8Array;\n /** OOXML relationship type used in workbook.xml.rels. */\n relType: string;\n /** ZIP archive path the bytes are written to. */\n archivePath: string;\n /** Override content type for [Content_Types].xml. */\n contentType: string;\n /** Per-sheet rels — populated only if the sheet has hyperlinks / drawings / etc. */\n rels?: Relationships;\n }\n const sheetEmits: SheetEmit[] = [];\n // Workbook-global table counter so xl/tables/tableN.xml ids stay unique.\n const tableEmits: Array<{ id: number; bytes: Uint8Array }> = [];\n let nextTableId = 1;\n // Same counter pattern for comments parts. The comments part and the VML\n // drawing share their N because Excel always emits them paired.\n interface CommentEmit {\n id: number;\n commentsBytes: Uint8Array;\n vmlBytes: Uint8Array;\n }\n const commentEmits: CommentEmit[] = [];\n // Passthrough carries form-control / header-footer VML under the same\n // xl/drawings/vmlDrawingN.vml naming, so start past whatever is already\n // taken — the zip writer rejects duplicate entries.\n let nextCommentsId = nextFreeIndex(wb, /^xl\\/drawings\\/vmlDrawing(\\d+)\\.vml$/);\n // Drawings: workbook-global drawingN counter for xl/drawings/drawingN.xml +\n // matching drawing-rels file (when chart items are present).\n interface DrawingEmit {\n id: number;\n bytes: Uint8Array;\n /** Drawing rels — when non-empty we also emit the rels file. */\n rels?: Relationships;\n }\n const drawingEmits: DrawingEmit[] = [];\n let nextDrawingId = 1;\n // Chart parts share a workbook-global counter so xl/charts/chartN.xml ids\n // stay unique.\n const chartEmits: Array<{\n id: number;\n bytes: Uint8Array;\n isCx: boolean;\n /** Per-chart rels file (only emitted when chart.userShapes is set). */\n rels?: Relationships;\n }> = [];\n let nextChartId = 1;\n // Workbook-global counter for chartDrawing parts. Each chart with userShapes\n // set produces one xl/drawings/chartDrawingN.xml entry.\n const userShapeEmits: Array<{ id: number; bytes: Uint8Array }> = [];\n let nextUserShapesId = 1;\n // Workbook-global counter for image media parts. Excel uses\n // xl/media/imageN.{ext} where N is shared across the package.\n interface ImageEmit {\n id: number;\n /** File extension (without the dot) — also drives the manifest Default. */\n ext: string;\n bytes: Uint8Array;\n }\n const imageEmits: ImageEmit[] = [];\n const imageExts = new Set<string>();\n // Same collision guard as for VML: images referenced by passthrough parts\n // keep their original xl/media/imageN.* names.\n let nextImageId = nextFreeIndex(wb, /^xl\\/media\\/image(\\d+)\\./);\n // Track separate counters so worksheet / chartsheet IDs do not collide (Excel\n // uses xl/worksheets/sheetN.xml and xl/chartsheets/sheetM.xml independently\n // of one another).\n let nextWorksheetId = 1;\n let nextChartsheetId = 1;\n // Pre-claim every original rId so freshly allocated ones (sheets without a\n // captured rId, plus modeled non-sheet rels) avoid collision with captured\n // workbookRelsExtras and the modeled non-sheet original ids.\n const claimedRIds = new Set<string>();\n for (const ref of wb.sheets) {\n if (ref.rId !== undefined) claimedRIds.add(ref.rId);\n }\n if (wb.workbookRelOriginalIds) {\n for (const v of Object.values(wb.workbookRelOriginalIds)) {\n if (typeof v === 'string') claimedRIds.add(v);\n }\n }\n if (wb.workbookRelsExtras) {\n for (const e of wb.workbookRelsExtras) claimedRIds.add(e.id);\n }\n let nextRIdCursor = 1;\n const allocateRId = (): string => {\n let id = `rId${nextRIdCursor}`;\n while (claimedRIds.has(id)) {\n nextRIdCursor++;\n id = `rId${nextRIdCursor}`;\n }\n claimedRIds.add(id);\n nextRIdCursor++;\n return id;\n };\n wb.sheets.forEach((ref, _i) => {\n const isChartsheet = ref.kind === 'chartsheet';\n const target = isChartsheet ? `chartsheets/sheet${nextChartsheetId}.xml` : `worksheets/sheet${nextWorksheetId}.xml`;\n const archivePath = isChartsheet\n ? `xl/chartsheets/sheet${nextChartsheetId}.xml`\n : `xl/worksheets/sheet${nextWorksheetId}.xml`;\n if (isChartsheet) nextChartsheetId++;\n else nextWorksheetId++;\n const sheetRels = makeRelationships();\n // Pre-claim every captured relsExtras rId so freshly allocated modeled rels\n // never clash. The extras themselves are appended to sheetRels at the end\n // so any rId already used inside the worksheet body (drawing / hyperlink\n // refs) keeps pointing at our newly emitted rel.\n const sheetRelsClaimed = new Set<string>();\n const sheetRelsExtras = ref.kind === 'worksheet' ? (ref.sheet.relsExtras ?? []) : [];\n for (const e of sheetRelsExtras) sheetRelsClaimed.add(e.id);\n let sheetRIdCursor = 1;\n const allocateSheetRId = (): string => {\n let id = `rId${sheetRIdCursor}`;\n while (sheetRelsClaimed.has(id) || sheetRels.rels.some((r) => r.id === id)) {\n sheetRIdCursor++;\n id = `rId${sheetRIdCursor}`;\n }\n sheetRIdCursor++;\n return id;\n };\n const registerTable = (table: TableDefinition): { rId: string } => {\n const tableId = nextTableId++;\n const rId = allocateSheetRId();\n sheetRels.rels.push({\n id: rId,\n type: TABLE_REL,\n target: `../tables/table${tableId}.xml`,\n });\n // Emit the table part with its workbook-global id baked in.\n const xmlTable: TableDefinition = { ...table, id: tableId };\n tableEmits.push({ id: tableId, bytes: tableToBytes(xmlTable) });\n return { rId };\n };\n const registerComments = (comments: ReadonlyArray<LegacyComment>): { vmlRelId: string } => {\n const id = nextCommentsId++;\n const commentsRelId = allocateSheetRId();\n sheetRels.rels.push({\n id: commentsRelId,\n type: COMMENTS_REL,\n target: `../comments${id}.xml`,\n });\n const vmlRelId = allocateSheetRId();\n sheetRels.rels.push({\n id: vmlRelId,\n type: VML_DRAWING_REL,\n target: `../drawings/vmlDrawing${id}.vml`,\n });\n commentEmits.push({\n id,\n commentsBytes: commentsToBytes(comments),\n vmlBytes: placeholderVmlDrawing(comments),\n });\n return { vmlRelId };\n };\n const registerDrawing = (drawing: Drawing): { rId: string } => {\n const id = nextDrawingId++;\n const rId = allocateSheetRId();\n sheetRels.rels.push({\n id: rId,\n type: DRAWING_REL,\n target: `../drawings/drawing${id}.xml`,\n });\n // Walk drawing items: for each chart with a payload, allocate a\n // workbook-global chartN id and a per-drawing rId. Emit the chart part +\n // collect a drawing-rels entry so drawing.xml's <c:chart r:id> resolves.\n const drawingRels = makeRelationships();\n // Rels the drawing's verbatim nodes reference keep their original ids —\n // those ids are baked into XML we re-emit untouched — so the allocator\n // below has to step over them.\n const usedDrawingRIds = new Set<string>();\n for (const rel of drawing.rawRels?.rels ?? []) {\n drawingRels.rels.push(rel);\n usedDrawingRIds.add(rel.id);\n }\n let nextDrawingRId = 1;\n const allocateDrawingRId = (): string => {\n let candidate = `rId${nextDrawingRId++}`;\n while (usedDrawingRIds.has(candidate)) candidate = `rId${nextDrawingRId++}`;\n usedDrawingRIds.add(candidate);\n return candidate;\n };\n const itemsForXml: DrawingItem[] = [];\n for (const item of drawing.items) {\n if (item.content.kind === 'chart' && (item.content.chart.space || item.content.chart.cxSpace)) {\n const chartId = nextChartId++;\n const chartRId = allocateDrawingRId();\n // chartex parts use a different relationship Type than the legacy\n // ECMA-376 charts. Excel rejects the workbook when the drawing rels\n // claim a `relationships/chart` target that actually contains a\n // `cx:chartSpace` root.\n const isCxChart = item.content.chart.cxSpace !== undefined;\n drawingRels.rels.push({\n id: chartRId,\n type: isCxChart ? CHARTEX_REL : CHART_REL,\n target: `../charts/chart${chartId}.xml`,\n });\n if (item.content.chart.cxSpace) {\n chartEmits.push({\n id: chartId,\n bytes: chartExToBytes(item.content.chart.cxSpace),\n isCx: true,\n });\n } else if (item.content.chart.space) {\n const space = item.content.chart.space;\n // If the chart carries user shapes, allocate the chartDrawingN part\n // + a per-chart rels file referencing it, then bake the resulting\n // r:id into the chart's <c:userShapes> element.\n let userShapesRId: string | undefined;\n let chartRelsFile: Relationships | undefined;\n if (space.userShapes && space.userShapes.shapes.length > 0) {\n const userShapesId = nextUserShapesId++;\n userShapesRId = 'rId1';\n chartRelsFile = makeRelationships();\n chartRelsFile.rels.push({\n id: userShapesRId,\n type: CHART_USER_SHAPES_REL,\n target: `../drawings/chartDrawing${userShapesId}.xml`,\n });\n userShapeEmits.push({\n id: userShapesId,\n bytes: userShapesToBytes(space.userShapes),\n });\n }\n const chartEmit: typeof chartEmits[number] = {\n id: chartId,\n bytes: chartToBytes(space, userShapesRId !== undefined ? { userShapesRId } : {}),\n isCx: false,\n };\n if (chartRelsFile) chartEmit.rels = chartRelsFile;\n chartEmits.push(chartEmit);\n }\n itemsForXml.push({\n anchor: item.anchor,\n content: {\n kind: 'chart',\n chart: {\n rId: chartRId,\n ...(item.content.chart.cxSpace ? { isCx: true } : {}),\n },\n },\n });\n } else if (item.content.kind === 'picture' && item.content.picture.image) {\n const img = item.content.picture.image;\n const ext = IMAGE_FORMAT_EXTENSION[img.format];\n const imageId = nextImageId++;\n const picRId = allocateDrawingRId();\n drawingRels.rels.push({\n id: picRId,\n type: IMAGE_REL,\n target: `../media/image${imageId}.${ext}`,\n });\n imageEmits.push({ id: imageId, ext, bytes: img.bytes });\n imageExts.add(ext);\n itemsForXml.push({\n anchor: item.anchor,\n content: {\n kind: 'picture',\n picture: {\n rId: picRId,\n ...(item.content.picture.name !== undefined ? { name: item.content.picture.name } : {}),\n ...(item.content.picture.descr !== undefined ? { descr: item.content.picture.descr } : {}),\n ...(item.content.picture.hidden !== undefined ? { hidden: item.content.picture.hidden } : {}),\n ...(item.content.picture.spPr ? { spPr: item.content.picture.spPr } : {}),\n },\n },\n });\n } else {\n itemsForXml.push(item);\n }\n }\n const emit: DrawingEmit = {\n id,\n bytes: drawingToBytes({\n items: itemsForXml,\n ...(drawing.rawChildren ? { rawChildren: drawing.rawChildren } : {}),\n }),\n };\n if (drawingRels.rels.length > 0) emit.rels = drawingRels;\n drawingEmits.push(emit);\n return { rId };\n };\n let bytes: Uint8Array;\n if (ref.kind === 'worksheet') {\n bytes = worksheetToBytes(ref.sheet, {\n sharedStrings: sst,\n date1904: wb.date1904,\n rels: sheetRels,\n registerTable,\n registerComments,\n registerDrawing,\n });\n } else {\n // Chartsheet: register the drawing (if any), then emit the chartsheet\n // part with the resulting r:id baked in.\n let drawingRId: string | undefined;\n if (ref.sheet.drawing) drawingRId = registerDrawing(ref.sheet.drawing).rId;\n bytes = chartsheetToBytes(ref.sheet, drawingRId !== undefined ? { drawingRId } : {});\n }\n // Append captured per-sheet rels passthrough (pivotTable / queryTable /\n // printerSettings / oleObject / customProperty / threadedComment …)\n // verbatim. Their original rIds were pre-claimed so the modeled allocations\n // above never collide with them.\n for (const e of sheetRelsExtras) {\n sheetRels.rels.push({ id: e.id, type: e.type, target: e.target });\n }\n const sheetRId = ref.rId ?? allocateRId();\n const emit: SheetEmit = {\n id: sheetRId,\n target,\n bytes,\n relType: isChartsheet ? CHARTSHEET_REL : `${REL_NS}/worksheet`,\n archivePath,\n contentType: isChartsheet ? CHARTSHEET_TYPE : WORKSHEET_TYPE,\n };\n if (sheetRels.rels.length > 0) emit.rels = sheetRels;\n sheetEmits.push(emit);\n });\n\n // ---- 2. workbook rels -- sheets first, then sst (if any), then styles, then\n // theme / vbaProject, then any captured workbookRelsExtras (e.g.\n // pivotCacheDefinition rels referenced by `<pivotCaches>`). Modeled non-sheet\n // rels prefer the rId captured at load time so any captured extras XML using\n // that Id still resolves after the round-trip.\n const wbRels = makeRelationships();\n wbRels.rels = sheetEmits.map((e) => ({\n id: e.id,\n type: e.relType,\n target: e.target,\n }));\n const orig = wb.workbookRelOriginalIds;\n if (sst.entries.length > 0) {\n wbRels.rels.push({\n id: orig?.sharedStrings ?? allocateRId(),\n type: `${REL_NS}/sharedStrings`,\n target: 'sharedStrings.xml',\n });\n }\n wbRels.rels.push({\n id: orig?.styles ?? allocateRId(),\n type: `${REL_NS}/styles`,\n target: 'styles.xml',\n });\n if (wb.themeXml) {\n wbRels.rels.push({\n id: orig?.theme ?? allocateRId(),\n type: THEME_REL,\n target: 'theme/theme1.xml',\n });\n }\n if (wb.vbaProject) {\n wbRels.rels.push({\n id: orig?.vbaProject ?? allocateRId(),\n type: `${REL_NS}/vbaProject`,\n target: 'vbaProject.bin',\n });\n }\n if (wb.workbookRelsExtras) {\n for (const e of wb.workbookRelsExtras) {\n wbRels.rels.push({ id: e.id, type: e.type, target: e.target });\n }\n }\n\n // ---- 3. workbook.xml ----------------------------------------------------\n const workbookXml = serializeWorkbookXml(\n wb,\n sheetEmits.map((e) => e.id),\n );\n await writer.addEntry(ARC_WORKBOOK, new TextEncoder().encode(workbookXml));\n await writer.addEntry(ARC_WORKBOOK_RELS, relsToBytes(wbRels));\n\n // ---- 4. each worksheet / chartsheet (and its rels file when present) --\n for (const e of sheetEmits) {\n await writer.addEntry(e.archivePath, e.bytes);\n if (e.rels) {\n // The rels file sits alongside its part: `xl/<dir>/_rels/<file>.rels`.\n const slash = e.archivePath.lastIndexOf('/');\n const dir = e.archivePath.slice(0, slash);\n const file = e.archivePath.slice(slash + 1);\n await writer.addEntry(`${dir}/_rels/${file}.rels`, relsToBytes(e.rels));\n }\n }\n\n // ---- 4b. table parts ----------------------------------------------------\n for (const t of tableEmits) {\n await writer.addEntry(`xl/tables/table${t.id}.xml`, t.bytes);\n }\n\n // ---- 4c. comments parts + matching VML drawings ------------------------\n for (const c of commentEmits) {\n await writer.addEntry(`xl/comments${c.id}.xml`, c.commentsBytes);\n await writer.addEntry(`xl/drawings/vmlDrawing${c.id}.vml`, c.vmlBytes);\n }\n\n // ---- 4d. drawings + their rels (when charts are embedded) -------------\n for (const d of drawingEmits) {\n await writer.addEntry(`xl/drawings/drawing${d.id}.xml`, d.bytes);\n if (d.rels) {\n await writer.addEntry(`xl/drawings/_rels/drawing${d.id}.xml.rels`, relsToBytes(d.rels));\n }\n }\n\n // ---- 4e. chart parts (and per-chart rels when user shapes attach) -----\n for (const c of chartEmits) {\n await writer.addEntry(`xl/charts/chart${c.id}.xml`, c.bytes);\n if (c.rels) {\n await writer.addEntry(`xl/charts/_rels/chart${c.id}.xml.rels`, relsToBytes(c.rels));\n }\n }\n\n // ---- 4f. user-shape drawings (xl/drawings/chartDrawingN.xml) ----------\n for (const us of userShapeEmits) {\n await writer.addEntry(`xl/drawings/chartDrawing${us.id}.xml`, us.bytes);\n }\n\n // ---- 4g. embedded images (xl/media/imageN.{ext}) ----------------------\n for (const img of imageEmits) {\n await writer.addEntry(`xl/media/image${img.id}.${img.ext}`, img.bytes);\n }\n\n // ---- 5. styles.xml + sharedStrings.xml (if any) -------------------------\n await writer.addEntry(ARC_STYLE, stylesheetToBytes(wb.styles));\n if (sst.entries.length > 0) {\n await writer.addEntry(ARC_SHARED_STRINGS, sharedStringsToBytes(sst));\n }\n\n // ---- 5b. theme1.xml (passthrough) — only when wb carries one. The theme rel\n // was already added to wbRels above so workbook.xml.rels references it.\n if (wb.themeXml) await writer.addEntry(ARC_THEME, wb.themeXml);\n\n // ---- 5c. docProps/{core,app,custom}.xml (when present) ------------------\n if (wb.properties) await writer.addEntry(ARC_CORE, corePropsToBytes(wb.properties));\n if (wb.appProperties) await writer.addEntry(ARC_APP, extendedPropsToBytes(wb.appProperties));\n if (wb.customProperties) await writer.addEntry(ARC_CUSTOM, customPropsToBytes(wb.customProperties));\n\n // ---- 5d. VBA project + signature (when present) -------------------------\n if (wb.vbaProject) await writer.addEntry('xl/vbaProject.bin', wb.vbaProject);\n if (wb.vbaSignature) await writer.addEntry('xl/vbaProjectSignature.bin', wb.vbaSignature);\n\n // ---- 5e. Pass-through bytes (pivot / activeX / customXml / etc.) -------\n if (wb.passthrough) {\n for (const [path, bytes] of wb.passthrough) await writer.addEntry(path, bytes);\n }\n\n // ---- 6. root rels -------------------------------------------------------\n const rootRels: Relationships = { rels: [] };\n rootRels.rels.push({\n id: 'rId1',\n type: `${REL_NS}/officeDocument`,\n target: 'xl/workbook.xml',\n });\n if (wb.properties) {\n rootRels.rels.push({\n id: `rId${rootRels.rels.length + 1}`,\n type: CORE_PROPS_REL,\n target: 'docProps/core.xml',\n });\n }\n if (wb.appProperties) {\n rootRels.rels.push({\n id: `rId${rootRels.rels.length + 1}`,\n type: EXT_PROPS_REL,\n target: 'docProps/app.xml',\n });\n }\n if (wb.customProperties) {\n rootRels.rels.push({\n id: `rId${rootRels.rels.length + 1}`,\n type: CUSTOM_PROPS_REL,\n target: 'docProps/custom.xml',\n });\n }\n await writer.addEntry(ARC_ROOT_RELS, relsToBytes(rootRels));\n\n // ---- 7. [Content_Types].xml --------------------------------------------\n const manifest = makeManifest();\n addDefault(manifest, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');\n addDefault(manifest, 'xml', 'application/xml');\n // Defaults carried over for passthrough parts go first: addDefault is\n // last-wins, so the writer's own registrations below (vml for comments, bin\n // for VBA, image formats) still take precedence on a shared extension.\n if (wb.passthroughDefaults) {\n for (const [ext, ct] of wb.passthroughDefaults) addDefault(manifest, ext, ct);\n }\n // VBA-bearing workbooks promote the workbook content type to xlsm.\n const workbookContentType = wb.vbaProject\n ? 'application/vnd.ms-excel.sheet.macroEnabled.main+xml'\n : XLSX_TYPE;\n addOverride(manifest, `/${ARC_WORKBOOK}`, workbookContentType);\n for (const e of sheetEmits) {\n addOverride(manifest, `/${e.archivePath}`, e.contentType);\n }\n addOverride(manifest, `/${ARC_STYLE}`, STYLES_TYPE);\n if (sst.entries.length > 0) {\n addOverride(manifest, `/${ARC_SHARED_STRINGS}`, SHARED_STRINGS_TYPE);\n }\n if (wb.themeXml) addOverride(manifest, `/${ARC_THEME}`, THEME_TYPE);\n for (const t of tableEmits) {\n addOverride(manifest, `/xl/tables/table${t.id}.xml`, TABLE_TYPE);\n }\n if (commentEmits.length > 0) {\n addDefault(manifest, 'vml', VML_DRAWING_TYPE);\n }\n for (const c of commentEmits) {\n addOverride(manifest, `/xl/comments${c.id}.xml`, COMMENTS_TYPE);\n }\n for (const d of drawingEmits) {\n addOverride(manifest, `/xl/drawings/drawing${d.id}.xml`, DRAWING_TYPE);\n }\n for (const c of chartEmits) {\n addOverride(manifest, `/xl/charts/chart${c.id}.xml`, c.isCx ? CHARTEX_TYPE : CHART_TYPE);\n }\n for (const us of userShapeEmits) {\n addOverride(manifest, `/xl/drawings/chartDrawing${us.id}.xml`, DRAWING_TYPE);\n }\n if (wb.vbaProject) {\n addDefault(manifest, 'bin', 'application/vnd.ms-office.vbaProject');\n }\n if (wb.passthrough) {\n for (const path of wb.passthrough.keys()) {\n const ct = wb.passthroughContentTypes?.get(path);\n if (ct !== undefined) addOverride(manifest, `/${path}`, ct);\n }\n }\n // Each unique image extension gets a Default entry (`<Default Extension=\"png\"\n // ContentType=\"image/png\"/>`).\n for (const ext of imageExts) {\n const fmt = (Object.entries(IMAGE_FORMAT_EXTENSION).find(([, e]) => e === ext) ?? [])[0] as\n | XlsxImageFormat\n | undefined;\n if (fmt) addDefault(manifest, ext, IMAGE_FORMAT_MIME[fmt]);\n }\n if (wb.properties) addOverride(manifest, `/${ARC_CORE}`, CORE_PROPS_TYPE);\n if (wb.appProperties) addOverride(manifest, `/${ARC_APP}`, EXT_PROPS_TYPE);\n if (wb.customProperties) addOverride(manifest, `/${ARC_CUSTOM}`, CPROPS_TYPE);\n await writer.addEntry(ARC_CONTENT_TYPES, manifestToBytes(manifest));\n\n // ---- 8. close ----------------------------------------------------------\n await writer.finalize();\n}\n\n/**\n * 1 + the highest N among passthrough paths matching `pattern` (whose first\n * capture group is N), so freshly numbered modeled parts never collide with a\n * carried-over one.\n */\nfunction nextFreeIndex(wb: Workbook, pattern: RegExp): number {\n let max = 0;\n if (wb.passthrough) {\n for (const path of wb.passthrough.keys()) {\n const m = pattern.exec(path);\n if (m?.[1] !== undefined) max = Math.max(max, Number.parseInt(m[1], 10));\n }\n }\n return max + 1;\n}\n\n/** Serialise the minimum `<workbook><sheets/></workbook>` Excel needs to load a sheet list. */\nfunction serializeWorkbookXml(wb: Workbook, sheetRIds: ReadonlyArray<string>): string {\n // Excel's markup-compatibility header: the extra prefixes it declares on the\n // root and the `mc:Ignorable` list naming them. Both are re-emitted exactly\n // as written, and the same prefix map is handed to every captured child so\n // `xr:revisionPtr` doesn't come back as `x16:revisionPtr`.\n const rootNs = wb.workbookXmlRoot?.namespaces ?? [];\n const nsDecls = rootNs.map((d) => ` xmlns:${d.prefix}=\"${escapeAttr(d.ns)}\"`).join('');\n const ignorable = wb.workbookXmlRoot?.ignorable;\n const ignorableAttr = ignorable !== undefined ? ` mc:Ignorable=\"${escapeAttr(ignorable)}\"` : '';\n const inScopePrefixes: Record<string, string> = { [SHEET_MAIN_NS]: '', [REL_NS]: 'r' };\n for (const d of rootNs) inScopePrefixes[d.ns] ??= d.prefix;\n const emitExtra = (node: import('../xml/tree').XmlNode): string =>\n serializeChildNode(node, inScopePrefixes);\n\n const parts: string[] = [\n '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>',\n `<workbook xmlns=\"${SHEET_MAIN_NS}\" xmlns:r=\"${REL_NS}\"${nsDecls}${ignorableAttr}>`,\n ];\n if (wb.fileVersion) {\n const fv = serializeFileVersion(wb.fileVersion);\n if (fv) parts.push(fv);\n }\n if (wb.fileSharing) {\n const fs = serializeFileSharing(wb.fileSharing);\n if (fs) parts.push(fs);\n }\n // Emit <workbookPr> from the typed model; fall back to a minimal {date1904:\n // true} synthesis so a fresh workbook (no load history) still round-trips\n // through Excel with the right epoch.\n const effectiveWp = effectiveWorkbookProperties(wb);\n if (effectiveWp) {\n const wp = serializeWorkbookProperties(effectiveWp);\n if (wp) parts.push(wp);\n }\n if (wb.workbookProtection) {\n const wp = serializeWorkbookProtection(wb.workbookProtection);\n if (wp) parts.push(wp);\n }\n // The unmodeled head — `mc:AlternateContent`, `xr:revisionPtr` — sits after\n // `<workbookPr>` in what Excel writes, and CT_Workbook has no slot for it,\n // so it goes here rather than ahead of the modeled elements.\n if (wb.workbookXmlExtras?.beforeSheets) {\n for (const node of wb.workbookXmlExtras.beforeSheets) parts.push(emitExtra(node));\n }\n if (wb.bookViews && wb.bookViews.length > 0) parts.push(serializeBookViews(wb.bookViews, inScopePrefixes));\n parts.push('<sheets>');\n wb.sheets.forEach((ref, i) => {\n const stateAttr = ref.state === 'visible' ? '' : ` state=\"${ref.state}\"`;\n const rId = sheetRIds[i] ?? `rId${i + 1}`;\n parts.push(`<sheet name=\"${escapeAttr(ref.sheet.title)}\" sheetId=\"${ref.sheetId}\"${stateAttr} r:id=\"${rId}\"/>`);\n });\n parts.push('</sheets>');\n // ECMA-376 §18.2.27 makes CT_Workbook an xsd:sequence, so the order below is\n // normative: functionGroups, externalReferences, definedNames, calcPr,\n // oleSize, customWorkbookViews, pivotCaches, smartTagPr, smartTagTypes,\n // webPublishing, fileRecoveryPr, webPublishObjects, extLst. Excel rejects a\n // workbook whose children are out of sequence. `afterSheets` is the\n // unmodeled tail (webPublishing / webPublishObjects / extLst /\n // mc:AlternateContent), so it goes last.\n if (wb.functionGroups) {\n const fg = serializeFunctionGroups(wb.functionGroups);\n if (fg) parts.push(fg);\n }\n if (wb.externalReferences && wb.externalReferences.length > 0) {\n const inner: string[] = ['<externalReferences>'];\n for (const er of wb.externalReferences) {\n inner.push(`<externalReference r:id=\"${escapeAttr(er.rId)}\"/>`);\n }\n inner.push('</externalReferences>');\n parts.push(inner.join(''));\n }\n if (wb.definedNames.length > 0) {\n parts.push('<definedNames>');\n for (const dn of wb.definedNames) {\n let attrs = ` name=\"${escapeAttr(dn.name)}\"`;\n if (dn.scope !== undefined) attrs += ` localSheetId=\"${dn.scope}\"`;\n if (dn.hidden) attrs += ' hidden=\"1\"';\n if (dn.comment !== undefined) attrs += ` comment=\"${escapeAttr(dn.comment)}\"`;\n parts.push(`<definedName${attrs}>${escapeText(dn.value)}</definedName>`);\n }\n parts.push('</definedNames>');\n }\n if (wb.calcProperties) {\n const cp = serializeCalcProperties(wb.calcProperties);\n if (cp) parts.push(cp);\n }\n // No default `<calcPr>` — Excel handles its absence by inserting one on save.\n // Hand-rolled `calcId` values (especially the modern 191029) made Excel\n // reject workbooks whose future-function formulas (LET / LAMBDA / FILTER…)\n // couldn't be evaluated against the declared engine.\n if (wb.oleSize !== undefined) {\n parts.push(`<oleSize ref=\"${escapeAttr(wb.oleSize)}\"/>`);\n }\n if (wb.customWorkbookViews && wb.customWorkbookViews.length > 0) {\n parts.push(serializeCustomWorkbookViews(wb.customWorkbookViews));\n }\n if (wb.pivotCaches && wb.pivotCaches.length > 0) {\n const inner: string[] = ['<pivotCaches>'];\n for (const pc of wb.pivotCaches) {\n inner.push(`<pivotCache cacheId=\"${pc.cacheId}\" r:id=\"${escapeAttr(pc.rId)}\"/>`);\n }\n inner.push('</pivotCaches>');\n parts.push(inner.join(''));\n }\n if (wb.smartTagPr) {\n const stp = serializeSmartTagPr(wb.smartTagPr);\n if (stp) parts.push(stp);\n }\n if (wb.smartTagTypes && wb.smartTagTypes.length > 0) {\n parts.push(serializeSmartTagTypes(wb.smartTagTypes));\n }\n if (wb.fileRecoveryPr) {\n const fp = serializeFileRecoveryPr(wb.fileRecoveryPr);\n if (fp) parts.push(fp);\n }\n if (wb.workbookXmlExtras?.afterSheets) {\n for (const node of wb.workbookXmlExtras.afterSheets) parts.push(emitExtra(node));\n }\n parts.push('</workbook>');\n return parts.join('');\n}\n\nfunction serializeCalcProperties(\n cp: import('../workbook/calc-properties').CalcProperties,\n): string | undefined {\n let attrs = '';\n if (cp.calcId !== undefined) attrs += ` calcId=\"${cp.calcId}\"`;\n if (cp.calcMode !== undefined) attrs += ` calcMode=\"${cp.calcMode}\"`;\n if (cp.fullCalcOnLoad !== undefined) attrs += ` fullCalcOnLoad=\"${cp.fullCalcOnLoad ? '1' : '0'}\"`;\n if (cp.refMode !== undefined) attrs += ` refMode=\"${cp.refMode}\"`;\n if (cp.iterate !== undefined) attrs += ` iterate=\"${cp.iterate ? '1' : '0'}\"`;\n if (cp.iterateCount !== undefined) attrs += ` iterateCount=\"${cp.iterateCount}\"`;\n if (cp.iterateDelta !== undefined) attrs += ` iterateDelta=\"${cp.iterateDelta}\"`;\n if (cp.fullPrecision !== undefined) attrs += ` fullPrecision=\"${cp.fullPrecision ? '1' : '0'}\"`;\n if (cp.calcCompleted !== undefined) attrs += ` calcCompleted=\"${cp.calcCompleted ? '1' : '0'}\"`;\n if (cp.calcOnSave !== undefined) attrs += ` calcOnSave=\"${cp.calcOnSave ? '1' : '0'}\"`;\n if (cp.concurrentCalc !== undefined) attrs += ` concurrentCalc=\"${cp.concurrentCalc ? '1' : '0'}\"`;\n if (cp.concurrentManualCount !== undefined) attrs += ` concurrentManualCount=\"${cp.concurrentManualCount}\"`;\n if (cp.forceFullCalc !== undefined) attrs += ` forceFullCalc=\"${cp.forceFullCalc ? '1' : '0'}\"`;\n if (attrs.length === 0) return undefined;\n return `<calcPr${attrs}/>`;\n}\n\nfunction serializeFunctionGroups(\n fg: import('../workbook/function-groups').FunctionGroups,\n): string | undefined {\n let attrs = '';\n if (fg.builtInGroupCount !== undefined) attrs += ` builtInGroupCount=\"${fg.builtInGroupCount}\"`;\n if (fg.groups.length === 0 && attrs.length === 0) return undefined;\n if (fg.groups.length === 0) return `<functionGroups${attrs}/>`;\n const inner: string[] = [`<functionGroups${attrs}>`];\n for (const g of fg.groups) inner.push(`<functionGroup name=\"${escapeAttr(g.name)}\"/>`);\n inner.push('</functionGroups>');\n return inner.join('');\n}\n\nfunction serializeSmartTagPr(\n stp: import('../workbook/smart-tags').SmartTagProperties,\n): string | undefined {\n let attrs = '';\n if (stp.embed !== undefined) attrs += ` embed=\"${stp.embed ? '1' : '0'}\"`;\n if (stp.show !== undefined) attrs += ` show=\"${stp.show}\"`;\n if (attrs.length === 0) return undefined;\n return `<smartTagPr${attrs}/>`;\n}\n\nfunction serializeSmartTagTypes(\n tags: ReadonlyArray<import('../workbook/smart-tags').SmartTagType>,\n): string {\n const inner: string[] = ['<smartTagTypes>'];\n for (const t of tags) {\n let attrs = '';\n if (t.namespaceUri !== undefined) attrs += ` namespaceUri=\"${escapeAttr(t.namespaceUri)}\"`;\n if (t.name !== undefined) attrs += ` name=\"${escapeAttr(t.name)}\"`;\n if (t.url !== undefined) attrs += ` url=\"${escapeAttr(t.url)}\"`;\n inner.push(`<smartTagType${attrs}/>`);\n }\n inner.push('</smartTagTypes>');\n return inner.join('');\n}\n\nfunction serializeFileRecoveryPr(\n fp: import('../workbook/file-recovery').FileRecoveryProperties,\n): string | undefined {\n let attrs = '';\n if (fp.autoRecover !== undefined) attrs += ` autoRecover=\"${fp.autoRecover ? '1' : '0'}\"`;\n if (fp.crashSave !== undefined) attrs += ` crashSave=\"${fp.crashSave ? '1' : '0'}\"`;\n if (fp.dataExtractLoad !== undefined) attrs += ` dataExtractLoad=\"${fp.dataExtractLoad ? '1' : '0'}\"`;\n if (fp.repairLoad !== undefined) attrs += ` repairLoad=\"${fp.repairLoad ? '1' : '0'}\"`;\n if (attrs.length === 0) return undefined;\n return `<fileRecoveryPr${attrs}/>`;\n}\n\nfunction serializeFileSharing(\n fs: import('../workbook/file-sharing').FileSharing,\n): string | undefined {\n let attrs = '';\n if (fs.readOnlyRecommended !== undefined)\n attrs += ` readOnlyRecommended=\"${fs.readOnlyRecommended ? '1' : '0'}\"`;\n if (fs.userName !== undefined) attrs += ` userName=\"${escapeAttr(fs.userName)}\"`;\n if (fs.reservationPassword !== undefined)\n attrs += ` reservationPassword=\"${escapeAttr(fs.reservationPassword)}\"`;\n if (fs.algorithmName !== undefined) attrs += ` algorithmName=\"${escapeAttr(fs.algorithmName)}\"`;\n if (fs.hashValue !== undefined) attrs += ` hashValue=\"${escapeAttr(fs.hashValue)}\"`;\n if (fs.saltValue !== undefined) attrs += ` saltValue=\"${escapeAttr(fs.saltValue)}\"`;\n if (fs.spinCount !== undefined) attrs += ` spinCount=\"${fs.spinCount}\"`;\n if (attrs.length === 0) return undefined;\n return `<fileSharing${attrs}/>`;\n}\n\nfunction serializeFileVersion(\n fv: import('../workbook/file-version').FileVersion,\n): string | undefined {\n let attrs = '';\n if (fv.appName !== undefined) attrs += ` appName=\"${escapeAttr(fv.appName)}\"`;\n if (fv.lastEdited !== undefined) attrs += ` lastEdited=\"${escapeAttr(fv.lastEdited)}\"`;\n if (fv.lowestEdited !== undefined) attrs += ` lowestEdited=\"${escapeAttr(fv.lowestEdited)}\"`;\n if (fv.rupBuild !== undefined) attrs += ` rupBuild=\"${escapeAttr(fv.rupBuild)}\"`;\n if (fv.codeName !== undefined) attrs += ` codeName=\"${escapeAttr(fv.codeName)}\"`;\n if (attrs.length === 0) return undefined;\n return `<fileVersion${attrs}/>`;\n}\n\nfunction effectiveWorkbookProperties(\n wb: Workbook,\n): import('../workbook/workbook-properties').WorkbookProperties | undefined {\n const explicit = wb.workbookProperties;\n if (explicit) {\n // Mirror the canonical date1904 flag if the typed model omits it.\n if (wb.date1904 && explicit.date1904 === undefined) {\n return { ...explicit, date1904: true };\n }\n return explicit;\n }\n if (wb.date1904) return { date1904: true };\n return undefined;\n}\n\nfunction serializeWorkbookProperties(\n wp: import('../workbook/workbook-properties').WorkbookProperties,\n): string | undefined {\n let attrs = '';\n const boolKeys: ReadonlyArray<keyof import('../workbook/workbook-properties').WorkbookProperties> = [\n 'date1904',\n 'dateCompatibility',\n 'showBorderUnselectedTables',\n 'filterPrivacy',\n 'promptedSolutions',\n 'showInkAnnotation',\n 'backupFile',\n 'saveExternalLinkValues',\n 'hidePivotFieldList',\n 'showPivotChartFilter',\n 'allowRefreshQuery',\n 'publishItems',\n 'checkCompatibility',\n 'autoCompressPictures',\n 'refreshAllConnections',\n ];\n for (const k of boolKeys) {\n const v = wp[k];\n if (v !== undefined) attrs += ` ${k}=\"${v ? '1' : '0'}\"`;\n }\n if (wp.showObjects !== undefined) attrs += ` showObjects=\"${wp.showObjects}\"`;\n if (wp.updateLinks !== undefined) attrs += ` updateLinks=\"${wp.updateLinks}\"`;\n if (wp.codeName !== undefined) attrs += ` codeName=\"${escapeAttr(wp.codeName)}\"`;\n if (wp.defaultThemeVersion !== undefined) attrs += ` defaultThemeVersion=\"${wp.defaultThemeVersion}\"`;\n if (attrs.length === 0) return undefined;\n return `<workbookPr${attrs}/>`;\n}\n\nfunction serializeCustomWorkbookViews(\n views: ReadonlyArray<import('../workbook/views').CustomWorkbookView>,\n): string {\n const parts: string[] = ['<customWorkbookViews>'];\n for (const v of views) {\n let attrs = ` name=\"${escapeAttr(v.name)}\" guid=\"${escapeAttr(v.guid)}\"`;\n if (v.autoUpdate !== undefined) attrs += ` autoUpdate=\"${v.autoUpdate ? '1' : '0'}\"`;\n if (v.mergeInterval !== undefined) attrs += ` mergeInterval=\"${v.mergeInterval}\"`;\n if (v.changesSavedWin !== undefined) attrs += ` changesSavedWin=\"${v.changesSavedWin ? '1' : '0'}\"`;\n if (v.onlySync !== undefined) attrs += ` onlySync=\"${v.onlySync ? '1' : '0'}\"`;\n if (v.personalView !== undefined) attrs += ` personalView=\"${v.personalView ? '1' : '0'}\"`;\n if (v.includePrintSettings !== undefined)\n attrs += ` includePrintSettings=\"${v.includePrintSettings ? '1' : '0'}\"`;\n if (v.includeHiddenRowCol !== undefined)\n attrs += ` includeHiddenRowCol=\"${v.includeHiddenRowCol ? '1' : '0'}\"`;\n if (v.maximized !== undefined) attrs += ` maximized=\"${v.maximized ? '1' : '0'}\"`;\n if (v.minimized !== undefined) attrs += ` minimized=\"${v.minimized ? '1' : '0'}\"`;\n if (v.showHorizontalScroll !== undefined)\n attrs += ` showHorizontalScroll=\"${v.showHorizontalScroll ? '1' : '0'}\"`;\n if (v.showVerticalScroll !== undefined)\n attrs += ` showVerticalScroll=\"${v.showVerticalScroll ? '1' : '0'}\"`;\n if (v.showSheetTabs !== undefined) attrs += ` showSheetTabs=\"${v.showSheetTabs ? '1' : '0'}\"`;\n if (v.xWindow !== undefined) attrs += ` xWindow=\"${v.xWindow}\"`;\n if (v.yWindow !== undefined) attrs += ` yWindow=\"${v.yWindow}\"`;\n attrs += ` windowWidth=\"${v.windowWidth}\" windowHeight=\"${v.windowHeight}\"`;\n if (v.tabRatio !== undefined) attrs += ` tabRatio=\"${v.tabRatio}\"`;\n attrs += ` activeSheetId=\"${v.activeSheetId}\"`;\n if (v.showFormulaBar !== undefined) attrs += ` showFormulaBar=\"${v.showFormulaBar ? '1' : '0'}\"`;\n if (v.showStatusbar !== undefined) attrs += ` showStatusbar=\"${v.showStatusbar ? '1' : '0'}\"`;\n if (v.showComments !== undefined) attrs += ` showComments=\"${v.showComments}\"`;\n if (v.showObjects !== undefined) attrs += ` showObjects=\"${v.showObjects}\"`;\n parts.push(`<customWorkbookView${attrs}/>`);\n }\n parts.push('</customWorkbookViews>');\n return parts.join('');\n}\n\nfunction serializeBookViews(\n views: ReadonlyArray<import('../workbook/views').WorkbookView>,\n prefixOf: Readonly<Record<string, string>>,\n): string {\n const parts: string[] = ['<bookViews>'];\n for (const v of views) {\n let attrs = '';\n if (v.visibility !== undefined) attrs += ` visibility=\"${v.visibility}\"`;\n if (v.minimized !== undefined) attrs += ` minimized=\"${v.minimized ? '1' : '0'}\"`;\n if (v.showHorizontalScroll !== undefined)\n attrs += ` showHorizontalScroll=\"${v.showHorizontalScroll ? '1' : '0'}\"`;\n if (v.showVerticalScroll !== undefined)\n attrs += ` showVerticalScroll=\"${v.showVerticalScroll ? '1' : '0'}\"`;\n if (v.showSheetTabs !== undefined) attrs += ` showSheetTabs=\"${v.showSheetTabs ? '1' : '0'}\"`;\n if (v.xWindow !== undefined) attrs += ` xWindow=\"${v.xWindow}\"`;\n if (v.yWindow !== undefined) attrs += ` yWindow=\"${v.yWindow}\"`;\n if (v.windowWidth !== undefined) attrs += ` windowWidth=\"${v.windowWidth}\"`;\n if (v.windowHeight !== undefined) attrs += ` windowHeight=\"${v.windowHeight}\"`;\n if (v.tabRatio !== undefined) attrs += ` tabRatio=\"${v.tabRatio}\"`;\n if (v.firstSheet !== undefined) attrs += ` firstSheet=\"${v.firstSheet}\"`;\n if (v.activeTab !== undefined) attrs += ` activeTab=\"${v.activeTab}\"`;\n if (v.autoFilterDateGrouping !== undefined)\n attrs += ` autoFilterDateGrouping=\"${v.autoFilterDateGrouping ? '1' : '0'}\"`;\n // Namespaced extras (`xr2:uid`) only make sense with a prefix the root\n // declares; without one the attribute would be unreadable, so skip it.\n for (const [clark, value] of Object.entries(v.extAttrs ?? {})) {\n const { ns, local } = parseQName(clark);\n const prefix = prefixOf[ns];\n if (prefix === undefined) continue;\n attrs += ` ${prefix}:${local}=\"${escapeAttr(value)}\"`;\n }\n parts.push(`<workbookView${attrs}/>`);\n }\n parts.push('</bookViews>');\n return parts.join('');\n}\n\nfunction serializeWorkbookProtection(\n wp: import('../workbook/protection').WorkbookProtection,\n): string | undefined {\n let attrs = '';\n const strAttrs = [\n 'workbookPassword',\n 'workbookPasswordCharacterSet',\n 'workbookAlgorithmName',\n 'workbookHashValue',\n 'workbookSaltValue',\n 'revisionsPassword',\n 'revisionsPasswordCharacterSet',\n 'revisionsAlgorithmName',\n 'revisionsHashValue',\n 'revisionsSaltValue',\n ] as const;\n for (const k of strAttrs) {\n const v = wp[k];\n if (v !== undefined) attrs += ` ${k}=\"${escapeAttr(v)}\"`;\n }\n if (wp.workbookSpinCount !== undefined) attrs += ` workbookSpinCount=\"${wp.workbookSpinCount}\"`;\n if (wp.revisionsSpinCount !== undefined) attrs += ` revisionsSpinCount=\"${wp.revisionsSpinCount}\"`;\n if (wp.lockStructure !== undefined) attrs += ` lockStructure=\"${wp.lockStructure ? '1' : '0'}\"`;\n if (wp.lockWindows !== undefined) attrs += ` lockWindows=\"${wp.lockWindows ? '1' : '0'}\"`;\n if (wp.lockRevision !== undefined) attrs += ` lockRevision=\"${wp.lockRevision ? '1' : '0'}\"`;\n if (attrs.length === 0) return undefined;\n return `<workbookProtection${attrs}/>`;\n}\n\n// Local aliases keep the dense call sites below readable. Canonical\n// implementations live in utils/escape.ts so all three writers (this file,\n// xml/serializer, xml/stream-writer) agree on how `>` / whitespace are\n// handled.\nconst escapeText = escapeXmlText;\nconst escapeAttr = escapeXmlAttr;\n\n/**\n * Serialise an XmlNode child of `<workbook>` for inline injection back into the\n * workbook XML stream. Reuses serializeXml then strips the declaration.\n * Captured nodes carry Clark-notation names, so `inScopePrefixes` is what keeps\n * a child on the prefix the root declared for it instead of a freshly\n * allocated one — which matters when `mc:Ignorable` names those prefixes.\n */\nfunction serializeChildNode(\n node: import('../xml/tree').XmlNode,\n inScopePrefixes?: Readonly<Record<string, string>>,\n): string {\n const bytes = serializeXmlNode(node, {\n xmlDeclaration: false,\n ...(inScopePrefixes ? { inScopePrefixes } : {}),\n });\n return new TextDecoder().decode(bytes);\n}\n"],"mappings":";;;;;;;;;;AAiEA,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;AACvB,MAAM,iBAAiB,GAAG,WAAW;AACrC,MAAM,gBAAgB,GAAG,OAAO;AAChC,MAAM,mBAAmB,GAAG,OAAO;AACnC,MAAM,YAAY,GAAG,OAAO;AAC5B,MAAM,YAAY,GAAG,OAAO;AAC5B,MAAM,aAAa;AACnB,MAAM,eAAe,GAAG,OAAO;AAC/B,MAAM,kBAAkB,GAAG,OAAO;AAClC,MAAM,gBAAgB;AACtB,MAAM,mBAAmB;AACzB,MAAM,cAAc,GAAG,OAAO;AA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,CAAC,OAAO,KAAK;AACvC"}
|
package/dist/schema.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as toTree, r as defineSchema, t as fromTree } from "./serialize-
|
|
1
|
+
import { n as toTree, r as defineSchema, t as fromTree } from "./serialize-BC2Wu3bR.mjs";
|
|
2
2
|
export { defineSchema, fromTree, toTree };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { o as OpenXmlSchemaError } from "./exceptions-D-CFwxgm.mjs";
|
|
2
|
-
import { jt as qname, n as el } from "./tree-
|
|
2
|
+
import { jt as qname, n as el } from "./tree-BX-sRRVp.mjs";
|
|
3
3
|
//#region src/schema/core.ts
|
|
4
4
|
/**
|
|
5
5
|
* Identity helper that pins inference to the supplied `T`. The runtime is just
|
|
@@ -251,4 +251,4 @@ function fromTree(node, schema) {
|
|
|
251
251
|
//#endregion
|
|
252
252
|
export { toTree as n, defineSchema as r, fromTree as t };
|
|
253
253
|
|
|
254
|
-
//# sourceMappingURL=serialize-
|
|
254
|
+
//# sourceMappingURL=serialize-BC2Wu3bR.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize-55EnT30e.mjs","names":[],"sources":["../src/schema/core.ts","../src/schema/serialize.ts"],"sourcesContent":["// Schema layer types — pure data, zero classes.\n//\n// A Schema<T> describes how a plain `T` is mapped to and from an XmlNode. The\n// runtime serialiser (src/schema/serialize.ts) is a single switch-on-kind walk\n// that needs nothing beyond these tables; bundlers can drop schemas a build\n// path never imports because each lives in its own const.\n//\n// Lazy element schemas (`schema: () => SideSchema`) are how circular references\n// — Border has Sides, Side has its own Color, etc. — stay expressible without\n// hoisting hazards at module load.\n\n/** Primitive coercion kinds attribute values may declare. */\nexport type Primitive = 'string' | 'int' | 'float' | 'bool';\n\nexport interface AttrDef {\n kind: Primitive | 'enum';\n /** Allowed values when kind === 'enum'. */\n values?: readonly string[];\n /** Treat the attribute as optional; missing → undefined. */\n optional?: boolean;\n /**\n * Default applied during fromTree when the attribute is absent. Never\n * auto-stripped during toTree — round-trip equivalence is the primary\n * concern; explicit emitters decide when to omit.\n */\n default?: unknown;\n /** Numeric range bounds for kind in {'int', 'float'}. */\n min?: number;\n max?: number;\n /** XML attribute name when it differs from the property key on T. */\n xmlName?: string;\n /** Namespace URI for the XML attribute. Empty / undefined = no namespace. */\n xmlNs?: string;\n}\n\n/**\n * Discriminated union of element shapes a schema can declare. Names are\n * unprefixed (`xmlNs` carries the namespace); the serialiser pairs them into\n * Clark names internally.\n */\nexport type ElementDef =\n | {\n kind: 'text';\n /** Property key on T — the value lands at `T[key]`. */\n key: string;\n /** XML element local name. Defaults to `key` when omitted. */\n name?: string;\n xmlNs?: string;\n primitive: Primitive;\n optional?: boolean;\n default?: unknown;\n /**\n * Fixed attributes always emitted on this element. Keys in Clark\n * notation. On parse, attributes other than these are ignored; keep the\n * schema strict only for the value (text content).\n *\n * Used for the `xsi:type=\"dcterms:W3CDTF\"` marker that docProps/core.xml\n * emits on its <dcterms:created> / <dcterms:modified> children, and\n * similar fixed-marker patterns.\n */\n attrs?: Record<string, string>;\n }\n | {\n kind: 'object';\n key: string;\n name?: string;\n xmlNs?: string;\n // Schema is contravariant in T at the element boundary; named types\n // come back via T anyway. The `any` is intentional — `unknown` would\n // force a cast in every concrete schema definition with no extra safety.\n // oxlint-disable-next-line typescript/no-explicit-any\n schema: () => Schema<any>;\n optional?: boolean;\n }\n | {\n kind: 'sequence';\n key: string;\n /** Local name of the repeated child element. */\n itemName: string;\n itemNs?: string;\n // See ElementDef.kind === 'object' for why `any` survives here.\n // oxlint-disable-next-line typescript/no-explicit-any\n itemSchema: () => Schema<any>;\n /** Optional wrapper element holding the items. */\n container?: { name: string; xmlNs?: string; count?: boolean };\n }\n | {\n /**\n * Empty marker element — `<key/>` whose presence sets `T[key] = true`,\n * absence leaves it `undefined`. Used by Font's `<b/>`, `<i/>`, etc.\n */\n kind: 'empty';\n key: string;\n name?: string;\n xmlNs?: string;\n }\n | {\n /**\n * `<key val=\"value\"/>` style child carrying a single primitive value via\n * an attribute (default `val`). This is openpyxl's NestedString /\n * NestedFloat / NestedInteger / NestedBool / NestedNoneSet pattern; it\n * shows up across Font, NumberFormat children and several chart\n * sub-elements.\n */\n kind: 'nested';\n key: string;\n name?: string;\n xmlNs?: string;\n primitive: Primitive | 'enum';\n /** Allowed values when primitive === 'enum'. */\n values?: readonly string[];\n /** Attribute name carrying the value. Defaults to \"val\". */\n valAttr?: string;\n /** Numeric range bounds (int / float). */\n min?: number;\n max?: number;\n optional?: boolean;\n default?: unknown;\n }\n | {\n /**\n * Opaque round-trip slot — fromTree stores the matched child as a raw\n * `XmlNode`, toTree splices it back verbatim. Used for subtrees we don't\n * want to model in detail (e.g. the vt:vector content under app.xml's\n * HeadingPairs / TitlesOfParts) but still need to preserve byte-for-byte\n * through edits.\n */\n kind: 'raw';\n key: string;\n name?: string;\n xmlNs?: string;\n optional?: boolean;\n };\n\nexport interface Schema<T> {\n /** Local element name. */\n tagname: string;\n /** Namespace URI of this element. */\n xmlNs?: string;\n /** Attribute table keyed by property name on T. */\n attrs: { readonly [K in keyof T & string]?: AttrDef };\n /** Child elements in declaration / emission order. */\n elements: ReadonlyArray<ElementDef>;\n /** Hook to normalise the parsed object (e.g., infer derived fields). */\n postParse?: (value: T, node: import('../xml/tree').XmlNode) => T;\n /** Hook to normalise the value before serialisation. */\n preSerialize?: (value: T) => T;\n}\n\n/**\n * Identity helper that pins inference to the supplied `T`. The runtime is just\n * `s => s`; the value matters only for the call-site type.\n */\nexport function defineSchema<T>(s: Schema<T>): Schema<T> {\n return s;\n}\n","// Schema-driven (de)serialisation. One switch-on-kind walk in each direction;\n// the schema does the work, no class-based descriptors.\n//\n// `toTree` and `fromTree` are pure functions over plain data. They run the same\n// on the schema layer's own types and on user types in the styles / chart /\n// drawing modules.\n\nimport { OpenXmlSchemaError } from '../utils/exceptions';\nimport { qname } from '../xml/namespaces';\nimport { el, type XmlNode } from '../xml/tree';\nimport type { AttrDef, ElementDef, Primitive, Schema } from './core';\n\n// ---- coercion helpers -------------------------------------------------------\n\nconst coerceFromString = (raw: string, kind: Primitive | 'enum', def: AttrDef): unknown => {\n switch (kind) {\n case 'string':\n return raw;\n case 'int': {\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n)) throw new OpenXmlSchemaError(`expected an integer, got \"${raw}\"`);\n if (def.min !== undefined && n < def.min) throw new OpenXmlSchemaError(`int \"${n}\" is below min ${def.min}`);\n if (def.max !== undefined && n > def.max) throw new OpenXmlSchemaError(`int \"${n}\" is above max ${def.max}`);\n return n;\n }\n case 'float': {\n const n = Number.parseFloat(raw);\n if (!Number.isFinite(n)) throw new OpenXmlSchemaError(`expected a number, got \"${raw}\"`);\n if (def.min !== undefined && n < def.min) throw new OpenXmlSchemaError(`float \"${n}\" is below min ${def.min}`);\n if (def.max !== undefined && n > def.max) throw new OpenXmlSchemaError(`float \"${n}\" is above max ${def.max}`);\n return n;\n }\n case 'bool': {\n // openpyxl accepts 'true'/'t'/'1' or 'false'/'f'/'0' (case-insensitive).\n const v = raw.toLowerCase();\n if (v === 'true' || v === 't' || v === '1') return true;\n if (v === 'false' || v === 'f' || v === '0') return false;\n throw new OpenXmlSchemaError(`expected a boolean, got \"${raw}\"`);\n }\n case 'enum': {\n if (!def.values?.includes(raw)) {\n throw new OpenXmlSchemaError(`expected one of [${def.values?.join(', ') ?? ''}], got \"${raw}\"`);\n }\n return raw;\n }\n }\n};\n\nconst coerceToString = (value: unknown, kind: Primitive | 'enum', def: AttrDef): string => {\n switch (kind) {\n case 'string':\n if (typeof value !== 'string') throw new OpenXmlSchemaError(`expected a string, got ${typeof value}`);\n return value;\n case 'int': {\n if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value)) {\n throw new OpenXmlSchemaError(`expected an integer, got ${String(value)}`);\n }\n return String(value);\n }\n case 'float': {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new OpenXmlSchemaError(`expected a number, got ${String(value)}`);\n }\n return String(value);\n }\n case 'bool': {\n if (typeof value !== 'boolean') throw new OpenXmlSchemaError(`expected a boolean, got ${typeof value}`);\n // OOXML convention: '1' / '0'.\n return value ? '1' : '0';\n }\n case 'enum': {\n if (typeof value !== 'string' || !def.values || !def.values.includes(value)) {\n throw new OpenXmlSchemaError(`enum value out of range: ${String(value)}`);\n }\n return value;\n }\n }\n};\n\nconst attrXmlKey = (key: string, def: AttrDef): string => qname(def.xmlNs ?? '', def.xmlName ?? key);\nconst elementXmlName = (def: ElementDef, fallbackNs: string | undefined): string => {\n const local = def.kind === 'sequence' ? (def.container?.name ?? def.itemName) : (def.name ?? def.key);\n const ns = def.kind === 'sequence' ? (def.container?.xmlNs ?? def.itemNs ?? fallbackNs) : (def.xmlNs ?? fallbackNs);\n return qname(ns ?? '', local);\n};\n\n// ---- toTree -----------------------------------------------------------------\n\n/** Serialise a value of `T` into an {@link XmlNode} per the supplied schema. */\nexport function toTree<T>(value: T, schema: Schema<T>): XmlNode {\n const v = (schema.preSerialize ? schema.preSerialize(value) : value) as Record<string, unknown>;\n const node = el(qname(schema.xmlNs ?? '', schema.tagname));\n\n for (const [key, def] of Object.entries(schema.attrs) as Array<[string, AttrDef | undefined]>) {\n if (def === undefined) continue;\n const raw = v[key];\n if (raw === undefined) {\n if (def.optional) continue;\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required attribute \"${key}\" is missing`);\n }\n node.attrs[attrXmlKey(key, def)] = coerceToString(raw, def.kind, def);\n }\n\n for (const def of schema.elements) {\n const raw = v[def.key];\n if (raw === undefined) {\n // 'empty' has no `optional`; presence is binary on the value itself.\n // 'sequence' is implicitly optional (undefined / [] = no items). 'raw'\n // uses the same optional flag as text/object.\n if ((def.kind === 'text' || def.kind === 'object' || def.kind === 'raw') && !def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required element \"${def.key}\" is missing`);\n }\n continue;\n }\n switch (def.kind) {\n case 'text': {\n const text = coerceToString(raw, def.primitive, { kind: def.primitive });\n node.children.push(el(elementXmlName(def, schema.xmlNs), def.attrs ?? {}, [], text));\n break;\n }\n case 'empty': {\n if (raw === true) node.children.push(el(elementXmlName(def, schema.xmlNs), {}, []));\n break;\n }\n case 'nested': {\n const valAttr = def.valAttr ?? 'val';\n const fakeAttrDef: AttrDef = {\n kind: def.primitive,\n ...(def.values !== undefined ? { values: def.values } : {}),\n ...(def.min !== undefined ? { min: def.min } : {}),\n ...(def.max !== undefined ? { max: def.max } : {}),\n };\n const text = coerceToString(raw, def.primitive, fakeAttrDef);\n node.children.push(el(elementXmlName(def, schema.xmlNs), { [valAttr]: text }, []));\n break;\n }\n case 'object': {\n const sub = toTree(raw as Record<string, unknown>, def.schema());\n // Allow the schema to declare its own tagname OR be addressed by the\n // parent under a different element name. We honour the schema's tagname\n // (it's the canonical identity for that type).\n if (def.name !== undefined && sub.name !== qname(def.xmlNs ?? schema.xmlNs ?? '', def.name)) {\n // Re-tag the produced element to the parent-defined name.\n sub.name = qname(def.xmlNs ?? schema.xmlNs ?? '', def.name);\n } else if (def.name === undefined && def.key !== sub.name.replace(/^\\{[^}]*\\}/, '')) {\n // No explicit `name` — use the property key as the local name.\n sub.name = qname(def.xmlNs ?? schema.xmlNs ?? '', def.key);\n }\n node.children.push(sub);\n break;\n }\n case 'raw': {\n // Splice the stored XmlNode back verbatim, but realign its name to the\n // schema-declared position so the output is well-formed even when the\n // caller mutates `name`.\n const sub = raw as XmlNode;\n const expected = elementXmlName(def, schema.xmlNs);\n if (sub.name !== expected) {\n node.children.push({ ...sub, name: expected });\n } else {\n node.children.push(sub);\n }\n break;\n }\n case 'sequence': {\n const items = raw as ReadonlyArray<Record<string, unknown>>;\n const itemSchema = def.itemSchema();\n const built = items.map((item) => {\n const sub = toTree(item, itemSchema);\n // Re-tag to the declared itemName / itemNs.\n sub.name = qname(def.itemNs ?? itemSchema.xmlNs ?? '', def.itemName);\n return sub;\n });\n if (def.container !== undefined) {\n const containerAttrs: Record<string, string> = {};\n if (def.container.count) containerAttrs['count'] = String(items.length);\n node.children.push(el(elementXmlName(def, schema.xmlNs), containerAttrs, built));\n } else {\n for (const c of built) node.children.push(c);\n }\n break;\n }\n }\n }\n\n return node;\n}\n\n// ---- fromTree ---------------------------------------------------------------\n\nconst childByName = (node: XmlNode, fullName: string): XmlNode | undefined => {\n for (const c of node.children) if (c.name === fullName) return c;\n return undefined;\n};\n\nconst childrenByName = (node: XmlNode, fullName: string): XmlNode[] => {\n const out: XmlNode[] = [];\n for (const c of node.children) if (c.name === fullName) out.push(c);\n return out;\n};\n\n/** Materialise a value of `T` from an {@link XmlNode} per the supplied schema. */\nexport function fromTree<T>(node: XmlNode, schema: Schema<T>): T {\n const out: Record<string, unknown> = {};\n const expectedTag = qname(schema.xmlNs ?? '', schema.tagname);\n if (node.name !== expectedTag) {\n throw new OpenXmlSchemaError(`fromTree: expected <${schema.tagname}> (Clark \"${expectedTag}\"), got \"${node.name}\"`);\n }\n\n for (const [key, def] of Object.entries(schema.attrs) as Array<[string, AttrDef | undefined]>) {\n if (def === undefined) continue;\n const xmlKey = attrXmlKey(key, def);\n const raw = node.attrs[xmlKey];\n if (raw === undefined) {\n if (def.default !== undefined) out[key] = def.default;\n else if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required attribute \"${key}\" is missing`);\n }\n continue;\n }\n out[key] = coerceFromString(raw, def.kind, def);\n }\n\n for (const def of schema.elements) {\n const fullName = elementXmlName(def, schema.xmlNs);\n switch (def.kind) {\n case 'text': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (def.default !== undefined) out[def.key] = def.default;\n else if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required text element \"${def.key}\" is missing`);\n }\n break;\n }\n out[def.key] = coerceFromString(child.text ?? '', def.primitive, { kind: def.primitive });\n break;\n }\n case 'empty': {\n const child = childByName(node, fullName);\n if (child !== undefined) out[def.key] = true;\n // Absent → leave key unset so the value compares equal to the\n // round-trip-emitting input that omitted it.\n break;\n }\n case 'nested': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n // The element itself was omitted: leave the field unset for optional\n // nested elements, regardless of `default`. `default` here governs\n // the \"child present but @val omitted\" case (e.g. <u/> meaning\n // underline=single per ECMA-376 §18.4.13), not \"child entirely\n // absent\".\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required nested element \"${def.key}\" is missing`);\n }\n break;\n }\n const valAttr = def.valAttr ?? 'val';\n const raw = child.attrs[valAttr];\n if (raw === undefined) {\n if (def.default !== undefined) {\n out[def.key] = def.default;\n break;\n }\n throw new OpenXmlSchemaError(\n `<${schema.tagname}>: nested element \"${def.key}\" missing attribute \"${valAttr}\"`,\n );\n }\n out[def.key] = coerceFromString(raw, def.primitive, {\n kind: def.primitive,\n ...(def.values !== undefined ? { values: def.values } : {}),\n ...(def.min !== undefined ? { min: def.min } : {}),\n ...(def.max !== undefined ? { max: def.max } : {}),\n });\n break;\n }\n case 'raw': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required raw element \"${def.key}\" is missing`);\n }\n break;\n }\n out[def.key] = child;\n break;\n }\n case 'object': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required element \"${def.key}\" is missing`);\n }\n break;\n }\n const itemSchema = def.schema();\n // Address the child by the parent-declared name even if the child\n // schema names itself differently.\n const renamed: XmlNode =\n child.name === qname(itemSchema.xmlNs ?? '', itemSchema.tagname)\n ? child\n : { ...child, name: qname(itemSchema.xmlNs ?? '', itemSchema.tagname) };\n out[def.key] = fromTree(renamed, itemSchema);\n break;\n }\n case 'sequence': {\n const itemSchema = def.itemSchema();\n const itemFullName = qname(def.itemNs ?? itemSchema.xmlNs ?? '', def.itemName);\n const itemSchemaName = qname(itemSchema.xmlNs ?? '', itemSchema.tagname);\n const containerNode = def.container !== undefined ? childByName(node, fullName) : node;\n const items: unknown[] = [];\n if (containerNode !== undefined) {\n const matches = childrenByName(containerNode, itemFullName);\n for (const m of matches) {\n const renamed: XmlNode = m.name === itemSchemaName ? m : { ...m, name: itemSchemaName };\n items.push(fromTree(renamed, itemSchema));\n }\n }\n out[def.key] = items;\n break;\n }\n }\n }\n\n const built = out as unknown as T;\n return schema.postParse ? schema.postParse(built, node) : built;\n}\n"],"mappings":";;;;;;;AAyJA,SAAgB,aAAgB,GAAyB;CACvD,OAAO;AACT;;;AC7IA,MAAM,oBAAoB,KAAa,MAA0B,QAA0B;CACzF,QAAQ,MAAR;EACE,KAAK,UACH,OAAO;EACT,KAAK,OAAO;GACV,MAAM,IAAI,OAAO,SAAS,KAAK,EAAE;GACjC,IAAI,CAAC,OAAO,SAAS,CAAC,GAAG,MAAM,IAAI,mBAAmB,6BAA6B,IAAI,EAAE;GACzF,IAAI,IAAI,QAAQ,KAAA,KAAa,IAAI,IAAI,KAAK,MAAM,IAAI,mBAAmB,QAAQ,EAAE,iBAAiB,IAAI,KAAK;GAC3G,IAAI,IAAI,QAAQ,KAAA,KAAa,IAAI,IAAI,KAAK,MAAM,IAAI,mBAAmB,QAAQ,EAAE,iBAAiB,IAAI,KAAK;GAC3G,OAAO;EACT;EACA,KAAK,SAAS;GACZ,MAAM,IAAI,OAAO,WAAW,GAAG;GAC/B,IAAI,CAAC,OAAO,SAAS,CAAC,GAAG,MAAM,IAAI,mBAAmB,2BAA2B,IAAI,EAAE;GACvF,IAAI,IAAI,QAAQ,KAAA,KAAa,IAAI,IAAI,KAAK,MAAM,IAAI,mBAAmB,UAAU,EAAE,iBAAiB,IAAI,KAAK;GAC7G,IAAI,IAAI,QAAQ,KAAA,KAAa,IAAI,IAAI,KAAK,MAAM,IAAI,mBAAmB,UAAU,EAAE,iBAAiB,IAAI,KAAK;GAC7G,OAAO;EACT;EACA,KAAK,QAAQ;GAEX,MAAM,IAAI,IAAI,YAAY;GAC1B,IAAI,MAAM,UAAU,MAAM,OAAO,MAAM,KAAK,OAA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AC1C;IACF;IACA,IAAI,IAAI,OAAO;IACf;GACF;EACF;CACF;CAEA,MAAM,QAAQ;CACd,OAAO,OAAO,YAAY,OAAO,UAAU,OAAO,IAAI,IAAI;AAC5D"}
|
|
1
|
+
{"version":3,"file":"serialize-BC2Wu3bR.mjs","names":[],"sources":["../src/schema/core.ts","../src/schema/serialize.ts"],"sourcesContent":["// Schema layer types — pure data, zero classes.\n//\n// A Schema<T> describes how a plain `T` is mapped to and from an XmlNode. The\n// runtime serialiser (src/schema/serialize.ts) is a single switch-on-kind walk\n// that needs nothing beyond these tables; bundlers can drop schemas a build\n// path never imports because each lives in its own const.\n//\n// Lazy element schemas (`schema: () => SideSchema`) are how circular references\n// — Border has Sides, Side has its own Color, etc. — stay expressible without\n// hoisting hazards at module load.\n\n/** Primitive coercion kinds attribute values may declare. */\nexport type Primitive = 'string' | 'int' | 'float' | 'bool';\n\nexport interface AttrDef {\n kind: Primitive | 'enum';\n /** Allowed values when kind === 'enum'. */\n values?: readonly string[];\n /** Treat the attribute as optional; missing → undefined. */\n optional?: boolean;\n /**\n * Default applied during fromTree when the attribute is absent. Never\n * auto-stripped during toTree — round-trip equivalence is the primary\n * concern; explicit emitters decide when to omit.\n */\n default?: unknown;\n /** Numeric range bounds for kind in {'int', 'float'}. */\n min?: number;\n max?: number;\n /** XML attribute name when it differs from the property key on T. */\n xmlName?: string;\n /** Namespace URI for the XML attribute. Empty / undefined = no namespace. */\n xmlNs?: string;\n}\n\n/**\n * Discriminated union of element shapes a schema can declare. Names are\n * unprefixed (`xmlNs` carries the namespace); the serialiser pairs them into\n * Clark names internally.\n */\nexport type ElementDef =\n | {\n kind: 'text';\n /** Property key on T — the value lands at `T[key]`. */\n key: string;\n /** XML element local name. Defaults to `key` when omitted. */\n name?: string;\n xmlNs?: string;\n primitive: Primitive;\n optional?: boolean;\n default?: unknown;\n /**\n * Fixed attributes always emitted on this element. Keys in Clark\n * notation. On parse, attributes other than these are ignored; keep the\n * schema strict only for the value (text content).\n *\n * Used for the `xsi:type=\"dcterms:W3CDTF\"` marker that docProps/core.xml\n * emits on its <dcterms:created> / <dcterms:modified> children, and\n * similar fixed-marker patterns.\n */\n attrs?: Record<string, string>;\n }\n | {\n kind: 'object';\n key: string;\n name?: string;\n xmlNs?: string;\n // Schema is contravariant in T at the element boundary; named types\n // come back via T anyway. The `any` is intentional — `unknown` would\n // force a cast in every concrete schema definition with no extra safety.\n // oxlint-disable-next-line typescript/no-explicit-any\n schema: () => Schema<any>;\n optional?: boolean;\n }\n | {\n kind: 'sequence';\n key: string;\n /** Local name of the repeated child element. */\n itemName: string;\n itemNs?: string;\n // See ElementDef.kind === 'object' for why `any` survives here.\n // oxlint-disable-next-line typescript/no-explicit-any\n itemSchema: () => Schema<any>;\n /** Optional wrapper element holding the items. */\n container?: { name: string; xmlNs?: string; count?: boolean };\n }\n | {\n /**\n * Empty marker element — `<key/>` whose presence sets `T[key] = true`,\n * absence leaves it `undefined`. Used by Font's `<b/>`, `<i/>`, etc.\n */\n kind: 'empty';\n key: string;\n name?: string;\n xmlNs?: string;\n }\n | {\n /**\n * `<key val=\"value\"/>` style child carrying a single primitive value via\n * an attribute (default `val`). This is openpyxl's NestedString /\n * NestedFloat / NestedInteger / NestedBool / NestedNoneSet pattern; it\n * shows up across Font, NumberFormat children and several chart\n * sub-elements.\n */\n kind: 'nested';\n key: string;\n name?: string;\n xmlNs?: string;\n primitive: Primitive | 'enum';\n /** Allowed values when primitive === 'enum'. */\n values?: readonly string[];\n /** Attribute name carrying the value. Defaults to \"val\". */\n valAttr?: string;\n /** Numeric range bounds (int / float). */\n min?: number;\n max?: number;\n optional?: boolean;\n default?: unknown;\n }\n | {\n /**\n * Opaque round-trip slot — fromTree stores the matched child as a raw\n * `XmlNode`, toTree splices it back verbatim. Used for subtrees we don't\n * want to model in detail (e.g. the vt:vector content under app.xml's\n * HeadingPairs / TitlesOfParts) but still need to preserve byte-for-byte\n * through edits.\n */\n kind: 'raw';\n key: string;\n name?: string;\n xmlNs?: string;\n optional?: boolean;\n };\n\nexport interface Schema<T> {\n /** Local element name. */\n tagname: string;\n /** Namespace URI of this element. */\n xmlNs?: string;\n /** Attribute table keyed by property name on T. */\n attrs: { readonly [K in keyof T & string]?: AttrDef };\n /** Child elements in declaration / emission order. */\n elements: ReadonlyArray<ElementDef>;\n /** Hook to normalise the parsed object (e.g., infer derived fields). */\n postParse?: (value: T, node: import('../xml/tree').XmlNode) => T;\n /** Hook to normalise the value before serialisation. */\n preSerialize?: (value: T) => T;\n}\n\n/**\n * Identity helper that pins inference to the supplied `T`. The runtime is just\n * `s => s`; the value matters only for the call-site type.\n */\nexport function defineSchema<T>(s: Schema<T>): Schema<T> {\n return s;\n}\n","// Schema-driven (de)serialisation. One switch-on-kind walk in each direction;\n// the schema does the work, no class-based descriptors.\n//\n// `toTree` and `fromTree` are pure functions over plain data. They run the same\n// on the schema layer's own types and on user types in the styles / chart /\n// drawing modules.\n\nimport { OpenXmlSchemaError } from '../utils/exceptions';\nimport { qname } from '../xml/namespaces';\nimport { el, type XmlNode } from '../xml/tree';\nimport type { AttrDef, ElementDef, Primitive, Schema } from './core';\n\n// ---- coercion helpers -------------------------------------------------------\n\nconst coerceFromString = (raw: string, kind: Primitive | 'enum', def: AttrDef): unknown => {\n switch (kind) {\n case 'string':\n return raw;\n case 'int': {\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n)) throw new OpenXmlSchemaError(`expected an integer, got \"${raw}\"`);\n if (def.min !== undefined && n < def.min) throw new OpenXmlSchemaError(`int \"${n}\" is below min ${def.min}`);\n if (def.max !== undefined && n > def.max) throw new OpenXmlSchemaError(`int \"${n}\" is above max ${def.max}`);\n return n;\n }\n case 'float': {\n const n = Number.parseFloat(raw);\n if (!Number.isFinite(n)) throw new OpenXmlSchemaError(`expected a number, got \"${raw}\"`);\n if (def.min !== undefined && n < def.min) throw new OpenXmlSchemaError(`float \"${n}\" is below min ${def.min}`);\n if (def.max !== undefined && n > def.max) throw new OpenXmlSchemaError(`float \"${n}\" is above max ${def.max}`);\n return n;\n }\n case 'bool': {\n // openpyxl accepts 'true'/'t'/'1' or 'false'/'f'/'0' (case-insensitive).\n const v = raw.toLowerCase();\n if (v === 'true' || v === 't' || v === '1') return true;\n if (v === 'false' || v === 'f' || v === '0') return false;\n throw new OpenXmlSchemaError(`expected a boolean, got \"${raw}\"`);\n }\n case 'enum': {\n if (!def.values?.includes(raw)) {\n throw new OpenXmlSchemaError(`expected one of [${def.values?.join(', ') ?? ''}], got \"${raw}\"`);\n }\n return raw;\n }\n }\n};\n\nconst coerceToString = (value: unknown, kind: Primitive | 'enum', def: AttrDef): string => {\n switch (kind) {\n case 'string':\n if (typeof value !== 'string') throw new OpenXmlSchemaError(`expected a string, got ${typeof value}`);\n return value;\n case 'int': {\n if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value)) {\n throw new OpenXmlSchemaError(`expected an integer, got ${String(value)}`);\n }\n return String(value);\n }\n case 'float': {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new OpenXmlSchemaError(`expected a number, got ${String(value)}`);\n }\n return String(value);\n }\n case 'bool': {\n if (typeof value !== 'boolean') throw new OpenXmlSchemaError(`expected a boolean, got ${typeof value}`);\n // OOXML convention: '1' / '0'.\n return value ? '1' : '0';\n }\n case 'enum': {\n if (typeof value !== 'string' || !def.values || !def.values.includes(value)) {\n throw new OpenXmlSchemaError(`enum value out of range: ${String(value)}`);\n }\n return value;\n }\n }\n};\n\nconst attrXmlKey = (key: string, def: AttrDef): string => qname(def.xmlNs ?? '', def.xmlName ?? key);\nconst elementXmlName = (def: ElementDef, fallbackNs: string | undefined): string => {\n const local = def.kind === 'sequence' ? (def.container?.name ?? def.itemName) : (def.name ?? def.key);\n const ns = def.kind === 'sequence' ? (def.container?.xmlNs ?? def.itemNs ?? fallbackNs) : (def.xmlNs ?? fallbackNs);\n return qname(ns ?? '', local);\n};\n\n// ---- toTree -----------------------------------------------------------------\n\n/** Serialise a value of `T` into an {@link XmlNode} per the supplied schema. */\nexport function toTree<T>(value: T, schema: Schema<T>): XmlNode {\n const v = (schema.preSerialize ? schema.preSerialize(value) : value) as Record<string, unknown>;\n const node = el(qname(schema.xmlNs ?? '', schema.tagname));\n\n for (const [key, def] of Object.entries(schema.attrs) as Array<[string, AttrDef | undefined]>) {\n if (def === undefined) continue;\n const raw = v[key];\n if (raw === undefined) {\n if (def.optional) continue;\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required attribute \"${key}\" is missing`);\n }\n node.attrs[attrXmlKey(key, def)] = coerceToString(raw, def.kind, def);\n }\n\n for (const def of schema.elements) {\n const raw = v[def.key];\n if (raw === undefined) {\n // 'empty' has no `optional`; presence is binary on the value itself.\n // 'sequence' is implicitly optional (undefined / [] = no items). 'raw'\n // uses the same optional flag as text/object.\n if ((def.kind === 'text' || def.kind === 'object' || def.kind === 'raw') && !def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required element \"${def.key}\" is missing`);\n }\n continue;\n }\n switch (def.kind) {\n case 'text': {\n const text = coerceToString(raw, def.primitive, { kind: def.primitive });\n node.children.push(el(elementXmlName(def, schema.xmlNs), def.attrs ?? {}, [], text));\n break;\n }\n case 'empty': {\n if (raw === true) node.children.push(el(elementXmlName(def, schema.xmlNs), {}, []));\n break;\n }\n case 'nested': {\n const valAttr = def.valAttr ?? 'val';\n const fakeAttrDef: AttrDef = {\n kind: def.primitive,\n ...(def.values !== undefined ? { values: def.values } : {}),\n ...(def.min !== undefined ? { min: def.min } : {}),\n ...(def.max !== undefined ? { max: def.max } : {}),\n };\n const text = coerceToString(raw, def.primitive, fakeAttrDef);\n node.children.push(el(elementXmlName(def, schema.xmlNs), { [valAttr]: text }, []));\n break;\n }\n case 'object': {\n const sub = toTree(raw as Record<string, unknown>, def.schema());\n // Allow the schema to declare its own tagname OR be addressed by the\n // parent under a different element name. We honour the schema's tagname\n // (it's the canonical identity for that type).\n if (def.name !== undefined && sub.name !== qname(def.xmlNs ?? schema.xmlNs ?? '', def.name)) {\n // Re-tag the produced element to the parent-defined name.\n sub.name = qname(def.xmlNs ?? schema.xmlNs ?? '', def.name);\n } else if (def.name === undefined && def.key !== sub.name.replace(/^\\{[^}]*\\}/, '')) {\n // No explicit `name` — use the property key as the local name.\n sub.name = qname(def.xmlNs ?? schema.xmlNs ?? '', def.key);\n }\n node.children.push(sub);\n break;\n }\n case 'raw': {\n // Splice the stored XmlNode back verbatim, but realign its name to the\n // schema-declared position so the output is well-formed even when the\n // caller mutates `name`.\n const sub = raw as XmlNode;\n const expected = elementXmlName(def, schema.xmlNs);\n if (sub.name !== expected) {\n node.children.push({ ...sub, name: expected });\n } else {\n node.children.push(sub);\n }\n break;\n }\n case 'sequence': {\n const items = raw as ReadonlyArray<Record<string, unknown>>;\n const itemSchema = def.itemSchema();\n const built = items.map((item) => {\n const sub = toTree(item, itemSchema);\n // Re-tag to the declared itemName / itemNs.\n sub.name = qname(def.itemNs ?? itemSchema.xmlNs ?? '', def.itemName);\n return sub;\n });\n if (def.container !== undefined) {\n const containerAttrs: Record<string, string> = {};\n if (def.container.count) containerAttrs['count'] = String(items.length);\n node.children.push(el(elementXmlName(def, schema.xmlNs), containerAttrs, built));\n } else {\n for (const c of built) node.children.push(c);\n }\n break;\n }\n }\n }\n\n return node;\n}\n\n// ---- fromTree ---------------------------------------------------------------\n\nconst childByName = (node: XmlNode, fullName: string): XmlNode | undefined => {\n for (const c of node.children) if (c.name === fullName) return c;\n return undefined;\n};\n\nconst childrenByName = (node: XmlNode, fullName: string): XmlNode[] => {\n const out: XmlNode[] = [];\n for (const c of node.children) if (c.name === fullName) out.push(c);\n return out;\n};\n\n/** Materialise a value of `T` from an {@link XmlNode} per the supplied schema. */\nexport function fromTree<T>(node: XmlNode, schema: Schema<T>): T {\n const out: Record<string, unknown> = {};\n const expectedTag = qname(schema.xmlNs ?? '', schema.tagname);\n if (node.name !== expectedTag) {\n throw new OpenXmlSchemaError(`fromTree: expected <${schema.tagname}> (Clark \"${expectedTag}\"), got \"${node.name}\"`);\n }\n\n for (const [key, def] of Object.entries(schema.attrs) as Array<[string, AttrDef | undefined]>) {\n if (def === undefined) continue;\n const xmlKey = attrXmlKey(key, def);\n const raw = node.attrs[xmlKey];\n if (raw === undefined) {\n if (def.default !== undefined) out[key] = def.default;\n else if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required attribute \"${key}\" is missing`);\n }\n continue;\n }\n out[key] = coerceFromString(raw, def.kind, def);\n }\n\n for (const def of schema.elements) {\n const fullName = elementXmlName(def, schema.xmlNs);\n switch (def.kind) {\n case 'text': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (def.default !== undefined) out[def.key] = def.default;\n else if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required text element \"${def.key}\" is missing`);\n }\n break;\n }\n out[def.key] = coerceFromString(child.text ?? '', def.primitive, { kind: def.primitive });\n break;\n }\n case 'empty': {\n const child = childByName(node, fullName);\n if (child !== undefined) out[def.key] = true;\n // Absent → leave key unset so the value compares equal to the\n // round-trip-emitting input that omitted it.\n break;\n }\n case 'nested': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n // The element itself was omitted: leave the field unset for optional\n // nested elements, regardless of `default`. `default` here governs\n // the \"child present but @val omitted\" case (e.g. <u/> meaning\n // underline=single per ECMA-376 §18.4.13), not \"child entirely\n // absent\".\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required nested element \"${def.key}\" is missing`);\n }\n break;\n }\n const valAttr = def.valAttr ?? 'val';\n const raw = child.attrs[valAttr];\n if (raw === undefined) {\n if (def.default !== undefined) {\n out[def.key] = def.default;\n break;\n }\n throw new OpenXmlSchemaError(\n `<${schema.tagname}>: nested element \"${def.key}\" missing attribute \"${valAttr}\"`,\n );\n }\n out[def.key] = coerceFromString(raw, def.primitive, {\n kind: def.primitive,\n ...(def.values !== undefined ? { values: def.values } : {}),\n ...(def.min !== undefined ? { min: def.min } : {}),\n ...(def.max !== undefined ? { max: def.max } : {}),\n });\n break;\n }\n case 'raw': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required raw element \"${def.key}\" is missing`);\n }\n break;\n }\n out[def.key] = child;\n break;\n }\n case 'object': {\n const child = childByName(node, fullName);\n if (child === undefined) {\n if (!def.optional) {\n throw new OpenXmlSchemaError(`<${schema.tagname}>: required element \"${def.key}\" is missing`);\n }\n break;\n }\n const itemSchema = def.schema();\n // Address the child by the parent-declared name even if the child\n // schema names itself differently.\n const renamed: XmlNode =\n child.name === qname(itemSchema.xmlNs ?? '', itemSchema.tagname)\n ? child\n : { ...child, name: qname(itemSchema.xmlNs ?? '', itemSchema.tagname) };\n out[def.key] = fromTree(renamed, itemSchema);\n break;\n }\n case 'sequence': {\n const itemSchema = def.itemSchema();\n const itemFullName = qname(def.itemNs ?? itemSchema.xmlNs ?? '', def.itemName);\n const itemSchemaName = qname(itemSchema.xmlNs ?? '', itemSchema.tagname);\n const containerNode = def.container !== undefined ? childByName(node, fullName) : node;\n const items: unknown[] = [];\n if (containerNode !== undefined) {\n const matches = childrenByName(containerNode, itemFullName);\n for (const m of matches) {\n const renamed: XmlNode = m.name === itemSchemaName ? m : { ...m, name: itemSchemaName };\n items.push(fromTree(renamed, itemSchema));\n }\n }\n out[def.key] = items;\n break;\n }\n }\n }\n\n const built = out as unknown as T;\n return schema.postParse ? schema.postParse(built, node) : built;\n}\n"],"mappings":";;;;;;;AAyJA,SAAgB,aAAgB,GAAyB;CACvD,OAAO;AACT;;;AC7IA,MAAM,oBAAoB,KAAa,MAA0B,QAA0B;CACzF,QAAQ,MAAR;EACE,KAAK,UACH,OAAO;EACT,KAAK,OAAO;GACV,MAAM,IAAI,OAAO,SAAS,KAAK,EAAE;GACjC,IAAI,CAAC,OAAO,SAAS,CAAC,GAAG,MAAM,IAAI,mBAAmB,6BAA6B,IAAI,EAAE;GACzF,IAAI,IAAI,QAAQ,KAAA,KAAa,IAA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,MAAM,SAAS,MAAM,WAAW,SAAS,IAAI,WAAW,OAAO,IAC3D,QACA;KAAE,GAAG;KAAO,MAAM,MAAM,WAAW,SAAS,IAAI,WAAW,OAAO;IAAE;IAC1E,IAAI,IAAI,OAAO,SAAS,SAAS,UAAU;IAC3C;GACF;GACA,KAAK,YAAY;IACf,MAAM,aAAa,IAAI,WAAW;IAClC,MAAM,eAAe,MAAM,IAAI,UAAU,WAAW,SAAS,IAAI,IAAI,QAAQ;IAC7E,MAAM,iBAAiB,MAAM,WAAW,SAAS,IAAI,WAAW,OAAO;IACvE,MAAM,gBAAgB,IAAI,cAAc,KAAA,IAAY,YAAY,MAAM,QAAQ,IAAI;IAClF,MAAM,QAAmB,CAAC;IAC1B,IAAI,kBAAkB,KAAA,GAAW;KAC/B,MAAM,UAAU,eAAe,eAAe,YAAY;KAC1D,KAAK,MAAM,KAAK,SAAS;MACvB,MAAM,UAAmB,EAAE,SAAS,iBAAiB,IAAI;OAAE,GAAG;OAAG,MAAM;MAAe;MACtF,MAAM,KAAK,SAAS,SAAS,UAAU,CAAC;KAC1C;IACF;IACA,IAAI,IAAI,OAAO;IACf;GACF;EACF;CACF;CAEA,MAAM,QAAQ;CACd,OAAO,OAAO,YAAY,OAAO,UAAU,OAAO,IAAI,IAAI;AAC5D"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as escapeXmlAttr, r as escapeXmlText } from "./escape-DFTE7ZJc.mjs";
|
|
2
|
-
import { At as parseQName, Dt as XML_NS, V as DEFAULT_PREFIXES } from "./tree-
|
|
2
|
+
import { At as parseQName, Dt as XML_NS, V as DEFAULT_PREFIXES } from "./tree-BX-sRRVp.mjs";
|
|
3
3
|
//#region src/xml/serializer.ts
|
|
4
4
|
/**
|
|
5
5
|
* Serialize an {@link XmlNode} tree into a UTF-8 byte payload. The
|
|
@@ -8,8 +8,8 @@ import { At as parseQName, Dt as XML_NS, V as DEFAULT_PREFIXES } from "./tree-Bb
|
|
|
8
8
|
* (which both directions preserve on Node 18+ / V8).
|
|
9
9
|
*/
|
|
10
10
|
function serializeXml(root, opts = {}) {
|
|
11
|
-
const { xmlDeclaration = true, standalone = "yes" } = opts;
|
|
12
|
-
const allocation = allocatePrefixes(root);
|
|
11
|
+
const { xmlDeclaration = true, standalone = "yes", inScopePrefixes } = opts;
|
|
12
|
+
const allocation = allocatePrefixes(root, inScopePrefixes);
|
|
13
13
|
const out = [];
|
|
14
14
|
if (xmlDeclaration) {
|
|
15
15
|
out.push("<?xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
@@ -19,21 +19,30 @@ function serializeXml(root, opts = {}) {
|
|
|
19
19
|
emit(out, root, allocation, true);
|
|
20
20
|
return new TextEncoder().encode(out.join(""));
|
|
21
21
|
}
|
|
22
|
-
const allocatePrefixes = (root) => {
|
|
22
|
+
const allocatePrefixes = (root, inScope) => {
|
|
23
23
|
const used = /* @__PURE__ */ new Set();
|
|
24
24
|
collectNamespaces(root, used, true);
|
|
25
25
|
used.delete("");
|
|
26
|
+
const prefixOf = /* @__PURE__ */ new Map();
|
|
27
|
+
const inherited = /* @__PURE__ */ new Set();
|
|
28
|
+
for (const [ns, prefix] of Object.entries(inScope ?? {})) {
|
|
29
|
+
if (ns === "" || prefixOf.has(ns)) continue;
|
|
30
|
+
prefixOf.set(ns, prefix);
|
|
31
|
+
inherited.add(ns);
|
|
32
|
+
}
|
|
26
33
|
const rootNs = parseQName(root.name).ns;
|
|
27
34
|
let defaultNs = "";
|
|
28
|
-
if (rootNs && DEFAULT_PREFIXES[rootNs] === ""
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
if (rootNs && DEFAULT_PREFIXES[rootNs] === "" && !prefixOf.has(rootNs)) {
|
|
36
|
+
defaultNs = rootNs;
|
|
37
|
+
prefixOf.set(defaultNs, "");
|
|
38
|
+
}
|
|
31
39
|
prefixOf.set(XML_NS, "xml");
|
|
32
40
|
let auto = 0;
|
|
33
41
|
const usedPrefixes = /* @__PURE__ */ new Set([
|
|
34
42
|
"",
|
|
35
43
|
"xml",
|
|
36
|
-
"xmlns"
|
|
44
|
+
"xmlns",
|
|
45
|
+
...prefixOf.values()
|
|
37
46
|
]);
|
|
38
47
|
const ordered = Array.from(used).sort();
|
|
39
48
|
for (const ns of ordered) {
|
|
@@ -56,9 +65,12 @@ const allocatePrefixes = (root) => {
|
|
|
56
65
|
prefix: "",
|
|
57
66
|
ns: defaultNs
|
|
58
67
|
});
|
|
68
|
+
const inheritedDefault = [...inherited].find((ns) => prefixOf.get(ns) === "");
|
|
59
69
|
for (const ns of ordered) {
|
|
60
70
|
if (ns === defaultNs) continue;
|
|
71
|
+
if (ns === inheritedDefault) continue;
|
|
61
72
|
if (ns === "http://www.w3.org/XML/1998/namespace") continue;
|
|
73
|
+
if (inherited.has(ns)) continue;
|
|
62
74
|
const prefix = prefixOf.get(ns);
|
|
63
75
|
if (prefix === void 0) continue;
|
|
64
76
|
declarations.push({
|
|
@@ -113,4 +125,4 @@ const emit = (out, node, allocation, isRoot) => {
|
|
|
113
125
|
//#endregion
|
|
114
126
|
export { serializeXml as t };
|
|
115
127
|
|
|
116
|
-
//# sourceMappingURL=serializer-
|
|
128
|
+
//# sourceMappingURL=serializer-BtO2TrI7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer-BtO2TrI7.mjs","names":[],"sources":["../src/xml/serializer.ts"],"sourcesContent":["// DOM-style XML serializer. Inverse of src/xml/parser.ts.\n//\n// XmlNode trees use Clark-notation names (`{ns}local`); the serializer\n// rebuilds prefix mappings and emits a single UTF-8 byte payload.\n//\n// Algorithm:\n// 1. walk the tree once, collecting every namespace URI used by\n// elements or attributes;\n// 2. assign each URI a prefix — DEFAULT_PREFIXES first, then\n// auto-generated `ns{N}` — and decide which (if any) becomes the\n// default (xmlns=\"…\") namespace by reusing the root element's NS;\n// 3. emit the XML declaration, root open tag with all xmlns / xmlns:*\n// declarations, then a recursive children walk that escapes text\n// and attribute values.\n//\n// Output style matches openpyxl/Excel: no whitespace between elements,\n// `<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>` on its own\n// line, attribute values quoted with `\"`.\n\nimport { escapeXmlAttr, escapeXmlText } from '../utils/escape';\nimport { DEFAULT_PREFIXES, parseQName, XML_NS } from './namespaces';\nimport type { XmlNode } from './tree';\n\nexport interface SerializeOptions {\n /** Emit `<?xml … ?>` declaration. Defaults to true. */\n xmlDeclaration?: boolean;\n /** `standalone` attribute on the declaration. Defaults to 'yes'. */\n standalone?: 'yes' | 'no' | 'omit';\n /**\n * Namespace URI → prefix already bound by an ancestor, for a fragment being\n * spliced into a larger document. Those bindings are reused and not\n * redeclared on the fragment's own root. `''` means the ancestor's default\n * namespace — attributes never inherit a default binding, so only use it for\n * a namespace whose attributes are unqualified (every SpreadsheetML part).\n */\n inScopePrefixes?: Readonly<Record<string, string>>;\n}\n\n/**\n * Serialize an {@link XmlNode} tree into a UTF-8 byte payload. The\n * inverse of {@link parseXml}: `parseXml(serializeXml(n))` yields a\n * tree structurally equivalent to `n` modulo attribute insertion order\n * (which both directions preserve on Node 18+ / V8).\n */\nexport function serializeXml(root: XmlNode, opts: SerializeOptions = {}): Uint8Array {\n const { xmlDeclaration = true, standalone = 'yes', inScopePrefixes } = opts;\n\n const allocation = allocatePrefixes(root, inScopePrefixes);\n const out: string[] = [];\n\n if (xmlDeclaration) {\n out.push('<?xml version=\"1.0\" encoding=\"UTF-8\"');\n if (standalone !== 'omit') out.push(` standalone=\"${standalone}\"`);\n out.push('?>\\n');\n }\n\n emit(out, root, allocation, /* isRoot */ true);\n\n return new TextEncoder().encode(out.join(''));\n}\n\n// ---- prefix allocation ------------------------------------------------------\n\ninterface Allocation {\n /** ns URI → prefix ('' = default namespace) */\n prefixOf: Map<string, string>;\n /** prefix declarations to emit on the root element, in deterministic order */\n declarations: Array<{ prefix: string; ns: string }>;\n}\n\nconst allocatePrefixes = (root: XmlNode, inScope?: Readonly<Record<string, string>>): Allocation => {\n const used = new Set<string>();\n collectNamespaces(root, used, /* attrsToo */ true);\n used.delete(''); // empty NS = unprefixed names; not emitted as xmlns\n\n const prefixOf = new Map<string, string>();\n // Bindings an ancestor already declared: reuse them, and leave them out of\n // this fragment's own declarations.\n const inherited = new Set<string>();\n for (const [ns, prefix] of Object.entries(inScope ?? {})) {\n if (ns === '' || prefixOf.has(ns)) continue;\n prefixOf.set(ns, prefix);\n inherited.add(ns);\n }\n\n // Decide the default namespace. Prefer the root element's NS if its\n // canonical prefix in DEFAULT_PREFIXES is empty (i.e. designed to live\n // as a default), so the bulk of the root subtree stays unprefixed — unless\n // an ancestor already bound that namespace, in which case its binding wins.\n const rootNs = parseQName(root.name).ns;\n let defaultNs = '';\n if (rootNs && DEFAULT_PREFIXES[rootNs] === '' && !prefixOf.has(rootNs)) {\n defaultNs = rootNs;\n prefixOf.set(defaultNs, '');\n }\n // `xml` and `xmlns` are reserved by the XMLNS spec; xml ↔ XML_NS is\n // predefined and must NOT be redeclared via xmlns:xml=\"…\".\n prefixOf.set(XML_NS, 'xml');\n\n // Pass 1: respect DEFAULT_PREFIXES for namespaces that have a\n // canonical short prefix.\n let auto = 0;\n const usedPrefixes = new Set<string>(['', 'xml', 'xmlns', ...prefixOf.values()]);\n const ordered = Array.from(used).sort();\n for (const ns of ordered) {\n if (prefixOf.has(ns)) continue;\n const preferred = DEFAULT_PREFIXES[ns];\n if (preferred !== undefined && preferred !== '' && !usedPrefixes.has(preferred)) {\n prefixOf.set(ns, preferred);\n usedPrefixes.add(preferred);\n }\n }\n // Pass 2: auto-allocate for the remainder.\n for (const ns of ordered) {\n if (prefixOf.has(ns)) continue;\n let prefix = `ns${auto++}`;\n while (usedPrefixes.has(prefix)) prefix = `ns${auto++}`;\n prefixOf.set(ns, prefix);\n usedPrefixes.add(prefix);\n }\n\n // Emission order: default first, then other prefixes alphabetically by ns\n // URI for determinism. XML_NS is reserved and never declared.\n const declarations: Array<{ prefix: string; ns: string }> = [];\n if (defaultNs !== '') declarations.push({ prefix: '', ns: defaultNs });\n const inheritedDefault = [...inherited].find((ns) => prefixOf.get(ns) === '');\n for (const ns of ordered) {\n if (ns === defaultNs) continue;\n if (ns === inheritedDefault) continue;\n if (ns === XML_NS) continue;\n if (inherited.has(ns)) continue;\n const prefix = prefixOf.get(ns);\n if (prefix === undefined) continue;\n declarations.push({ prefix, ns });\n }\n\n return { prefixOf, declarations };\n};\n\nconst collectNamespaces = (node: XmlNode, into: Set<string>, attrsToo: boolean): void => {\n into.add(parseQName(node.name).ns);\n if (attrsToo) {\n for (const attrName of Object.keys(node.attrs)) {\n into.add(parseQName(attrName).ns);\n }\n }\n for (const c of node.children) collectNamespaces(c, into, attrsToo);\n};\n\n// ---- emission ---------------------------------------------------------------\n\nconst buildElementPrefix = (name: string, prefixOf: Map<string, string>): string => {\n const { ns, local } = parseQName(name);\n if (ns === '') return local;\n const prefix = prefixOf.get(ns);\n if (prefix === undefined || prefix === '') return local;\n return `${prefix}:${local}`;\n};\n\nconst buildAttrPrefix = (name: string, prefixOf: Map<string, string>): string => {\n const { ns, local } = parseQName(name);\n if (ns === '') return local;\n // Attributes never inherit the default namespace; if the namespace is\n // mapped to the default prefix, they still need an explicit prefix.\n const prefix = prefixOf.get(ns);\n if (prefix === undefined || prefix === '') {\n // Attribute lives in a namespace that we marked as default-only;\n // fall through to no prefix (the namespace must already be declared\n // by ancestor walk, but for default-namespace attrs that's invalid\n // XML — we keep it bare anyway since OOXML never produces this).\n return local;\n }\n return `${prefix}:${local}`;\n};\n\nconst emit = (out: string[], node: XmlNode, allocation: Allocation, isRoot: boolean): void => {\n const tag = buildElementPrefix(node.name, allocation.prefixOf);\n out.push('<', tag);\n\n if (isRoot) {\n for (const { prefix, ns } of allocation.declarations) {\n out.push(prefix === '' ? ` xmlns=\"${escapeXmlAttr(ns)}\"` : ` xmlns:${prefix}=\"${escapeXmlAttr(ns)}\"`);\n }\n }\n\n for (const [name, value] of Object.entries(node.attrs)) {\n const attrName = buildAttrPrefix(name, allocation.prefixOf);\n out.push(' ', attrName, '=\"', escapeXmlAttr(value), '\"');\n }\n\n const text = node.text;\n const hasText = text !== undefined && text !== '';\n const hasChildren = node.children.length > 0;\n if (!hasText && !hasChildren) {\n out.push('/>');\n return;\n }\n\n out.push('>');\n if (hasText) out.push(escapeXmlText(text));\n for (const c of node.children) emit(out, c, allocation, /* isRoot */ false);\n out.push('</', tag, '>');\n};\n"],"mappings":";;;;;;;;;AA4CA,SAAgB,aAAa,MAAe,OAAyB,CAAC,GAAe;CACnF,MAAM,EAAE,iBAAiB,MAAM,aAAa,OAAO,oBAAoB;CAEvE,MAAM,aAAa,iBAAiB,MAAM,eAAe;CACzD,MAAM,MAAgB,CAAC;CAEvB,IAAI,gBAAgB;EAClB,IAAI,KAAK,0CAAsC;EAC/C,IAAI,eAAe,QAAQ,IAAI,KAAK,gBAAgB,WAAW,EAAE;EACjE,IAAI,KAAK,MAAM;CACjB;CAEA,KAAK,KAAK,MAAM,YAAyB,IAAI;CAE7C,OAAO,IAAI,YAAY,CAAC,CAAC,OAAO,IAAI,KAAK,EAAE,CAAC;AAC9C;AAWA,MAAM,oBAAoB,MAAe,YAA2D;CAClG,MAAM,uBAAO,IAAI,IAAY;CAC7B,kBAAkB,MAAM,MAAqB,IAAI;CACjD,KAAK,OAAO,EAAE;CAEd,MAAM,2BAAW,IAAI,IAAoB;CAGzC,MAAM,4BAAY,IAAI,IAAY;CAClC,KAAK,MAAM,CAAC,IAAI,WAAW,OAAO,QAAQ,WAAW,CAAC,CAAC,GAAG;EACxD,IAAI,OAAO,MAAM,SAAS,IAAI,EAAE,GAAG;EACnC,SAAS,IAAI,IAAI,MAAM;EACvB,UAAU,IAAI,EAAE;CAClB;CAMA,MAAM,SAAS,WAAW,KAAK,IAAI,CAAC,CAAC;CACrC,IAAI,YAAY;CAChB,IAAI,UAAU,iBAAiB,YAAY,MAAM,CAAC,SAAS,IAAI,MAAM,GAAG;EACtE,YAAY;EACZ,SAAS,IAAI,WAAW,EAAE;CAC5B;CA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AAI,KAAK,IAAI;EACb;CACF;CAEA,IAAI,KAAK,GAAG;CACZ,IAAI,SAAS,IAAI,KAAK,cAAc,IAAI,CAAC;CACzC,KAAK,MAAM,KAAK,KAAK,UAAU,KAAK,KAAK,GAAG,YAAyB,KAAK;CAC1E,IAAI,KAAK,MAAM,KAAK,GAAG;AACzB"}
|
|
@@ -22,6 +22,8 @@ export interface ReadOnlyWorksheet {
|
|
|
22
22
|
export interface ReadOnlyWorkbook {
|
|
23
23
|
sheetNames: string[];
|
|
24
24
|
styles: Stylesheet;
|
|
25
|
+
/** Date1904 mode toggles between Excel's two epoch systems. */
|
|
26
|
+
date1904: boolean;
|
|
25
27
|
openWorksheet(name: string): ReadOnlyWorksheet;
|
|
26
28
|
close(): Promise<void>;
|
|
27
29
|
}
|
package/dist/streaming.mjs
CHANGED
|
@@ -2,21 +2,21 @@ import { i as OpenXmlIoError, o as OpenXmlSchemaError } from "./exceptions-D-CFw
|
|
|
2
2
|
import { s as coordinateToTuple } from "./coordinate-96Ecci4d.mjs";
|
|
3
3
|
import { t as ERROR_CODES } from "./inference-BrI9uIY4.mjs";
|
|
4
4
|
import { n as escapeXmlAttr } from "./escape-DFTE7ZJc.mjs";
|
|
5
|
-
import { _ as ARC_WORKBOOK_RELS, a as findChildren, c as ARC_CONTENT_TYPES, f as ARC_ROOT_RELS, ft as SHEET_MAIN_NS, g as ARC_WORKBOOK, i as findChild, lt as REL_NS, m as ARC_STYLE, p as ARC_SHARED_STRINGS, pt as STYLES_TYPE, ut as SHARED_STRINGS_TYPE, wt as XLSX_TYPE, yt as WORKSHEET_TYPE } from "./tree-
|
|
6
|
-
import {
|
|
7
|
-
import { t as parseXml } from "./parser-
|
|
8
|
-
import { Q as findById, a as makeManifest, n as addOverride, nt as relsFromBytes, o as manifestFromBytes, rt as relsToBytes, s as manifestToBytes, t as addDefault, tt as makeRelationships } from "./manifest-
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { n as
|
|
5
|
+
import { _ as ARC_WORKBOOK_RELS, a as findChildren, c as ARC_CONTENT_TYPES, f as ARC_ROOT_RELS, ft as SHEET_MAIN_NS, g as ARC_WORKBOOK, i as findChild, lt as REL_NS, m as ARC_STYLE, p as ARC_SHARED_STRINGS, pt as STYLES_TYPE, ut as SHARED_STRINGS_TYPE, wt as XLSX_TYPE, yt as WORKSHEET_TYPE } from "./tree-BX-sRRVp.mjs";
|
|
6
|
+
import { t as stylesheetToBytes, v as serializeCell } from "./stylesheet-writer-BrOw4jMY.mjs";
|
|
7
|
+
import { t as parseXml } from "./parser-D3SLaOFe.mjs";
|
|
8
|
+
import { Q as findById, a as makeManifest, n as addOverride, nt as relsFromBytes, o as manifestFromBytes, rt as relsToBytes, s as manifestToBytes, t as addDefault, tt as makeRelationships } from "./manifest-Cm9K7D62.mjs";
|
|
9
|
+
import { at as addNumFmt, et as addBorder, ft as makeStylesheet, it as addFont, nt as addCellXf, ot as defaultCellXf, rt as addFill } from "./cell-style-DuvS_Lsg.mjs";
|
|
10
|
+
import { D as makeSharedStrings, S as validateSheetTitle, j as sharedStringsToBytes, k as parseSharedStringsXml } from "./workbook-CARZS94U.mjs";
|
|
11
|
+
import { i as parseStylesheetXml, n as parseDate1904, r as resolveRelTarget } from "./load-MyOIjtrU.mjs";
|
|
12
12
|
import { t as openZip } from "./reader-BNaUTVCy.mjs";
|
|
13
13
|
import { t as createZipWriter } from "./writer-IyFccCRJ.mjs";
|
|
14
|
-
import { n as iterParse, t as utf8ByteLength } from "./utf8-
|
|
14
|
+
import { n as iterParse, t as utf8ByteLength } from "./utf8-Tn3nzyik.mjs";
|
|
15
15
|
//#region src/streaming/read-only.ts
|
|
16
16
|
const SHEET_TAG = `{${SHEET_MAIN_NS}}sheet`;
|
|
17
17
|
const SHEETS_TAG = `{${SHEET_MAIN_NS}}sheets`;
|
|
18
|
-
const parseSheetList = (
|
|
19
|
-
const sheetsEl = findChild(
|
|
18
|
+
const parseSheetList = (root, workbookPath, archive) => {
|
|
19
|
+
const sheetsEl = findChild(root, SHEETS_TAG);
|
|
20
20
|
if (!sheetsEl) return [];
|
|
21
21
|
const wbRelsPath = relsPathFor(workbookPath);
|
|
22
22
|
const wbRels = archive.has(wbRelsPath) ? relsFromBytes(archive.read(wbRelsPath)) : { rels: [] };
|
|
@@ -309,9 +309,10 @@ const makeStreamingReadOnlyWorksheet = (title, archive, partPath, sst) => {
|
|
|
309
309
|
* sheet list / styles / shared strings. Per the project-wide "no classes" rule
|
|
310
310
|
* (CLAUDE.md), the workbook is a plain object closing over the archive handle.
|
|
311
311
|
*/
|
|
312
|
-
const makeStreamingReadOnlyWorkbook = (sheetNames, styles, archive, entries, sst) => ({
|
|
312
|
+
const makeStreamingReadOnlyWorkbook = (sheetNames, styles, date1904, archive, entries, sst) => ({
|
|
313
313
|
sheetNames,
|
|
314
314
|
styles,
|
|
315
|
+
date1904,
|
|
315
316
|
openWorksheet(name) {
|
|
316
317
|
const entry = entries.get(name);
|
|
317
318
|
if (!entry) throw new OpenXmlSchemaError(`loadWorkbookStream: no worksheet named "${name}"`);
|
|
@@ -331,17 +332,15 @@ async function loadWorkbookStream(source, opts = {}) {
|
|
|
331
332
|
if (!officeDocRel) throw new OpenXmlSchemaError(`loadWorkbookStream: no officeDocument relationship in root rels`);
|
|
332
333
|
const workbookPath = resolveRelTarget("", officeDocRel.target);
|
|
333
334
|
if (!archive.has(workbookPath)) throw new OpenXmlSchemaError(`loadWorkbookStream: workbook part "${workbookPath}" missing`);
|
|
334
|
-
const
|
|
335
|
+
const workbookRoot = parseXml(archive.read(workbookPath));
|
|
336
|
+
const sheetEntries = parseSheetList(workbookRoot, workbookPath, archive);
|
|
335
337
|
const entryMap = /* @__PURE__ */ new Map();
|
|
336
338
|
for (const e of sheetEntries) entryMap.set(e.name, e);
|
|
337
|
-
let sst =
|
|
338
|
-
entries: [],
|
|
339
|
-
index: /* @__PURE__ */ new Map()
|
|
340
|
-
};
|
|
339
|
+
let sst = makeSharedStrings();
|
|
341
340
|
if (archive.has(ARC_SHARED_STRINGS)) sst = parseSharedStringsXml(archive.read(ARC_SHARED_STRINGS));
|
|
342
341
|
let styles = makeStylesheet();
|
|
343
342
|
if (archive.has(ARC_STYLE)) styles = parseStylesheetXml(archive.read(ARC_STYLE));
|
|
344
|
-
return makeStreamingReadOnlyWorkbook(sheetEntries.map((e) => e.name), styles, archive, entryMap, sst.entries.map((e) => typeof e === "string" ? e : e.runs.map((r) => r.text).join("")));
|
|
343
|
+
return makeStreamingReadOnlyWorkbook(sheetEntries.map((e) => e.name), styles, parseDate1904(workbookRoot), archive, entryMap, sst.entries.map((e) => typeof e === "string" ? e : e.runs.map((r) => r.text).join("")));
|
|
345
344
|
}
|
|
346
345
|
//#endregion
|
|
347
346
|
//#region src/streaming/write-only.ts
|