AI-native design editor. Open-source Figma alternative.
feat(dom-css): map browser text casing
Danila Poyarkov3f1a5ce77945
diff --git a/packages/dom-css/src/from-scene-graph.ts b/packages/dom-css/src/from-scene-graph.tsindex 1a52510ae..a16482dd8 100644--- a/packages/dom-css/src/from-scene-graph.ts+++ b/packages/dom-css/src/from-scene-graph.ts@@ -46,6 +46,13 @@ function alignSelfToCSS(value: SceneNode['layoutAlignSelf']): string | undefinedreturn undefined}+function textCaseToCSS(value: SceneNode['textCase']): string | undefined {+ if (value === 'UPPER') return 'uppercase'+ if (value === 'LOWER') return 'lowercase'+ if (value === 'TITLE') return 'capitalize'+ return undefined+}+function addPositioning(style: DesignStyleDeclaration, node: SceneNode): void {if (node.layoutPositioning !== 'ABSOLUTE') returnstyle.position = 'absolute'@@ -182,6 +189,9 @@ function styleFromTextNode(node: SceneNode): DesignStyleDeclaration {style['text-decoration-line'] =node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'}+ const textTransform = textCaseToCSS(node.textCase)+ if (textTransform) style['text-transform'] = textTransform+ if (node.maxLines === 1) style['white-space'] = 'nowrap'return style}diff --git a/packages/dom-css/src/runtime/browser.ts b/packages/dom-css/src/runtime/browser.tsindex 01342cdb9..61dfa627f 100644--- a/packages/dom-css/src/runtime/browser.ts+++ b/packages/dom-css/src/runtime/browser.ts@@ -14,6 +14,7 @@ export interface BrowserCSSRuntimeOptions {const DEFAULT_COMPUTED_PROPERTIES = ['align-items',+ 'aspect-ratio','background-color','background-image','border-bottom-color',@@ -51,6 +52,7 @@ const DEFAULT_COMPUTED_PROPERTIES = ['max-width','min-height','min-width',+ 'object-fit','opacity','overflow','padding-bottom',@@ -63,7 +65,9 @@ const DEFAULT_COMPUTED_PROPERTIES = ['text-align','text-decoration-line','text-shadow',+ 'text-transform','top',+ 'white-space','width'] as constdiff --git a/packages/dom-css/src/to-scene-graph.ts b/packages/dom-css/src/to-scene-graph.tsindex c7fbd1261..72229032a 100644--- a/packages/dom-css/src/to-scene-graph.ts+++ b/packages/dom-css/src/to-scene-graph.ts@@ -149,6 +149,13 @@ function layoutAlignSelfFromCSS(value: string | undefined): SceneNode['layoutAlireturn 'AUTO'}+function textCaseFromCSS(value: string | undefined): SceneNode['textCase'] {+ if (value === 'uppercase') return 'UPPER'+ if (value === 'lowercase') return 'LOWER'+ if (value === 'capitalize') return 'TITLE'+ return 'ORIGINAL'+}+function applyFlexGap(node: SceneNode, style: DesignStyleDeclaration): void {const gap = firstCSSNumber(style, 'gap')const rowGap = firstCSSNumber(style, 'row-gap')@@ -257,6 +264,11 @@ function applyTextStyle(node: SceneNode, style: DesignStyleDeclaration): void {const textDecoration = pickStyle(style, 'text-decoration-line')if (textDecoration === 'underline') node.textDecoration = 'UNDERLINE'if (textDecoration === 'line-through') node.textDecoration = 'STRIKETHROUGH'++ const textCase = textCaseFromCSS(pickStyle(style, 'text-transform'))+ if (textCase !== 'ORIGINAL') node.textCase = textCase++ if (pickStyle(style, 'white-space') === 'nowrap') node.maxLines = 1}function createTextNode(diff --git a/packages/dom-css/tests/conversion.test.ts b/packages/dom-css/tests/conversion.test.tsindex efeedf9cd..af57d3112 100644--- a/packages/dom-css/tests/conversion.test.ts+++ b/packages/dom-css/tests/conversion.test.ts@@ -61,6 +61,8 @@ function createStyleRoundTripGraph() {'line-height': '24px','letter-spacing': '0.2px','text-align': 'center',+ 'text-transform': 'uppercase',+ 'white-space': 'nowrap',opacity: '0.5','text-shadow': `0px 1px 2px 0px ${TEST_COLORS.slateShadow}`},@@ -95,6 +97,8 @@ function expectStyleRoundTripText(graph: SceneGraph, panel: SceneNode) {expect(heading.lineHeight).toBe(24)expect(heading.letterSpacing).toBe(0.2)expect(heading.textAlignHorizontal).toBe('CENTER')+ expect(heading.textCase).toBe('UPPER')+ expect(heading.maxLines).toBe(1)expect(heading.opacity).toBe(0.5)expect(heading.effects[0]?.type).toBe('DROP_SHADOW')}@@ -114,6 +118,8 @@ function expectRoundTripHeadingStyle(element: DesignElement) {expect(element.inlineStyle?.['line-height']).toBe('24px')expect(element.inlineStyle?.['letter-spacing']).toBe('0.2px')expect(element.inlineStyle?.['text-align']).toBe('center')+ expect(element.inlineStyle?.['text-transform']).toBe('uppercase')+ expect(element.inlineStyle?.['white-space']).toBe('nowrap')expect(element.inlineStyle?.opacity).toBe('0.5')expect(element.inlineStyle?.['text-shadow']).toContain('2px')}diff --git a/tests/e2e/dom-css/browser-runtime.spec.ts b/tests/e2e/dom-css/browser-runtime.spec.tsindex 232be2823..859c3a0aa 100644--- a/tests/e2e/dom-css/browser-runtime.spec.ts+++ b/tests/e2e/dom-css/browser-runtime.spec.ts@@ -1,7 +1,3 @@-import process from 'node:process'--import type { Page } from '@playwright/test'-import {compileTailwindCSS,designDocumentToSceneGraph,@@ -20,117 +16,17 @@ import {tailwindInputClasses,tailwindNavClasses} from '#tests/helpers/dom-css'+import {+ browserRuntimeComputeStyles,+ computedStyleProperties,+ publicBrowserHTMLSceneGraph,+ publicBrowserSceneGraph,+ publicBrowserTextNode,+ setStyledContent+} from '#tests/helpers/dom-css-browser'import { expect, test } from '../fixtures'-async function setStyledContent(page: Page, css: string, body: string) {- await page.setContent(`- <style>${css}</style>- ${body}- `)-}--const BROWSER_RUNTIME_MODULE = `http://localhost:1420/@fs${process.cwd()}/packages/dom-css/src/runtime/browser.ts`-const DOM_CSS_BROWSER_MODULE = 'http://localhost:1420/@id/@open-pencil/dom-css/browser'--async function browserRuntimeComputeStyles(- page: Page,- document: DesignDocument,- cssText: string,- sandbox: 'shadow-root' | 'iframe' = 'iframe'-) {- if (!page.url().startsWith('http://localhost:1420')) {- await page.goto('/')- }-- return page.evaluate(- async ({ designDocument, css, modulePath, sandboxMode }) => {- const { createBrowserCSSRuntime } = await import(modulePath)- const runtime = createBrowserCSSRuntime({ document: window.document, sandbox: sandboxMode })- return runtime.computeStyles(designDocument, css)- },- {- designDocument: document,- css: cssText,- modulePath: BROWSER_RUNTIME_MODULE,- sandboxMode: sandbox- }- )-}--async function publicBrowserHTMLSceneGraph(page: Page, html: string, cssText = '') {- if (!page.url().startsWith('http://localhost:1420')) {- await page.goto('/')- await page.setContent('<main></main>')- }-- return page.evaluate(- async ({ sourceHTML, css, modulePath }) => {- const { browserHTMLToSceneGraph } = await import(modulePath)- const graph = await browserHTMLToSceneGraph(sourceHTML, { cssText: css })- const pageNode = graph.getPages()[0]- const card = pageNode ? graph.getChildren(pageNode.id)[0] : undefined- return card- ? {- height: card.height,- itemSpacing: card.itemSpacing,- layoutMode: card.layoutMode,- paddingLeft: card.paddingLeft,- type: card.type,- width: card.width- }- : null- },- { sourceHTML: html, css: cssText, modulePath: DOM_CSS_BROWSER_MODULE }- )-}--async function publicBrowserSceneGraph(page: Page, classes: string[], cssText: string) {- if (!page.url().startsWith('http://localhost:1420')) {- await page.goto('/')- await page.setContent('<main></main>')- }-- return page.evaluate(- async ({ candidates, css, modulePath }) => {- const { browserJSXToSceneGraph, jsx } = await import(modulePath)- const graph = await browserJSXToSceneGraph(- jsx('article', {- class: candidates.join(' '),- children: jsx('h1', { children: 'OpenPencil' })- }),- { cssText: css }- )- const pageNode = graph.getPages()[0]- const card = pageNode ? graph.getChildren(pageNode.id)[0] : undefined- return card- ? {- height: card.height,- itemSpacing: card.itemSpacing,- layoutMode: card.layoutMode,- paddingLeft: card.paddingLeft,- type: card.type,- width: card.width- }- : null- },- { candidates: classes, css: cssText, modulePath: DOM_CSS_BROWSER_MODULE }- )-}--async function computedStyleProperties(- page: Page,- selector: string,- properties: readonly string[]-) {- return page.locator(selector).evaluate((element, styleProperties) => {- const computed = getComputedStyle(element)- return Object.fromEntries(- styleProperties.map((property) => [property, computed.getPropertyValue(property)])- )- }, properties)-}-test.describe('@open-pencil/dom-css browser CSS runtime oracle', () => {test('resolves Tailwind card variables and calc values in a real browser', async ({ page }) => {const css = await compileTailwindCSS(tailwindCardClasses)@@ -378,6 +274,58 @@ test.describe('@open-pencil/dom-css browser CSS runtime oracle', () => {expect(styles.width).toBe('176px')})+ test('resolves aspect ratio, object fit, text transform, and white space in a real browser', async ({+ page+ }) => {+ await setStyledContent(+ page,+ `+ .ratio {+ aspect-ratio: 16 / 9;+ width: 320px;+ }+ .media {+ object-fit: contain;+ width: 320px;+ height: 180px;+ }+ .title {+ text-transform: uppercase;+ white-space: nowrap;+ }+ `,+ '<div class="ratio"></div><img class="media" alt="Preview" /><h1 class="title">OpenPencil</h1>'+ )++ const ratio = await computedStyleProperties(page, '.ratio', ['aspect-ratio', 'height', 'width'])+ expect(ratio['aspect-ratio']).toBe('16 / 9')+ expect(ratio.width).toBe('320px')+ expect(ratio.height).toBe('180px')++ const media = await computedStyleProperties(page, '.media', ['height', 'object-fit', 'width'])+ expect(media['object-fit']).toBe('contain')+ expect(media.width).toBe('320px')+ expect(media.height).toBe('180px')++ const title = await computedStyleProperties(page, '.title', ['text-transform', 'white-space'])+ expect(title['text-transform']).toBe('uppercase')+ expect(title['white-space']).toBe('nowrap')+ })++ test('projects browser text transform and nowrap into scene graph text fields', async ({+ page+ }) => {+ const textNode = await publicBrowserTextNode(+ page,+ '<h1 class="title">OpenPencil</h1>',+ '.title { color: #111827; text-transform: uppercase; white-space: nowrap; }'+ )++ expect(textNode?.type).toBe('TEXT')+ expect(textNode?.textCase).toBe('UPPER')+ expect(textNode?.maxLines).toBe(1)+ })+test('resolves flex wrap, self alignment, absolute positioning, and clipping', async ({page}) => {diff --git a/tests/helpers/dom-css-browser.ts b/tests/helpers/dom-css-browser.tsnew file mode 100644index 000000000..2c01417d4--- /dev/null+++ b/tests/helpers/dom-css-browser.ts@@ -0,0 +1,127 @@+import process from 'node:process'++import type { Page } from '@playwright/test'++import type { DesignDocument } from '@open-pencil/dom-css'++const BROWSER_RUNTIME_MODULE = `http://localhost:1420/@fs${process.cwd()}/packages/dom-css/src/runtime/browser.ts`+const DOM_CSS_BROWSER_MODULE = 'http://localhost:1420/@id/@open-pencil/dom-css/browser'++async function ensureAppPage(page: Page) {+ if (!page.url().startsWith('http://localhost:1420')) {+ await page.goto('/')+ }+}++export async function setStyledContent(page: Page, css: string, body: string) {+ await page.setContent(`+ <style>${css}</style>+ ${body}+ `)+}++export async function browserRuntimeComputeStyles(+ page: Page,+ document: DesignDocument,+ cssText: string,+ sandbox: 'shadow-root' | 'iframe' = 'iframe'+) {+ await ensureAppPage(page)++ return page.evaluate(+ async ({ designDocument, css, modulePath, sandboxMode }) => {+ const { createBrowserCSSRuntime } = await import(modulePath)+ const runtime = createBrowserCSSRuntime({ document: window.document, sandbox: sandboxMode })+ return runtime.computeStyles(designDocument, css)+ },+ {+ designDocument: document,+ css: cssText,+ modulePath: BROWSER_RUNTIME_MODULE,+ sandboxMode: sandbox+ }+ )+}++export async function publicBrowserHTMLSceneGraph(page: Page, html: string, cssText = '') {+ await ensureAppPage(page)+ await page.setContent('<main></main>')++ return page.evaluate(+ async ({ sourceHTML, css, modulePath }) => {+ const { browserHTMLToSceneGraph } = await import(modulePath)+ const graph = await browserHTMLToSceneGraph(sourceHTML, { cssText: css })+ const pageNode = graph.getPages()[0]+ const card = pageNode ? graph.getChildren(pageNode.id)[0] : undefined+ return card+ ? {+ height: card.height,+ itemSpacing: card.itemSpacing,+ layoutMode: card.layoutMode,+ paddingLeft: card.paddingLeft,+ type: card.type,+ width: card.width+ }+ : null+ },+ { sourceHTML: html, css: cssText, modulePath: DOM_CSS_BROWSER_MODULE }+ )+}++export async function publicBrowserSceneGraph(page: Page, classes: string[], cssText: string) {+ await ensureAppPage(page)+ await page.setContent('<main></main>')++ return page.evaluate(+ async ({ candidates, css, modulePath }) => {+ const { browserJSXToSceneGraph, jsx } = await import(modulePath)+ const graph = await browserJSXToSceneGraph(+ jsx('article', {+ class: candidates.join(' '),+ children: jsx('h1', { children: 'OpenPencil' })+ }),+ { cssText: css }+ )+ const pageNode = graph.getPages()[0]+ const card = pageNode ? graph.getChildren(pageNode.id)[0] : undefined+ return card+ ? {+ height: card.height,+ itemSpacing: card.itemSpacing,+ layoutMode: card.layoutMode,+ paddingLeft: card.paddingLeft,+ type: card.type,+ width: card.width+ }+ : null+ },+ { candidates: classes, css: cssText, modulePath: DOM_CSS_BROWSER_MODULE }+ )+}++export async function publicBrowserTextNode(page: Page, html: string, cssText: string) {+ await ensureAppPage(page)+ await page.setContent('<main></main>')++ return page.evaluate(+ async ({ sourceHTML, css, modulePath }) => {+ const { browserHTMLToSceneGraph } = await import(modulePath)+ const graph = await browserHTMLToSceneGraph(sourceHTML, { cssText: css })+ return graph.getAllNodes().find((node) => node.type === 'TEXT')+ },+ { sourceHTML: html, css: cssText, modulePath: DOM_CSS_BROWSER_MODULE }+ )+}++export async function computedStyleProperties(+ page: Page,+ selector: string,+ properties: readonly string[]+) {+ return page.locator(selector).evaluate((element, styleProperties) => {+ const computed = getComputedStyle(element)+ return Object.fromEntries(+ styleProperties.map((property) => [property, computed.getPropertyValue(property)])+ )+ }, properties)+}