|
| 1 | + |
| 2 | + module.exports = function(tree) { |
| 3 | + var _compact, _filter, _isLogicalSection, _multi, _selectors, _shortened, _single; |
| 4 | + tree.Import.prototype.toLess = function() { |
| 5 | + if (this.css) { |
| 6 | + return this.toCSS(); |
| 7 | + } else { |
| 8 | + return "@import " + this._path.toCSS() + ";"; |
| 9 | + } |
| 10 | + }; |
| 11 | + tree.mixin.Definition.prototype.toLess = function(ctx, env) { |
| 12 | + var merged, params, rules, sel; |
| 13 | + params = this.params.map(function(p) { |
| 14 | + if (p.value != null) { |
| 15 | + return p.name + ": " + p.value.toLess(env); |
| 16 | + } else { |
| 17 | + return p.name; |
| 18 | + } |
| 19 | + }).join(", ").trim(); |
| 20 | + sel = this.selectors[0].toLess(env).trim() + "(" + (params || "") + ")"; |
| 21 | + rules = this.rules.reduce(function(_css, r) { |
| 22 | + _css += ctx + " " + r.toLess(ctx + " ", env).trim(); |
| 23 | + return _css; |
| 24 | + }, ""); |
| 25 | + merged = _multi(sel, ctx, env, rules); |
| 26 | + if (merged.length > 80) { |
| 27 | + return ctx + merged; |
| 28 | + } else { |
| 29 | + return ctx + _single(sel, ctx, { |
| 30 | + compress: true |
| 31 | + }, rules); |
| 32 | + } |
| 33 | + }; |
| 34 | + tree.Selector.prototype.toLess = function(env) { |
| 35 | + if (this._css) { |
| 36 | + console.log(this._css); |
| 37 | + this._css; |
| 38 | + } |
| 39 | + return this.elements.map(function(e) { |
| 40 | + if (typeof e === 'string') { |
| 41 | + return ' ' + e.trim(); |
| 42 | + } else { |
| 43 | + return e.toLess(env); |
| 44 | + } |
| 45 | + }).join(""); |
| 46 | + }; |
| 47 | + tree.Color.prototype.toLess = function() { |
| 48 | + return this.toCSS(); |
| 49 | + }; |
| 50 | + tree.Call.prototype.toLess = function(env) { |
| 51 | + var args, _ref; |
| 52 | + args = (_ref = this.args) != null ? _ref.map(function(a) { |
| 53 | + return a.toLess(env); |
| 54 | + }).join(", ").trim() : void 0; |
| 55 | + return this.name + (args != null ? "(" + args + ")" : ""); |
| 56 | + }; |
| 57 | + tree.mixin.Call.prototype.toLess = function(env) { |
| 58 | + var args, _ref; |
| 59 | + args = (_ref = this.arguments) != null ? _ref.map(function(a) { |
| 60 | + return a.toLess(env); |
| 61 | + }).join(", ").trim() : void 0; |
| 62 | + return this.selector.toLess(env).trim() + (args != null ? "(" + args + ");" : ";"); |
| 63 | + }; |
| 64 | + tree.Directive.prototype.toLess = function(ctx, env) { |
| 65 | + var merged, params, rules, sel; |
| 66 | + params = this.params.map(function(p) { |
| 67 | + return p.toLess(env); |
| 68 | + }).join(", ").trim(); |
| 69 | + sel = this.selectors[0].toLess(env).trim() + "(" + (params || "") + ")"; |
| 70 | + rules = this.rules.map(function(r) { |
| 71 | + return r.toLess(env).trim(); |
| 72 | + }); |
| 73 | + merged = _multi(sel, ctx, env, rules); |
| 74 | + if (merged.length > 80) { |
| 75 | + return merged; |
| 76 | + } else { |
| 77 | + return _single(sel, ctx, { |
| 78 | + compress: true |
| 79 | + }, rules); |
| 80 | + } |
| 81 | + }; |
| 82 | + tree.Dimension.prototype.toLess = function() { |
| 83 | + return this.toCSS(); |
| 84 | + }; |
| 85 | + tree.Anonymous.prototype.toLess = function() { |
| 86 | + return this.toCSS(); |
| 87 | + }; |
| 88 | + tree.Alpha.prototype.toLess = function() { |
| 89 | + return this.toCSS(); |
| 90 | + }; |
| 91 | + tree.Element.prototype.toLess = function(env) { |
| 92 | + return this.combinator.toLess(env || {}) + this.value; |
| 93 | + }; |
| 94 | + tree.Combinator.prototype.toLess = function(env) { |
| 95 | + return { |
| 96 | + '': '', |
| 97 | + ' ': ' ', |
| 98 | + '&': '&', |
| 99 | + '& ': '& ', |
| 100 | + ':': ' :', |
| 101 | + '::': '::', |
| 102 | + '+': ' + ', |
| 103 | + '~': ' ~ ', |
| 104 | + '>': ' > ' |
| 105 | + }[this.value]; |
| 106 | + }; |
| 107 | + tree.Expression.prototype.toLess = function(env) { |
| 108 | + return this.value.map(function(e) { |
| 109 | + return e.toLess(env); |
| 110 | + }).join(' '); |
| 111 | + }; |
| 112 | + tree.JavaScript.prototype.toLess = function() { |
| 113 | + return "" + (this.escaped && "~" || "") + "`" + this.expression + "`"; |
| 114 | + }; |
| 115 | + tree.Keyword.prototype.toLess = function() { |
| 116 | + return this.toCSS(); |
| 117 | + }; |
| 118 | + tree.Quoted.prototype.toLess = function() { |
| 119 | + return (this.escaped && "~" || "") + this.quote + this.value + this.quote; |
| 120 | + }; |
| 121 | + tree.URL.prototype.toLess = function(env) { |
| 122 | + return "url(" + this.value.toLess(env) + ")"; |
| 123 | + }; |
| 124 | + tree.Variable.prototype.toLess = function() { |
| 125 | + return this.name; |
| 126 | + }; |
| 127 | + tree.Value.prototype.toLess = function(env) { |
| 128 | + return this.value.map(function(e) { |
| 129 | + return e.toLess(env); |
| 130 | + }).join(env.compress ? ',' : ', '); |
| 131 | + }; |
| 132 | + tree.Shorthand.prototype.toLess = function(env) { |
| 133 | + return this.a.toLess(env) + "/" + this.b.toLess(env); |
| 134 | + }; |
| 135 | + tree.Comment.prototype.toLess = function(env) { |
| 136 | + return this.toCSS(env); |
| 137 | + }; |
| 138 | + tree.Rule.prototype.toLess = function(env) { |
| 139 | + return this.name + (env.compress ? ':' : ': ') + this.value.toLess(env) + this.important + ";"; |
| 140 | + }; |
| 141 | + tree.Operation.prototype.toLess = function(env) { |
| 142 | + return "(" + [this.operands[0].toLess(env), this.op, this.operands[1].toLess(env)].join(" ") + ")"; |
| 143 | + }; |
| 144 | + _multi = function(selector, context, env, rules) { |
| 145 | + return "" + selector + " {\n" + rules + "\n" + context + "}"; |
| 146 | + }; |
| 147 | + _single = function(selector, context, env, rules) { |
| 148 | + return selector + ' { ' + rules.trim() + ' }'; |
| 149 | + }; |
| 150 | + _selectors = function(paths, env, padding) { |
| 151 | + var i, mapped, part, results; |
| 152 | + mapped = paths.map(function(p) { |
| 153 | + return p.map(function(s) { |
| 154 | + return s.toLess(env); |
| 155 | + }).join('').trim(); |
| 156 | + }); |
| 157 | + results = [padding + mapped.shift()]; |
| 158 | + if (mapped.length !== 0) { |
| 159 | + for (i in mapped) { |
| 160 | + part = mapped[i]; |
| 161 | + if (env.compress) { |
| 162 | + results.push(',' + part); |
| 163 | + } else { |
| 164 | + if (paths.length > 3 && i % 3 === 2) { |
| 165 | + results.push(',\n' + padding + part); |
| 166 | + } else { |
| 167 | + results.push(', ' + part); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + return results.join(""); |
| 173 | + }; |
| 174 | + _filter = function(obj, iterator, context) { |
| 175 | + var results; |
| 176 | + results = []; |
| 177 | + if (obj === null) return results; |
| 178 | + return obj.filter(iterator, context); |
| 179 | + }; |
| 180 | + _compact = function(array) { |
| 181 | + return _filter(array, function(value) { |
| 182 | + return !!value; |
| 183 | + }); |
| 184 | + }; |
| 185 | + _isLogicalSection = function(content) { |
| 186 | + var lastChar; |
| 187 | + lastChar = content.charAt(content.length - 2); |
| 188 | + return lastChar === ";" || lastChar === "}"; |
| 189 | + }; |
| 190 | + _shortened = function(content) { |
| 191 | + return content.replace(/\s+/g, " ").trim(); |
| 192 | + }; |
| 193 | + tree.Ruleset.prototype.toLess = function(context, env) { |
| 194 | + var css, merged, padding, paths, rules, rulesets, sel; |
| 195 | + var _this = this; |
| 196 | + css = ""; |
| 197 | + paths = []; |
| 198 | + rulesets = []; |
| 199 | + if (!this.root) { |
| 200 | + paths = this.selectors.map(function(s) { |
| 201 | + return [s]; |
| 202 | + }); |
| 203 | + } |
| 204 | + padding = this.root ? "" : context + " "; |
| 205 | + rules = this.rules.reduce(function(_css, rule, r, list) { |
| 206 | + var comment, content, sel; |
| 207 | + if (rule.rules || (rule instanceof tree.Directive)) { |
| 208 | + sel = rule.selectors.map(function(s) { |
| 209 | + return s.toLess(env); |
| 210 | + }).join(", ").trim(); |
| 211 | + if (_isLogicalSection(_css)) _css += "\n"; |
| 212 | + content = rule.toLess(padding, env); |
| 213 | + if (_shortened(content).length < 90) { |
| 214 | + content = padding + _shortened(content); |
| 215 | + } |
| 216 | + _css += content; |
| 217 | + } else { |
| 218 | + if (rule instanceof tree.Comment) { |
| 219 | + if (_isLogicalSection(_css)) _css += "\n"; |
| 220 | + if (_this.root) { |
| 221 | + _css += rule.toLess(env).trim(); |
| 222 | + } else { |
| 223 | + comment = rule.value.toString().trim(); |
| 224 | + _css += padding + comment; |
| 225 | + } |
| 226 | + } else { |
| 227 | + if (rule.toLess && !rule.variable) { |
| 228 | + _css += padding + rule.toLess(env); |
| 229 | + } else if (rule.value) { |
| 230 | + if (rule.variable) { |
| 231 | + _css += context + padding + rule.name + ": " + rule.value.toLess(env) + ";"; |
| 232 | + } else { |
| 233 | + _css += context + rule.value.toString(); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + if (list.length !== r + 1) _css += '\n'; |
| 239 | + return _css; |
| 240 | + }, ""); |
| 241 | + if (this.root) { |
| 242 | + css += rules; |
| 243 | + } else { |
| 244 | + sel = _selectors(paths, env, context); |
| 245 | + merged = _multi(sel, context, env, rules); |
| 246 | + if (merged.length > 80) { |
| 247 | + css += merged; |
| 248 | + } else { |
| 249 | + sel = _selectors(paths, { |
| 250 | + compress: false |
| 251 | + }, context); |
| 252 | + css += _single(sel, context, env, rules); |
| 253 | + } |
| 254 | + } |
| 255 | + return css; |
| 256 | + }; |
| 257 | + }; |
0 commit comments