123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- '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'];
|