( /** Style object defintion. */ value: Dict, )
| 16 | |
| 17 | /** Returns a string of CSS from an object of CSS. */ |
| 18 | export function stringify( |
| 19 | /** Style object defintion. */ |
| 20 | value: Dict, |
| 21 | ) { |
| 22 | /** Set used to manage the opened and closed state of rules. */ |
| 23 | const used = new WeakSet() |
| 24 | |
| 25 | const write = ( |
| 26 | cssText: string, |
| 27 | selectors: StringifyContext['selectors'], |
| 28 | conditions: String[], |
| 29 | name: string, |
| 30 | data: string | number | boolean, |
| 31 | isAtRuleLike: boolean, |
| 32 | isVariableLike: boolean, |
| 33 | ) => { |
| 34 | if (data === false) return '' |
| 35 | |
| 36 | // Add conditions |
| 37 | for (let i = 0; i < conditions.length; ++i) { |
| 38 | if (!used.has(conditions[i])) { |
| 39 | used.add(conditions[i]) |
| 40 | cssText += `${conditions[i]} {` |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Then selectors |
| 45 | if (selectors.length && !used.has(selectors)) { |
| 46 | used.add(selectors) |
| 47 | cssText += `${selectors.map((s) => s.replace('& ', ''))} {` |
| 48 | } |
| 49 | |
| 50 | let value = data |
| 51 | |
| 52 | if (typeof value === 'number') { |
| 53 | const shouldAddPx = !(value === 0 || unitlessProperties.has(name) || isVariableLike) |
| 54 | if (shouldAddPx) { |
| 55 | value = `${value}px` |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Format property |
| 60 | if (isAtRuleLike) { |
| 61 | name = `${name} ` |
| 62 | } else if (isVariableLike) { |
| 63 | name = `${name}: ` |
| 64 | } else { |
| 65 | name = `${hypenateProperty(name)}: ` |
| 66 | } |
| 67 | |
| 68 | // Add it to the CSS string |
| 69 | cssText += `${name + String(value)};\n` |
| 70 | |
| 71 | return cssText |
| 72 | } |
| 73 | |
| 74 | const parse = (style: Dict, selectors: Array<string>, conditions: String[]) => { |
| 75 | let cssText = '' |
no test coverage detected