1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Node = require('./node');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var Block = module.exports = function Block(parent, node){
|
22 | Node.call(this);
|
23 | this.nodes = [];
|
24 | this.parent = parent;
|
25 | this.node = node;
|
26 | this.scope = true;
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Block.prototype.__proto__ = Node.prototype;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | Block.prototype.__defineGetter__('hasProperties', function(){
|
43 | for (var i = 0, len = this.nodes.length; i < len; ++i) {
|
44 | if ('property' == this.nodes[i].nodeName) {
|
45 | return true;
|
46 | }
|
47 | }
|
48 | });
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | Block.prototype.__defineGetter__('hasMedia', function(){
|
58 | for (var i = 0, len = this.nodes.length; i < len; ++i) {
|
59 | var nodeName = this.nodes[i].nodeName;
|
60 | if ('media' == nodeName) {
|
61 | return true;
|
62 | }
|
63 | }
|
64 | return false;
|
65 | });
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | Block.prototype.__defineGetter__('isEmpty', function(){
|
75 | return !this.nodes.length;
|
76 | });
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Block.prototype.clone = function(parent, node){
|
86 | parent = parent || this.parent;
|
87 | var clone = new Block(parent, node || this.node);
|
88 | clone.lineno = this.lineno;
|
89 | clone.column = this.column;
|
90 | clone.filename = this.filename;
|
91 | clone.scope = this.scope;
|
92 | this.nodes.forEach(function(node){
|
93 | clone.push(node.clone(clone, clone));
|
94 | });
|
95 | return clone;
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | Block.prototype.push = function(node){
|
106 | this.nodes.push(node);
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Block.prototype.toJSON = function(){
|
117 | return {
|
118 | __type: 'Block',
|
119 |
|
120 |
|
121 | scope: this.scope,
|
122 | lineno: this.lineno,
|
123 | column: this.column,
|
124 | filename: this.filename,
|
125 | nodes: this.nodes
|
126 | };
|
127 | };
|