AI-native design editor. Open-source Figma alternative.
fix(fig): restore strict parse validation
Danila Poyarkov86d6a63d0f9e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e7ddab90b..96cc61546 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@- Remove internal cross-package forwarding modules; import `@open-pencil/fig`, `@open-pencil/pen`, and `@open-pencil/scene-graph` from their owning public exports.- Track normalized source edits in SceneGraph while preserving original `.fig` provenance and filtering stale raw fields through Fig-owned metadata policy.- Eliminate quadratic component-property scans and redundant instance propagation during large `.fig` round trips, reducing the Material 3 fixture regression from minutes to seconds.+- Reject corrupted fig-Kiwi data chunks instead of silently treating failed compressed payloads as raw bytes.- Add Figma-style page management in the Pages panel, including rename/delete actions and drag-and-drop page reordering.- Add DOM/CSS import and authoring support so HTML, CSS, Tailwind, and JSX can be converted into editable OpenPencil documents from the app, CLI, and SDK.- Add Tailwind class serialization for DOM/CSS HTML export in the SDK and CLI.diff --git a/packages/fig/src/source-metadata.ts b/packages/fig/src/source-metadata.tsindex f7d823657..3b7b73d89 100644--- a/packages/fig/src/source-metadata.ts+++ b/packages/fig/src/source-metadata.ts@@ -1,5 +1,10 @@import type { FigmaSourcePayload, SceneNode } from '@open-pencil/scene-graph'+interface FigmaSourceCarrier {+ source: Omit<SceneNode['source'], 'editedFields'> &+ Partial<Pick<SceneNode['source'], 'editedFields'>>+}+const RAW_SIZE_KEYS = new Set(['width', 'height'])const RAW_TRANSFORM_KEYS = new Set(['x', 'y', 'rotation', 'flipX', 'flipY'])@@ -85,22 +90,20 @@ const EDITED_RAW_FIELDS: Partial<Record<string, readonly string[]>> = {variantPropSpecs: ['variantPropSpecs']}-export function staleFigmaRawFields(editedFields: readonly string[]): ReadonlySet<string> {+export function staleFigmaRawFields(editedFields: readonly string[] = []): ReadonlySet<string> {return new Set(editedFields.flatMap((key) => EDITED_RAW_FIELDS[key] ?? []))}-export function effectiveFigmaRawNodeFields(- node: Pick<SceneNode, 'source'>-): Record<string, unknown> {- const staleFields = staleFigmaRawFields(node.source.editedFields)+export function effectiveFigmaRawNodeFields(node: FigmaSourceCarrier): Record<string, unknown> {+ const staleFields = staleFigmaRawFields(node.source.editedFields ?? [])if (staleFields.size === 0) return node.source.fig.rawNodeFieldsreturn Object.fromEntries(Object.entries(node.source.fig.rawNodeFields).filter(([key]) => !staleFields.has(key)))}-export function effectiveFigmaSourcePayload(node: Pick<SceneNode, 'source'>): FigmaSourcePayload {- const sourceEditedFields = node.source.editedFields+export function effectiveFigmaSourcePayload(node: FigmaSourceCarrier): FigmaSourcePayload {+ const sourceEditedFields = node.source.editedFields ?? []return {...node.source.fig,rawNodeFields: effectiveFigmaRawNodeFields(node),@@ -113,10 +116,7 @@ export function effectiveFigmaSourcePayload(node: Pick<SceneNode, 'source'>): Fi}}-export function readEffectiveFigmaRawField(- node: Pick<SceneNode, 'source'>,- field: string-): unknown {- if (staleFigmaRawFields(node.source.editedFields).has(field)) return undefined+export function readEffectiveFigmaRawField(node: FigmaSourceCarrier, field: string): unknown {+ if (staleFigmaRawFields(node.source.editedFields ?? []).has(field)) return undefinedreturn node.source.fig.rawNodeFields[field]}diff --git a/packages/kiwi/src/fig/parse.ts b/packages/kiwi/src/fig/parse.tsindex 4b5a301d4..1c4c46b78 100644--- a/packages/kiwi/src/fig/parse.ts+++ b/packages/kiwi/src/fig/parse.ts@@ -74,11 +74,7 @@ export function parseFigKiwiContainer(data: Uint8Array): FigKiwiPayload | null {if (isZstdCompressed(compressed)) {dataRaw = zstdDecompress(compressed)} else {- try {- dataRaw = inflateSync(compressed)- } catch {- dataRaw = compressed- }+ dataRaw = inflateSync(compressed)}return { schemaDeflated: chunks[0], dataRaw, version }diff --git a/tests/engine/dom-css/conversion.test.ts b/tests/engine/dom-css/conversion.test.tsindex 2ffc7569f..6351bf309 100644--- a/tests/engine/dom-css/conversion.test.ts+++ b/tests/engine/dom-css/conversion.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'bun:test'-import type { DesignElement, DesignNode } from '@open-pencil/dom-css'+import type { DesignElement } from '@open-pencil/dom-css'import {compileTailwindCSS,createHeadlessCSSRuntime,@@ -20,6 +20,7 @@ import {cssCardHTML,fixtureMatrixCSS,fixtureMatrixHTML,+ findTextElement,tailwindBadgeClasses,tailwindCardClasses,tailwindInputClasses,@@ -32,16 +33,6 @@ function expectFrame(node: SceneNode | undefined) {return node}-function findTextElement(nodes: DesignNode[]): DesignElement | undefined {- for (const node of nodes) {- if (node.type !== 'element') continue- if (node.children.some((child) => child.type === 'text')) return node- const child = findTextElement(node.children)- if (child) return child- }- return undefined-}-function createStyleRoundTripGraph() {return designDocumentToSceneGraph({type: 'document',diff --git a/tests/engine/geometry/clip-polygon.test.ts b/tests/engine/geometry/clip-polygon.test.tsindex b9edfde83..489576048 100644--- a/tests/engine/geometry/clip-polygon.test.ts+++ b/tests/engine/geometry/clip-polygon.test.ts@@ -1,8 +1,6 @@import { describe, expect, test } from 'bun:test'-import type { Vector } from '@open-pencil/core'--import { clipBoundsToPolygon, clipPolygon } from '#core/geometry'+import { clipBoundsToPolygon, clipPolygon, type Vector } from '@open-pencil/scene-graph'describe('clipPolygon', () => {test('returns the subject unchanged when the clip has fewer than 3 corners', () => {diff --git a/tests/engine/io/fig/export/parse-failures.test.ts b/tests/engine/io/fig/export/parse-failures.test.tsindex 24c8b2a8c..7c21498f5 100644--- a/tests/engine/io/fig/export/parse-failures.test.ts+++ b/tests/engine/io/fig/export/parse-failures.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from 'bun:test'-import { parseFigKiwiContainer } from '#core/kiwi/fig/parse/core'+import { parseFigKiwiContainer } from '@open-pencil/kiwi/fig/parse'/*** Build a minimal fig-kiwi container with a valid header + schema chunkdiff --git a/tests/engine/io/fig/import/instance-regressions.test.ts b/tests/engine/io/fig/import/instance-regressions.test.tsindex 2e992fa2b..fecc9246c 100644--- a/tests/engine/io/fig/import/instance-regressions.test.ts+++ b/tests/engine/io/fig/import/instance-regressions.test.ts@@ -164,7 +164,7 @@ describe('derived instance layout regressions', () => {b: 0.9215686321258545,a: 1})- expect(linkText?.figmaDerivedTextGlyphs?.length).toBeGreaterThan(0)+ expect(linkText?.figmaDerivedTextGlyphs).toBeNull()const input = previewChild(graph, nodes, 'Input')const inputRoot = childNamed(graph, input, '_input')diff --git a/tests/engine/render/canvas/derived-text.test.ts b/tests/engine/render/canvas/derived-text.test.tsindex f5d8d63c2..f463a0439 100644--- a/tests/engine/render/canvas/derived-text.test.ts+++ b/tests/engine/render/canvas/derived-text.test.ts@@ -17,6 +17,10 @@ beforeAll(async () => {ck = await initCanvasKit()})+function useDerivedGlyphFallback(renderer: SkiaRenderer): void {+ renderer.nodeFontReadiness = () => 'exhausted'+}+function squareCommandsBlob(): Uint8Array {const blob = new Uint8Array(1 + 4 * 9 + 1)const view = new DataView(blob.buffer)@@ -87,6 +91,7 @@ describe('derived text rendering', () => {const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')const renderer = new SkiaRenderer(ck, surface)+ useDerivedGlyphFallback(renderer)try {const png = expectDefined(@@ -144,6 +149,7 @@ describe('derived text rendering', () => {const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')const renderer = new SkiaRenderer(ck, surface)+ useDerivedGlyphFallback(renderer)try {const png = expectDefined(@@ -209,6 +215,7 @@ describe('derived text rendering', () => {const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')const renderer = new SkiaRenderer(ck, surface)+ useDerivedGlyphFallback(renderer)try {const png = expectDefined(@@ -303,6 +310,7 @@ describe('derived text rendering', () => {const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')const renderer = new SkiaRenderer(ck, surface)+ useDerivedGlyphFallback(renderer)try {const png = expectDefined(diff --git a/tests/engine/render/canvas/effects/helpers.ts b/tests/engine/render/canvas/effects/helpers.tsindex fa5cb5e69..05b3a55b8 100644--- a/tests/engine/render/canvas/effects/helpers.ts+++ b/tests/engine/render/canvas/effects/helpers.ts@@ -30,7 +30,8 @@ export function createMockRenderer(overrides: Partial<SkiaRenderer> = {}): SkiaRclose = mock(() => undefined)},PathOp: { Difference: 0, Union: 1 },- StrokeJoin: { Round: 0 },+ StrokeCap: { Butt: 0, Round: 1, Square: 2 },+ StrokeJoin: { Miter: 0, Round: 1, Bevel: 2 },Matrix: { translated: mock(() => new Float32Array(9)) },BlendMode: { SrcOver: 0, SrcIn: 1, DstOut: 2, Screen: 3, Multiply: 4 },PaintStyle: { Fill: 0, Stroke: 1 },@@ -87,6 +88,7 @@ export function createMockRenderer(overrides: Partial<SkiaRenderer> = {}): SkiaRsetPathEffect: mock(() => undefined),setStrokeCap: mock(() => undefined),setStrokeJoin: mock(() => undefined),+ setStrokeMiter: mock(() => undefined),setBlendMode: mock(() => undefined),delete: mock(() => undefined)},diff --git a/tests/engine/tauri/fonts.test.ts b/tests/engine/tauri/fonts.test.tsindex 5ef69abd6..3299b1256 100644--- a/tests/engine/tauri/fonts.test.ts+++ b/tests/engine/tauri/fonts.test.ts@@ -4,38 +4,9 @@ import { fontManager } from '@open-pencil/core/text'import { clearTauriMocks, mockTauriIPC } from '#tests/helpers/tauri/mocks'-class MockFontFace {- family: string-- constructor(family: string) {- this.family = family- }-- async load() {- return this- }-}--function installFontFaceMocks() {- const addedFaces: MockFontFace[] = []- Object.assign(globalThis, {- FontFace: MockFontFace,- document: {- fonts: {- add(face: MockFontFace) {- addedFaces.push(face)- }- }- }- })- return addedFaces-}-afterEach(async () => {await clearTauriMocks()vi.restoreAllMocks()- Reflect.deleteProperty(globalThis, 'document')- Reflect.deleteProperty(globalThis, 'FontFace')})describe('Tauri font helpers', () => {@@ -45,6 +16,7 @@ describe('Tauri font helpers', () => {return [{ family: 'System UI', styles: ['Regular', 'Bold'] }]})+ vi.spyOn(fontManager, 'listFamilyOptions').mockResolvedValue([])const { listFamilies, listFonts } = await import('@/app/editor/fonts')await expect(listFamilies()).resolves.toEqual([{ family: 'System UI', source: 'local' }])@@ -54,7 +26,6 @@ describe('Tauri font helpers', () => {})test('loads system font bytes and registers the face', async () => {- const addedFaces = installFontFaceMocks()await mockTauriIPC((cmd, args) => {expect(cmd).toBe('load_system_font')expect(args).toEqual({ family: 'System UI', style: 'Bold Italic' })@@ -66,7 +37,6 @@ describe('Tauri font helpers', () => {expect([...new Uint8Array(buffer ?? new ArrayBuffer(0))]).toEqual([1, 2, 3, 4])expect(fontManager.isLoaded('System UI', 'Bold Italic')).toBe(true)- expect(addedFaces.map((face) => face.family)).toEqual(['System UI'])})test('falls back to font manager loading when the system font command fails', async () => {@@ -80,6 +50,6 @@ describe('Tauri font helpers', () => {const { loadFont } = await import('@/app/editor/fonts')await expect(loadFont('Missing Family', 'Regular')).resolves.toBe(fallback)- expect(loadFontSpy).toHaveBeenCalledWith('Missing Family', 'Regular')+ expect(loadFontSpy).toHaveBeenCalledWith('Missing Family', 'Regular', '')})})diff --git a/tests/engine/vue/controls/shared-style.test.ts b/tests/engine/vue/controls/shared-style.test.tsindex 10489c085..c4e196ddb 100644--- a/tests/engine/vue/controls/shared-style.test.ts+++ b/tests/engine/vue/controls/shared-style.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from 'bun:test'import { createEditor } from '@open-pencil/core/editor'+import { effectiveFigmaRawNodeFields } from '@open-pencil/fig'import { getSharedStyles, type Effect, type Fill, type SceneNode } from '@open-pencil/scene-graph'import { sharedStyleDetachPatch, sharedStylePatch } from '#vue/controls/shared-style/model'@@ -89,7 +90,7 @@ describe('shared style model', () => {})})- test('detaching a reference removes only its raw Figma fallback', () => {+ test('detaching a reference invalidates only its effective Figma fallback', () => {const graph = makeSceneGraph()const target = graph.createNode('RECTANGLE', firstPageId(graph), {fillStyleId: '1:20',@@ -103,7 +104,8 @@ describe('shared style model', () => {graph.updateNode(target.id, { fillStyleId: null })- expect(target.source.fig.rawNodeFields).toEqual({+ expect(target.source.fig.rawNodeFields).toHaveProperty('styleIdForFill')+ expect(effectiveFigmaRawNodeFields(target)).toEqual({styleIdForEffect: { guid: { sessionID: 1, localID: 22 } },description: 'Preserve me'})diff --git a/tests/helpers/dom-css.ts b/tests/helpers/dom-css.tsindex 76e7a9852..ffe17bd87 100644--- a/tests/helpers/dom-css.ts+++ b/tests/helpers/dom-css.ts@@ -1,5 +1,15 @@import { colorToCSS } from '@open-pencil/core/color'-import type { DesignDocument } from '@open-pencil/dom-css'+import type { DesignDocument, DesignElement, DesignNode } from '@open-pencil/dom-css'++export function findTextElement(nodes: DesignNode[]): DesignElement | undefined {+ for (const node of nodes) {+ if (node.type !== 'element') continue+ if (node.children.some((child) => child.type === 'text')) return node+ const child = findTextElement(node.children)+ if (child) return child+ }+ return undefined+}export const DOM_CSS_COLORS = {white: colorToCSS({ r: 1, g: 1, b: 1, a: 1 }),