| 1 | 'use strict';
|
|---|
| 2 | const { unit } = require('postcss-value-parser');
|
|---|
| 3 | const { getArguments } = require('cssnano-utils');
|
|---|
| 4 | const addSpace = require('../lib/addSpace');
|
|---|
| 5 | const getValue = require('../lib/getValue');
|
|---|
| 6 | const mathFunctions = require('../lib/mathfunctions.js');
|
|---|
| 7 | const vendorUnprefixed = require('../lib/vendorUnprefixed.js');
|
|---|
| 8 |
|
|---|
| 9 | // box-shadow: inset? && <length>{2,4} && <color>?
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @param {import('postcss-value-parser').ParsedValue} parsed
|
|---|
| 13 | * @return {string}
|
|---|
| 14 | */
|
|---|
| 15 | module.exports = function normalizeBoxShadow(parsed) {
|
|---|
| 16 | let args = getArguments(parsed);
|
|---|
| 17 |
|
|---|
| 18 | const normalized = normalize(args);
|
|---|
| 19 |
|
|---|
| 20 | if (normalized === false) {
|
|---|
| 21 | return parsed.toString();
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | return getValue(normalized);
|
|---|
| 25 | };
|
|---|
| 26 | /**
|
|---|
| 27 | * @param {import('postcss-value-parser').Node[][]} args
|
|---|
| 28 | * @return {false | import('postcss-value-parser').Node[][]}
|
|---|
| 29 | */
|
|---|
| 30 | function normalize(args) {
|
|---|
| 31 | const list = [];
|
|---|
| 32 | let abort = false;
|
|---|
| 33 | for (const arg of args) {
|
|---|
| 34 | /** @type {import('postcss-value-parser').Node[]} */
|
|---|
| 35 | let val = [];
|
|---|
| 36 | /** @type {Record<'inset'|'color', import('postcss-value-parser').Node[]>} */
|
|---|
| 37 | let state = {
|
|---|
| 38 | inset: [],
|
|---|
| 39 | color: [],
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | arg.forEach((node) => {
|
|---|
| 43 | const { type, value } = node;
|
|---|
| 44 |
|
|---|
| 45 | if (
|
|---|
| 46 | type === 'function' &&
|
|---|
| 47 | mathFunctions.has(vendorUnprefixed(value.toLowerCase()))
|
|---|
| 48 | ) {
|
|---|
| 49 | abort = true;
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (type === 'space') {
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (unit(value)) {
|
|---|
| 58 | val = [...val, node, addSpace()];
|
|---|
| 59 | } else if (value.toLowerCase() === 'inset') {
|
|---|
| 60 | state.inset = [...state.inset, node, addSpace()];
|
|---|
| 61 | } else {
|
|---|
| 62 | state.color = [...state.color, node, addSpace()];
|
|---|
| 63 | }
|
|---|
| 64 | });
|
|---|
| 65 |
|
|---|
| 66 | if (abort) {
|
|---|
| 67 | return false;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | list.push([...state.inset, ...val, ...state.color]);
|
|---|
| 71 | }
|
|---|
| 72 | return list;
|
|---|
| 73 | }
|
|---|