UNPKG

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