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.

50 lines
1.3 KiB

4 years ago
  1. var all = require('./helpers').all;
  2. function store(serializeContext, token) {
  3. var value = typeof token == 'string' ?
  4. token :
  5. token[1];
  6. var wrap = serializeContext.wrap;
  7. wrap(serializeContext, value);
  8. track(serializeContext, value);
  9. serializeContext.output.push(value);
  10. }
  11. function wrap(serializeContext, value) {
  12. if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
  13. track(serializeContext, serializeContext.format.breakWith);
  14. serializeContext.output.push(serializeContext.format.breakWith);
  15. }
  16. }
  17. function track(serializeContext, value) {
  18. var parts = value.split('\n');
  19. serializeContext.line += parts.length - 1;
  20. serializeContext.column = parts.length > 1 ? 0 : (serializeContext.column + parts.pop().length);
  21. }
  22. function serializeStyles(tokens, context) {
  23. var serializeContext = {
  24. column: 0,
  25. format: context.options.format,
  26. indentBy: 0,
  27. indentWith: '',
  28. line: 1,
  29. output: [],
  30. spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
  31. store: store,
  32. wrap: context.options.format.wrapAt ?
  33. wrap :
  34. function () { /* noop */ }
  35. };
  36. all(serializeContext, tokens);
  37. return {
  38. styles: serializeContext.output.join('')
  39. };
  40. }
  41. module.exports = serializeStyles;