UNPKG

2.25 kBJavaScriptView Raw
1
2/*!
3 * Stylus - Block
4 * Copyright (c) Automattic <developer.wordpress.com>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
12var Node = require('./node');
13
14/**
15 * Initialize a new `Block` node with `parent` Block.
16 *
17 * @param {Block} parent
18 * @api public
19 */
20
21var 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 * Inherit from `Node.prototype`.
31 */
32
33Block.prototype.__proto__ = Node.prototype;
34
35/**
36 * Check if this block has properties..
37 *
38 * @return {Boolean}
39 * @api public
40 */
41
42Block.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 * Check if this block has @media nodes.
52 *
53 * @return {Boolean}
54 * @api public
55 */
56
57Block.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 * Check if this block is empty.
69 *
70 * @return {Boolean}
71 * @api public
72 */
73
74Block.prototype.__defineGetter__('isEmpty', function(){
75 return !this.nodes.length;
76});
77
78/**
79 * Return a clone of this node.
80 *
81 * @return {Node}
82 * @api public
83 */
84
85Block.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 * Push a `node` to this block.
100 *
101 * @param {Node} node
102 * @api public
103 */
104
105Block.prototype.push = function(node){
106 this.nodes.push(node);
107};
108
109/**
110 * Return a JSON representation of this node.
111 *
112 * @return {Object}
113 * @api public
114 */
115
116Block.prototype.toJSON = function(){
117 return {
118 __type: 'Block',
119 // parent: this.parent,
120 // node: this.node,
121 scope: this.scope,
122 lineno: this.lineno,
123 column: this.column,
124 filename: this.filename,
125 nodes: this.nodes
126 };
127};