UNPKG

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