1 | 'use strict';
|
2 |
|
3 | var Node = require('./node');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Block = module.exports = function Block(node){
|
13 | this.nodes = [];
|
14 | if (node) this.push(node);
|
15 | };
|
16 |
|
17 |
|
18 | Block.prototype = Object.create(Node.prototype);
|
19 | Block.prototype.constructor = Block;
|
20 |
|
21 | Block.prototype.type = 'Block';
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | Block.prototype.isBlock = true;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | Block.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 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Block.prototype.push = function(node){
|
53 | return this.nodes.push(node);
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | Block.prototype.isEmpty = function(){
|
64 | return 0 == this.nodes.length;
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | Block.prototype.unshift = function(node){
|
76 | return this.nodes.unshift(node);
|
77 | };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | Block.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 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Block.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 | };
|