compress.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Module dependencies.
  3. */
  4. var Base = require('./compiler');
  5. var inherits = require('inherits');
  6. /**
  7. * Expose compiler.
  8. */
  9. module.exports = Compiler;
  10. /**
  11. * Initialize a new `Compiler`.
  12. */
  13. function Compiler(options) {
  14. Base.call(this, options);
  15. }
  16. /**
  17. * Inherit from `Base.prototype`.
  18. */
  19. inherits(Compiler, Base);
  20. /**
  21. * Compile `node`.
  22. */
  23. Compiler.prototype.compile = function(node){
  24. return node.stylesheet
  25. .rules.map(this.visit, this)
  26. .join('');
  27. };
  28. /**
  29. * Visit comment node.
  30. */
  31. Compiler.prototype.comment = function(node){
  32. return this.emit('', node.position);
  33. };
  34. /**
  35. * Visit import node.
  36. */
  37. Compiler.prototype.import = function(node){
  38. return this.emit('@import ' + node.import + ';', node.position);
  39. };
  40. /**
  41. * Visit media node.
  42. */
  43. Compiler.prototype.media = function(node){
  44. return this.emit('@media ' + node.media, node.position)
  45. + this.emit('{')
  46. + this.mapVisit(node.rules)
  47. + this.emit('}');
  48. };
  49. /**
  50. * Visit document node.
  51. */
  52. Compiler.prototype.document = function(node){
  53. var doc = '@' + (node.vendor || '') + 'document ' + node.document;
  54. return this.emit(doc, node.position)
  55. + this.emit('{')
  56. + this.mapVisit(node.rules)
  57. + this.emit('}');
  58. };
  59. /**
  60. * Visit charset node.
  61. */
  62. Compiler.prototype.charset = function(node){
  63. return this.emit('@charset ' + node.charset + ';', node.position);
  64. };
  65. /**
  66. * Visit namespace node.
  67. */
  68. Compiler.prototype.namespace = function(node){
  69. return this.emit('@namespace ' + node.namespace + ';', node.position);
  70. };
  71. /**
  72. * Visit supports node.
  73. */
  74. Compiler.prototype.supports = function(node){
  75. return this.emit('@supports ' + node.supports, node.position)
  76. + this.emit('{')
  77. + this.mapVisit(node.rules)
  78. + this.emit('}');
  79. };
  80. /**
  81. * Visit keyframes node.
  82. */
  83. Compiler.prototype.keyframes = function(node){
  84. return this.emit('@'
  85. + (node.vendor || '')
  86. + 'keyframes '
  87. + node.name, node.position)
  88. + this.emit('{')
  89. + this.mapVisit(node.keyframes)
  90. + this.emit('}');
  91. };
  92. /**
  93. * Visit keyframe node.
  94. */
  95. Compiler.prototype.keyframe = function(node){
  96. var decls = node.declarations;
  97. return this.emit(node.values.join(','), node.position)
  98. + this.emit('{')
  99. + this.mapVisit(decls)
  100. + this.emit('}');
  101. };
  102. /**
  103. * Visit page node.
  104. */
  105. Compiler.prototype.page = function(node){
  106. var sel = node.selectors.length
  107. ? node.selectors.join(', ')
  108. : '';
  109. return this.emit('@page ' + sel, node.position)
  110. + this.emit('{')
  111. + this.mapVisit(node.declarations)
  112. + this.emit('}');
  113. };
  114. /**
  115. * Visit font-face node.
  116. */
  117. Compiler.prototype['font-face'] = function(node){
  118. return this.emit('@font-face', node.position)
  119. + this.emit('{')
  120. + this.mapVisit(node.declarations)
  121. + this.emit('}');
  122. };
  123. /**
  124. * Visit host node.
  125. */
  126. Compiler.prototype.host = function(node){
  127. return this.emit('@host', node.position)
  128. + this.emit('{')
  129. + this.mapVisit(node.rules)
  130. + this.emit('}');
  131. };
  132. /**
  133. * Visit custom-media node.
  134. */
  135. Compiler.prototype['custom-media'] = function(node){
  136. return this.emit('@custom-media ' + node.name + ' ' + node.media + ';', node.position);
  137. };
  138. /**
  139. * Visit rule node.
  140. */
  141. Compiler.prototype.rule = function(node){
  142. var decls = node.declarations;
  143. if (!decls.length) return '';
  144. return this.emit(node.selectors.join(','), node.position)
  145. + this.emit('{')
  146. + this.mapVisit(decls)
  147. + this.emit('}');
  148. };
  149. /**
  150. * Visit declaration node.
  151. */
  152. Compiler.prototype.declaration = function(node){
  153. return this.emit(node.property + ':' + node.value, node.position) + this.emit(';');
  154. };