-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstringify.ts
More file actions
164 lines (138 loc) · 4.58 KB
/
stringify.ts
File metadata and controls
164 lines (138 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { kebabCase } from 'scule'
/**
* Forked from https://github.com/stitchesjs/stitches/blob/canary/packages/stringify
* Authors:
* - [Pedro Duarte](https://twitter.com/peduarte)
* - [Jonathan Neal](https://twitter.com/jon_neal)
* - [Abdulhadi Alhallak](https://twitter.com/hadi_hlk)
*
* I had to fork this part to have control over it and add special integrations
* for Vue like `v-bind()`, `:global`, and other `$` prefix.
*/
/** Comma matcher outside rounded brackets. */
const comma = /\s*,\s*(?![^()]*\))/
/** Returns selectors resolved from parent selectors and nested selectors. */
export function getResolvedSelectors(parentSelectors,
/** @type {string[]} Nested selectors (e.g. `["&:hover", "&:focus"]`). */
nestedSelectors) {
return parentSelectors.reduce(
(resolvedSelectors, parentSelector) => {
resolvedSelectors.push(
...nestedSelectors.map(
selector => (
selector.includes('&')
? selector.replace(
/&/g,
(/[ +>|~]/.test(parentSelector) && /&.*&/.test(selector))
? `:is(${parentSelector})`
: parentSelector,
)
: `${parentSelector} ${selector}`
),
),
)
return resolvedSelectors
},
[],
)
}
/* Grab object prototype to compare in the loop */
const { prototype: { toString } } = Object
/** Returns a string of CSS from an object of CSS. */
export function stringify(value,
/** Replacer function. */
replacer = undefined) {
/** Set used to manage the opened and closed state of rules. */
const used = new WeakSet()
const write = (cssText, selectors, conditions, name, data, isAtRuleLike, isVariableLike) => {
for (let i = 0; i < conditions.length; ++i) {
if (!used.has(conditions[i])) {
used.add(conditions[i])
cssText += `${conditions[i]}{`
}
}
if (selectors.length && !used.has(selectors)) {
used.add(selectors)
cssText += `${selectors}{`
}
if (isAtRuleLike) {
name = `${name} `
}
else if (isVariableLike) {
name = `${name}:`
}
else {
name = `${kebabCase(name)}:`
}
cssText += `${name + String(data)};`
return cssText
}
const parse = (style, selectors, conditions, prevName?, prevData?) => {
let cssText = ''
for (const name in style) {
const isAtRuleLike = name.charCodeAt(0) === 64
const isVariableLike = (name.charCodeAt(0) === 45 && name.charCodeAt(1) === 45)
for (const data of (isAtRuleLike && Array.isArray(style[name])) ? style[name] : [style[name]]) {
if (replacer && (name !== prevName || data !== prevData)) {
const next = replacer(name, data, style, selectors)
if (next !== null) {
cssText += (typeof next === 'object' && next) ? parse(next, selectors, conditions, name, data) : next == null ? '' : next
continue
}
}
const isObjectLike = typeof data === 'object' && data && data.toString === toString
if (isObjectLike) {
if (used.has(selectors)) {
used.delete(selectors)
cssText += '}'
}
const usedName = Object(name)
let nextSelectors
if (isAtRuleLike) {
nextSelectors = selectors
cssText += parse(
data,
nextSelectors,
conditions.concat(usedName),
)
}
else {
nextSelectors = selectors.length
? getResolvedSelectors(selectors, name.split(comma))
: name.split(comma)
cssText += parse(
data,
nextSelectors,
conditions,
)
}
if (used.has(usedName)) {
used.delete(usedName)
cssText += '}'
}
if (used.has(nextSelectors)) {
used.delete(nextSelectors)
cssText += '}'
}
}
else {
cssText = write(cssText, selectors, conditions, name, data, isAtRuleLike, isVariableLike)
}
}
}
return cssText
}
return parse(value, [], [])
}
export function stringifyKeyFrames(value) {
if (!value) { return '' }
let cssText = ''
const { keyFrameDeclaration, keyFrameName } = value
const animationKey = keyFrameName
const animateRule: string[] = []
Object.entries(keyFrameDeclaration).forEach(([key, content]) => {
animateRule.push(`${key} ${JSON.stringify(content).replace(/'|"/g, '')}`)
})
cssText = `@keyframes ${animationKey} { ${animateRule.join(' ')} } `
return cssText || ''
}