group.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. * Stylus - Group
  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 `Group`.
  12. *
  13. * @api public
  14. */
  15. var Group = module.exports = function Group(){
  16. Node.call(this);
  17. this.nodes = [];
  18. this.extends = [];
  19. };
  20. /**
  21. * Inherit from `Node.prototype`.
  22. */
  23. Group.prototype.__proto__ = Node.prototype;
  24. /**
  25. * Push the given `selector` node.
  26. *
  27. * @param {Selector} selector
  28. * @api public
  29. */
  30. Group.prototype.push = function(selector){
  31. this.nodes.push(selector);
  32. };
  33. /**
  34. * Return this set's `Block`.
  35. */
  36. Group.prototype.__defineGetter__('block', function(){
  37. return this.nodes[0].block;
  38. });
  39. /**
  40. * Assign `block` to each selector in this set.
  41. *
  42. * @param {Block} block
  43. * @api public
  44. */
  45. Group.prototype.__defineSetter__('block', function(block){
  46. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  47. this.nodes[i].block = block;
  48. }
  49. });
  50. /**
  51. * Check if this set has only placeholders.
  52. *
  53. * @return {Boolean}
  54. * @api public
  55. */
  56. Group.prototype.__defineGetter__('hasOnlyPlaceholders', function(){
  57. return this.nodes.every(function(selector) { return selector.isPlaceholder; });
  58. });
  59. /**
  60. * Return a clone of this node.
  61. *
  62. * @return {Node}
  63. * @api public
  64. */
  65. Group.prototype.clone = function(parent){
  66. var clone = new Group;
  67. clone.lineno = this.lineno;
  68. clone.column = this.column;
  69. this.nodes.forEach(function(node){
  70. clone.push(node.clone(parent, clone));
  71. });
  72. clone.filename = this.filename;
  73. clone.block = this.block.clone(parent, clone);
  74. return clone;
  75. };
  76. /**
  77. * Return a JSON representation of this node.
  78. *
  79. * @return {Object}
  80. * @api public
  81. */
  82. Group.prototype.toJSON = function(){
  83. return {
  84. __type: 'Group',
  85. nodes: this.nodes,
  86. block: this.block,
  87. lineno: this.lineno,
  88. column: this.column,
  89. filename: this.filename
  90. };
  91. };