Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions src/colors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { colorKeywords as color_keywords, cssKeywords as css_keywords } from '@projectwallace/css-analyzer'
import { EXTENSION_AUTHORED_AS, type ColorToken, type UnparsedToken } from './types.js'
import { EXTENSION_AUTHORED_AS, type ColorToken, type ColorValue, type UnparsedToken } from './types.js'
import {
parse,
ColorSpace,
Expand Down Expand Up @@ -51,22 +51,16 @@ ColorSpace.register(OKLab)
ColorSpace.register(OKLCH)
ColorSpace.register(OKLrab)

export function color_to_token(color: string): ColorToken | UnparsedToken | null {
export function color_to_token(color: string): ColorValue | null {
let lowercased = color.toLowerCase()

// The keyword "transparent" specifies a transparent black.
// > https://drafts.csswg.org/css-color-4/#transparent-color
if (lowercased === 'transparent') {
return {
$type: 'color',
$value: {
colorSpace: 'srgb',
components: [0, 0, 0],
alpha: 0,
},
$extensions: {
[EXTENSION_AUTHORED_AS]: color
}
colorSpace: 'srgb',
components: [0, 0, 0],
alpha: 0,
}
}

Expand All @@ -83,27 +77,16 @@ export function color_to_token(color: string): ColorToken | UnparsedToken | null
let [component_a, component_b, component_c] = parsed_color.coords

return {
$type: 'color',
$value: {
colorSpace: parsed_color.spaceId,
components: [
component_a ?? 'none',
component_b ?? 'none',
component_c ?? 'none',
],
alpha: parsed_color.alpha ?? 0,
},
$extensions: {
[EXTENSION_AUTHORED_AS]: color
}
colorSpace: parsed_color.spaceId,
components: [
component_a ?? 'none',
component_b ?? 'none',
component_c ?? 'none',
],
alpha: parsed_color.alpha ?? 0,
}
} catch (error) {
// A catch for edge cases that we don't support yet.
return {
$value: color,
$extensions: {
[EXTENSION_AUTHORED_AS]: color
}
}
return null
}
}
4 changes: 2 additions & 2 deletions src/destructure-box-shadow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ test('multiple shadows', () => {
inset: false
},
{
color: color_to_token('#fff')?.$value,
color: color_to_token('#fff'),
offsetX: create_px_length(0),
offsetY: create_px_length(0),
blur: create_px_length(0),
Expand All @@ -165,7 +165,7 @@ describe('color formats', () => {
test(`1px ${color}`, () => {
expect(destructure_box_shadow(`1px ${color}`)).toEqual([
{
color: color_to_token(color)?.$value,
color: color_to_token(color),
offsetX: create_px_length(1),
offsetY: create_px_length(0),
blur: create_px_length(0),
Expand Down
18 changes: 9 additions & 9 deletions src/destructure-box-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
current_shadow.inset = true
} else if (named_colors.has(node.name) || system_colors.has(node.name)) {
let color_token = color_to_token(node.name)
if (color_token === null || color_token.$type !== 'color') {
if (color_token === null) {
return
}
current_shadow.color = color_token.$value
current_shadow.color = color_token
}
}
else if (node.type === 'Dimension' || (node.type === 'Number' && node.value === '0')) {
Expand All @@ -82,24 +82,24 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
else if (node.type === 'Function') {
if (color_functions.has(node.name)) {
let color_token = color_to_token(generate(node))
if (color_token === null || color_token.$type !== 'color') {
if (color_token === null) {
return
}
current_shadow.color = color_token.$value
current_shadow.color = color_token
} else if (node.name.toLowerCase() === 'var' && !current_shadow.color) {
let color_token = color_to_token(generate(node))
if (color_token === null || color_token.$type !== 'color') {
if (color_token === null) {
return
}
current_shadow.color = color_token.$value
current_shadow.color = color_token
}
}
else if (node.type === 'Hash') {
let color_token = color_to_token(generate(node))
if (color_token === null || color_token.$type !== 'color') {
if (color_token === null) {
return
}
current_shadow.color = color_token.$value
current_shadow.color = color_token
}
else if (node.type === 'Operator' && node.value === ',') {
// Start a new shadow, but only after we've made sure that the current shadow is valid
Expand Down Expand Up @@ -138,7 +138,7 @@ function complete_shadow_token(token: DestructuredShadow) {
token.spread = DIMENSION_ZERO
}
if (!token.color) {
token.color = (color_to_token('#000') as ColorToken)?.$value
token.color = color_to_token('#000')!
}
return token
}
12 changes: 6 additions & 6 deletions src/destructure-easing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ describe('cubic-bezier', () => {

describe('unsupported', () => {
test('var(--test)', () => {
expect(destructure_easing('var(--test)')).toBeUndefined()
expect(destructure_easing('var(--test)')).toBeNull()
})
test('var(--test, ease-in)', () => {
expect(destructure_easing('var(--test, ease-in)')).toBeUndefined()
expect(destructure_easing('var(--test, ease-in)')).toBeNull()
})
test('step-start', () => {
expect(destructure_easing('step-start')).toBeUndefined()
expect(destructure_easing('step-start')).toBeNull()
})
test('steps(4, end)', () => {
expect(destructure_easing('steps(4, end)')).toBeUndefined()
expect(destructure_easing('steps(4, end)')).toBeNull()
})
test('cubic-bezier(1, var(--test), 0 0)', () => {
expect(destructure_easing('cubic-bezier(1, var(--test), 0 0)')).toBeUndefined()
expect(destructure_easing('cubic-bezier(1, var(--test), 0 0)')).toBeNull()
})
test('cubic-bezier(1, 2, 3)', () => {
expect(destructure_easing('cubic-bezier(1, 2, 3)')).toBeUndefined()
expect(destructure_easing('cubic-bezier(1, 2, 3)')).toBeNull()
})
})
6 changes: 3 additions & 3 deletions src/destructure-easing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const EASING_KEYWORDS = new Map<string, Easing>([
['ease-in-out', [0.42, 0, 0.58, 1]]
])

export function destructure_easing(easing: string): Easing | undefined {
export function destructure_easing(easing: string): Easing | null {
easing = easing.trim().toLowerCase()

if (EASING_KEYWORDS.has(easing)) {
return EASING_KEYWORDS.get(easing) as Easing
}

if (easing.includes('var(')) {
return undefined
return null
}

if (easing.startsWith('cubic-bezier(')) {
Expand All @@ -30,5 +30,5 @@ export function destructure_easing(easing: string): Easing | undefined {
}
}

return undefined
return null
}
Loading