block.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var Node = require('./node');
  3. /**
  4. * Initialize a new `Block` with an optional `node`.
  5. *
  6. * @param {Node} node
  7. * @api public
  8. */
  9. var Block = module.exports = function Block(node){
  10. this.nodes = [];
  11. if (node) this.push(node);
  12. };
  13. // Inherit from `Node`.
  14. Block.prototype = Object.create(Node.prototype);
  15. Block.prototype.constructor = Block;
  16. Block.prototype.type = 'Block';
  17. /**
  18. * Block flag.
  19. */
  20. Block.prototype.isBlock = true;
  21. /**
  22. * Replace the nodes in `other` with the nodes
  23. * in `this` block.
  24. *
  25. * @param {Block} other
  26. * @api private
  27. */
  28. Block.prototype.replace = function(other){
  29. var err = new Error('block.replace is deprecated and will be removed in v2.0.0');
  30. console.warn(err.stack);
  31. other.nodes = this.nodes;
  32. };
  33. /**
  34. * Push the given `node`.
  35. *
  36. * @param {Node} node
  37. * @return {Number}
  38. * @api public
  39. */
  40. Block.prototype.push = function(node){
  41. return this.nodes.push(node);
  42. };
  43. /**
  44. * Check if this block is empty.
  45. *
  46. * @return {Boolean}
  47. * @api public
  48. */
  49. Block.prototype.isEmpty = function(){
  50. return 0 == this.nodes.length;
  51. };
  52. /**
  53. * Unshift the given `node`.
  54. *
  55. * @param {Node} node
  56. * @return {Number}
  57. * @api public
  58. */
  59. Block.prototype.unshift = function(node){
  60. return this.nodes.unshift(node);
  61. };
  62. /**
  63. * Return the "last" block, or the first `yield` node.
  64. *
  65. * @return {Block}
  66. * @api private
  67. */
  68. Block.prototype.includeBlock = function(){
  69. var ret = this
  70. , node;
  71. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  72. node = this.nodes[i];
  73. if (node.yield) return node;
  74. else if (node.textOnly) continue;
  75. else if (node.includeBlock) ret = node.includeBlock();
  76. else if (node.block && !node.block.isEmpty()) ret = node.block.includeBlock();
  77. if (ret.yield) return ret;
  78. }
  79. return ret;
  80. };
  81. /**
  82. * Return a clone of this block.
  83. *
  84. * @return {Block}
  85. * @api private
  86. */
  87. Block.prototype.clone = function(){
  88. var err = new Error('block.clone is deprecated and will be removed in v2.0.0');
  89. console.warn(err.stack);
  90. var clone = new Block;
  91. for (var i = 0, len = this.nodes.length; i < len; ++i) {
  92. clone.push(this.nodes[i].clone());
  93. }
  94. return clone;
  95. };