3 exports.__esModule = true;
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21 function capitalize(str) {
22 return str[0].toUpperCase() + str.slice(1);
25 var Stringifier = function () {
26 function Stringifier(builder) {
27 _classCallCheck(this, Stringifier);
29 this.builder = builder;
32 Stringifier.prototype.stringify = function stringify(node, semicolon) {
33 this[node.type](node, semicolon);
36 Stringifier.prototype.root = function root(node) {
38 if (node.raws.after) this.builder(node.raws.after);
41 Stringifier.prototype.comment = function comment(node) {
42 var left = this.raw(node, 'left', 'commentLeft');
43 var right = this.raw(node, 'right', 'commentRight');
44 this.builder('/*' + left + node.text + right + '*/', node);
47 Stringifier.prototype.decl = function decl(node, semicolon) {
48 var between = this.raw(node, 'between', 'colon');
49 var string = node.prop + between + this.rawValue(node, 'value');
52 string += node.raws.important || ' !important';
55 if (semicolon) string += ';';
56 this.builder(string, node);
59 Stringifier.prototype.rule = function rule(node) {
60 this.block(node, this.rawValue(node, 'selector'));
63 Stringifier.prototype.atrule = function atrule(node, semicolon) {
64 var name = '@' + node.name;
65 var params = node.params ? this.rawValue(node, 'params') : '';
67 if (typeof node.raws.afterName !== 'undefined') {
68 name += node.raws.afterName;
74 this.block(node, name + params);
76 var end = (node.raws.between || '') + (semicolon ? ';' : '');
77 this.builder(name + params + end, node);
81 Stringifier.prototype.body = function body(node) {
82 var last = node.nodes.length - 1;
84 if (node.nodes[last].type !== 'comment') break;
88 var semicolon = this.raw(node, 'semicolon');
89 for (var i = 0; i < node.nodes.length; i++) {
90 var child = node.nodes[i];
91 var before = this.raw(child, 'before');
92 if (before) this.builder(before);
93 this.stringify(child, last !== i || semicolon);
97 Stringifier.prototype.block = function block(node, start) {
98 var between = this.raw(node, 'between', 'beforeOpen');
99 this.builder(start + between + '{', node, 'start');
102 if (node.nodes && node.nodes.length) {
104 after = this.raw(node, 'after');
106 after = this.raw(node, 'after', 'emptyBody');
109 if (after) this.builder(after);
110 this.builder('}', node, 'end');
113 Stringifier.prototype.raw = function raw(node, own, detect) {
115 if (!detect) detect = own;
119 value = node.raws[own];
120 if (typeof value !== 'undefined') return value;
123 var parent = node.parent;
125 // Hack for first rule in CSS
126 if (detect === 'before') {
127 if (!parent || parent.type === 'root' && parent.first === node) {
132 // Floating child without parent
133 if (!parent) return defaultRaw[detect];
135 // Detect style by other nodes
136 var root = node.root();
137 if (!root.rawCache) root.rawCache = {};
138 if (typeof root.rawCache[detect] !== 'undefined') {
139 return root.rawCache[detect];
142 if (detect === 'before' || detect === 'after') {
143 return this.beforeAfter(node, detect);
145 var method = 'raw' + capitalize(detect);
147 value = this[method](root, node);
149 root.walk(function (i) {
151 if (typeof value !== 'undefined') return false;
156 if (typeof value === 'undefined') value = defaultRaw[detect];
158 root.rawCache[detect] = value;
162 Stringifier.prototype.rawSemicolon = function rawSemicolon(root) {
164 root.walk(function (i) {
165 if (i.nodes && i.nodes.length && i.last.type === 'decl') {
166 value = i.raws.semicolon;
167 if (typeof value !== 'undefined') return false;
173 Stringifier.prototype.rawEmptyBody = function rawEmptyBody(root) {
175 root.walk(function (i) {
176 if (i.nodes && i.nodes.length === 0) {
177 value = i.raws.after;
178 if (typeof value !== 'undefined') return false;
184 Stringifier.prototype.rawIndent = function rawIndent(root) {
185 if (root.raws.indent) return root.raws.indent;
187 root.walk(function (i) {
189 if (p && p !== root && p.parent && p.parent === root) {
190 if (typeof i.raws.before !== 'undefined') {
191 var parts = i.raws.before.split('\n');
192 value = parts[parts.length - 1];
193 value = value.replace(/[^\s]/g, '');
201 Stringifier.prototype.rawBeforeComment = function rawBeforeComment(root, node) {
203 root.walkComments(function (i) {
204 if (typeof i.raws.before !== 'undefined') {
205 value = i.raws.before;
206 if (value.indexOf('\n') !== -1) {
207 value = value.replace(/[^\n]+$/, '');
212 if (typeof value === 'undefined') {
213 value = this.raw(node, null, 'beforeDecl');
218 Stringifier.prototype.rawBeforeDecl = function rawBeforeDecl(root, node) {
220 root.walkDecls(function (i) {
221 if (typeof i.raws.before !== 'undefined') {
222 value = i.raws.before;
223 if (value.indexOf('\n') !== -1) {
224 value = value.replace(/[^\n]+$/, '');
229 if (typeof value === 'undefined') {
230 value = this.raw(node, null, 'beforeRule');
235 Stringifier.prototype.rawBeforeRule = function rawBeforeRule(root) {
237 root.walk(function (i) {
238 if (i.nodes && (i.parent !== root || root.first !== i)) {
239 if (typeof i.raws.before !== 'undefined') {
240 value = i.raws.before;
241 if (value.indexOf('\n') !== -1) {
242 value = value.replace(/[^\n]+$/, '');
251 Stringifier.prototype.rawBeforeClose = function rawBeforeClose(root) {
253 root.walk(function (i) {
254 if (i.nodes && i.nodes.length > 0) {
255 if (typeof i.raws.after !== 'undefined') {
256 value = i.raws.after;
257 if (value.indexOf('\n') !== -1) {
258 value = value.replace(/[^\n]+$/, '');
267 Stringifier.prototype.rawBeforeOpen = function rawBeforeOpen(root) {
269 root.walk(function (i) {
270 if (i.type !== 'decl') {
271 value = i.raws.between;
272 if (typeof value !== 'undefined') return false;
278 Stringifier.prototype.rawColon = function rawColon(root) {
280 root.walkDecls(function (i) {
281 if (typeof i.raws.between !== 'undefined') {
282 value = i.raws.between.replace(/[^\s:]/g, '');
289 Stringifier.prototype.beforeAfter = function beforeAfter(node, detect) {
291 if (node.type === 'decl') {
292 value = this.raw(node, null, 'beforeDecl');
293 } else if (node.type === 'comment') {
294 value = this.raw(node, null, 'beforeComment');
295 } else if (detect === 'before') {
296 value = this.raw(node, null, 'beforeRule');
298 value = this.raw(node, null, 'beforeClose');
301 var buf = node.parent;
303 while (buf && buf.type !== 'root') {
308 if (value.indexOf('\n') !== -1) {
309 var indent = this.raw(node, null, 'indent');
311 for (var step = 0; step < depth; step++) {
320 Stringifier.prototype.rawValue = function rawValue(node, prop) {
321 var value = node[prop];
322 var raw = node.raws[prop];
323 if (raw && raw.value === value) {
333 exports.default = Stringifier;
334 module.exports = exports['default'];