block.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * Stylus - Block
  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 `Block` node with `parent` Block.
  12. *
  13. * @param {Block} parent
  14. * @api public
  15. */
  16. var Block = module.exports = function Block(parent, node){
  17. Node.call(this);
  18. this.nodes = [];
  19. this.parent = parent;
  20. this.node = node;
  21. this.scope = true;
  22. };
  23. /**
  24. * Inherit from `Node.prototype`.
  25. */
  26. Block.prototype.__proto__ = Node.prototype;
  27. /**
  28. * Check if this block has properties..
  29. *
  30. * @return {Boolean}
  31. * @api public
  32. */
  33. Block.prototype.__defineGetter__('hasProperties', function(){
  34. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  35. if ('property' == this.nodes[i].nodeName) {
  36. return true;
  37. }
  38. }
  39. });
  40. /**
  41. * Check if this block has @media nodes.
  42. *
  43. * @return {Boolean}
  44. * @api public
  45. */
  46. Block.prototype.__defineGetter__('hasMedia', function(){
  47. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  48. var nodeName = this.nodes[i].nodeName;
  49. if ('media' == nodeName) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. });
  55. /**
  56. * Check if this block is empty.
  57. *
  58. * @return {Boolean}
  59. * @api public
  60. */
  61. Block.prototype.__defineGetter__('isEmpty', function(){
  62. return !this.nodes.length || this.nodes.every(function(n){return n.nodeName == 'comment'});
  63. });
  64. /**
  65. * Return a clone of this node.
  66. *
  67. * @return {Node}
  68. * @api public
  69. */
  70. Block.prototype.clone = function(parent, node){
  71. parent = parent || this.parent;
  72. var clone = new Block(parent, node || this.node);
  73. clone.lineno = this.lineno;
  74. clone.column = this.column;
  75. clone.filename = this.filename;
  76. clone.scope = this.scope;
  77. this.nodes.forEach(function(node){
  78. clone.push(node.clone(clone, clone));
  79. });
  80. return clone;
  81. };
  82. /**
  83. * Push a `node` to this block.
  84. *
  85. * @param {Node} node
  86. * @api public
  87. */
  88. Block.prototype.push = function(node){
  89. this.nodes.push(node);
  90. };
  91. /**
  92. * Return a JSON representation of this node.
  93. *
  94. * @return {Object}
  95. * @api public
  96. */
  97. Block.prototype.toJSON = function(){
  98. return {
  99. __type: 'Block',
  100. // parent: this.parent,
  101. // node: this.node,
  102. scope: this.scope,
  103. lineno: this.lineno,
  104. column: this.column,
  105. filename: this.filename,
  106. nodes: this.nodes
  107. };
  108. };