atrule.js
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*!
* Stylus - at-rule
* Copyright (c) Automattic <developer.wordpress.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Node = require('./node');
/**
* Initialize a new at-rule node.
*
* @param {String} type
* @api public
*/
var Atrule = module.exports = function Atrule(type){
Node.call(this);
this.type = type;
};
/**
* Inherit from `Node.prototype`.
*/
Atrule.prototype.__proto__ = Node.prototype;
/**
* Check if at-rule's block has only properties.
*
* @return {Boolean}
* @api public
*/
Atrule.prototype.__defineGetter__('hasOnlyProperties', function(){
if (!this.block) return false;
var nodes = this.block.nodes;
for (var i = 0, len = nodes.length; i < len; ++i) {
var nodeName = nodes[i].nodeName;
switch(nodes[i].nodeName) {
case 'property':
case 'expression':
case 'comment':
continue;
default:
return false;
}
}
return true;
});
/**
* Return a clone of this node.
*
* @return {Node}
* @api public
*/
Atrule.prototype.clone = function(parent){
var clone = new Atrule(this.type);
if (this.block) clone.block = this.block.clone(parent, clone);
clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
clone.lineno = this.lineno;
clone.column = this.column;
clone.filename = this.filename;
return clone;
};
/**
* Return a JSON representation of this node.
*
* @return {Object}
* @api public
*/
Atrule.prototype.toJSON = function(){
var json = {
__type: 'Atrule',
type: this.type,
segments: this.segments,
lineno: this.lineno,
column: this.column,
filename: this.filename
};
if (this.block) json.block = this.block;
return json;
};
/**
* Return @<type>.
*
* @return {String}
* @api public
*/
Atrule.prototype.toString = function(){
return '@' + this.type;
};
/**
* Check if the at-rule's block has output nodes.
*
* @return {Boolean}
* @api public
*/
Atrule.prototype.__defineGetter__('hasOutput', function(){
return !!this.block && hasOutput(this.block);
});
function hasOutput(block) {
var nodes = block.nodes;
// only placeholder selectors
if (nodes.every(function(node){
return 'group' == node.nodeName && node.hasOnlyPlaceholders;
})) return false;
// something visible
return nodes.some(function(node) {
switch (node.nodeName) {
case 'property':
case 'literal':
case 'import':
return true;
case 'block':
return hasOutput(node);
default:
if (node.block) return hasOutput(node.block);
}
});
}