| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- '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();
|