getUnitFromValueNode.js 942 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const blurInterpolation = require('./blurInterpolation');
  3. const isStandardSyntaxValue = require('./isStandardSyntaxValue');
  4. const valueParser = require('postcss-value-parser');
  5. /**
  6. * Get unit from value node
  7. *
  8. * Returns `null` if the unit is not found.
  9. *
  10. * @param {import('postcss-value-parser').Node} node
  11. *
  12. * @returns {string | null}
  13. */
  14. module.exports = function (node) {
  15. if (!node || !node.value) {
  16. return null;
  17. }
  18. // Ignore non-word nodes
  19. if (node.type !== 'word') {
  20. return null;
  21. }
  22. // Ignore non standard syntax
  23. if (!isStandardSyntaxValue(node.value)) {
  24. return null;
  25. }
  26. // Ignore HEX
  27. if (node.value.startsWith('#')) {
  28. return null;
  29. }
  30. // Remove non standard stuff
  31. const value = blurInterpolation(node.value, '')
  32. // ignore hack unit
  33. .replace('\\0', '')
  34. .replace('\\9', '');
  35. const parsedUnit = valueParser.unit(value);
  36. if (!parsedUnit) {
  37. return null;
  38. }
  39. return parsedUnit.unit;
  40. };