You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
34 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var DEFAULT_RAW = {
  5. colon: ': ',
  6. indent: ' ',
  7. beforeDecl: '\n',
  8. beforeRule: '\n',
  9. beforeOpen: ' ',
  10. beforeClose: '\n',
  11. beforeComment: '\n',
  12. after: '\n',
  13. emptyBody: '',
  14. commentLeft: ' ',
  15. commentRight: ' ',
  16. semicolon: false
  17. };
  18. function capitalize(str) {
  19. return str[0].toUpperCase() + str.slice(1);
  20. }
  21. var Stringifier =
  22. /*#__PURE__*/
  23. function () {
  24. function Stringifier(builder) {
  25. this.builder = builder;
  26. }
  27. var _proto = Stringifier.prototype;
  28. _proto.stringify = function stringify(node, semicolon) {
  29. this[node.type](node, semicolon);
  30. };
  31. _proto.root = function root(node) {
  32. this.body(node);
  33. if (node.raws.after) this.builder(node.raws.after);
  34. };
  35. _proto.comment = function comment(node) {
  36. var left = this.raw(node, 'left', 'commentLeft');
  37. var right = this.raw(node, 'right', 'commentRight');
  38. this.builder('/*' + left + node.text + right + '*/', node);
  39. };
  40. _proto.decl = function decl(node, semicolon) {
  41. var between = this.raw(node, 'between', 'colon');
  42. var string = node.prop + between + this.rawValue(node, 'value');
  43. if (node.important) {
  44. string += node.raws.important || ' !important';
  45. }
  46. if (semicolon) string += ';';
  47. this.builder(string, node);
  48. };
  49. _proto.rule = function rule(node) {
  50. this.block(node, this.rawValue(node, 'selector'));
  51. if (node.raws.ownSemicolon) {
  52. this.builder(node.raws.ownSemicolon, node, 'end');
  53. }
  54. };
  55. _proto.atrule = function atrule(node, semicolon) {
  56. var name = '@' + node.name;
  57. var params = node.params ? this.rawValue(node, 'params') : '';
  58. if (typeof node.raws.afterName !== 'undefined') {
  59. name += node.raws.afterName;
  60. } else if (params) {
  61. name += ' ';
  62. }
  63. if (node.nodes) {
  64. this.block(node, name + params);
  65. } else {
  66. var end = (node.raws.between || '') + (semicolon ? ';' : '');
  67. this.builder(name + params + end, node);
  68. }
  69. };
  70. _proto.body = function body(node) {
  71. var last = node.nodes.length - 1;
  72. while (last > 0) {
  73. if (node.nodes[last].type !== 'comment') break;
  74. last -= 1;
  75. }
  76. var semicolon = this.raw(node, 'semicolon');
  77. for (var i = 0; i < node.nodes.length; i++) {
  78. var child = node.nodes[i];
  79. var before = this.raw(child, 'before');
  80. if (before) this.builder(before);
  81. this.stringify(child, last !== i || semicolon);
  82. }
  83. };
  84. _proto.block = function block(node, start) {
  85. var between = this.raw(node, 'between', 'beforeOpen');
  86. this.builder(start + between + '{', node, 'start');
  87. var after;
  88. if (node.nodes && node.nodes.length) {
  89. this.body(node);
  90. after = this.raw(node, 'after');
  91. } else {
  92. after = this.raw(node, 'after', 'emptyBody');
  93. }
  94. if (after) this.builder(after);
  95. this.builder('}', node, 'end');
  96. };
  97. _proto.raw = function raw(node, own, detect) {
  98. var value;
  99. if (!detect) detect = own; // Already had
  100. if (own) {
  101. value = node.raws[own];
  102. if (typeof value !== 'undefined') return value;
  103. }
  104. var parent = node.parent; // Hack for first rule in CSS
  105. if (detect === 'before') {
  106. if (!parent || parent.type === 'root' && parent.first === node) {
  107. return '';
  108. }
  109. } // Floating child without parent
  110. if (!parent) return DEFAULT_RAW[detect]; // Detect style by other nodes
  111. var root = node.root();
  112. if (!root.rawCache) root.rawCache = {};
  113. if (typeof root.rawCache[detect] !== 'undefined') {
  114. return root.rawCache[detect];
  115. }
  116. if (detect === 'before' || detect === 'after') {
  117. return this.beforeAfter(node, detect);
  118. } else {
  119. var method = 'raw' + capitalize(detect);
  120. if (this[method]) {
  121. value = this[method](root, node);
  122. } else {
  123. root.walk(function (i) {
  124. value = i.raws[own];
  125. if (typeof value !== 'undefined') return false;
  126. });
  127. }
  128. }
  129. if (typeof value === 'undefined') value = DEFAULT_RAW[detect];
  130. root.rawCache[detect] = value;
  131. return value;
  132. };
  133. _proto.rawSemicolon = function rawSemicolon(root) {
  134. var value;
  135. root.walk(function (i) {
  136. if (i.nodes && i.nodes.length && i.last.type === 'decl') {
  137. value = i.raws.semicolon;
  138. if (typeof value !== 'undefined') return false;
  139. }
  140. });
  141. return value;
  142. };
  143. _proto.rawEmptyBody = function rawEmptyBody(root) {
  144. var value;
  145. root.walk(function (i) {
  146. if (i.nodes && i.nodes.length === 0) {
  147. value = i.raws.after;
  148. if (typeof value !== 'undefined') return false;
  149. }
  150. });
  151. return value;
  152. };
  153. _proto.rawIndent = function rawIndent(root) {
  154. if (root.raws.indent) return root.raws.indent;
  155. var value;
  156. root.walk(function (i) {
  157. var p = i.parent;
  158. if (p && p !== root && p.parent && p.parent === root) {
  159. if (typeof i.raws.before !== 'undefined') {
  160. var parts = i.raws.before.split('\n');
  161. value = parts[parts.length - 1];
  162. value = value.replace(/[^\s]/g, '');
  163. return false;
  164. }
  165. }
  166. });
  167. return value;
  168. };
  169. _proto.rawBeforeComment = function rawBeforeComment(root, node) {
  170. var value;
  171. root.walkComments(function (i) {
  172. if (typeof i.raws.before !== 'undefined') {
  173. value = i.raws.before;
  174. if (value.indexOf('\n') !== -1) {
  175. value = value.replace(/[^\n]+$/, '');
  176. }
  177. return false;
  178. }
  179. });
  180. if (typeof value === 'undefined') {
  181. value = this.raw(node, null, 'beforeDecl');
  182. } else if (value) {
  183. value = value.replace(/[^\s]/g, '');
  184. }
  185. return value;
  186. };
  187. _proto.rawBeforeDecl = function rawBeforeDecl(root, node) {
  188. var value;
  189. root.walkDecls(function (i) {
  190. if (typeof i.raws.before !== 'undefined') {
  191. value = i.raws.before;
  192. if (value.indexOf('\n') !== -1) {
  193. value = value.replace(/[^\n]+$/, '');
  194. }
  195. return false;
  196. }
  197. });
  198. if (typeof value === 'undefined') {
  199. value = this.raw(node, null, 'beforeRule');
  200. } else if (value) {
  201. value = value.replace(/[^\s]/g, '');
  202. }
  203. return value;
  204. };
  205. _proto.rawBeforeRule = function rawBeforeRule(root) {
  206. var value;
  207. root.walk(function (i) {
  208. if (i.nodes && (i.parent !== root || root.first !== i)) {
  209. if (typeof i.raws.before !== 'undefined') {
  210. value = i.raws.before;
  211. if (value.indexOf('\n') !== -1) {
  212. value = value.replace(/[^\n]+$/, '');
  213. }
  214. return false;
  215. }
  216. }
  217. });
  218. if (value) value = value.replace(/[^\s]/g, '');
  219. return value;
  220. };
  221. _proto.rawBeforeClose = function rawBeforeClose(root) {
  222. var value;
  223. root.walk(function (i) {
  224. if (i.nodes && i.nodes.length > 0) {
  225. if (typeof i.raws.after !== 'undefined') {
  226. value = i.raws.after;
  227. if (value.indexOf('\n') !== -1) {
  228. value = value.replace(/[^\n]+$/, '');
  229. }
  230. return false;
  231. }
  232. }
  233. });
  234. if (value) value = value.replace(/[^\s]/g, '');
  235. return value;
  236. };
  237. _proto.rawBeforeOpen = function rawBeforeOpen(root) {
  238. var value;
  239. root.walk(function (i) {
  240. if (i.type !== 'decl') {
  241. value = i.raws.between;
  242. if (typeof value !== 'undefined') return false;
  243. }
  244. });
  245. return value;
  246. };
  247. _proto.rawColon = function rawColon(root) {
  248. var value;
  249. root.walkDecls(function (i) {
  250. if (typeof i.raws.between !== 'undefined') {
  251. value = i.raws.between.replace(/[^\s:]/g, '');
  252. return false;
  253. }
  254. });
  255. return value;
  256. };
  257. _proto.beforeAfter = function beforeAfter(node, detect) {
  258. var value;
  259. if (node.type === 'decl') {
  260. value = this.raw(node, null, 'beforeDecl');
  261. } else if (node.type === 'comment') {
  262. value = this.raw(node, null, 'beforeComment');
  263. } else if (detect === 'before') {
  264. value = this.raw(node, null, 'beforeRule');
  265. } else {
  266. value = this.raw(node, null, 'beforeClose');
  267. }
  268. var buf = node.parent;
  269. var depth = 0;
  270. while (buf && buf.type !== 'root') {
  271. depth += 1;
  272. buf = buf.parent;
  273. }
  274. if (value.indexOf('\n') !== -1) {
  275. var indent = this.raw(node, null, 'indent');
  276. if (indent.length) {
  277. for (var step = 0; step < depth; step++) {
  278. value += indent;
  279. }
  280. }
  281. }
  282. return value;
  283. };
  284. _proto.rawValue = function rawValue(node, prop) {
  285. var value = node[prop];
  286. var raw = node.raws[prop];
  287. if (raw && raw.value === value) {
  288. return raw.raw;
  289. }
  290. return value;
  291. };
  292. return Stringifier;
  293. }();
  294. var _default = Stringifier;
  295. exports.default = _default;
  296. module.exports = exports.default;
  297. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0cmluZ2lmaWVyLmVzNiJdLCJuYW1lcyI6WyJERUZBVUxUX1JBVyIsImNvbG9uIiwiaW5kZW50IiwiYmVmb3JlRGVjbCIsImJlZm9yZVJ1bGUiLCJiZWZvcmVPcGVuIiwiYmVmb3JlQ2xvc2UiLCJiZWZvcmVDb21tZW50IiwiYWZ0ZXIiLCJlbXB0eUJvZHkiLCJjb21tZW50TGVmdCIsImNvbW1lbnRSaWdodCIsInNlbWljb2xvbiIsImNhcGl0YWxpemUiLCJzdHIiLCJ0b1VwcGVyQ2FzZSIsInNsaWNlIiwiU3RyaW5naWZpZXIiLCJidWlsZGVyIiwic3RyaW5naWZ5Iiwibm9kZSIsInR5cGUiLCJyb290IiwiYm9keSIsInJhd3MiLCJjb21tZW50IiwibGVmdCIsInJhdyIsInJpZ2h0IiwidGV4dCIsImRlY2wiLCJiZXR3ZWVuIiwic3RyaW5nIiwicHJvcCIsInJhd1ZhbHVlIiwiaW1wb3J0YW50IiwicnVsZSIsImJsb2NrIiwib3duU2VtaWNvbG9uIiwiYXRydWxlIiwibmFtZSIsInBhcmFtcyIsImFmdGVyTmFtZSIsIm5vZGVzIiwiZW5kIiwibGFzdCIsImxlbmd0aCIsImkiLCJjaGlsZCIsImJlZm9yZSIsInN0YXJ0Iiwib3duIiwiZGV0ZWN0IiwidmFsdWUiLCJwYXJlbnQiLCJmaXJzdCIsInJhd0NhY2hlIiwiYmVmb3JlQWZ0ZXIiLCJtZXRob2QiLCJ3YWxrIiwicmF3U2VtaWNvbG9uIiwicmF3RW1wdHlCb2R5IiwicmF3SW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