127 lines
12 KiB
JavaScript
127 lines
12 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
|
||
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||
|
|
||
|
var defaultRaw = {
|
||
|
colon: ': ',
|
||
|
indent: ' ',
|
||
|
commentLeft: ' ',
|
||
|
commentRight: ' '
|
||
|
};
|
||
|
|
||
|
var Stringifier = function () {
|
||
|
function Stringifier(builder) {
|
||
|
_classCallCheck(this, Stringifier);
|
||
|
|
||
|
this.builder = builder;
|
||
|
}
|
||
|
|
||
|
Stringifier.prototype.stringify = function stringify(node, semicolon) {
|
||
|
this[node.type](node, semicolon);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.root = function root(node) {
|
||
|
this.body(node);
|
||
|
if (node.raws.after) this.builder(node.raws.after);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.comment = function comment(node) {
|
||
|
var left = defaultRaw.commentLeft;
|
||
|
var right = defaultRaw.commentRight;
|
||
|
if (this.has(node.raws.left)) left = node.raws.left;
|
||
|
|
||
|
if (node.raws.inline) {
|
||
|
if (this.has(node.raws.inlineRight)) {
|
||
|
right = node.raws.inlineRight;
|
||
|
} else {
|
||
|
right = '';
|
||
|
}
|
||
|
if (node.raws.extraIndent) {
|
||
|
this.builder(node.raws.extraIndent);
|
||
|
}
|
||
|
this.builder('//' + left + node.text + right, node);
|
||
|
} else {
|
||
|
if (this.has(node.raws.right)) right = node.raws.right;
|
||
|
this.builder('/*' + left + node.text + right + '*/', node);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.decl = function decl(node) {
|
||
|
var between = node.raws.between || defaultRaw.colon;
|
||
|
var string = node.prop + between + this.rawValue(node, 'value');
|
||
|
|
||
|
if (node.important) {
|
||
|
string += node.raws.important || ' !important';
|
||
|
}
|
||
|
|
||
|
this.builder(string, node);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rule = function rule(node) {
|
||
|
this.block(node, this.rawValue(node, 'selector'));
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.atrule = function atrule(node) {
|
||
|
var name = '@' + node.name;
|
||
|
var params = node.params ? this.rawValue(node, 'params') : '';
|
||
|
|
||
|
if (this.has(node.raws.afterName)) {
|
||
|
name += node.raws.afterName;
|
||
|
} else if (params) {
|
||
|
name += ' ';
|
||
|
}
|
||
|
|
||
|
this.block(node, name + params);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.body = function body(node) {
|
||
|
var indent = node.root().raws.indent || defaultRaw.indent;
|
||
|
|
||
|
for (var i = 0; i < node.nodes.length; i++) {
|
||
|
var child = node.nodes[i];
|
||
|
var before = child.raws.before.replace(/[^\n]*$/, '') + this.indent(node, indent);
|
||
|
if (child.type === 'comment' && child.raws.before.indexOf('\n') === -1) {
|
||
|
before = child.raws.before;
|
||
|
}
|
||
|
if (before) this.builder(before);
|
||
|
this.stringify(child);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.block = function block(node, start) {
|
||
|
var between = node.raws.sssBetween || '';
|
||
|
this.builder(start + between, node, 'start');
|
||
|
if (this.has(node.nodes)) this.body(node);
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.indent = function indent(node, step) {
|
||
|
var result = '';
|
||
|
while (node.parent) {
|
||
|
result += step;
|
||
|
node = node.parent;
|
||
|
}
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.has = function has(value) {
|
||
|
return typeof value !== 'undefined';
|
||
|
};
|
||
|
|
||
|
Stringifier.prototype.rawValue = function rawValue(node, prop) {
|
||
|
var value = node[prop];
|
||
|
var raw = node.raws[prop];
|
||
|
if (raw && raw.value === value) {
|
||
|
return raw.sss || raw.raw;
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return Stringifier;
|
||
|
}();
|
||
|
|
||
|
exports.default = Stringifier;
|
||
|
module.exports = exports['default'];
|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