| 1 | let camelcase = require('camelcase-css')
|
|---|
| 2 |
|
|---|
| 3 | let UNITLESS = {
|
|---|
| 4 | boxFlex: true,
|
|---|
| 5 | boxFlexGroup: true,
|
|---|
| 6 | columnCount: true,
|
|---|
| 7 | flex: true,
|
|---|
| 8 | flexGrow: true,
|
|---|
| 9 | flexPositive: true,
|
|---|
| 10 | flexShrink: true,
|
|---|
| 11 | flexNegative: true,
|
|---|
| 12 | fontWeight: true,
|
|---|
| 13 | lineClamp: true,
|
|---|
| 14 | lineHeight: true,
|
|---|
| 15 | opacity: true,
|
|---|
| 16 | order: true,
|
|---|
| 17 | orphans: true,
|
|---|
| 18 | tabSize: true,
|
|---|
| 19 | widows: true,
|
|---|
| 20 | zIndex: true,
|
|---|
| 21 | zoom: true,
|
|---|
| 22 | fillOpacity: true,
|
|---|
| 23 | strokeDashoffset: true,
|
|---|
| 24 | strokeOpacity: true,
|
|---|
| 25 | strokeWidth: true
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | function atRule(node) {
|
|---|
| 29 | if (typeof node.nodes === 'undefined') {
|
|---|
| 30 | return true
|
|---|
| 31 | } else {
|
|---|
| 32 | return process(node)
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | function process(node, options = {}) {
|
|---|
| 37 | let name
|
|---|
| 38 | let result = {}
|
|---|
| 39 | let { stringifyImportant } = options;
|
|---|
| 40 |
|
|---|
| 41 | node.each(child => {
|
|---|
| 42 | if (child.type === 'atrule') {
|
|---|
| 43 | name = '@' + child.name
|
|---|
| 44 | if (child.params) name += ' ' + child.params
|
|---|
| 45 | if (typeof result[name] === 'undefined') {
|
|---|
| 46 | result[name] = atRule(child)
|
|---|
| 47 | } else if (Array.isArray(result[name])) {
|
|---|
| 48 | result[name].push(atRule(child))
|
|---|
| 49 | } else {
|
|---|
| 50 | result[name] = [result[name], atRule(child)]
|
|---|
| 51 | }
|
|---|
| 52 | } else if (child.type === 'rule') {
|
|---|
| 53 | let body = process(child)
|
|---|
| 54 | if (result[child.selector]) {
|
|---|
| 55 | for (let i in body) {
|
|---|
| 56 | let object = result[child.selector];
|
|---|
| 57 | if (stringifyImportant && object[i] && object[i].endsWith('!important')) {
|
|---|
| 58 | if (body[i].endsWith('!important')) {
|
|---|
| 59 | object[i] = body[i]
|
|---|
| 60 | }
|
|---|
| 61 | } else {
|
|---|
| 62 | object[i] = body[i]
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | } else {
|
|---|
| 66 | result[child.selector] = body
|
|---|
| 67 | }
|
|---|
| 68 | } else if (child.type === 'decl') {
|
|---|
| 69 | if (child.prop[0] === '-' && child.prop[1] === '-') {
|
|---|
| 70 | name = child.prop
|
|---|
| 71 | } else if (child.parent && child.parent.selector === ':export') {
|
|---|
| 72 | name = child.prop
|
|---|
| 73 | } else {
|
|---|
| 74 | name = camelcase(child.prop)
|
|---|
| 75 | }
|
|---|
| 76 | let value = child.value
|
|---|
| 77 | if (!isNaN(child.value) && UNITLESS[name]) {
|
|---|
| 78 | value = parseFloat(child.value)
|
|---|
| 79 | }
|
|---|
| 80 | if (child.important) value += ' !important'
|
|---|
| 81 | if (typeof result[name] === 'undefined') {
|
|---|
| 82 | result[name] = value
|
|---|
| 83 | } else if (Array.isArray(result[name])) {
|
|---|
| 84 | result[name].push(value)
|
|---|
| 85 | } else {
|
|---|
| 86 | result[name] = [result[name], value]
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | })
|
|---|
| 90 | return result
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | module.exports = process
|
|---|