diff --git a/.changeset/dom-style-rendering.md b/.changeset/dom-style-rendering.md new file mode 100644 index 0000000..9921fce --- /dev/null +++ b/.changeset/dom-style-rendering.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': patch +--- + +Apply element styles to terminal layout, spacing, borders, and colors, including default display modes and inherited foreground colors. diff --git a/packages/ui/src/layout.test.ts b/packages/ui/src/layout.test.ts index 28b24be..0169d30 100644 --- a/packages/ui/src/layout.test.ts +++ b/packages/ui/src/layout.test.ts @@ -1,5 +1,5 @@ -import { close, open, text } from '@bomb.sh/tty'; -import { describe, expect, it } from 'vitest'; +import { close, open, rgba, snapshot, text, type Op } from '@bomb.sh/tty'; +import { assert, describe, expect, it } from 'vitest'; import { Document } from './dom.ts'; import { Layout } from './layout.ts'; @@ -33,7 +33,7 @@ describe('Layout', () => { expect([...layout.layout(child)]).toEqual([open('DOCUMENT: 1'), text('inside'), close()]); }); - it('lays out a deeply nested tree without recursive generator delegation', () => { + it('lays out a deeply nested tree without recursive traversal', () => { const document = new Document(() => {}); const root = document.body; const depth = 20_000; @@ -53,6 +53,9 @@ describe('Layout', () => { expect(iterator.next().value).toEqual(close()); } expect(iterator.next().done).toBe(true); + + root.style.display = 'inline'; + expect([...new Layout(root).layout()]).toEqual([text('leaf')]); }); it('closes active elements when iteration is returned early', () => { @@ -71,3 +74,91 @@ describe('Layout', () => { expect(iterator.next().done).toBe(true); }); }); +describe('sparse layout options', () => { + it('omits default layout options and saves the whole packed layout block', () => { + const document = new Document(() => {}); + const { body } = document; + // Explicit defaults have the same packing requirements as undeclared defaults. + body.style.display = 'flex'; + body.style.flexDirection = 'row'; + body.style.gap = '0px'; + body.style.paddingLeft = '0px'; + body.style.borderColor = 'blue'; + const ops = [...new Layout(body).layout()]; + const id = rootId(ops); + const dense = [ + open(id, { + layout: { + direction: 'ltr', + gap: 0, + padding: { top: 0, right: 0, bottom: 0, left: 0 }, + }, + }), + close(), + ]; + expect(ops).toEqual([open(id), close()]); + expect(packed(dense).byteLength - packed(ops).byteLength).toBe(36); + }); + + it('packs nondefault layout and border options identically to explicit zero sides', () => { + const document = new Document(() => {}); + const { body } = document; + body.style.flexDirection = 'column'; + body.style.gap = '2px'; + body.style.paddingLeft = '2px'; + body.style.borderStyle = 'solid'; + body.style.borderColor = 'blue'; + body.style.borderTopWidth = '0px'; + body.style.borderRightWidth = '0px'; + body.style.borderBottomWidth = '0px'; + const ops = [...new Layout(body).layout()]; + const id = rootId(ops); + expect(ops).toEqual([ + open(id, { + layout: { direction: 'ttb', gap: 2, padding: { left: 2 } }, + border: { color: rgba(0, 0, 255), left: 1 }, + }), + close(), + ]); + expect(packed(ops)).toEqual( + packed([ + open(id, { + layout: { direction: 'ttb', gap: 2, padding: { top: 0, right: 0, bottom: 0, left: 2 } }, + border: { color: rgba(0, 0, 255), top: 0, right: 0, bottom: 0, left: 1 }, + }), + close(), + ]), + ); + }); + + it('omits terminal-default text color but preserves inherited packed zero', () => { + const document = new Document(() => {}); + const { body } = document; + const span = document.createElement('span'); + span.appendChild(document.createTextNode('inline')); + body.appendChild(document.createTextNode('direct')); + body.appendChild(span); + const layout = new Layout(body); + const ops = [...layout.layout()]; + const id = rootId(ops); + expect(ops).toEqual([open(id), text('direct'), text('inline'), close()]); + body.style.color = 'transparent'; + expect([...layout.layout()]).toEqual([ + open(id), + text('direct', { color: 0 }), + text('inline', { color: 0 }), + close(), + ]); + }); +}); + +function rootId(ops: Op[]): string { + const root = ops[0]; + assert(root && 'id' in root); + return root.id; +} + +function packed(ops: Op[]): Uint8Array { + const result = snapshot(ops) as { data: Uint8Array }; + return result.data; +} diff --git a/packages/ui/src/layout.ts b/packages/ui/src/layout.ts index 22dc588..533e049 100644 --- a/packages/ui/src/layout.ts +++ b/packages/ui/src/layout.ts @@ -1,5 +1,11 @@ -import { close, open, text, type Op } from '@bomb.sh/tty'; +/* oxlint-disable bombshell-dev/max-params -- Layout helpers pass the element and rendering context explicitly. */ +import { close, open, rgba, text, type Op } from '@bomb.sh/tty'; +import { convert } from 'hueful'; import { Element, Text } from './dom.ts'; +import type { StyleValues } from './style.ts'; + +type TTYContainerOptions = NonNullable[1]>; +type TTYTextOptions = NonNullable[1]>; export class Layout { #lastIdNumber = 0; @@ -11,17 +17,27 @@ export class Layout { } /** Serialize an element to TTY Ops. */ - *layout(element: Element = this.#root): IterableIterator { - yield this.#open(element); + *layout(element: Element = this.#root, inheritedColor?: number): IterableIterator { + const style = computeStyle(element); + const color = resolveColorValue(style.color.value, inheritedColor); + if (style.display.value === 'inline') { + const content = collectInlineText(element); + if (content !== '') { + yield text(content, ttyTextOptions(color)); + } + return; + } - const stack = [element.childNodes[Symbol.iterator]()]; + const stack = [{ children: element.childNodes[Symbol.iterator](), color }]; try { + yield this.#open(element, style, color); let content = ''; while (stack.length > 0) { - const next = stack[stack.length - 1]!.next(); + const frame = stack[stack.length - 1]!; + const next = frame.children.next(); if (next.done) { if (content !== '') { - yield text(content); + yield text(content, ttyTextOptions(frame.color)); content = ''; } stack.pop(); @@ -33,11 +49,20 @@ export class Layout { content += child.data; } else if (child instanceof Element) { if (content !== '') { - yield text(content); + yield text(content, ttyTextOptions(frame.color)); content = ''; } - yield this.#open(child); - stack.push(child.childNodes[Symbol.iterator]()); + const childStyle = computeStyle(child); + const childColor = resolveColorValue(childStyle.color.value, frame.color); + if (childStyle.display.value === 'inline') { + const inlineContent = collectInlineText(child); + if (inlineContent !== '') { + yield text(inlineContent, ttyTextOptions(childColor)); + } + } else { + stack.push({ children: child.childNodes[Symbol.iterator](), color: childColor }); + yield this.#open(child, childStyle, childColor); + } } } } finally { @@ -48,12 +73,157 @@ export class Layout { } } - #open(element: Element): Op { + #open(element: Element, style: StyleValues, color?: number): Op { let stableId = this.#stableIds.get(element); if (!stableId) { stableId = `${element.nodeName}: ${this.#lastIdNumber++}`; this.#stableIds.set(element, stableId); } - return open(stableId); + return open(stableId, ttyContainerOptions(style, color)); + } +} + +/** Inline elements collect descendant text using the outer element's styles. */ +function collectInlineText(element: Element): string { + let content = ''; + const stack = [element.childNodes[Symbol.iterator]()]; + while (stack.length > 0) { + const next = stack[stack.length - 1]!.next(); + if (next.done) { + stack.pop(); + } else if (next.value instanceof Text) { + content += next.value.data; + } else if (next.value instanceof Element) { + stack.push(next.value.childNodes[Symbol.iterator]()); + } } + return content; +} + +/** + * Convert a color which is stored internally as a hueful `ParseResult` into + * an RGB numeric value which is what TTY speaks natively. + */ +function resolveColorValue( + value: StyleValues['color']['value'], + inheritedColor?: number, +): number | undefined { + if (value === 'currentColor') { + return inheritedColor; + } + const [r, g, b, alpha = 1] = convert(value.space, 'rgb', value.value); + const channel = (component: number): number => Math.round(Math.max(0, Math.min(255, component))); + return rgba(channel(r), channel(g), channel(b), channel(alpha * 255)); +} + +/** + * Map a layout container element's styles to TTY options. The mapping is + * sparse so that even though we have a completely known computed style, + * we only send through to WASM enough information to render. + */ +function ttyContainerOptions(style: StyleValues, color?: number): TTYContainerOptions { + const props: TTYContainerOptions = {}; + const layout: NonNullable = {}; + if (style.flexDirection.value === 'column') { + layout.direction = 'ttb'; + } + if (style.gap.value !== 0) { + layout.gap = style.gap.value; + } + + let top = style.paddingTop.value; + let right = style.paddingRight.value; + let bottom = style.paddingBottom.value; + let left = style.paddingLeft.value; + const borderTop = style.borderTopWidth.value; + const borderRight = style.borderRightWidth.value; + const borderBottom = style.borderBottomWidth.value; + const borderLeft = style.borderLeftWidth.value; + if ( + style.borderStyle.value === 'solid' && + (borderTop || borderRight || borderBottom || borderLeft) + ) { + const borderColor = resolveColorValue(style.borderColor.value, color); + // TTY requires an explicit border color. Keep the space when it cannot draw the border. + if (borderColor !== undefined) { + props.border = { color: borderColor }; + if (borderTop !== 0) { + props.border.top = borderTop; + } + if (borderRight !== 0) { + props.border.right = borderRight; + } + if (borderBottom !== 0) { + props.border.bottom = borderBottom; + } + if (borderLeft !== 0) { + props.border.left = borderLeft; + } + } else { + // TTY cannot draw borders with the terminal-default foreground yet. + // Reserve their space with padding until https://github.com/bombshell-dev/tty/issues/155 + // is implemented; then emit the border and remove this fallback. + top += borderTop; + right += borderRight; + bottom += borderBottom; + left += borderLeft; + } + } + + if (top || right || bottom || left) { + layout.padding = {}; + if (top !== 0) { + layout.padding.top = top; + } + if (right !== 0) { + layout.padding.right = right; + } + if (bottom !== 0) { + layout.padding.bottom = bottom; + } + if (left !== 0) { + layout.padding.left = left; + } + } + // Even an empty layout object makes TTY pack the entire layout block. + if (Object.keys(layout).length > 0) { + props.layout = layout; + } + return props; +} + +function ttyTextOptions(color?: number): TTYTextOptions { + return color === undefined ? {} : { color }; +} + +function computeStyle(element: Element): StyleValues { + const defaults = computeDefaultStyles(element); + return applyInlineStyles(element, defaults); +} + +function computeDefaultStyles(element: Element): StyleValues { + const display = + element.namespaceURI === 'http://www.w3.org/1999/xhtml' && element.localName === 'span' + ? 'inline' + : 'flex'; + return { + color: { value: 'currentColor', representation: 'currentColor' }, + display: { value: display, representation: display }, + flexDirection: { value: 'row', representation: 'row' }, + gap: { value: 0, representation: '0px' }, + paddingTop: { value: 0, representation: '0px' }, + paddingRight: { value: 0, representation: '0px' }, + paddingBottom: { value: 0, representation: '0px' }, + paddingLeft: { value: 0, representation: '0px' }, + borderStyle: { value: 'none', representation: 'none' }, + borderTopWidth: { value: 1, representation: '1px' }, + borderRightWidth: { value: 1, representation: '1px' }, + borderBottomWidth: { value: 1, representation: '1px' }, + borderLeftWidth: { value: 1, representation: '1px' }, + borderColor: { value: 'currentColor', representation: 'currentColor' }, + }; +} + +function applyInlineStyles(element: Element, defaults: StyleValues): StyleValues { + return { ...defaults, ...element.style.values }; } diff --git a/packages/ui/src/style-rendering.test.ts b/packages/ui/src/style-rendering.test.ts new file mode 100644 index 0000000..d0b7fe8 --- /dev/null +++ b/packages/ui/src/style-rendering.test.ts @@ -0,0 +1,329 @@ +import { describe, expect, it } from 'vitest'; +import { createTestUI } from '../test/ui.ts'; +import type { UI } from './ui.ts'; + +const green = { r: 0, g: 205, b: 0 }; +const blue = { r: 0, g: 0, b: 238 }; + +describe('style rendering', () => { + it('defaults div to a row with no gap or padding, and repaints when direction changes', async () => { + await using ui = await createTestUI({ cols: 12, rows: 4 }); + const { document, terminal } = ui; + const row = document.createElement('div'); + row.appendChild(label(document, 'A')); + row.appendChild(label(document, 'B')); + document.body.appendChild(row); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['AB', '', '', '']); + + row.style.flexDirection = 'column'; + await expect.poll(() => terminal.screen.getLines()).toEqual(['A', 'B', '', '']); + + row.style.flexDirection = ''; + await expect.poll(() => terminal.screen.getLines()).toEqual(['AB', '', '', '']); + }); + + it('lets a span become a flex container', async () => { + await using ui = await createTestUI({ cols: 12, rows: 4 }); + const { document, terminal } = ui; + const column = document.createElement('span'); + column.style.display = 'flex'; + column.style.flexDirection = 'column'; + column.style.gap = '1px'; + column.appendChild(label(document, 'A')); + column.appendChild(label(document, 'B')); + document.body.appendChild(column); + document.body.appendChild(label(document, 'C')); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['AC', '', 'B', '']); + }); + + it('lets a div become one inline text item', async () => { + await using ui = await createTestUI({ cols: 12, rows: 4 }); + const { document, terminal } = ui; + document.body.style.flexDirection = 'column'; + document.body.style.gap = '1px'; + const flow = document.createElement('div'); + flow.style.display = 'inline'; + // These declarations apply to flex containers, not inline text. + flow.style.flexDirection = 'column'; + flow.style.gap = '3px'; + flow.appendChild(document.createTextNode('A')); + flow.appendChild(label(document, 'B')); + document.body.appendChild(flow); + document.body.appendChild(label(document, 'C')); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['AB', '', 'C', '']); + }); + + it('uses gap and individual padding as whole terminal cells without inheriting them', async () => { + await using ui = await createTestUI({ cols: 12, rows: 5 }); + const { document, terminal } = ui; + document.body.style.flexDirection = 'column'; + const row = document.createElement('div'); + const padded = document.createElement('div'); + padded.style.gap = '2px'; + padded.style.paddingTop = '1px'; + padded.style.paddingRight = '3px'; + padded.style.paddingBottom = '1px'; + padded.style.paddingLeft = '2px'; + const inner = document.createElement('div'); + inner.appendChild(label(document, 'A')); + inner.appendChild(label(document, 'B')); + padded.appendChild(inner); + padded.appendChild(label(document, 'C')); + row.appendChild(padded); + row.appendChild(label(document, 'R')); + document.body.appendChild(row); + document.body.appendChild(label(document, 'D')); + + // R observes right padding; D observes bottom padding and the row's height. + await expect + .poll(() => terminal.screen.getLines()) + .toEqual([' R', ' AB C', '', 'D', '']); + }); + + it.each([ + ['borderTopWidth', ['─', 'X', '']], + ['borderRightWidth', ['X│', '', '']], + ['borderBottomWidth', ['X', '─', '']], + ['borderLeftWidth', ['│X', '', '']], + ] as const)('draws only the side selected by %s', async (side, lines) => { + await using ui = await createTestUI({ cols: 8, rows: 3 }); + const { document, terminal } = ui; + const box = document.createElement('div'); + box.style.borderStyle = 'solid'; + box.style.borderColor = 'rgb(0 0 238)'; + box.style.borderTopWidth = '0px'; + box.style.borderRightWidth = '0px'; + box.style.borderBottomWidth = '0px'; + box.style.borderLeftWidth = '0px'; + box.style[side] = '1px'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect.poll(() => terminal.screen.getLines()).toEqual(lines); + }); + + it('requires solid to show a border and retains its widths while hidden', async () => { + await using ui = await createTestUI({ cols: 8, rows: 4 }); + const { document, terminal } = ui; + const box = document.createElement('div'); + box.style.borderColor = 'rgb(0 0 238)'; + box.style.borderTopWidth = '1px'; + box.style.borderRightWidth = '1px'; + box.style.borderBottomWidth = '1px'; + box.style.borderLeftWidth = '1px'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['X', '', '', '']); + + box.style.borderStyle = 'solid'; + await expect.poll(() => terminal.screen.getLines()).toEqual(['┌─┐', '│X│', '└─┘', '']); + + box.style.borderStyle = 'none'; + await expect.poll(() => terminal.screen.getLines()).toEqual(['X', '', '', '']); + expect(box.style.borderTopWidth).toBe('1px'); + expect(box.style.borderRightWidth).toBe('1px'); + expect(box.style.borderBottomWidth).toBe('1px'); + expect(box.style.borderLeftWidth).toBe('1px'); + + box.style.borderStyle = 'solid'; + await expect.poll(() => terminal.screen.getLines()).toEqual(['┌─┐', '│X│', '└─┘', '']); + }); + + it('resolves the default border width to one cell outside the padding', async () => { + await using ui = await createTestUI({ cols: 8, rows: 6 }); + const { document, terminal } = ui; + const box = document.createElement('div'); + box.style.borderStyle = 'solid'; + box.style.borderColor = 'rgb(0 0 238)'; + box.style.paddingTop = '1px'; + box.style.paddingRight = '1px'; + box.style.paddingBottom = '1px'; + box.style.paddingLeft = '1px'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect + .poll(() => terminal.screen.getLines()) + .toEqual(['┌───┐', '│ │', '│ X │', '│ │', '└───┘', '']); + // Resolved defaults do not become authored inline declarations. + expect(box.style.borderTopWidth).toBe(''); + }); + + it('inherits color, preserves overrides, and repaints descendants when declarations change or clear', async () => { + await using ui = await createTestUI({ cols: 8, rows: 2 }); + const { document, terminal } = ui; + document.body.style.color = 'rgb(229 229 229)'; + const parent = document.createElement('div'); + parent.style.color = 'rgb(0 205 0)'; + const inherited = label(document, 'A'); + const overridden = label(document, 'B'); + overridden.style.color = 'rgb(0 0 0)'; + parent.appendChild(inherited); + parent.appendChild(overridden); + document.body.appendChild(parent); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['AB', '']); + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject(green); + expect(terminal.getCell(0, 1).fg).toMatchObject({ r: 0, g: 0, b: 0 }); + expect(inherited.style.color).toBe(''); + + parent.style.color = 'rgb(0 0 238)'; + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject(blue); + expect(terminal.getCell(0, 1).fg).toMatchObject({ r: 0, g: 0, b: 0 }); + + overridden.style.color = ''; + await expect.poll(() => terminal.getCell(0, 1).fg).toMatchObject(blue); + + parent.style.color = ''; + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject({ r: 229, g: 229, b: 229 }); + expect(terminal.getCell(0, 1).fg).toMatchObject({ r: 229, g: 229, b: 229 }); + + document.body.style.color = ''; + await expect.poll(() => terminal.getCell(0, 0).fg).toBeNull(); + expect(terminal.getCell(0, 1).fg).toBeNull(); + }); + + it.each([ + ['green', { r: 0, g: 128, b: 0 }], + ['#00cd00', green], + ['hsl(240 100% 50%)', { r: 0, g: 0, b: 255 }], + ] as const)('converts %s to a terminal text color', async (color, expected) => { + await using ui = await createTestUI({ cols: 8, rows: 2 }); + const { document, terminal } = ui; + const text = label(document, 'X'); + text.style.color = color; + document.body.appendChild(text); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['X', '']); + expect(terminal.getCell(0, 0).fg).toMatchObject(expected); + }); + + it('resolves default and explicit currentColor borders from inherited text color', async () => { + await using ui = await createTestUI({ cols: 8, rows: 4 }); + const { document, terminal } = ui; + document.body.style.color = 'rgb(0 205 0)'; + const box = document.createElement('div'); + box.style.borderStyle = 'solid'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['┌─┐', '│X│', '└─┘', '']); + expect(terminal.getCell(0, 0).fg).toMatchObject(green); + expect(terminal.getCell(1, 1).fg).toMatchObject(green); + + box.style.borderColor = 'rgb(0 0 238)'; + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject(blue); + expect(terminal.getCell(1, 1).fg).toMatchObject(green); + + box.style.borderColor = 'currentColor'; + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject(green); + + document.body.style.color = 'rgb(0 0 238)'; + await expect.poll(() => terminal.getCell(0, 0).fg).toMatchObject(blue); + expect(terminal.getCell(1, 1).fg).toMatchObject(blue); + }); + + it('concatenates adjacent text nodes verbatim without inserting gaps inside a span', async () => { + await using ui = await createTestUI({ cols: 32, rows: 2 }); + const { document, terminal } = ui; + document.body.style.gap = '2px'; + const flow = document.createElement('span'); + flow.appendChild(document.createTextNode(' Hello ')); + flow.appendChild(document.createTextNode(' world ')); + flow.appendChild(document.createTextNode('!')); + document.body.appendChild(flow); + document.body.appendChild(label(document, '|')); + + await expect.poll(() => terminal.screen.getLines()).toEqual([' Hello world ! |', '']); + }); + + it('wraps nested spans with equal resolved styles as one text flow', async () => { + await using ui = await createTestUI({ cols: 10, rows: 5 }); + const { document, terminal } = ui; + document.body.style.flexDirection = 'column'; + document.body.style.gap = '1px'; + document.body.style.color = 'rgb(0 205 0)'; + const flow = document.createElement('span'); + flow.appendChild(document.createTextNode('one ')); + const nested = label(document, 'two three'); + // Different declaration spelling, same resolved color as its parent. + nested.style.color = 'rgb(0, 205, 0)'; + flow.appendChild(nested); + flow.appendChild(document.createTextNode(' four')); + document.body.appendChild(flow); + document.body.appendChild(label(document, '!')); + + await expect + .poll(() => terminal.screen.getLines()) + .toEqual(['one two', 'three four', '', '!', '']); + expect(terminal.getCell(1, 0).fg).toMatchObject(green); + }); + + it.each(['span', 'div'] as const)( + 'collects text from a nested %s using the outer inline color', + async (tagName) => { + await using ui = await createTestUI({ cols: 20, rows: 2 }); + const { document, terminal } = ui; + const flow = document.createElement('span'); + flow.style.color = 'rgb(0 205 0)'; + flow.appendChild(document.createTextNode('Hello ')); + const nested = document.createElement(tagName); + nested.style.color = 'rgb(0 0 238)'; + nested.appendChild(document.createTextNode('world')); + flow.appendChild(nested); + flow.appendChild(document.createTextNode('!')); + document.body.appendChild(flow); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['Hello world!', '']); + for (let column = 0; column < 'Hello world!'.length; column++) { + expect(terminal.getCell(0, column).fg).toMatchObject(green); + } + }, + ); + + it('preserves border space and keeps rendering when the border color is terminal-default', async () => { + await using ui = await createTestUI({ cols: 8, rows: 4 }); + const { document, terminal } = ui; + const box = document.createElement('div'); + box.style.borderStyle = 'solid'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['', ' X', '', '']); + expect(terminal.getCell(1, 1).fg).toBeNull(); + + box.style.borderColor = 'rgb(0 0 238)'; + await expect.poll(() => terminal.screen.getLines()).toEqual(['┌─┐', '│X│', '└─┘', '']); + expect(terminal.getCell(0, 0).fg).toMatchObject(blue); + + box.style.borderColor = ''; + document.body.appendChild(label(document, '!')); + await expect.poll(() => terminal.screen.getLines()).toEqual([' !', ' X', '', '']); + }); + + it('allows terminal-default foreground when every border side has zero width', async () => { + await using ui = await createTestUI({ cols: 8, rows: 2 }); + const { document, terminal } = ui; + const box = document.createElement('div'); + box.style.borderStyle = 'solid'; + box.style.borderTopWidth = '0px'; + box.style.borderRightWidth = '0px'; + box.style.borderBottomWidth = '0px'; + box.style.borderLeftWidth = '0px'; + box.appendChild(document.createTextNode('X')); + document.body.appendChild(box); + + await expect.poll(() => terminal.screen.getLines()).toEqual(['X', '']); + expect(terminal.getCell(0, 0).fg).toBeNull(); + }); +}); + +function label(document: UI['document'], content: string): UI['document']['documentElement'] { + const element = document.createElement('span'); + element.appendChild(document.createTextNode(content)); + return element; +} diff --git a/packages/ui/test/terminal.ts b/packages/ui/test/terminal.ts index a6cf845..fc680d3 100644 --- a/packages/ui/test/terminal.ts +++ b/packages/ui/test/terminal.ts @@ -1,5 +1,5 @@ import { PassThrough, Writable } from 'node:stream'; -import { keyToAnsi, parseKey, type Region } from '@termless/core'; +import { keyToAnsi, parseKey, type Cell, type Region } from '@termless/core'; import { createTestTerminal as createTermlessTerminal, type SyncTestTerminalOptions, @@ -9,6 +9,7 @@ export interface TestTerminal extends AsyncDisposable { input: PassThrough; output: Writable; readonly screen: Region; + getCell(row: number, col: number): Cell; type(text: string): void; press(key: string): void; } @@ -36,6 +37,9 @@ export function createTestTerminal(options: SyncTestTerminalOptions = {}): TestT get screen() { return terminal.screen; }, + getCell(row, col) { + return terminal.backend.getCell(row, col); + }, type(text) { input.write(text); },