This commit is contained in:
2025-09-19 14:25:20 +08:00
parent 269893a435
commit fbf3f77229
24949 changed files with 2839404 additions and 0 deletions

421
node_modules/postcss-calc/src/__tests__/convertUnit.js generated vendored Normal file
View File

@@ -0,0 +1,421 @@
'use strict';
const { test } = require('uvu');
const assert = require('uvu/assert');
const convertUnit = require('../lib/convertUnit.js');
test('valid conversions', () => {
const conversions = [
// source value, source unit, expected value, target unit
[10, 'px', 10, 'px'],
[10, 'px', 0.26458, 'cm'],
[10, 'px', 2.64583, 'mm'],
[10, 'px', 10.58333, 'q'],
[10, 'px', 0.10417, 'in'],
[10, 'px', 7.5, 'pt'],
[10, 'px', 0.625, 'pc'],
[10, 'cm', 377.95276, 'px'],
[10, 'cm', 10, 'cm'],
[10, 'cm', 100, 'mm'],
[10, 'cm', 400, 'q'],
[10, 'cm', 3.93701, 'in'],
[10, 'cm', 283.46457, 'pt'],
[10, 'cm', 23.62205, 'pc'],
[10, 'mm', 37.79528, 'px'],
[10, 'mm', 1, 'cm'],
[10, 'mm', 10, 'mm'],
[10, 'mm', 40, 'q'],
[10, 'mm', 0.3937, 'in'],
[10, 'mm', 28.34646, 'pt'],
[10, 'mm', 2.3622, 'pc'],
[10, 'q', 9.44882, 'px'],
[10, 'q', 0.25, 'cm'],
[10, 'q', 2.5, 'mm'],
[10, 'q', 0.09843, 'in'],
[10, 'q', 7.08661, 'pt'],
[10, 'q', 0.59055, 'pc'],
[10, 'in', 960, 'px'],
[10, 'in', 25.4, 'cm'],
[10, 'in', 254, 'mm'],
[10, 'in', 1016, 'q'],
[10, 'in', 10, 'in'],
[10, 'in', 720, 'pt'],
[10, 'in', 60, 'pc'],
[10, 'pt', 13.33333, 'px'],
[10, 'pt', 0.35278, 'cm'],
[10, 'pt', 3.52778, 'mm'],
[10, 'pt', 14.11111, 'q'],
[10, 'pt', 0.13889, 'in'],
[10, 'pt', 10, 'pt'],
[10, 'pt', 0.83333, 'pc'],
[10, 'pc', 160, 'px'],
[10, 'pc', 4.23333, 'cm'],
[10, 'pc', 42.33333, 'mm'],
[10, 'pc', 169.33333, 'q'],
[10, 'pc', 1.66667, 'in'],
[10, 'pc', 120, 'pt'],
[10, 'pc', 10, 'pc'],
[10, 'deg', 10, 'deg'],
[10, 'deg', 11.11111, 'grad'],
[10, 'deg', 0.17453, 'rad'],
[10, 'deg', 0.02778, 'turn'],
[10, 'grad', 9, 'deg'],
[10, 'grad', 10, 'grad'],
[10, 'grad', 0.15708, 'rad'],
[10, 'grad', 0.025, 'turn'],
[10, 'rad', 572.9578, 'deg'],
[10, 'rad', 636.61977, 'grad'],
[10, 'rad', 10, 'rad'],
[10, 'rad', 1.59155, 'turn'],
[10, 'turn', 3600, 'deg'],
[10, 'turn', 4000, 'grad'],
[10, 'turn', 62.83185, 'rad'],
[10, 'turn', 10, 'turn'],
[10, 's', 10, 's'],
[10, 's', 10000, 'ms'],
[10, 'ms', 0.01, 's'],
[10, 'ms', 10, 'ms'],
[10, 'Hz', 10, 'Hz'],
[10, 'Hz', 0.01, 'kHz'],
[10, 'kHz', 10000, 'Hz'],
[10, 'kHz', 10, 'kHz'],
[10, 'dpi', 10, 'dpi'],
[10, 'dpi', 25.4, 'dpcm'],
[10, 'dpi', 960, 'dppx'],
[10, 'dpcm', 3.93701, 'dpi'],
[10, 'dpcm', 10, 'dpcm'],
[10, 'dpcm', 377.95276, 'dppx'],
[10, 'dppx', 0.10417, 'dpi'],
[10, 'dppx', 0.26458, 'dpcm'],
[10, 'dppx', 10, 'dppx'],
];
conversions.forEach(function (e) {
const value = e[0];
const unit = e[1];
const expected = e[2];
const targetUnit = e[3];
assert.is(
convertUnit(value, unit, targetUnit),
expected,
unit + ' -> ' + targetUnit
);
});
});
test('invalid conversions', () => {
const invalid_units = {
px: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
cm: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
mm: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
q: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
in: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
pt: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
pc: [
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
deg: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
grad: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
rad: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
turn: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
's',
'ms',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
s: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
ms: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
'Hz',
'kHz',
'dpi',
'dpcm',
'dppx',
],
Hz: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'dpi',
'dpcm',
'dppx',
],
kHz: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'dpi',
'dpcm',
'dppx',
],
dpi: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
],
dpcm: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
],
dppx: [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'deg',
'grad',
'rad',
'turn',
's',
'ms',
'Hz',
'kHz',
],
};
for (const unit in invalid_units) {
invalid_units[unit].forEach((targetUnit) => {
let failed = false;
try {
convertUnit(10, unit, targetUnit);
} catch (e) {
failed = true;
}
assert.ok(failed, unit + ' -> ' + targetUnit);
});
}
});
test('precision', () => {
const precision = 10;
const conversions = [
// source value, source unit, expected value, target unit
[10, 'px', 0.2645833333, 'cm'],
[10, 'px', 2.6458333333, 'mm'],
[10, 'px', 0.1041666667, 'in'],
[10, 'cm', 377.9527559055, 'px'],
];
conversions.forEach((e) => {
const value = e[0];
const unit = e[1];
const expected = e[2];
const targetUnit = e[3];
assert.is(
convertUnit(value, unit, targetUnit, precision),
expected,
unit + ' -> ' + targetUnit
);
});
});
test('falsey precision', () => {
assert.is(convertUnit(10, 'px', 'cm', false), 0.26458333333333334);
});
test.run();

903
node_modules/postcss-calc/src/__tests__/index.js generated vendored Normal file
View File

@@ -0,0 +1,903 @@
'use strict';
const { test } = require('uvu');
const assert = require('uvu/assert');
const postcss = require('postcss');
const reduceCalc = require('../index.js');
const postcssOpts = { from: undefined };
function testValue(fixture, expected, opts = {}) {
fixture = `foo{bar:${fixture}}`;
expected = `foo{bar:${expected}}`;
return async () => {
const result = await postcss(reduceCalc(opts)).process(
fixture,
postcssOpts
);
assert.is(result.css, expected);
};
}
function testCss(fixture, expected, opts = {}) {
return async () => {
const result = await postcss(reduceCalc(opts)).process(
fixture,
postcssOpts
);
assert.is(result.css, expected);
};
}
function testThrows(fixture, expected, warning, opts = {}) {
fixture = `foo{bar:${fixture}}`;
expected = `foo{bar:${expected}}`;
return async () => {
const result = await postcss(reduceCalc(opts)).process(
fixture,
postcssOpts
);
const warnings = result.warnings();
assert.is(result.css, expected);
assert.is(warnings[0].text, warning);
};
}
test('should reduce simple calc (1)', testValue('calc(1px + 1px)', '2px'));
test(
'should reduce simple calc (2)',
testValue('calc(1px + 1px);baz:calc(2px+3px)', '2px;baz:5px')
);
test('should reduce simple calc (3)', testValue('calc(1rem * 1.5)', '1.5rem'));
test('should reduce simple calc (4)', testValue('calc(3em - 1em)', '2em'));
test('should reduce simple calc (5', testValue('calc(2ex / 2)', '1ex'));
test(
'should reduce simple calc (6)',
testValue('calc(50px - (20px - 30px))', '60px')
);
test(
'should reduce simple calc (7)',
testValue('calc(100px - (100px - 100%))', '100%')
);
test(
'should reduce simple calc (8)',
testValue('calc(100px + (100px - 100%))', 'calc(200px - 100%)')
);
test(
'should reduce additions and subtractions (1)',
testValue('calc(100% - 10px + 20px)', 'calc(100% + 10px)')
);
test(
'should reduce additions and subtractions (2)',
testValue('calc(100% + 10px - 20px)', 'calc(100% - 10px)')
);
test(
'should reduce additions and subtractions (3)',
testValue('calc(1px - (2em + 3%))', 'calc(1px - 2em - 3%)')
);
test(
'should reduce additions and subtractions (4)',
testValue('calc((100vw - 50em) / 2)', 'calc(50vw - 25em)')
);
test(
'should reduce additions and subtractions (5)',
testValue('calc(10px - (100vw - 50em) / 2)', 'calc(10px - 50vw + 25em)')
);
test(
'should reduce additions and subtractions (6)',
testValue('calc(1px - (2em + 4vh + 3%))', 'calc(1px - 2em - 4vh - 3%)')
);
test(
'should reduce additions and subtractions (7)',
testValue(
'calc(0px - (24px - (var(--a) - var(--b)) / 2 + var(--c)))',
'calc(-24px + (var(--a) - var(--b))/2 - var(--c))'
)
);
test(
'should reduce additions and subtractions (8)',
testValue('calc(1px + (2em + (3vh + 4px)))', 'calc(5px + 2em + 3vh)')
);
test(
'should reduce additions and subtractions (9)',
testValue('calc(1px - (2em + 4px - 6vh) / 2)', 'calc(-1px - 1em + 3vh)')
);
test(
'should reduce multiplication',
testValue('calc(((var(--a) + 4px) * 2) * 2)', 'calc((var(--a) + 4px)*2*2)')
);
test(
'should reduce multiplication before reducing additions',
testValue(
'calc(((var(--a) + 4px) * 2) * 2 + 4px)',
'calc((var(--a) + 4px)*2*2 + 4px)'
)
);
test(
'should reduce division',
testValue('calc(((var(--a) + 4px) / 2) / 2)', 'calc((var(--a) + 4px)/2/2)')
);
test(
'should reduce division before reducing additions',
testValue(
'calc(((var(--a) + 4px) / 2) / 2 + 4px)',
'calc((var(--a) + 4px)/2/2 + 4px)'
)
);
test(
'should ignore value surrounding calc function (1)',
testValue('a calc(1px + 1px)', 'a 2px')
);
test(
'should ignore value surrounding calc function (2)',
testValue('calc(1px + 1px) a', '2px a')
);
test(
'should ignore value surrounding calc function (3)',
testValue('a calc(1px + 1px) b', 'a 2px b')
);
test(
'should ignore value surrounding calc function (4)',
testValue('a calc(1px + 1px) b calc(1em + 2em) c', 'a 2px b 3em c')
);
test(
'should reduce nested calc',
testValue('calc(100% - calc(50% + 25px))', 'calc(50% - 25px)')
);
test(
'should reduce vendor-prefixed nested calc',
testValue(
'-webkit-calc(100% - -webkit-calc(50% + 25px))',
'-webkit-calc(50% - 25px)'
)
);
test('should reduce uppercase calc (1)', testValue('CALC(1px + 1px)', '2px'));
test(
'should reduce uppercase calc (2)',
testValue('CALC(1px + CALC(2px / 2))', '2px')
);
test(
'should reduce uppercase calc (3)',
testValue('-WEBKIT-CALC(1px + 1px)', '2px')
);
test(
'should reduce uppercase calc (4)',
testValue('-WEBKIT-CALC(1px + -WEBKIT-CALC(2px / 2))', '2px')
);
test(
'should ignore calc with css variables (1)',
testValue('calc(var(--mouseX) * 1px)', 'calc(var(--mouseX)*1px)')
);
test(
'should ignore calc with css variables (2)',
testValue(
'calc(10px - (100px * var(--mouseX)))',
'calc(10px - 100px*var(--mouseX))'
)
);
test(
'should ignore calc with css variables (3)',
testValue(
'calc(10px - (100px + var(--mouseX)))',
'calc(-90px - var(--mouseX))'
)
);
test(
'should ignore calc with css variables (4)',
testValue(
'calc(10px - (100px / var(--mouseX)))',
'calc(10px - 100px/var(--mouseX))'
)
);
test(
'should ignore calc with css variables (5)',
testValue(
'calc(10px - (100px - var(--mouseX)))',
'calc(-90px + var(--mouseX))'
)
);
test(
'should ignore calc with css variables (6)',
testValue('calc(var(--popupHeight) / 2)', 'calc(var(--popupHeight)/2)')
);
test(
'should ignore calc with css variables (7)',
testValue(
'calc(var(--popupHeight) / 2 + var(--popupWidth) / 2)',
'calc(var(--popupHeight)/2 + var(--popupWidth)/2)'
)
);
test(
'should reduce calc with newline characters',
testValue('calc(\n1rem \n* 2 \n* 1.5)', '3rem')
);
test(
'should preserve calc with incompatible units',
testValue('calc(100% + 1px)', 'calc(100% + 1px)')
);
test(
'should parse fractions without leading zero',
testValue('calc(2rem - .14285em)', 'calc(2rem - 0.14285em)')
);
test('should handle precision correctly (1)', testValue('calc(1/100)', '0.01'));
test(
'should handle precision correctly (2)',
testValue('calc(5/1000000)', '0.00001')
);
test(
'should handle precision correctly (3)',
testValue('calc(5/1000000)', '0.000005', { precision: 6 })
);
test(
'should reduce browser-prefixed calc (1)',
testValue('-webkit-calc(1px + 1px)', '2px')
);
test(
'should reduce browser-prefixed calc (2)',
testValue('-moz-calc(1px + 1px)', '2px')
);
test(
'should discard zero values (#2) (1)',
testValue('calc(100vw / 2 - 6px + 0px)', 'calc(50vw - 6px)')
);
test(
'should discard zero values (#2) (2)',
testValue('calc(500px - 0px)', '500px')
);
test(
'should not perform addition on unitless values (#3)',
testValue('calc(1px + 1)', 'calc(1px + 1)')
);
test(
'should reduce consecutive substractions (#24) (1)',
testValue('calc(100% - 120px - 60px)', 'calc(100% - 180px)')
);
test(
'should reduce consecutive substractions (#24) (2)',
testValue('calc(100% - 10px - 20px)', 'calc(100% - 30px)')
);
test(
'should reduce mixed units of time (postcss-calc#33)',
testValue('calc(1s - 50ms)', '0.95s')
);
test(
'should correctly reduce calc with mixed units (cssnano#211)',
testValue('calc(99.99% * 1/1 - 0rem)', '99.99%')
);
test(
'should apply optimization (cssnano#320)',
testValue('calc(50% + (5em + 5%))', 'calc(55% + 5em)')
);
test(
'should reduce substraction from zero',
testValue('calc( 0 - 10px)', '-10px')
);
test(
'should reduce subtracted expression from zero',
testValue('calc( 0 - calc(1px + 1em) )', 'calc(-1px - 1em)')
);
test(
'should reduce substracted expression from zero (1)',
testValue('calc( 0 - (100vw - 10px) / 2 )', 'calc(-50vw + 5px)')
);
test(
'should reduce substracted expression from zero (2)',
testValue('calc( 0px - (100vw - 10px))', 'calc(10px - 100vw)')
);
test(
'should reduce substracted expression from zero (3)',
testValue('calc( 0px - (100vw - 10px) * 2 )', 'calc(20px - 200vw)')
);
test(
'should reduce substracted expression from zero (4)',
testValue('calc( 0px - (100vw + 10px))', 'calc(-10px - 100vw)')
);
test(
'should reduce substracted expression from zero (css-variable)',
testValue(
'calc( 0px - (var(--foo, 4px) / 2))',
'calc(0px - var(--foo, 4px)/2)'
)
);
test(
'should reduce nested expression',
testValue('calc( (1em - calc( 10px + 1em)) / 2)', '-5px')
);
test(
'should skip constant function',
testValue(
'calc(constant(safe-area-inset-left))',
'calc(constant(safe-area-inset-left))'
)
);
test(
'should skip env function',
testValue(
'calc(env(safe-area-inset-left))',
'calc(env(safe-area-inset-left))'
)
);
test(
'should skip env function (#1)',
testValue(
'calc(env(safe-area-inset-left, 50px 20px))',
'calc(env(safe-area-inset-left, 50px 20px))'
)
);
test(
'should skip unknown function',
testValue(
'calc(unknown(safe-area-inset-left))',
'calc(unknown(safe-area-inset-left))'
)
);
test(
'should preserve the original declaration when `preserve` option is set to true',
testCss('foo{bar:calc(1rem * 1.5)}', 'foo{bar:1.5rem;bar:calc(1rem * 1.5)}', {
preserve: true,
})
);
test(
'should not yield warnings when nothing is wrong',
testValue('calc(500px - 0px)', '500px', { warnWhenCannotResolve: true })
);
test(
'should warn when calc expression cannot be reduced to a single value',
testValue('calc(100% + 1px)', 'calc(100% + 1px)', {
warnWhenCannotResolve: true,
})
);
test(
'should reduce mixed units of time (#33)',
testValue('calc(1s - 50ms)', '0.95s')
);
test(
'should not parse variables as calc expressions (#35)',
testCss(
'foo:nth-child(2n + $var-calc){}',
'foo:nth-child(2n + $var-calc){}',
{ selectors: true }
)
);
test(
'should apply algebraic reduction (cssnano#319)',
testValue('calc((100px - 1em) + (-50px + 1em))', '50px')
);
test(
'should discard zero values (reduce-css-calc#2) (1)',
testValue('calc(100vw / 2 - 6px + 0px)', 'calc(50vw - 6px)')
);
test(
'should discard zero values (reduce-css-calc#2) (2)',
testValue('calc(500px - 0px)', '500px')
);
test(
'should not perform addition on unitless values (reduce-css-calc#3)',
testValue('calc(1px + 1)', 'calc(1px + 1)')
);
test(
'should return the same and not thrown an exception for attribute selectors without a value',
testCss('button[disabled]{}', 'button[disabled]{}', { selectors: true })
);
test(
'should ignore reducing custom property',
testCss(
':root { --foo: calc(var(--bar) / 8); }',
':root { --foo: calc(var(--bar)/8); }'
)
);
test(
'should ignore media queries',
testCss(
'@media (min-width:calc(10px+10px)){}',
'@media (min-width:calc(10px+10px)){}'
)
);
test(
'should reduce calc in media queries when `mediaQueries` option is set to true',
testCss('@media (min-width:calc(10px+10px)){}', '@media (min-width:20px){}', {
mediaQueries: true,
})
);
test(
'should ignore selectors (1)',
testCss('div[data-size="calc(3*3)"]{}', 'div[data-size="calc(3*3)"]{}')
);
test(
'should ignore selectors (2)',
testCss('div:nth-child(2n + calc(3*3)){}', 'div:nth-child(2n + calc(3*3)){}')
);
test(
'should reduce calc in selectors when `selectors` option is set to true (1)',
testCss('div[data-size="calc(3*3)"]{}', 'div[data-size="9"]{}', {
selectors: true,
})
);
test(
'should reduce calc in selectors when `selectors` option is set to true (2)',
testCss('div:nth-child(2n + calc(3*3)){}', 'div:nth-child(2n + 9){}', {
selectors: true,
})
);
test(
'should not reduce 100% to 1 (reduce-css-calc#44)',
testCss(
'.@supports (width:calc(100% - constant(safe-area-inset-left))){.a{width:calc(100% - constant(safe-area-inset-left))}}',
'.@supports (width:calc(100% - constant(safe-area-inset-left))){.a{width:calc(100% - constant(safe-area-inset-left))}}'
)
);
test(
'should not break css variables that have "calc" in their names',
testCss(
'a{transform: translateY(calc(-100% - var(--tooltip-calculated-offset)))}',
'a{transform: translateY(calc(-100% - var(--tooltip-calculated-offset)))}'
)
);
test(
'should handle complex calculations (reduce-css-calc#45) (1)',
testValue(
'calc(100% + (2 * 100px) - ((75.37% - 63.5px) - 900px))',
'calc(24.63% + 1163.5px)'
)
);
test(
'should handle complex calculations (reduce-css-calc#45) (2)',
testValue(
'calc(((((100% + (2 * 30px) + 63.5px) / 0.7537) - (100vw - 60px)) / 2) + 30px)',
'calc(66.33939% + 141.92915px - 50vw)'
)
);
test(
'should handle advanced arithmetic (1)',
testValue(
'calc(((75.37% - 63.5px) - 900px) + (2 * 100px))',
'calc(75.37% - 763.5px)'
)
);
test(
'should handle advanced arithmetic (2)',
testValue(
'calc((900px - (10% - 63.5px)) + (2 * 100px))',
'calc(1163.5px - 10%)'
)
);
test(
'should handle nested calc statements (reduce-css-calc#49)',
testValue('calc(calc(2.25rem + 2px) - 1px * 2)', '2.25rem')
);
test(
'should throw an exception when attempting to divide by zero',
testThrows('calc(500px/0)', 'calc(500px/0)', 'Cannot divide by zero')
);
test(
'should throw an exception when attempting to divide by unit (#1)',
testThrows(
'calc(500px/2px)',
'calc(500px/2px)',
'Cannot divide by "px", number expected'
)
);
test(
'nested var (reduce-css-calc#50)',
testValue(
'calc(var(--xxx, var(--yyy)) / 2)',
'calc(var(--xxx, var(--yyy))/2)'
)
);
test(
'should not throw an exception when unknow function exist in calc',
testValue(
'calc(unknown(#fff) - other-unknown(200px))',
'calc(unknown(#fff) - other-unknown(200px))'
)
);
test(
'should not throw an exception when unknow function exist in calc (#1)',
testValue(
'calc(unknown(#fff) * other-unknown(200px))',
'calc(unknown(#fff)*other-unknown(200px))'
)
);
test(
'should not strip calc with single CSS custom variable',
testValue('calc(var(--foo))', 'calc(var(--foo))')
);
test(
'should strip unnecessary calc with single CSS custom variable',
testValue('calc(calc(var(--foo)))', 'calc(var(--foo))')
);
test(
'should not strip calc with single CSS custom variables and value',
testValue('calc(var(--foo) + 10px)', 'calc(var(--foo) + 10px)')
);
test('should reduce calc (uppercase)', testValue('CALC(1PX + 1PX)', '2PX'));
test(
'should reduce calc (uppercase) (#1)',
testValue('CALC(VAR(--foo) + VAR(--bar))', 'CALC(VAR(--foo) + VAR(--bar))')
);
test(
'should reduce calc (uppercase) (#2)',
testValue('CALC( (1EM - CALC( 10PX + 1EM)) / 2)', '-5PX')
);
test(
'should handle nested calc function (#1)',
testValue(
'calc(calc(var(--foo) + var(--bar)) + var(--baz))',
'calc(var(--foo) + var(--bar) + var(--baz))'
)
);
test(
'should handle nested calc function (#2)',
testValue(
'calc(var(--foo) + calc(var(--bar) + var(--baz)))',
'calc(var(--foo) + var(--bar) + var(--baz))'
)
);
test(
'should handle nested calc function (#3)',
testValue(
'calc(calc(var(--foo) - var(--bar)) - var(--baz))',
'calc(var(--foo) - var(--bar) - var(--baz))'
)
);
test(
'should handle nested calc function (#4)',
testValue(
'calc(var(--foo) - calc(var(--bar) - var(--baz)))',
'calc(var(--foo) - var(--bar) + var(--baz))'
)
);
test(
'should handle nested calc function (#5)',
testValue(
'calc(calc(var(--foo) + var(--bar)) - var(--baz))',
'calc(var(--foo) + var(--bar) - var(--baz))'
)
);
test(
'should handle nested calc function (#6)',
testValue(
'calc(var(--foo) + calc(var(--bar) - var(--baz)))',
'calc(var(--foo) + var(--bar) - var(--baz))'
)
);
test(
'should handle nested calc function (#7)',
testValue(
'calc(calc(var(--foo) - var(--bar)) + var(--baz))',
'calc(var(--foo) - var(--bar) + var(--baz))'
)
);
test(
'should handle nested calc function (#8)',
testValue(
'calc(var(--foo) - calc(var(--bar) + var(--baz)))',
'calc(var(--foo) - var(--bar) - var(--baz))'
)
);
test(
'should handle nested calc function (#9)',
testValue(
'calc(calc(var(--foo) + var(--bar)) * var(--baz))',
'calc((var(--foo) + var(--bar))*var(--baz))'
)
);
test(
'should handle nested calc function (#10)',
testValue(
'calc(var(--foo) * calc(var(--bar) + var(--baz)))',
'calc(var(--foo)*(var(--bar) + var(--baz)))'
)
);
test(
'should handle nested calc function (#11)',
testValue(
'calc(calc(var(--foo) + var(--bar)) / var(--baz))',
'calc((var(--foo) + var(--bar))/var(--baz))'
)
);
test(
'should handle nested calc function (#12)',
testValue(
'calc(var(--foo) / calc(var(--bar) + var(--baz)))',
'calc(var(--foo)/(var(--bar) + var(--baz)))'
)
);
test(
'should handle nested calc function (#13)',
testValue(
'calc(100vh - 5rem - calc(10rem + 100px))',
'calc(100vh - 15rem - 100px)'
)
);
test(
'should handle nested calc function (#14)',
testValue('calc(100% - calc(10px + 2vw))', 'calc(100% - 10px - 2vw)')
);
test(
'should handle nested calc function (#15)',
testValue('calc(100% - calc(10px - 2vw))', 'calc(100% - 10px + 2vw)')
);
test(
'should preserve division precedence',
testValue(
'calc(100%/(var(--aspect-ratio)))',
'calc(100%/(var(--aspect-ratio)))'
)
);
test(
'should preserve division precedence (2)',
testValue(
`calc(
(var(--fluid-screen) - ((var(--fluid-min-width) / 16) * 1rem)) /
((var(--fluid-max-width) / 16) - (var(--fluid-min-width) / 16))
)`,
'calc((var(--fluid-screen) - ((var(--fluid-min-width)/16)*1rem))/(var(--fluid-max-width)/16 - var(--fluid-min-width)/16))'
)
);
test(
'should preserve division precedence (3)',
testValue('calc(1/(10/var(--dot-size)))', 'calc(1/(10/var(--dot-size)))')
);
test(
'should correctly preserve parentheses',
testValue(
'calc(1/((var(--a) - var(--b))/16))',
'calc(1/(var(--a) - var(--b))/16)'
)
);
test('precision for calc', testValue('calc(100% / 3 * 3)', '100%'));
test(
'precision for nested calc',
testValue('calc(calc(100% / 3) * 3)', '100%')
);
test('plus sign', testValue('calc(+100px + +100px)', '200px'));
test('plus sign (#1)', testValue('calc(+100px - +100px)', '0px'));
test('plus sign (#2)', testValue('calc(200px * +1)', '200px'));
test('plus sign (#3)', testValue('calc(200px / +1)', '200px'));
test('minus sign', testValue('calc(-100px + -100px)', '-200px'));
test('minus sign (#2)', testValue('calc(-100px - -100px)', '0px'));
test('minus sign (#3)', testValue('calc(200px * -1)', '-200px'));
test('minus sign (#4)', testValue('calc(200px / -1)', '-200px'));
test('whitespace', testValue('calc( 100px + 100px )', '200px'));
test('whitespace (#1)', testValue('calc(\t100px\t+\t100px\t)', '200px'));
test('whitespace (#2)', testValue('calc(\n100px\n+\n100px\n)', '200px'));
test(
'whitespace (#4)',
testValue('calc(\r\n100px\r\n+\r\n100px\r\n)', '200px')
);
test(
'comments',
testValue('calc(/*test*/100px/*test*/ + /*test*/100px/*test*/)', '200px')
);
test(
'comments (#1)',
testValue('calc(/*test*/100px/*test*/*/*test*/2/*test*/)', '200px')
);
test(
'comments nested',
testValue(
'calc(/*test*/100px + calc(/*test*/100px/*test*/ + /*test*/100px/*test*/))',
'300px'
)
);
test('exponent composed', testValue('calc(1.1e+1px + 1.1e+1px)', '22px'));
test('exponent composed (#1)', testValue('calc(10e+1px + 10e+1px)', '200px'));
test(
'exponent composed (#2)',
testValue('calc(1.1e+10px + 1.1e+10px)', '22000000000px')
);
test('exponent composed (#3)', testValue('calc(9e+1 * 1px)', '90px'));
test('exponent composed (#4)', testValue('calc(9e+1% + 10%)', '100%'));
test(
'exponent composed (uppercase)',
testValue('calc(1.1E+1px + 1.1E+1px)', '22px')
);
test('convert units', testValue('calc(1cm + 1px)', '1.02646cm'));
test('convert units (#1)', testValue('calc(1px + 1cm)', '38.79528px'));
test('convert units (#2)', testValue('calc(10Q + 10Q)', '20Q'));
test('convert units (#3)', testValue('calc(100.9q + 10px)', '111.48333q'));
test('convert units (#4)', testValue('calc(10px + 100.9q)', '105.33858px'));
test('convert units (#5)', testValue('calc(10cm + 1px)', '10.02646cm'));
test('convert units (#6)', testValue('calc(10mm + 1px)', '10.26458mm'));
test('convert units (#7)', testValue('calc(10px + 1q)', '10.94488px'));
test('convert units (#8)', testValue('calc(10cm + 1q)', '10.025cm'));
test('convert units (#9)', testValue('calc(10mm + 1q)', '10.25mm'));
test('convert units (#10)', testValue('calc(10in + 1q)', '10.00984in'));
test('convert units (#11)', testValue('calc(10pt + 1q)', '10.70866pt'));
test('convert units (#12)', testValue('calc(10pc + 1q)', '10.05906pc'));
test('convert units (#13)', testValue('calc(1q + 10px)', '11.58333q'));
test('convert units (#14)', testValue('calc(1q + 10cm)', '401q'));
test('convert units (#15)', testValue('calc(1q + 10mm)', '41q'));
test('convert units (#16)', testValue('calc(1q + 10in)', '1017q'));
test('convert units (#17)', testValue('calc(1q + 10pt)', '15.11111q'));
test('convert units (#18)', testValue('calc(1q + 10pc)', '170.33333q'));
test(
'unknown units',
testValue('calc(1unknown + 2unknown)', 'calc(1unknown + 2unknown)')
);
test(
'unknown units with known',
testValue('calc(1unknown + 2px)', 'calc(1unknown + 2px)')
);
test(
'unknown units with known (#1)',
testValue('calc(1px + 2unknown)', 'calc(1px + 2unknown)')
);
test(
'error with parsing',
testThrows(
'calc(10pc + unknown)',
'calc(10pc + unknown)',
'Lexical error on line 1: Unrecognized text.\n\n Erroneous area:\n1: 10pc + unknown\n^.........^'
)
);
test.run();

51
node_modules/postcss-calc/src/index.js generated vendored Normal file
View File

@@ -0,0 +1,51 @@
'use strict';
const transform = require('./lib/transform.js');
/**
* @typedef {{precision?: number | false,
* preserve?: boolean,
* warnWhenCannotResolve?: boolean,
* mediaQueries?: boolean,
* selectors?: boolean}} PostCssCalcOptions
*/
/**
* @type {import('postcss').PluginCreator<PostCssCalcOptions>}
* @param {PostCssCalcOptions} opts
* @return {import('postcss').Plugin}
*/
function pluginCreator(opts) {
const options = Object.assign(
{
precision: 5,
preserve: false,
warnWhenCannotResolve: false,
mediaQueries: false,
selectors: false,
},
opts
);
return {
postcssPlugin: 'postcss-calc',
OnceExit(css, { result }) {
css.walk((node) => {
const { type } = node;
if (type === 'decl') {
transform(node, 'value', options, result);
}
if (type === 'atrule' && options.mediaQueries) {
transform(node, 'params', options, result);
}
if (type === 'rule' && options.selectors) {
transform(node, 'selector', options, result);
}
});
},
};
}
pluginCreator.postcss = true;
module.exports = pluginCreator;

160
node_modules/postcss-calc/src/lib/convertUnit.js generated vendored Normal file
View File

@@ -0,0 +1,160 @@
'use strict';
/**
* @type {{[key:string]: {[key:string]: number}}}
*/
const conversions = {
// Absolute length units
px: {
px: 1,
cm: 96 / 2.54,
mm: 96 / 25.4,
q: 96 / 101.6,
in: 96,
pt: 96 / 72,
pc: 16,
},
cm: {
px: 2.54 / 96,
cm: 1,
mm: 0.1,
q: 0.025,
in: 2.54,
pt: 2.54 / 72,
pc: 2.54 / 6,
},
mm: {
px: 25.4 / 96,
cm: 10,
mm: 1,
q: 0.25,
in: 25.4,
pt: 25.4 / 72,
pc: 25.4 / 6,
},
q: {
px: 101.6 / 96,
cm: 40,
mm: 4,
q: 1,
in: 101.6,
pt: 101.6 / 72,
pc: 101.6 / 6,
},
in: {
px: 1 / 96,
cm: 1 / 2.54,
mm: 1 / 25.4,
q: 1 / 101.6,
in: 1,
pt: 1 / 72,
pc: 1 / 6,
},
pt: {
px: 0.75,
cm: 72 / 2.54,
mm: 72 / 25.4,
q: 72 / 101.6,
in: 72,
pt: 1,
pc: 12,
},
pc: {
px: 0.0625,
cm: 6 / 2.54,
mm: 6 / 25.4,
q: 6 / 101.6,
in: 6,
pt: 6 / 72,
pc: 1,
},
// Angle units
deg: {
deg: 1,
grad: 0.9,
rad: 180 / Math.PI,
turn: 360,
},
grad: {
deg: 400 / 360,
grad: 1,
rad: 200 / Math.PI,
turn: 400,
},
rad: {
deg: Math.PI / 180,
grad: Math.PI / 200,
rad: 1,
turn: Math.PI * 2,
},
turn: {
deg: 1 / 360,
grad: 0.0025,
rad: 0.5 / Math.PI,
turn: 1,
},
// Duration units
s: {
s: 1,
ms: 0.001,
},
ms: {
s: 1000,
ms: 1,
},
// Frequency units
hz: {
hz: 1,
khz: 1000,
},
khz: {
hz: 0.001,
khz: 1,
},
// Resolution units
dpi: {
dpi: 1,
dpcm: 1 / 2.54,
dppx: 1 / 96,
},
dpcm: {
dpi: 2.54,
dpcm: 1,
dppx: 2.54 / 96,
},
dppx: {
dpi: 96,
dpcm: 96 / 2.54,
dppx: 1,
},
};
/**
* @param {number} value
* @param {string} sourceUnit
* @param {string} targetUnit
* @param {number|false} precision
*/
function convertUnit(value, sourceUnit, targetUnit, precision) {
const sourceUnitNormalized = sourceUnit.toLowerCase();
const targetUnitNormalized = targetUnit.toLowerCase();
if (!conversions[targetUnitNormalized]) {
throw new Error('Cannot convert to ' + targetUnit);
}
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
throw new Error('Cannot convert from ' + sourceUnit + ' to ' + targetUnit);
}
const converted =
conversions[targetUnitNormalized][sourceUnitNormalized] * value;
if (precision !== false) {
precision = Math.pow(10, Math.ceil(precision) || 5);
return Math.round(converted * precision) / precision;
}
return converted;
}
module.exports = convertUnit;

362
node_modules/postcss-calc/src/lib/reducer.js generated vendored Normal file
View File

@@ -0,0 +1,362 @@
'use strict';
const convertUnit = require('./convertUnit.js');
/**
* @param {import('../parser').CalcNode} node
* @return {node is import('../parser').ValueExpression}
*/
function isValueType(node) {
switch (node.type) {
case 'LengthValue':
case 'AngleValue':
case 'TimeValue':
case 'FrequencyValue':
case 'ResolutionValue':
case 'EmValue':
case 'ExValue':
case 'ChValue':
case 'RemValue':
case 'VhValue':
case 'VwValue':
case 'VminValue':
case 'VmaxValue':
case 'PercentageValue':
case 'Number':
return true;
}
return false;
}
/** @param {'-'|'+'} operator */
function flip(operator) {
return operator === '+' ? '-' : '+';
}
/**
* @param {string} operator
* @returns {operator is '+'|'-'}
*/
function isAddSubOperator(operator) {
return operator === '+' || operator === '-';
}
/**
* @typedef {{preOperator: '+'|'-', node: import('../parser').CalcNode}} Collectible
*/
/**
* @param {'+'|'-'} preOperator
* @param {import('../parser').CalcNode} node
* @param {Collectible[]} collected
* @param {number} precision
*/
function collectAddSubItems(preOperator, node, collected, precision) {
if (!isAddSubOperator(preOperator)) {
throw new Error(`invalid operator ${preOperator}`);
}
if (isValueType(node)) {
const itemIndex = collected.findIndex((x) => x.node.type === node.type);
if (itemIndex >= 0) {
if (node.value === 0) {
return;
}
// can cast because of the criterion used to find itemIndex
const otherValueNode = /** @type import('../parser').ValueExpression*/ (
collected[itemIndex].node
);
const { left: reducedNode, right: current } = convertNodesUnits(
otherValueNode,
node,
precision
);
if (collected[itemIndex].preOperator === '-') {
collected[itemIndex].preOperator = '+';
reducedNode.value *= -1;
}
if (preOperator === '+') {
reducedNode.value += current.value;
} else {
reducedNode.value -= current.value;
}
// make sure reducedNode.value >= 0
if (reducedNode.value >= 0) {
collected[itemIndex] = { node: reducedNode, preOperator: '+' };
} else {
reducedNode.value *= -1;
collected[itemIndex] = { node: reducedNode, preOperator: '-' };
}
} else {
// make sure node.value >= 0
if (node.value >= 0) {
collected.push({ node, preOperator });
} else {
node.value *= -1;
collected.push({ node, preOperator: flip(preOperator) });
}
}
} else if (node.type === 'MathExpression') {
if (isAddSubOperator(node.operator)) {
collectAddSubItems(preOperator, node.left, collected, precision);
const collectRightOperator =
preOperator === '-' ? flip(node.operator) : node.operator;
collectAddSubItems(
collectRightOperator,
node.right,
collected,
precision
);
} else {
// * or /
const reducedNode = reduce(node, precision);
// prevent infinite recursive call
if (
reducedNode.type !== 'MathExpression' ||
isAddSubOperator(reducedNode.operator)
) {
collectAddSubItems(preOperator, reducedNode, collected, precision);
} else {
collected.push({ node: reducedNode, preOperator });
}
}
} else if (node.type === 'ParenthesizedExpression') {
collectAddSubItems(preOperator, node.content, collected, precision);
} else {
collected.push({ node, preOperator });
}
}
/**
* @param {import('../parser').CalcNode} node
* @param {number} precision
*/
function reduceAddSubExpression(node, precision) {
/** @type Collectible[] */
const collected = [];
collectAddSubItems('+', node, collected, precision);
const withoutZeroItem = collected.filter(
(item) => !(isValueType(item.node) && item.node.value === 0)
);
const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
// prevent producing "calc(-var(--a))" or "calc()"
// which is invalid css
if (
!firstNonZeroItem ||
(firstNonZeroItem.preOperator === '-' &&
!isValueType(firstNonZeroItem.node))
) {
const firstZeroItem = collected.find(
(item) => isValueType(item.node) && item.node.value === 0
);
if (firstZeroItem) {
withoutZeroItem.unshift(firstZeroItem);
}
}
// make sure the preOperator of the first item is +
if (
withoutZeroItem[0].preOperator === '-' &&
isValueType(withoutZeroItem[0].node)
) {
withoutZeroItem[0].node.value *= -1;
withoutZeroItem[0].preOperator = '+';
}
let root = withoutZeroItem[0].node;
for (let i = 1; i < withoutZeroItem.length; i++) {
root = {
type: 'MathExpression',
operator: withoutZeroItem[i].preOperator,
left: root,
right: withoutZeroItem[i].node,
};
}
return root;
}
/**
* @param {import('../parser').MathExpression} node
*/
function reduceDivisionExpression(node) {
if (!isValueType(node.right)) {
return node;
}
if (node.right.type !== 'Number') {
throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
}
return applyNumberDivision(node.left, node.right.value);
}
/**
* apply (expr) / number
*
* @param {import('../parser').CalcNode} node
* @param {number} divisor
* @return {import('../parser').CalcNode}
*/
function applyNumberDivision(node, divisor) {
if (divisor === 0) {
throw new Error('Cannot divide by zero');
}
if (isValueType(node)) {
node.value /= divisor;
return node;
}
if (node.type === 'MathExpression' && isAddSubOperator(node.operator)) {
// turn (a + b) / num into a/num + b/num
// is good for further reduction
// checkout the test case
// "should reduce division before reducing additions"
return {
type: 'MathExpression',
operator: node.operator,
left: applyNumberDivision(node.left, divisor),
right: applyNumberDivision(node.right, divisor),
};
}
// it is impossible to reduce it into a single value
// .e.g the node contains css variable
// so we just preserve the division and let browser do it
return {
type: 'MathExpression',
operator: '/',
left: node,
right: {
type: 'Number',
value: divisor,
},
};
}
/**
* @param {import('../parser').MathExpression} node
*/
function reduceMultiplicationExpression(node) {
// (expr) * number
if (node.right.type === 'Number') {
return applyNumberMultiplication(node.left, node.right.value);
}
// number * (expr)
if (node.left.type === 'Number') {
return applyNumberMultiplication(node.right, node.left.value);
}
return node;
}
/**
* apply (expr) * number
* @param {number} multiplier
* @param {import('../parser').CalcNode} node
* @return {import('../parser').CalcNode}
*/
function applyNumberMultiplication(node, multiplier) {
if (isValueType(node)) {
node.value *= multiplier;
return node;
}
if (node.type === 'MathExpression' && isAddSubOperator(node.operator)) {
// turn (a + b) * num into a*num + b*num
// is good for further reduction
// checkout the test case
// "should reduce multiplication before reducing additions"
return {
type: 'MathExpression',
operator: node.operator,
left: applyNumberMultiplication(node.left, multiplier),
right: applyNumberMultiplication(node.right, multiplier),
};
}
// it is impossible to reduce it into a single value
// .e.g the node contains css variable
// so we just preserve the division and let browser do it
return {
type: 'MathExpression',
operator: '*',
left: node,
right: {
type: 'Number',
value: multiplier,
},
};
}
/**
* @param {import('../parser').ValueExpression} left
* @param {import('../parser').ValueExpression} right
* @param {number} precision
*/
function convertNodesUnits(left, right, precision) {
switch (left.type) {
case 'LengthValue':
case 'AngleValue':
case 'TimeValue':
case 'FrequencyValue':
case 'ResolutionValue':
if (right.type === left.type && right.unit && left.unit) {
const converted = convertUnit(
right.value,
right.unit,
left.unit,
precision
);
right = {
type: left.type,
value: converted,
unit: left.unit,
};
}
return { left, right };
default:
return { left, right };
}
}
/**
* @param {import('../parser').ParenthesizedExpression} node
*/
function includesNoCssProperties(node) {
return (
node.content.type !== 'Function' &&
(node.content.type !== 'MathExpression' ||
(node.content.right.type !== 'Function' &&
node.content.left.type !== 'Function'))
);
}
/**
* @param {import('../parser').CalcNode} node
* @param {number} precision
* @return {import('../parser').CalcNode}
*/
function reduce(node, precision) {
if (node.type === 'MathExpression') {
if (isAddSubOperator(node.operator)) {
// reduceAddSubExpression will call reduce recursively
return reduceAddSubExpression(node, precision);
}
node.left = reduce(node.left, precision);
node.right = reduce(node.right, precision);
switch (node.operator) {
case '/':
return reduceDivisionExpression(node);
case '*':
return reduceMultiplicationExpression(node);
}
return node;
}
if (node.type === 'ParenthesizedExpression') {
if (includesNoCssProperties(node)) {
return reduce(node.content, precision);
}
}
return node;
}
module.exports = reduce;

93
node_modules/postcss-calc/src/lib/stringifier.js generated vendored Normal file
View File

@@ -0,0 +1,93 @@
'use strict';
const order = {
'*': 0,
'/': 0,
'+': 1,
'-': 1,
};
/**
* @param {number} value
* @param {number | false} prec
*/
function round(value, prec) {
if (prec !== false) {
const precision = Math.pow(10, prec);
return Math.round(value * precision) / precision;
}
return value;
}
/**
* @param {number | false} prec
* @param {import('../parser').CalcNode} node
*
* @return {string}
*/
function stringify(node, prec) {
switch (node.type) {
case 'MathExpression': {
const { left, right, operator: op } = node;
let str = '';
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
str += `(${stringify(left, prec)})`;
} else {
str += stringify(left, prec);
}
str += order[op] ? ` ${node.operator} ` : node.operator;
if (
right.type === 'MathExpression' &&
order[op] < order[right.operator]
) {
str += `(${stringify(right, prec)})`;
} else {
str += stringify(right, prec);
}
return str;
}
case 'Number':
return round(node.value, prec).toString();
case 'Function':
return node.value.toString();
case 'ParenthesizedExpression':
return `(${stringify(node.content, prec)})`;
default:
return round(node.value, prec) + node.unit;
}
}
/**
* @param {string} calc
* @param {import('../parser').CalcNode} node
* @param {string} originalValue
* @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
* @param {import("postcss").Result} result
* @param {import("postcss").ChildNode} item
*
* @returns {string}
*/
module.exports = function (calc, node, originalValue, options, result, item) {
let str = stringify(node, options.precision);
const shouldPrintCalc =
node.type === 'MathExpression' || node.type === 'Function';
if (shouldPrintCalc) {
// if calc expression couldn't be resolved to a single value, re-wrap it as
// a calc()
str = `${calc}(${str})`;
// if the warnWhenCannotResolve option is on, inform the user that the calc
// expression could not be resolved to a single value
if (options.warnWhenCannotResolve) {
result.warn('Could not reduce expression: ' + originalValue, {
plugin: 'postcss-calc',
node: item,
});
}
}
return str;
};

109
node_modules/postcss-calc/src/lib/transform.js generated vendored Normal file
View File

@@ -0,0 +1,109 @@
'use strict';
const selectorParser = require('postcss-selector-parser');
const valueParser = require('postcss-value-parser');
const { parser } = require('../parser.js');
const reducer = require('./reducer.js');
const stringifier = require('./stringifier.js');
const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
/**
* @param {string} value
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
* @param {import("postcss").Result} result
* @param {import("postcss").ChildNode} item
*/
function transformValue(value, options, result, item) {
return valueParser(value)
.walk((node) => {
// skip anything which isn't a calc() function
if (node.type !== 'function' || !MATCH_CALC.test(node.value)) {
return;
}
// stringify calc expression and produce an AST
const contents = valueParser.stringify(node.nodes);
const ast = parser.parse(contents);
// reduce AST to its simplest form, that is, either to a single value
// or a simplified calc expression
const reducedAst = reducer(ast, options.precision);
// stringify AST and write it back
/** @type {valueParser.Node} */ (node).type = 'word';
node.value = stringifier(
node.value,
reducedAst,
value,
options,
result,
item
);
return false;
})
.toString();
}
/**
* @param {import("postcss-selector-parser").Selectors} value
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
* @param {import("postcss").Result} result
* @param {import("postcss").ChildNode} item
*/
function transformSelector(value, options, result, item) {
return selectorParser((selectors) => {
selectors.walk((node) => {
// attribute value
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
if (node.type === 'attribute' && node.value) {
node.setValue(transformValue(node.value, options, result, item));
}
// tag value
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
if (node.type === 'tag') {
node.value = transformValue(node.value, options, result, item);
}
return;
});
}).processSync(value);
}
/**
* @param {any} node
* @param {{precision: number, preserve: boolean, warnWhenCannotResolve: boolean}} options
* @param {'value'|'params'|'selector'} property
* @param {import("postcss").Result} result
*/
module.exports = (node, property, options, result) => {
let value = node[property];
try {
value =
property === 'selector'
? transformSelector(node[property], options, result, node)
: transformValue(node[property], options, result, node);
} catch (error) {
if (error instanceof Error) {
result.warn(error.message, { node });
} else {
result.warn('Error', { node });
}
return;
}
// if the preserve option is enabled and the value has changed, write the
// transformed value into a cloned node which is inserted before the current
// node, preserving the original value. Otherwise, overwrite the original
// value.
if (options.preserve && node[property] !== value) {
const clone = node.clone();
clone[property] = value;
node.parent.insertBefore(node, clone);
} else {
node[property] = value;
}
};

51
node_modules/postcss-calc/src/parser.d.ts generated vendored Normal file
View File

@@ -0,0 +1,51 @@
export interface MathExpression {
type: 'MathExpression';
right: CalcNode;
left: CalcNode;
operator: '*' | '+' | '-' | '/';
}
export interface ParenthesizedExpression {
type: 'ParenthesizedExpression';
content: CalcNode;
}
export interface DimensionExpression {
type:
| 'LengthValue'
| 'AngleValue'
| 'TimeValue'
| 'FrequencyValue'
| 'PercentageValue'
| 'ResolutionValue'
| 'EmValue'
| 'ExValue'
| 'ChValue'
| 'RemValue'
| 'VhValue'
| 'VwValue'
| 'VminValue'
| 'VmaxValue';
value: number;
unit: string;
}
export interface NumberExpression {
type: 'Number';
value: number;
}
export interface FunctionExpression {
type: 'Function';
value: string;
}
export type ValueExpression = DimensionExpression | NumberExpression;
export type CalcNode = MathExpression | ValueExpression | FunctionExpression | ParenthesizedExpression;
export interface Parser {
parse: (arg: string) => CalcNode;
}
export const parser: Parser;

111
node_modules/postcss-calc/src/parser.jison generated vendored Normal file
View File

@@ -0,0 +1,111 @@
/* description: Parses expressions. */
/* lexical grammar */
%lex
%options case-insensitive
%%
\s+ /* skip whitespace */
(\-(webkit|moz)\-)?calc\b return 'CALC';
[a-z][a-z0-9-]*\s*\((?:(?:\"(?:\\.|[^\"\\])*\"|\'(?:\\.|[^\'\\])*\')|\([^)]*\)|[^\(\)]*)*\) return 'FUNCTION';
"*" return 'MUL';
"/" return 'DIV';
"+" return 'ADD';
"-" return 'SUB';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)em\b return 'EMS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ex\b return 'EXS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ch\b return 'CHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rem\b return 'REMS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vw\b return 'VWS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vh\b return 'VHS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmin\b return 'VMINS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmax\b return 'VMAXS';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cm\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)mm\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Q\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)in\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pt\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pc\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)px\b return 'LENGTH';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)deg\b return 'ANGLE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)grad\b return 'ANGLE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rad\b return 'ANGLE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)turn\b return 'ANGLE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)s\b return 'TIME';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ms\b return 'TIME';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Hz\b return 'FREQ';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)kHz\b return 'FREQ';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpi\b return 'RES';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpcm\b return 'RES';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dppx\b return 'RES';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)-?([a-zA-Z_]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))([a-zA-Z0-9_-]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))*\b return 'UNKNOWN_DIMENSION';
"(" return 'LPAREN';
")" return 'RPAREN';
<<EOF>> return 'EOF';
/lex
%left ADD SUB
%left MUL DIV
%left UPREC
%start expression
%%
expression
: math_expression EOF { return $1; }
;
math_expression
: CALC LPAREN math_expression RPAREN { $$ = $3; }
| math_expression ADD math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
| math_expression SUB math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
| math_expression MUL math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
| math_expression DIV math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
| LPAREN math_expression RPAREN { $$ = { type: 'ParenthesizedExpression', content: $2 }; }
| function { $$ = $1; }
| dimension { $$ = $1; }
| number { $$ = $1; }
;
function
: FUNCTION { $$ = { type: 'Function', value: $1 }; }
;
dimension
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| FREQ { $$ = { type: 'FrequencyValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| RES { $$ = { type: 'ResolutionValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| UNKNOWN_DIMENSION { $$ = { type: 'UnknownDimension', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| EMS { $$ = { type: 'EmValue', value: parseFloat($1), unit: 'em' }; }
| EXS { $$ = { type: 'ExValue', value: parseFloat($1), unit: 'ex' }; }
| CHS { $$ = { type: 'ChValue', value: parseFloat($1), unit: 'ch' }; }
| REMS { $$ = { type: 'RemValue', value: parseFloat($1), unit: 'rem' }; }
| VHS { $$ = { type: 'VhValue', value: parseFloat($1), unit: 'vh' }; }
| VWS { $$ = { type: 'VwValue', value: parseFloat($1), unit: 'vw' }; }
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
| ADD dimension { var prev = $2; $$ = prev; }
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }
;
number
: NUMBER { $$ = { type: 'Number', value: parseFloat($1) }; }
| ADD NUMBER { $$ = { type: 'Number', value: parseFloat($2) }; }
| SUB NUMBER { $$ = { type: 'Number', value: parseFloat($2) * -1 }; }
;

3808
node_modules/postcss-calc/src/parser.js generated vendored Normal file

File diff suppressed because it is too large Load Diff