| 1 |
import camelCaseToDash from '../../utils/camel-case-to-dash'; |
| 2 |
import { maybeDecode } from '../../utils/encode-decode'; |
| 3 |
|
| 4 |
const cssPropsWithPixels = [ |
| 5 |
'border-top-width', |
| 6 |
'border-right-width', |
| 7 |
'border-bottom-width', |
| 8 |
'border-left-width', |
| 9 |
'border-width', |
| 10 |
'border-bottom-left-radius', |
| 11 |
'border-bottom-right-radius', |
| 12 |
'border-top-left-radius', |
| 13 |
'border-top-right-radius', |
| 14 |
'border-radius', |
| 15 |
'bottom', |
| 16 |
'top', |
| 17 |
'left', |
| 18 |
'right', |
| 19 |
'font-size', |
| 20 |
'height', |
| 21 |
'width', |
| 22 |
'min-height', |
| 23 |
'min-width', |
| 24 |
'max-height', |
| 25 |
'max-width', |
| 26 |
'margin-left', |
| 27 |
'margin-right', |
| 28 |
'margin-top', |
| 29 |
'margin-bottom', |
| 30 |
'margin', |
| 31 |
'padding-left', |
| 32 |
'padding-right', |
| 33 |
'padding-top', |
| 34 |
'padding-bottom', |
| 35 |
'padding', |
| 36 |
'outline-width', |
| 37 |
]; |
| 38 |
|
| 39 |
/** |
| 40 |
* Get styles from object. |
| 41 |
* |
| 42 |
* @param {Object} data - styles data. |
| 43 |
* @param {string} selector - current styles selector (useful for nested styles). |
| 44 |
* @param {boolean} escape - escape strings to save in database. |
| 45 |
* @return {string} - ready to use styles string. |
| 46 |
*/ |
| 47 |
export default function getStyles(data = {}, selector = '', escape = true) { |
| 48 |
const result = {}; |
| 49 |
let resultCSS = ''; |
| 50 |
|
| 51 |
// add styles. |
| 52 |
Object.keys(data).forEach((key) => { |
| 53 |
// object values. |
| 54 |
if (data[key] !== null && typeof data[key] === 'object') { |
| 55 |
// media for different screens |
| 56 |
if (/^media_/.test(key)) { |
| 57 |
resultCSS += `${ |
| 58 |
resultCSS ? ' ' : '' |
| 59 |
}@media #{ghostkitvar:${key}} { ${getStyles( |
| 60 |
data[key], |
| 61 |
selector, |
| 62 |
escape |
| 63 |
)} }`; |
| 64 |
|
| 65 |
// @supports css |
| 66 |
} else if (/^@supports/.test(key)) { |
| 67 |
resultCSS += `${resultCSS ? ' ' : ''}${key} { ${getStyles( |
| 68 |
data[key], |
| 69 |
selector, |
| 70 |
escape |
| 71 |
)} }`; |
| 72 |
|
| 73 |
// nested selectors. |
| 74 |
} else { |
| 75 |
let nestedSelector = selector; |
| 76 |
|
| 77 |
if (nestedSelector) { |
| 78 |
if (key.indexOf('&') !== -1) { |
| 79 |
nestedSelector = key.replace(/&/g, nestedSelector); |
| 80 |
} else { |
| 81 |
nestedSelector = `${nestedSelector} ${key}`; |
| 82 |
} |
| 83 |
} else { |
| 84 |
nestedSelector = key; |
| 85 |
} |
| 86 |
|
| 87 |
resultCSS += |
| 88 |
(resultCSS ? ' ' : '') + |
| 89 |
getStyles(data[key], nestedSelector, escape); |
| 90 |
} |
| 91 |
|
| 92 |
// style properties and values. |
| 93 |
} else if (typeof data[key] !== 'undefined' && data[key] !== false) { |
| 94 |
// fix selector > and < usage. |
| 95 |
if (escape) { |
| 96 |
selector = selector.replace(/>/g, '>'); |
| 97 |
selector = selector.replace(/</g, '<'); |
| 98 |
} |
| 99 |
|
| 100 |
if (!result[selector]) { |
| 101 |
result[selector] = ''; |
| 102 |
} |
| 103 |
|
| 104 |
const isCustom = key === 'custom'; |
| 105 |
const propName = camelCaseToDash(key); |
| 106 |
let propValue = data[key]; |
| 107 |
|
| 108 |
// Prepare custom styles as text. |
| 109 |
if (isCustom) { |
| 110 |
if ( |
| 111 |
typeof propValue !== 'undefined' && |
| 112 |
propValue !== '' && |
| 113 |
selector |
| 114 |
) { |
| 115 |
if (!result.custom) { |
| 116 |
result.custom = ''; |
| 117 |
} else { |
| 118 |
result.custom += ' '; |
| 119 |
} |
| 120 |
|
| 121 |
result.custom += `${maybeDecode(propValue).replace( |
| 122 |
/selector/g, |
| 123 |
selector |
| 124 |
)}`; |
| 125 |
} |
| 126 |
|
| 127 |
// Prepare styles from props. |
| 128 |
} else { |
| 129 |
const withImportant = / !important$/.test(propValue); |
| 130 |
if (withImportant) { |
| 131 |
propValue = propValue.replace(/ !important$/, ''); |
| 132 |
} |
| 133 |
|
| 134 |
// add pixels. |
| 135 |
if ( |
| 136 |
// Ensure that value is number, or string, which contains numbers. |
| 137 |
((typeof propValue === 'number' && propValue !== 0) || |
| 138 |
(typeof propValue === 'string' && |
| 139 |
propValue && |
| 140 |
/^[0-9.-]*$/.test(propValue))) && |
| 141 |
// Unsure that property support units. |
| 142 |
// For example, we should not add pixes to z-index. |
| 143 |
cssPropsWithPixels.includes(propName) |
| 144 |
) { |
| 145 |
propValue += 'px'; |
| 146 |
} |
| 147 |
|
| 148 |
// add custom css. |
| 149 |
if (typeof propValue !== 'undefined' && propValue !== '') { |
| 150 |
if (withImportant) { |
| 151 |
propValue += ' !important'; |
| 152 |
} |
| 153 |
|
| 154 |
result[selector] += ` ${propName}: ${propValue};`; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
}); |
| 159 |
|
| 160 |
// add styles to selectors. |
| 161 |
Object.keys(result).forEach((key) => { |
| 162 |
if (key === 'custom') { |
| 163 |
resultCSS = `${result[key]}${resultCSS ? ` ${resultCSS}` : ''}`; |
| 164 |
} else { |
| 165 |
resultCSS = `${key} {${result[key]} }${ |
| 166 |
resultCSS ? ` ${resultCSS}` : '' |
| 167 |
}`; |
| 168 |
} |
| 169 |
}); |
| 170 |
|
| 171 |
return resultCSS; |
| 172 |
} |
| 173 |
|