getDimension.cjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const valueParser = require('postcss-value-parser');
  5. const blurInterpolation = require('./blurInterpolation.cjs');
  6. const isStandardSyntaxValue = require('./isStandardSyntaxValue.cjs');
  7. /**
  8. * Get Dimension from value node;
  9. * `unit` and `number` return null if neither is found
  10. *
  11. * @param {import('postcss-value-parser').Node} node
  12. *
  13. * @returns {{unit: null, number: null} | valueParser.Dimension}
  14. */
  15. function getDimension(node) {
  16. if (!node || !node.value) {
  17. return {
  18. unit: null,
  19. number: null,
  20. };
  21. }
  22. // Ignore non-word nodes
  23. if (node.type !== 'word') {
  24. return {
  25. unit: null,
  26. number: null,
  27. };
  28. }
  29. // Ignore non standard syntax
  30. if (!isStandardSyntaxValue(node.value)) {
  31. return {
  32. unit: null,
  33. number: null,
  34. };
  35. }
  36. // Ignore HEX
  37. if (node.value.startsWith('#')) {
  38. return {
  39. unit: null,
  40. number: null,
  41. };
  42. }
  43. // Remove non standard stuff
  44. const value = blurInterpolation(node.value, '')
  45. // ignore hack unit
  46. .replace('\\0', '')
  47. .replace('\\9', '');
  48. const parsedUnit = valueParser.unit(value);
  49. if (!parsedUnit) {
  50. return {
  51. unit: null,
  52. number: null,
  53. };
  54. }
  55. return parsedUnit;
  56. }
  57. module.exports = getDimension;