123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function stringifyNode(node, custom) {
  2. var type = node.type;
  3. var value = node.value;
  4. var buf;
  5. var customResult;
  6. if (custom && (customResult = custom(node)) !== undefined) {
  7. return customResult;
  8. } else if (type === 'word' || type === 'space') {
  9. return value;
  10. } else if (type === 'string') {
  11. buf = node.quote || '';
  12. return buf + value + (node.unclosed ? '' : buf);
  13. } else if (type === 'comment') {
  14. return '/*' + value + (node.unclosed ? '' : '*/');
  15. } else if (type === 'div') {
  16. return (node.before || '') + value + (node.after || '');
  17. } else if (Array.isArray(node.nodes)) {
  18. buf = stringify(node.nodes);
  19. if (type !== 'function') {
  20. return buf;
  21. }
  22. return value + '(' + (node.before || '') + buf + (node.after || '') + (node.unclosed ? '' : ')');
  23. }
  24. return value;
  25. }
  26. function stringify(nodes, custom) {
  27. var result, i;
  28. if (Array.isArray(nodes)) {
  29. result = '';
  30. for (i = nodes.length - 1; ~i; i -= 1) {
  31. result = stringifyNode(nodes[i], custom) + result;
  32. }
  33. return result;
  34. }
  35. return stringifyNode(nodes, custom);
  36. }
  37. module.exports = stringify;