|
| 1 | +module.exports = { |
| 2 | + |
| 3 | + /** |
| 4 | + * Internal |
| 5 | + * |
| 6 | + * Containt vendor-prefixes list. |
| 7 | + */ |
| 8 | + _prefixesList: [ |
| 9 | + 'webkit', |
| 10 | + 'moz', |
| 11 | + 'ms', |
| 12 | + 'o' |
| 13 | + ], |
| 14 | + |
| 15 | + /** |
| 16 | + * Internal |
| 17 | + * |
| 18 | + * Create object which contains info about vendor prefix used in propertyName. |
| 19 | + * @param {String} propertyName property name |
| 20 | + * @returns {Object|undefined} |
| 21 | + */ |
| 22 | + _getPrefixInfo: function(propertyName) { |
| 23 | + if (!propertyName) return; |
| 24 | + |
| 25 | + var result = { baseName: propertyName, prefixLength: 0 }; |
| 26 | + |
| 27 | + this._prefixesList.some(function(prefix) { |
| 28 | + prefix = '-' + prefix + '-'; |
| 29 | + if (propertyName.indexOf(prefix) !== 0) return; |
| 30 | + result = { |
| 31 | + baseName: propertyName.substr(prefix.length), |
| 32 | + prefixLength: prefix.length |
| 33 | + }; |
| 34 | + return true; |
| 35 | + }); |
| 36 | + |
| 37 | + return result; |
| 38 | + }, |
| 39 | + |
| 40 | + /** |
| 41 | + * Internal |
| 42 | + * |
| 43 | + * Walk across nodes, and call payload for every node that pass selector check. |
| 44 | + * @param {node} node |
| 45 | + * @param {function} selector |
| 46 | + * @param {function} payload |
| 47 | + */ |
| 48 | + _walk: function(node, selector, payload) { |
| 49 | + node.forEach(function(item, i) { |
| 50 | + var info = this._getPrefixInfo(selector(item)); |
| 51 | + if (!info) return; |
| 52 | + payload(info, i); |
| 53 | + }, this); |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + * Internal |
| 58 | + * |
| 59 | + * Selector for property name. |
| 60 | + * @param {node} item |
| 61 | + * @returns {String|false|undefined} |
| 62 | + */ |
| 63 | + _declName: function(item) { |
| 64 | + return item[0] === 'declaration' && item[1][1][1]; |
| 65 | + }, |
| 66 | + |
| 67 | + /** |
| 68 | + * Internal |
| 69 | + * |
| 70 | + * Selector for value name. |
| 71 | + * @param {node} item |
| 72 | + * @returns {String|false|undefined} |
| 73 | + */ |
| 74 | + _valName: function(item) { |
| 75 | + return item[0] === 'declaration' && item[2] && item[2][2] && |
| 76 | + item[2][2][0] === 'funktion' && item[2][2][1][0] === 'ident' && |
| 77 | + item[2][2][1][1]; |
| 78 | + }, |
| 79 | + |
| 80 | + /** |
| 81 | + * Internal |
| 82 | + * |
| 83 | + * Update dict which contains info about items align. |
| 84 | + * @param {Object} info, |
| 85 | + * @param {Object} dict, |
| 86 | + * @param {String} whitespaceNode |
| 87 | + */ |
| 88 | + _updateDict: function(info, dict, whitespaceNode) { |
| 89 | + if (info.prefixLength === 0) return; |
| 90 | + |
| 91 | + var indent = dict[info.baseName] || { prefixLength: 0, baseLength: 0 }; |
| 92 | + |
| 93 | + dict[info.baseName] = indent.prefixLength > info.prefixLength ? |
| 94 | + indent : |
| 95 | + { |
| 96 | + prefixLength: info.prefixLength, |
| 97 | + baseLength: whitespaceNode.substr(whitespaceNode.lastIndexOf('\n') + 1).length |
| 98 | + }; |
| 99 | + }, |
| 100 | + |
| 101 | + /** |
| 102 | + * Return string with correct number of spaces for info.baseName property. |
| 103 | + * @param {Object} info, |
| 104 | + * @param {Object} dict, |
| 105 | + * @param {String} whitespaceNode |
| 106 | + * @returns {String} |
| 107 | + */ |
| 108 | + _updateIndent: function(info, dict, whitespaceNode) { |
| 109 | + if (!dict[info.baseName]) |
| 110 | + return whitespaceNode; |
| 111 | + |
| 112 | + var firstPart = whitespaceNode.substr(0, whitespaceNode.lastIndexOf('\n') + 1 ); |
| 113 | + var extraIndent = new Array( |
| 114 | + dict[info.baseName].prefixLength - |
| 115 | + info.prefixLength + |
| 116 | + dict[info.baseName].baseLength + 1).join(' '); |
| 117 | + |
| 118 | + return firstPart.concat(extraIndent); |
| 119 | + }, |
| 120 | + |
| 121 | + /** |
| 122 | + * Sets handler value. |
| 123 | + * |
| 124 | + * @param {Array} value Option value |
| 125 | + * @returns {Object|undefined} |
| 126 | + */ |
| 127 | + setValue: function(value) { |
| 128 | + return value ? this : undefined; |
| 129 | + }, |
| 130 | + |
| 131 | + /** |
| 132 | + * Processes tree node. |
| 133 | + * @param {String} nodeType |
| 134 | + * @param {node} node |
| 135 | + */ |
| 136 | + process: function(nodeType, node) { |
| 137 | + if (nodeType !== 'block') return; |
| 138 | + |
| 139 | + var dict = {}; |
| 140 | + var _this = this; |
| 141 | + |
| 142 | + // Gathering Info |
| 143 | + this._walk(node, this._declName, function(info, i) { |
| 144 | + _this._updateDict(info, dict, node[i - 1][1]); |
| 145 | + }); |
| 146 | + this._walk(node, this._valName, function(info, i) { |
| 147 | + _this._updateDict(info, dict, node[i][2][1][1]); |
| 148 | + }); |
| 149 | + |
| 150 | + // Update nodes |
| 151 | + this._walk(node, this._declName, function(info, i) { |
| 152 | + node[i - 1][1] = _this._updateIndent(info, dict, node[i - 1][1]); |
| 153 | + }); |
| 154 | + this._walk(node, this._valName, function(info, i) { |
| 155 | + node[i][2][1][1] = _this._updateIndent(info, dict, node[i][2][1][1]); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | +}; |
0 commit comments