Skip to content

Commit f8f485c

Browse files
tonyganchhaoqunjiang
authored andcommitted
Hotfix for #389
1 parent b3d0ff1 commit f8f485c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

lib/options/unitless-zero.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module.exports = {
2+
name: 'unitless-zero',
3+
4+
syntax: ['css', 'less', 'sass', 'scss'],
5+
6+
accepts: { boolean: [true] },
7+
8+
/**
9+
* Processes tree node.
10+
*
11+
* @param {node} node
12+
*/
13+
process: function(node) {
14+
var UNITS = ['cm', 'em', 'ex', 'pt', 'px'];
15+
16+
if (!node.is('value') && !node.is('braces')) return;
17+
18+
node.forEach(function(value) {
19+
if (typeof value === 'string') return;
20+
21+
if (value.is('dimension')) {
22+
var unit = value.first('ident').content;
23+
if (value.first('number').content[0] === '0' &&
24+
UNITS.indexOf(unit) !== -1) {
25+
value.remove(1);
26+
}
27+
} else if (value.is('percentage')) {
28+
// XXX(tonyganch): There is a bug in Gonzales when in Less,
29+
// percentage's content is not wrapped as an array but actually
30+
// type of node's content is object. This bug has already been
31+
// fixed in newer versions of Gonzales so the issue should be
32+
// gone after update of dependencies and [email protected] release.
33+
// This hack is here as a hotfix for [email protected] and must be
34+
// removed once [email protected] is released. See #389.
35+
var number;
36+
if (!Array.isArray(value.content) &&
37+
value.content.is('number')) {
38+
number = value.content;
39+
} else {
40+
number = value.first('number').content;
41+
}
42+
43+
if (number[0] === '0') {
44+
value.type = 'number';
45+
value.content = number;
46+
}
47+
}
48+
});
49+
},
50+
51+
/**
52+
* Detects the value of an option at the tree node.
53+
*
54+
* @param {node} node
55+
*/
56+
detect: function(node) {
57+
var result;
58+
59+
// If we see a zero with unit and it is not degree, then we don’t have an option
60+
61+
if (node.is('percentage') && node.first('number').content[1] === '0') {
62+
result = false;
63+
} else if (node.is('dimension') &&
64+
node.first('number').content[0] === '0' &&
65+
node.first('ident').content !== 'deg') {
66+
result = false;
67+
}
68+
69+
// If we see a zero and previous node is not percentage or dimension, then we have an option
70+
if (node.is('number') &&
71+
node.content[0] === '0' &&
72+
this._prev !== 'percentage' &&
73+
this._prev !== 'dimension') {
74+
result = true;
75+
}
76+
77+
// Store the previous nodeType
78+
this._prev = node.type;
79+
80+
return result;
81+
}
82+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.test
2+
{
3+
width: 100%;
4+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
describe('options/unitless-zero (less)', function() {
2+
it('Issue 389', function() {
3+
this.comb.configure({ 'unitless-zero': true });
4+
this.shouldBeEqual('issue-389.less');
5+
});
6+
});

0 commit comments

Comments
 (0)