-
Notifications
You must be signed in to change notification settings - Fork 0
feat(configurator): unified validated import + mobile preview dropdown (UX redesign, phase 7) #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e47298
feat(configurator): unified validated import + mobile preview templat…
kiro-agent 48c6546
fix(configurator): preserve editor intent and undo steps
kiro-agent 3e746d4
fix(configurator): clear stale search focus
kiro-agent b482679
fix(configurator): report validated import outcomes
kiro-agent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /** | ||
| * @license | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Unified override import. | ||
| * | ||
| * The header import used to have two silent, inconsistent paths: a `.json` file | ||
| * REPLACED the whole state with no validation, while a `.css` file MERGED via a | ||
| * loose regex — and a file with nothing recognisable just did nothing, with no | ||
| * feedback. This parses either format through ONE pipeline: | ||
| * | ||
| * 1. detect JSON (flat map or a theme-file `{ tokens }`) vs CSS | ||
| * 2. sanitise every value (codec.sanitizeValue) and drop keys that aren't a | ||
| * real `--sf-*` name or whose value is structurally unsafe (would be | ||
| * dropped on export anyway) | ||
| * 3. migrate renamed/removed tokens and flag unknown ones (themeFile) | ||
| * 4. return the cleaned overrides plus a report the caller can surface | ||
| * | ||
| * The caller decides merge vs replace; the default header flow merges, which is | ||
| * the predictable, non-destructive choice for both formats. | ||
| */ | ||
| import { parseCSS, sanitizeValue } from "./codec"; | ||
| import { migrateOverrides } from "./themeFile"; | ||
| import { isStructurallySafe } from "./tokenModel"; | ||
|
|
||
| export interface ImportReport { | ||
| format: "json" | "css"; | ||
| /** Tokens accepted into the result. */ | ||
| accepted: number; | ||
| /** Old names migrated to their current name. */ | ||
| renamed: number; | ||
| /** Tokens dropped because the framework removed them. */ | ||
| removed: number; | ||
| /** Accepted tokens that aren't part of this framework build. */ | ||
| unknown: number; | ||
| /** Keys rejected for a bad name or an unsafe/empty value. */ | ||
| invalid: string[]; | ||
| /** Keys dropped because multiple legacy names map to one current token. */ | ||
| collisions: number; | ||
| /** True when the file couldn't be parsed into any tokens. */ | ||
| malformed: boolean; | ||
| } | ||
|
|
||
| export interface ImportResult { | ||
| overrides: Record<string, string>; | ||
| report: ImportReport; | ||
| } | ||
|
|
||
| const KEY_RE = /^--sf-[\w-]+$/; | ||
|
|
||
| function looksLikeJson(text: string): boolean { | ||
| return text.trim().startsWith("{"); | ||
| } | ||
|
|
||
| /** Extract a raw name→value map from JSON (flat, or a theme-file wrapper). */ | ||
| function readJsonMap(text: string): { map: Record<string, unknown>; malformed: boolean } { | ||
| try { | ||
| const data = JSON.parse(text); | ||
| if (!data || typeof data !== "object" || Array.isArray(data)) return { map: {}, malformed: true }; | ||
| const src = | ||
| "overrides" in data && data.overrides && typeof data.overrides === "object" && !Array.isArray(data.overrides) | ||
| ? (data.overrides as Record<string, unknown>) | ||
| : "tokens" in data && data.tokens && typeof data.tokens === "object" && !Array.isArray(data.tokens) | ||
| ? (data.tokens as Record<string, unknown>) | ||
| : (data as Record<string, unknown>); | ||
| return { map: src, malformed: false }; | ||
| } catch { | ||
| return { map: {}, malformed: true }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parse and validate an imported CSS or JSON override file. | ||
| * | ||
| * @param text the raw file contents | ||
| * @param filename used (with a content sniff) to pick the JSON vs CSS path | ||
| * @param liveTokens the set of token names in the current framework build | ||
| */ | ||
| export function parseImport( | ||
| text: string, | ||
| filename: string, | ||
| liveTokens: Set<string>, | ||
| ): ImportResult { | ||
| const isJson = /\.json$/i.test(filename) || looksLikeJson(text); | ||
| const { map: rawMap, malformed } = isJson | ||
| ? readJsonMap(text) | ||
| : { map: parseCSS(text), malformed: false }; | ||
|
|
||
| const cleaned: Record<string, string> = {}; | ||
| const invalid: string[] = []; | ||
| for (const [key, value] of Object.entries(rawMap)) { | ||
| if (!KEY_RE.test(key) || typeof value !== "string") { invalid.push(key); continue; } | ||
| // Preserve normal whitespace (sanitizeValue will collapse it) but reject | ||
| // non-printing control characters that theme files and share links reject. | ||
| if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(value)) { invalid.push(key); continue; } | ||
| const safe = sanitizeValue(value); | ||
| if (!isStructurallySafe(safe)) { invalid.push(key); continue; } | ||
| cleaned[key] = safe; | ||
| } | ||
|
|
||
| const migrated = migrateOverrides(cleaned, { live: liveTokens }); | ||
|
|
||
| return { | ||
| overrides: migrated.overrides, | ||
| report: { | ||
| format: isJson ? "json" : "css", | ||
| accepted: Object.keys(migrated.overrides).length, | ||
| renamed: migrated.renamed.length, | ||
| removed: migrated.removed.length, | ||
| unknown: migrated.unknown.length, | ||
| invalid, | ||
| collisions: migrated.collisions.length, | ||
| malformed: malformed && Object.keys(cleaned).length === 0, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** A short human summary of an import for a status banner. */ | ||
| export function summarizeImport(r: ImportReport): string { | ||
| if (r.malformed) return "Import failed — the file has no recognisable SLASHED tokens."; | ||
| if (r.accepted === 0) return "Nothing imported — no valid SLASHED tokens found."; | ||
| const parts = [`Imported ${r.accepted} token${r.accepted === 1 ? "" : "s"}`]; | ||
| if (r.renamed) parts.push(`${r.renamed} migrated`); | ||
| if (r.removed) parts.push(`${r.removed} removed`); | ||
| if (r.unknown) parts.push(`${r.unknown} unknown`); | ||
| if (r.invalid.length) parts.push(`${r.invalid.length} skipped`); | ||
| if (r.collisions) parts.push(`${r.collisions} migration collision${r.collisions === 1 ? "" : "s"}`); | ||
| return parts.join(" · ") + "."; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * Unit tests for src/lib/importOverrides.ts — the single, validated import | ||
| * pipeline that replaced the two silent, inconsistent header-import paths. | ||
| * Pins that CSS and JSON behave the same, values are sanitised, bad keys/values | ||
| * are reported (not silently dropped), and renamed tokens are migrated. | ||
| */ | ||
| import { describe, test, expect } from 'vitest'; | ||
| import { parseImport, summarizeImport } from '../src/lib/importOverrides'; | ||
|
|
||
| const LIVE = new Set([ | ||
| '--sf-color-primary', '--sf-space-m', '--sf-radius-l', | ||
| '--sf-color-primary-source-light', // migration target | ||
| ]); | ||
|
|
||
| describe('parseImport — JSON', () => { | ||
| test('accepts a flat name→value map', () => { | ||
| const r = parseImport('{"--sf-color-primary":"red","--sf-space-m":"2rem"}', 'x.json', LIVE); | ||
| expect(r.overrides).toEqual({ '--sf-color-primary': 'red', '--sf-space-m': '2rem' }); | ||
| expect(r.report.format).toBe('json'); | ||
| expect(r.report.accepted).toBe(2); | ||
| }); | ||
| test('accepts a theme-file { tokens } shape', () => { | ||
| const r = parseImport('{"schemaVersion":1,"tokens":{"--sf-space-m":"3rem"}}', 't.json', LIVE); | ||
| expect(r.overrides).toEqual({ '--sf-space-m': '3rem' }); | ||
| }); | ||
| test('accepts exported theme-file { overrides } and reports invalid values', () => { | ||
| const r = parseImport('{"schemaVersion":1,"overrides":{"--sf-space-m":"3rem","--sf-radius-l":4}}', 'theme.json', LIVE); | ||
| expect(r.overrides).toEqual({ '--sf-space-m': '3rem' }); | ||
| expect(r.report.invalid).toEqual(['--sf-radius-l']); | ||
| }); | ||
| test('repairs CSS-breaking values but rejects bad keys and empty-after-sanitise ones', () => { | ||
| // "1rem; }" sanitises to "1rem" (kept); ";;" sanitises to "" (rejected); | ||
| // a non --sf key is rejected. | ||
| const r = parseImport('{"notatoken":"x","--sf-space-m":";;","--sf-color-primary":"1rem; }","--sf-radius-l":"8px"}', 'x.json', LIVE); | ||
| expect(r.overrides).toEqual({ '--sf-color-primary': '1rem', '--sf-radius-l': '8px' }); | ||
| expect(r.report.invalid.sort()).toEqual(['--sf-space-m', 'notatoken']); | ||
| }); | ||
| test('malformed JSON is reported, not thrown', () => { | ||
| const r = parseImport('{ not json', 'x.json', LIVE); | ||
| expect(r.report.malformed).toBe(true); | ||
| expect(r.report.accepted).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseImport — CSS', () => { | ||
| test('parses declarations and merges consistently with JSON', () => { | ||
| const css = ':root{ --sf-color-primary: blue; --sf-radius-l: 12px; }'; | ||
| const r = parseImport(css, 'overrides.css', LIVE); | ||
| expect(r.overrides).toEqual({ '--sf-color-primary': 'blue', '--sf-radius-l': '12px' }); | ||
| expect(r.report.format).toBe('css'); | ||
| }); | ||
| test('a CSS file with no --sf tokens is reported empty', () => { | ||
| const r = parseImport('body { color: red; }', 'x.css', LIVE); | ||
| expect(r.report.accepted).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseImport — migration & unknown', () => { | ||
| test('migrates a renamed token to its current name', () => { | ||
| const r = parseImport('{"--sf-color-primary-light":"red"}', 'x.json', LIVE); | ||
| expect(r.overrides['--sf-color-primary-source-light']).toBe('red'); | ||
| expect(r.report.renamed).toBe(1); | ||
| }); | ||
| test('keeps but flags a token not in this build', () => { | ||
| const r = parseImport('{"--sf-made-up-token":"1px"}', 'x.json', LIVE); | ||
| expect(r.overrides['--sf-made-up-token']).toBe('1px'); | ||
| expect(r.report.unknown).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('summarizeImport', () => { | ||
| test('summarises a mixed result', () => { | ||
| const msg = summarizeImport({ format: 'json', accepted: 3, renamed: 1, removed: 0, unknown: 1, invalid: ['x'], collisions: 0, malformed: false }); | ||
| expect(msg).toContain('Imported 3 tokens'); | ||
| expect(msg).toContain('1 migrated'); | ||
| expect(msg).toContain('1 unknown'); | ||
| expect(msg).toContain('1 skipped'); | ||
| }); | ||
| test('reports malformed and empty distinctly', () => { | ||
| expect(summarizeImport({ format: 'css', accepted: 0, renamed: 0, removed: 0, unknown: 0, invalid: [], collisions: 0, malformed: true })).toMatch(/failed/i); | ||
| expect(summarizeImport({ format: 'css', accepted: 0, renamed: 0, removed: 0, unknown: 0, invalid: [], collisions: 0, malformed: false })).toMatch(/Nothing imported/i); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When two legacy token names migrate to the same current token, migration drops one value and records a collision, but this report omits that collision. The import banner consequently reports success without telling the user that one override was discarded.