Skip to content

Commit 628924e

Browse files
committed
fix #153
1 parent 816fcb8 commit 628924e

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

lib/options/vendor-prefix-align.js

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,49 @@ module.exports = {
1212
'o'
1313
],
1414

15+
/**
16+
* Internal
17+
*
18+
* Make namespace from property name
19+
* @param {String} propertyName
20+
* @returns {String|undefined}
21+
*/
22+
_makeNamespace: function(propertyName) {
23+
var info = this._getPrefixInfo(propertyName);
24+
return info && info.baseName;
25+
},
26+
1527
/**
1628
* Internal
1729
*
1830
* Create object which contains info about vendor prefix used in propertyName.
1931
* @param {String} propertyName property name
2032
* @returns {Object|undefined}
2133
*/
22-
_getPrefixInfo: function(propertyName) {
23-
if (!propertyName) return;
34+
_getPrefixInfo: function(propertyName, namespace) {
35+
var baseName = propertyName;
36+
var prefixLength = 0;
2437

25-
var result = { baseName: propertyName, prefixLength: 0 };
38+
namespace = namespace || '';
39+
40+
if (!propertyName) return;
2641

2742
this._prefixesList.some(function(prefix) {
2843
// TODO: Why don't we store prefixes with `-`?
2944
prefix = '-' + prefix + '-';
3045
if (propertyName.indexOf(prefix) !== 0) return;
31-
result = {
32-
baseName: propertyName.substr(prefix.length),
33-
prefixLength: prefix.length
34-
};
46+
47+
baseName = baseName.substr(prefix.length);
48+
prefixLength = prefix.length;
49+
3550
return true;
3651
});
3752

38-
return result;
53+
return {
54+
id: namespace + baseName,
55+
baseName: baseName,
56+
prefixLength: prefixLength
57+
};
3958
},
4059

4160
/**
@@ -46,9 +65,12 @@ module.exports = {
4665
* @param {function} selector
4766
* @param {function} payload
4867
*/
49-
_walk: function(node, selector, payload) {
68+
_walk: function(node, selector, payload, namespaceSelector) {
5069
node.forEach(function(item, i) {
51-
var info = this._getPrefixInfo(selector(item));
70+
var info = this._getPrefixInfo(
71+
selector(item),
72+
namespaceSelector && this._makeNamespace(namespaceSelector(item))
73+
);
5274
if (!info) return;
5375
payload(info, i);
5476
}, this);
@@ -103,14 +125,14 @@ module.exports = {
103125
_updateDict: function(info, dict, whitespaceNode) {
104126
if (info.prefixLength === 0) return;
105127

106-
var indent = dict[info.baseName] || { prefixLength: 0, baseLength: 0 };
128+
var indent = dict[info.id] || { prefixLength: 0, baseLength: 0 };
107129

108-
dict[info.baseName] = indent.prefixLength > info.prefixLength ?
109-
indent :
110-
{
111-
prefixLength: info.prefixLength,
112-
baseLength: whitespaceNode.substr(whitespaceNode.lastIndexOf('\n') + 1).length
113-
};
130+
dict[info.id] = indent.prefixLength > info.prefixLength ?
131+
indent :
132+
{
133+
prefixLength: info.prefixLength,
134+
baseLength: whitespaceNode.substr(whitespaceNode.lastIndexOf('\n') + 1).length
135+
};
114136
},
115137

116138
/**
@@ -121,14 +143,15 @@ module.exports = {
121143
* @returns {String}
122144
*/
123145
_updateIndent: function(info, dict, whitespaceNode) {
124-
if (!dict[info.baseName])
146+
var item = dict[info.id];
147+
if (!item)
125148
return whitespaceNode;
126149

127150
var firstPart = whitespaceNode.substr(0, whitespaceNode.lastIndexOf('\n') + 1 );
128151
var extraIndent = new Array(
129-
dict[info.baseName].prefixLength -
152+
item.prefixLength -
130153
info.prefixLength +
131-
dict[info.baseName].baseLength + 1).join(' ');
154+
item.baseLength + 1).join(' ');
132155

133156
return firstPart.concat(extraIndent);
134157
},
@@ -160,15 +183,16 @@ module.exports = {
160183
});
161184
this._walk(node, this._getValName, function(info, i) {
162185
_this._updateDict(info, dict, node[i][3][1][1]);
163-
});
186+
}, this._getDeclName);
164187

165188
// Update nodes
166189
this._walk(node, this._getDeclName, function(info, i) {
167190
node[i - 1][1] = _this._updateIndent(info, dict, node[i - 1][1]);
168191
});
169192
this._walk(node, this._getValName, function(info, i) {
170193
node[i][3][1][1] = _this._updateIndent(info, dict, node[i][3][1][1]);
171-
});
194+
}, this._getDeclName );
195+
172196
},
173197

174198
/**

0 commit comments

Comments
 (0)