atrule.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*!
  2. * Stylus - at-rule
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node');
  10. /**
  11. * Initialize a new at-rule node.
  12. *
  13. * @param {String} type
  14. * @api public
  15. */
  16. var Atrule = module.exports = function Atrule(type){
  17. Node.call(this);
  18. this.type = type;
  19. };
  20. /**
  21. * Inherit from `Node.prototype`.
  22. */
  23. Atrule.prototype.__proto__ = Node.prototype;
  24. /**
  25. * Check if at-rule's block has only properties.
  26. *
  27. * @return {Boolean}
  28. * @api public
  29. */
  30. Atrule.prototype.__defineGetter__('hasOnlyProperties', function(){
  31. if (!this.block) return false;
  32. var nodes = this.block.nodes;
  33. for (var i = 0, len = nodes.length; i < len; ++i) {
  34. var nodeName = nodes[i].nodeName;
  35. switch(nodes[i].nodeName) {
  36. case 'property':
  37. case 'expression':
  38. case 'comment':
  39. continue;
  40. default:
  41. return false;
  42. }
  43. }
  44. return true;
  45. });
  46. /**
  47. * Return a clone of this node.
  48. *
  49. * @return {Node}
  50. * @api public
  51. */
  52. Atrule.prototype.clone = function(parent){
  53. var clone = new Atrule(this.type);
  54. if (this.block) clone.block = this.block.clone(parent, clone);
  55. clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
  56. clone.lineno = this.lineno;
  57. clone.column = this.column;
  58. clone.filename = this.filename;
  59. return clone;
  60. };
  61. /**
  62. * Return a JSON representation of this node.
  63. *
  64. * @return {Object}
  65. * @api public
  66. */
  67. Atrule.prototype.toJSON = function(){
  68. var json = {
  69. __type: 'Atrule',
  70. type: this.type,
  71. segments: this.segments,
  72. lineno: this.lineno,
  73. column: this.column,
  74. filename: this.filename
  75. };
  76. if (this.block) json.block = this.block;
  77. return json;
  78. };
  79. /**
  80. * Return @<type>.
  81. *
  82. * @return {String}
  83. * @api public
  84. */
  85. Atrule.prototype.toString = function(){
  86. return '@' + this.type;
  87. };
  88. /**
  89. * Check if the at-rule's block has output nodes.
  90. *
  91. * @return {Boolean}
  92. * @api public
  93. */
  94. Atrule.prototype.__defineGetter__('hasOutput', function(){
  95. return !!this.block && hasOutput(this.block);
  96. });
  97. function hasOutput(block) {
  98. var nodes = block.nodes;
  99. // only placeholder selectors
  100. if (nodes.every(function(node){
  101. return 'group' == node.nodeName && node.hasOnlyPlaceholders;
  102. })) return false;
  103. // something visible
  104. return nodes.some(function(node) {
  105. switch (node.nodeName) {
  106. case 'property':
  107. case 'literal':
  108. case 'import':
  109. return true;
  110. case 'block':
  111. return hasOutput(node);
  112. default:
  113. if (node.block) return hasOutput(node.block);
  114. }
  115. });
  116. }