-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathunitless-zero.js
More file actions
77 lines (66 loc) · 1.82 KB
/
unitless-zero.js
File metadata and controls
77 lines (66 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
module.exports = {
name: 'unitless-zero',
syntax: ['css', 'less', 'sass', 'scss'],
accepts: {
boolean: [true]
},
/**
* Processes tree node.
*
* @param {node} ast
*/
process: function(ast) {
var UNITS = ['cm', 'em', 'ex', 'pt', 'px'];
ast.traverseByTypes(['value', 'parentheses'], function(node) {
node.forEach(function(value) {
if (typeof value === 'string') return;
if (value.is('dimension')) {
var unit = value.first('ident').content;
if (value.first('number').content === '0' &&
UNITS.indexOf(unit) !== -1) {
value.removeChild(1);
}
} else if (value.is('percentage')) {
var number = value.first('number').content;
if (number === '0') {
value.type = 'number';
value.content = number;
}
}
});
});
},
/**
* Detects the value of an option at the tree node.
*
* @param {node} ast
*/
detect: function(ast) {
let detected = [];
ast.traverse(function(node, index, parent) {
// If we see a zero with unit and it is not degree,
// then we don’t have an option
if (node.is('percentage') &&
node.first('number').content[1] === '0') {
detected.push(false);
return;
}
if (node.is('dimension') &&
node.first('number').content === '0' &&
node.first('ident').content !== 'deg') {
detected.push(false);
return;
}
// If we see a zero and previous node is not percentage
// or dimension, then we have an option
if (node.is('number') &&
node.content === '0' &&
!parent.is('percentage') &&
!parent.is('dimension')) {
detected.push(true);
}
});
return detected;
}
};