valid-calc.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // Less Functions
  3. //
  4. functions.add('add', function (value1, value2, returnCalc) {
  5. // If both values are numbers or dimensions and the sum of the two values can already be computed
  6. // then output the computed result instead of a `calc()`.
  7. //
  8. // > Note: We use the `compare()` method here as a shortcut to determine whether both values have
  9. // either the same unit, no unit, or at least one has no unit.
  10. if (value1.compare && value1.compare(value2) !== undefined)
  11. return value1.operate(this.context, '+', value2)
  12. const expression = `${value1.toCSS()} + ${value2.toCSS()}`
  13. if (returnCalc && returnCalc.value === 'false')
  14. return new tree.Quoted('', expression)
  15. return new tree.Quoted('', `calc(${expression})`)
  16. })
  17. functions.add('subtract', function (value1, value2, returnCalc) {
  18. // If both values are numbers or dimensions and the difference of the two values can already be
  19. // computed then output the computed result instead of a `calc()`.
  20. //
  21. // > Note: We use the `compare()` method here as a shortcut to determine whether both values have
  22. // either the same unit, no unit, or at least one has no unit.
  23. if (value1.compare && value1.compare(value2) !== undefined)
  24. return value1.operate(this.context, '+', value2)
  25. const expression = `${value1.toCSS()} - ${value2.toCSS()}`
  26. if (returnCalc && returnCalc.value === 'false')
  27. return new tree.Quoted('', expression)
  28. return new tree.Quoted('', `calc(${expression})`)
  29. })