Skip to content

Commit 95b2494

Browse files
author
Brad Cornes
committed
support css properties with multiple values
1 parent e39c398 commit 95b2494

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

packages/tailwindcss-class-names/src/extractClassNames.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ async function process(ast) {
5555

5656
const decls = {}
5757
rule.walkDecls((decl) => {
58-
decls[decl.prop] = decl.value
58+
if (decls[decl.prop]) {
59+
decls[decl.prop] = [
60+
...(Array.isArray(decls[decl.prop])
61+
? decls[decl.prop]
62+
: [decls[decl.prop]]),
63+
decl.value,
64+
]
65+
} else {
66+
decls[decl.prop] = decl.value
67+
}
5968
})
6069

6170
let p = rule

packages/tailwindcss-class-names/tests/extractClassNames.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,24 @@ test('processes multiple scopes for the same class name', async () => {
350350
},
351351
})
352352
})
353+
354+
test('processes multiple properties of the same name', async () => {
355+
const result = await processCss(`
356+
.bg-red {
357+
background-color: blue;
358+
background-color: red;
359+
}
360+
`)
361+
362+
expect(result).toEqual({
363+
context: {},
364+
classNames: {
365+
'bg-red': {
366+
__rule: true,
367+
__context: [],
368+
__scope: null,
369+
'background-color': ['blue', 'red'],
370+
},
371+
},
372+
})
373+
})

packages/tailwindcss-language-server/src/providers/completionProvider.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { isJsContext } from '../util/js'
2323
import { naturalExpand } from '../util/naturalExpand'
2424
import semver from 'semver'
2525
import { docsUrl } from '../util/docsUrl'
26+
import { ensureArray } from '../util/array'
2627

2728
function completionsFromClassList(
2829
state: State,
@@ -725,10 +726,19 @@ function isContextItem(state: State, keys: string[]): boolean {
725726
}
726727

727728
function stringifyDecls(obj: any): string {
728-
return Object.keys(obj)
729-
.map((prop) => {
730-
return `${prop}: ${obj[prop]};`
731-
})
729+
let props = Object.keys(obj)
730+
let nonCustomProps = props.filter((prop) => !prop.startsWith('--'))
731+
732+
if (props.length !== nonCustomProps.length && nonCustomProps.length !== 0) {
733+
props = nonCustomProps
734+
}
735+
736+
return props
737+
.map((prop) =>
738+
ensureArray(obj[prop])
739+
.map((value) => `${prop}: ${value};`)
740+
.join(' ')
741+
)
732742
.join(' ')
733743
}
734744

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function dedupe<T>(arr: Array<T>): Array<T> {
2+
return arr.filter((value, index, self) => self.indexOf(value) === index)
3+
}
4+
5+
export function ensureArray<T>(value: T | T[]): T[] {
6+
return Array.isArray(value) ? value : [value]
7+
}
8+
9+
export function flatten<T>(arrays: T[][]): T[] {
10+
return [].concat.apply([], arrays)
11+
}

packages/tailwindcss-language-server/src/util/stringify.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import removeMeta from './removeMeta'
22
const dlv = require('dlv')
33
import escapeClassName from 'css.escape'
4+
import { ensureArray } from './array'
45

56
export function stringifyConfigValue(x: any): string {
67
if (typeof x === 'string') return x
@@ -35,9 +36,10 @@ export function stringifyCss(className: string, obj: any): string {
3536

3637
const indentStr = '\t'.repeat(context.length)
3738
const decls = props.reduce((acc, curr, i) => {
38-
return `${acc}${i === 0 ? '' : '\n'}${indentStr + '\t'}${curr}: ${
39-
obj[curr]
40-
};`
39+
const propStr = ensureArray(obj[curr])
40+
.map((val) => `${indentStr + '\t'}${curr}: ${val};`)
41+
.join('\n')
42+
return `${acc}${i === 0 ? '' : '\n'}${propStr}`
4143
}, '')
4244
css += `${indentStr}${augmentClassName(
4345
className,

0 commit comments

Comments
 (0)