query.js
3.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*!
* Stylus - Query
* Copyright (c) Automattic <developer.wordpress.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Node = require('./node');
/**
* Initialize a new `Query`.
*
* @api public
*/
var Query = module.exports = function Query(){
Node.call(this);
this.nodes = [];
this.type = '';
this.predicate = '';
};
/**
* Inherit from `Node.prototype`.
*/
Query.prototype.__proto__ = Node.prototype;
/**
* Return a clone of this node.
*
* @return {Node}
* @api public
*/
Query.prototype.clone = function(parent){
var clone = new Query;
clone.predicate = this.predicate;
clone.type = this.type;
for (var i = 0, len = this.nodes.length; i < len; ++i) {
clone.push(this.nodes[i].clone(parent, clone));
}
clone.lineno = this.lineno;
clone.column = this.column;
clone.filename = this.filename;
return clone;
};
/**
* Push the given `feature`.
*
* @param {Feature} feature
* @api public
*/
Query.prototype.push = function(feature){
this.nodes.push(feature);
};
/**
* Return resolved type of this query.
*
* @return {String}
* @api private
*/
Query.prototype.__defineGetter__('resolvedType', function(){
if (this.type) {
return this.type.nodeName
? this.type.string
: this.type;
}
});
/**
* Return resolved predicate of this query.
*
* @return {String}
* @api private
*/
Query.prototype.__defineGetter__('resolvedPredicate', function(){
if (this.predicate) {
return this.predicate.nodeName
? this.predicate.string
: this.predicate;
}
});
/**
* Merges this query with the `other`.
*
* @param {Query} other
* @return {Query}
* @api private
*/
Query.prototype.merge = function(other){
var query = new Query
, p1 = this.resolvedPredicate
, p2 = other.resolvedPredicate
, t1 = this.resolvedType
, t2 = other.resolvedType
, type, pred;
// Stolen from Sass :D
t1 = t1 || t2;
t2 = t2 || t1;
if (('not' == p1) ^ ('not' == p2)) {
if (t1 == t2) return;
type = ('not' == p1) ? t2 : t1;
pred = ('not' == p1) ? p2 : p1;
} else if (('not' == p1) && ('not' == p2)) {
if (t1 != t2) return;
type = t1;
pred = 'not';
} else if (t1 != t2) {
return;
} else {
type = t1;
pred = p1 || p2;
}
query.predicate = pred;
query.type = type;
query.nodes = this.nodes.concat(other.nodes);
return query;
};
/**
* Return "<a> and <b> and <c>"
*
* @return {String}
* @api public
*/
Query.prototype.toString = function(){
var pred = this.predicate ? this.predicate + ' ' : ''
, type = this.type || ''
, len = this.nodes.length
, str = pred + type;
if (len) {
str += (type && ' and ') + this.nodes.map(function(expr){
return expr.toString();
}).join(' and ');
}
return str;
};
/**
* Return a JSON representation of this node.
*
* @return {Object}
* @api public
*/
Query.prototype.toJSON = function(){
return {
__type: 'Query',
predicate: this.predicate,
type: this.type,
nodes: this.nodes,
lineno: this.lineno,
column: this.column,
filename: this.filename
};
};