Skip to content

Commit 09e59b4

Browse files
committed
Added detection of the vendor-prefix-align option
1 parent 139c3b8 commit 09e59b4

3 files changed

Lines changed: 172 additions & 1 deletion

File tree

lib/options/vendor-prefix-align.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,88 @@ module.exports = {
169169
this._walk(node, this._getValName, function(info, i) {
170170
node[i][3][1][1] = _this._updateIndent(info, dict, node[i][3][1][1]);
171171
});
172+
},
173+
174+
/**
175+
* Detects the syntax at the tree node.
176+
*
177+
* @param {String} nodeType
178+
* @param {node} node
179+
*/
180+
detect: function(nodeType, node) {
181+
if (nodeType !== 'block') return;
182+
183+
var result = {
184+
true: 0,
185+
false: 0
186+
};
187+
188+
var maybePrefix = false;
189+
var prevPrefixLength = false;
190+
var prevProp;
191+
var prevSum;
192+
var partialResult = null;
193+
194+
var getResult = function(node, sum, info, i) {
195+
var prop = info.baseName;
196+
197+
// If this is the last item in a row and we have a result, then catch it
198+
if (prop !== prevProp && partialResult !== null) {
199+
if (partialResult) {
200+
result.true++;
201+
} else {
202+
result.false++;
203+
}
204+
partialResult = null;
205+
}
206+
207+
if (prop === prevProp && info.prefixLength !== prevPrefixLength) {
208+
maybePrefix = true;
209+
} else {
210+
maybePrefix = false;
211+
}
212+
213+
if (maybePrefix && partialResult !== false) {
214+
// If there is prefixed prop, check if the prefixes are aligned,
215+
// but only if we hadn't already catched that it is false
216+
if (sum === prevSum) {
217+
partialResult = true;
218+
} else {
219+
partialResult = false;
220+
}
221+
}
222+
223+
if (node.length === i + 3 && partialResult !== null) {
224+
// If we're at the last property and have a result, catch it
225+
if (partialResult) {
226+
result.true++;
227+
} else {
228+
result.false++;
229+
}
230+
}
231+
232+
prevPrefixLength = info.prefixLength;
233+
prevProp = prop;
234+
prevSum = sum;
235+
};
236+
237+
// Gathering Info
238+
this._walk(node, this._getDeclName, function(info, i) {
239+
var sum = node[i - 1][1].replace(/^[ \t]*\n+/, '').length + info.prefixLength;
240+
getResult(node, sum, info, i);
241+
});
242+
243+
this._walk(node, this._getValName, function(info, i) {
244+
var sum = node[i][3][1][1].replace(/^[ \t]*\n+/, '').length + info.prefixLength;
245+
getResult(node, sum, info, i);
246+
});
247+
248+
if (result.true > 0 || result.false > 0) {
249+
if (result.true >= result.false) {
250+
return true;
251+
} else {
252+
return false;
253+
}
254+
}
172255
}
173256
};

test/integral.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ vow.all(['origin', 'expect'].map(function(type) {
4747
'combinator-space': [' ', ' '],
4848
'rule-indent': ' ',
4949
'block-indent': ' ',
50-
'unitless-zero': true
50+
'unitless-zero': true,
51+
'vendor-prefix-align': true
5152
})
5253
);
5354

test/vendor-prefix-align.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,91 @@ describe('options/vendor-prefix-align', function() {
4848

4949
assert.equal(comb.processString(input), expected);
5050
});
51+
52+
// Helper to check the detection
53+
function should_detect(options, a, b) {
54+
comb.detect(options);
55+
assert.equal(
56+
JSON.stringify(comb.processString(a)),
57+
JSON.stringify(b)
58+
);
59+
}
60+
61+
it('Shouldn not detect anything if there are no prefixed groups', function() {
62+
should_detect(
63+
['vendor-prefix-align'],
64+
'a{ color: red }a{ -webkit-transform: translateZ(0) }',
65+
{}
66+
);
67+
});
68+
69+
it('Shouldn detect vendor-prefix-align as false in properties', function() {
70+
should_detect(
71+
['vendor-prefix-align'],
72+
fs.readFileSync('./test/vendor-prefix-align/property-align.css', 'utf8'),
73+
{
74+
'vendor-prefix-align': false
75+
}
76+
);
77+
});
78+
79+
it('Shouldn detect vendor-prefix-align as true in properties', function() {
80+
should_detect(
81+
['vendor-prefix-align'],
82+
fs.readFileSync('./test/vendor-prefix-align/property-align.expected.css', 'utf8'),
83+
{
84+
'vendor-prefix-align': true
85+
}
86+
);
87+
});
88+
89+
it('Shouldn detect vendor-prefix-align as false in values', function() {
90+
should_detect(
91+
['vendor-prefix-align'],
92+
fs.readFileSync('./test/vendor-prefix-align/value-align.css', 'utf8'),
93+
{
94+
'vendor-prefix-align': false
95+
}
96+
);
97+
});
98+
99+
it('Shouldn detect vendor-prefix-align as true in values', function() {
100+
should_detect(
101+
['vendor-prefix-align'],
102+
fs.readFileSync('./test/vendor-prefix-align/value-align.expected.css', 'utf8'),
103+
{
104+
'vendor-prefix-align': true
105+
}
106+
);
107+
});
108+
109+
it('Shouldn detect vendor-prefix-align as true, test 1', function() {
110+
should_detect(
111+
['vendor-prefix-align'],
112+
fs.readFileSync('./test/vendor-prefix-align/already-aligned.css', 'utf8'),
113+
{
114+
'vendor-prefix-align': true
115+
}
116+
);
117+
});
118+
119+
it('Shouldn detect vendor-prefix-align as true, test 2', function() {
120+
should_detect(
121+
['vendor-prefix-align'],
122+
fs.readFileSync('./test/vendor-prefix-align/complex.expected.css', 'utf8'),
123+
{
124+
'vendor-prefix-align': true
125+
}
126+
);
127+
});
128+
129+
it('Shouldn detect vendor-prefix-align as false', function() {
130+
should_detect(
131+
['vendor-prefix-align'],
132+
fs.readFileSync('./test/vendor-prefix-align/complex.css', 'utf8'),
133+
{
134+
'vendor-prefix-align': false
135+
}
136+
);
137+
});
51138
});

0 commit comments

Comments
 (0)