UNPKG

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