stringifier.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. exports.__esModule = true;
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. var defaultRaw = {
  5. colon: ': ',
  6. indent: ' ',
  7. commentLeft: ' ',
  8. commentRight: ' '
  9. };
  10. var Stringifier = function () {
  11. function Stringifier(builder) {
  12. _classCallCheck(this, Stringifier);
  13. this.builder = builder;
  14. }
  15. Stringifier.prototype.stringify = function stringify(node, semicolon) {
  16. this[node.type](node, semicolon);
  17. };
  18. Stringifier.prototype.root = function root(node) {
  19. this.body(node);
  20. if (node.raws.after) this.builder(node.raws.after);
  21. };
  22. Stringifier.prototype.comment = function comment(node) {
  23. var left = defaultRaw.commentLeft;
  24. var right = defaultRaw.commentRight;
  25. if (this.has(node.raws.left)) left = node.raws.left;
  26. if (node.raws.inline) {
  27. if (this.has(node.raws.inlineRight)) {
  28. right = node.raws.inlineRight;
  29. } else {
  30. right = '';
  31. }
  32. if (node.raws.extraIndent) {
  33. this.builder(node.raws.extraIndent);
  34. }
  35. this.builder('//' + left + node.text + right, node);
  36. } else {
  37. if (this.has(node.raws.right)) right = node.raws.right;
  38. this.builder('/*' + left + node.text + right + '*/', node);
  39. }
  40. };
  41. Stringifier.prototype.decl = function decl(node) {
  42. var between = node.raws.between || defaultRaw.colon;
  43. var string = node.prop + between + this.rawValue(node, 'value');
  44. if (node.important) {
  45. string += node.raws.important || ' !important';
  46. }
  47. this.builder(string, node);
  48. };
  49. Stringifier.prototype.rule = function rule(node) {
  50. this.block(node, this.rawValue(node, 'selector'));
  51. };
  52. Stringifier.prototype.atrule = function atrule(node) {
  53. var name = '@' + node.name;
  54. var params = node.params ? this.rawValue(node, 'params') : '';
  55. if (this.has(node.raws.afterName)) {
  56. name += node.raws.afterName;
  57. } else if (params) {
  58. name += ' ';
  59. }
  60. this.block(node, name + params);
  61. };
  62. Stringifier.prototype.body = function body(node) {
  63. var indent = node.root().raws.indent || defaultRaw.indent;
  64. for (var i = 0; i < node.nodes.length; i++) {
  65. var child = node.nodes[i];
  66. var before = child.raws.before.replace(/[^\n]*$/, '') + this.indent(node, indent);
  67. if (child.type === 'comment' && child.raws.before.indexOf('\n') === -1) {
  68. before = child.raws.before;
  69. }
  70. if (before) this.builder(before);
  71. this.stringify(child);
  72. }
  73. };
  74. Stringifier.prototype.block = function block(node, start) {
  75. var between = node.raws.sssBetween || '';
  76. this.builder(start + between, node, 'start');
  77. if (this.has(node.nodes)) this.body(node);
  78. };
  79. Stringifier.prototype.indent = function indent(node, step) {
  80. var result = '';
  81. while (node.parent) {
  82. result += step;
  83. node = node.parent;
  84. }
  85. return result;
  86. };
  87. Stringifier.prototype.has = function has(value) {
  88. return typeof value !== 'undefined';
  89. };
  90. Stringifier.prototype.rawValue = function rawValue(node, prop) {
  91. var value = node[prop];
  92. var raw = node.raws[prop];
  93. if (raw && raw.value === value) {
  94. return raw.sss || raw.raw;
  95. } else {
  96. return value;
  97. }
  98. };
  99. return Stringifier;
  100. }();
  101. exports.default = Stringifier;
  102. module.exports = exports['default'];
  103. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0cmluZ2lmaWVyLmVzNiJdLCJuYW1lcyI6WyJkZWZhdWx0UmF3IiwiY29sb24iLCJpbmRlbnQiLCJjb21tZW50TGVmdCIsImNvbW1lbnRSaWdodCIsIlN0cmluZ2lmaWVyIiwiYnVpbGRlciIsInN0cmluZ2lmeSIsIm5vZGUiLCJzZW1pY29sb24iLCJ0eXBlIiwicm9vdCIsImJvZHkiLCJyYXdzIiwiYWZ0ZXIiLCJjb21tZW50IiwibGVmdCIsInJpZ2h0IiwiaGFzIiwiaW5saW5lIiwiaW5saW5lUmlnaHQiLCJleHRyYUluZGVudCIsInR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