stylus-source 0.15.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.DS_Store +0 -0
- data/README.md +3 -0
- data/lib/.DS_Store +0 -0
- data/lib/node_modules/cssom/.idea/CSSOM.iml +9 -0
- data/lib/node_modules/cssom/.idea/dictionaries/nv.xml +3 -0
- data/lib/node_modules/cssom/.idea/encodings.xml +5 -0
- data/lib/node_modules/cssom/.idea/misc.xml +17 -0
- data/lib/node_modules/cssom/.idea/modules.xml +9 -0
- data/lib/node_modules/cssom/.idea/projectCodeStyle.xml +82 -0
- data/lib/node_modules/cssom/.idea/vcs.xml +8 -0
- data/lib/node_modules/cssom/.idea/workspace.xml +467 -0
- data/lib/node_modules/cssom/.livereload +19 -0
- data/lib/node_modules/cssom/Jakefile +37 -0
- data/lib/node_modules/cssom/README.mdown +33 -0
- data/lib/node_modules/cssom/Rakefile +23 -0
- data/lib/node_modules/cssom/docs/.livereload +19 -0
- data/lib/node_modules/cssom/docs/bar.css +3 -0
- data/lib/node_modules/cssom/docs/demo.css +0 -0
- data/lib/node_modules/cssom/docs/foo.css +4 -0
- data/lib/node_modules/cssom/docs/parse.html +170 -0
- data/lib/node_modules/cssom/docs/parse2.html +431 -0
- data/lib/node_modules/cssom/index.html +100 -0
- data/lib/node_modules/cssom/lib/CSSImportRule.js +34 -0
- data/lib/node_modules/cssom/lib/CSSMediaRule.js +38 -0
- data/lib/node_modules/cssom/lib/CSSOM.js +3 -0
- data/lib/node_modules/cssom/lib/CSSRule.js +38 -0
- data/lib/node_modules/cssom/lib/CSSStyleDeclaration.js +130 -0
- data/lib/node_modules/cssom/lib/CSSStyleRule.js +187 -0
- data/lib/node_modules/cssom/lib/CSSStyleSheet.js +85 -0
- data/lib/node_modules/cssom/lib/MediaList.js +61 -0
- data/lib/node_modules/cssom/lib/StyleSheet.js +15 -0
- data/lib/node_modules/cssom/lib/clone.js +69 -0
- data/lib/node_modules/cssom/lib/index.js +10 -0
- data/lib/node_modules/cssom/lib/parse.js +195 -0
- data/lib/node_modules/cssom/media.html +17 -0
- data/lib/node_modules/cssom/package.json +30 -0
- data/lib/node_modules/cssom/plugins/toHTML.js +32 -0
- data/lib/node_modules/cssom/server/index.html +22 -0
- data/lib/node_modules/cssom/server/index.js +21 -0
- data/lib/node_modules/cssom/shorthands.html +21 -0
- data/lib/node_modules/cssom/test/CSSStyleDeclaration.test.js +35 -0
- data/lib/node_modules/cssom/test/CSSStyleRule.test.js +12 -0
- data/lib/node_modules/cssom/test/CSSStyleSheet.test.js +16 -0
- data/lib/node_modules/cssom/test/MediaList.test.js +21 -0
- data/lib/node_modules/cssom/test/clone.test.js +38 -0
- data/lib/node_modules/cssom/test/fixtures/dummy.css +3 -0
- data/lib/node_modules/cssom/test/helper.js +97 -0
- data/lib/node_modules/cssom/test/index.html +42 -0
- data/lib/node_modules/cssom/test/parse.test.js +346 -0
- data/lib/node_modules/cssom/test/vendor/qunit.css +189 -0
- data/lib/node_modules/cssom/test/vendor/qunit.js +1341 -0
- data/lib/node_modules/growl/History.md +16 -0
- data/lib/node_modules/growl/Readme.md +74 -0
- data/lib/node_modules/growl/lib/growl.js +82 -0
- data/lib/node_modules/growl/package.json +6 -0
- data/lib/node_modules/growl/test.js +17 -0
- data/lib/stylus/colors.js +156 -0
- data/lib/stylus/convert/css.js +130 -0
- data/lib/stylus/errors.js +58 -0
- data/lib/stylus/functions/image.js +120 -0
- data/lib/stylus/functions/index.js +722 -0
- data/lib/stylus/functions/index.styl +123 -0
- data/lib/stylus/functions/url.js +98 -0
- data/lib/stylus/lexer.js +728 -0
- data/lib/stylus/middleware.js +223 -0
- data/lib/stylus/nodes/arguments.js +65 -0
- data/lib/stylus/nodes/binop.js +54 -0
- data/lib/stylus/nodes/block.js +99 -0
- data/lib/stylus/nodes/boolean.js +103 -0
- data/lib/stylus/nodes/call.js +57 -0
- data/lib/stylus/nodes/charset.js +42 -0
- data/lib/stylus/nodes/comment.js +32 -0
- data/lib/stylus/nodes/each.js +56 -0
- data/lib/stylus/nodes/expression.js +168 -0
- data/lib/stylus/nodes/fontface.js +55 -0
- data/lib/stylus/nodes/function.js +104 -0
- data/lib/stylus/nodes/group.js +79 -0
- data/lib/stylus/nodes/hsla.js +256 -0
- data/lib/stylus/nodes/ident.js +127 -0
- data/lib/stylus/nodes/if.js +55 -0
- data/lib/stylus/nodes/import.js +30 -0
- data/lib/stylus/nodes/index.js +52 -0
- data/lib/stylus/nodes/jsliteral.js +32 -0
- data/lib/stylus/nodes/keyframes.js +78 -0
- data/lib/stylus/nodes/literal.js +92 -0
- data/lib/stylus/nodes/media.js +42 -0
- data/lib/stylus/nodes/node.js +209 -0
- data/lib/stylus/nodes/null.js +72 -0
- data/lib/stylus/nodes/page.js +43 -0
- data/lib/stylus/nodes/params.js +72 -0
- data/lib/stylus/nodes/property.js +72 -0
- data/lib/stylus/nodes/return.js +44 -0
- data/lib/stylus/nodes/rgba.js +335 -0
- data/lib/stylus/nodes/root.js +50 -0
- data/lib/stylus/nodes/selector.js +57 -0
- data/lib/stylus/nodes/string.js +120 -0
- data/lib/stylus/nodes/ternary.js +51 -0
- data/lib/stylus/nodes/unaryop.js +46 -0
- data/lib/stylus/nodes/unit.js +207 -0
- data/lib/stylus/parser.js +1514 -0
- data/lib/stylus/renderer.js +157 -0
- data/lib/stylus/source.rb +7 -0
- data/lib/stylus/stack/frame.js +66 -0
- data/lib/stylus/stack/index.js +146 -0
- data/lib/stylus/stack/scope.js +53 -0
- data/lib/stylus/stylus.js +102 -0
- data/lib/stylus/token.js +53 -0
- data/lib/stylus/utils.js +237 -0
- data/lib/stylus/visitor/compiler.js +472 -0
- data/lib/stylus/visitor/evaluator.js +1070 -0
- data/lib/stylus/visitor/index.js +31 -0
- data/stylus-source.gemspec +15 -0
- metadata +158 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - If
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node');
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Initialize a new `If` with the given `cond`.
|
16
|
+
*
|
17
|
+
* @param {Expression} cond
|
18
|
+
* @param {Boolean|Block} negate, block
|
19
|
+
* @api public
|
20
|
+
*/
|
21
|
+
|
22
|
+
var If = module.exports = function If(cond, negate){
|
23
|
+
Node.call(this);
|
24
|
+
this.cond = cond;
|
25
|
+
this.elses = [];
|
26
|
+
if (negate && negate.nodeName) {
|
27
|
+
this.block = negate;
|
28
|
+
} else {
|
29
|
+
this.negate = negate;
|
30
|
+
}
|
31
|
+
};
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Inherit from `Node.prototype`.
|
35
|
+
*/
|
36
|
+
|
37
|
+
If.prototype.__proto__ = Node.prototype;
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Return a clone of this node.
|
41
|
+
*
|
42
|
+
* @return {Node}
|
43
|
+
* @api public
|
44
|
+
*/
|
45
|
+
|
46
|
+
If.prototype.clone = function(){
|
47
|
+
var cond = this.cond.clone()
|
48
|
+
, block = this.block.clone();
|
49
|
+
var clone = new If(cond, block);
|
50
|
+
clone.elses = this.elses.map(function(node){ return node.clone(); });
|
51
|
+
clone.negate = this.negate;
|
52
|
+
clone.lineno = this.lineno;
|
53
|
+
clone.filename = this.filename;
|
54
|
+
return clone;
|
55
|
+
};
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - Import
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node');
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Initialize a new `Import` with the given `expr`.
|
16
|
+
*
|
17
|
+
* @param {Expression} expr
|
18
|
+
* @api public
|
19
|
+
*/
|
20
|
+
|
21
|
+
var Import = module.exports = function Import(expr){
|
22
|
+
Node.call(this);
|
23
|
+
this.path = expr;
|
24
|
+
};
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Inherit from `Node.prototype`.
|
28
|
+
*/
|
29
|
+
|
30
|
+
Import.prototype.__proto__ = Node.prototype;
|
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - nodes
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Constructors
|
10
|
+
*/
|
11
|
+
|
12
|
+
exports.Node = require('./node');
|
13
|
+
exports.Root = require('./root');
|
14
|
+
exports.Null = require('./null');
|
15
|
+
exports.Each = require('./each');
|
16
|
+
exports.If = require('./if');
|
17
|
+
exports.Call = require('./call');
|
18
|
+
exports.Page = require('./page');
|
19
|
+
exports.FontFace = require('./fontface');
|
20
|
+
exports.UnaryOp = require('./unaryop');
|
21
|
+
exports.BinOp = require('./binop');
|
22
|
+
exports.Ternary = require('./ternary');
|
23
|
+
exports.Block = require('./block');
|
24
|
+
exports.Unit = require('./unit');
|
25
|
+
exports.String = require('./string');
|
26
|
+
exports.HSLA = require('./hsla');
|
27
|
+
exports.RGBA = require('./rgba');
|
28
|
+
exports.Ident = require('./ident');
|
29
|
+
exports.Group = require('./group');
|
30
|
+
exports.Literal = require('./literal');
|
31
|
+
exports.JSLiteral = require('./jsliteral');
|
32
|
+
exports.Boolean = require('./boolean');
|
33
|
+
exports.Return = require('./return');
|
34
|
+
exports.Media = require('./media');
|
35
|
+
exports.Params = require('./params');
|
36
|
+
exports.Comment = require('./comment');
|
37
|
+
exports.Keyframes = require('./keyframes');
|
38
|
+
exports.Charset = require('./charset');
|
39
|
+
exports.Import = require('./import');
|
40
|
+
exports.Function = require('./function');
|
41
|
+
exports.Property = require('./property');
|
42
|
+
exports.Selector = require('./selector');
|
43
|
+
exports.Expression = require('./expression');
|
44
|
+
exports.Arguments = require('./arguments');
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Singletons.
|
48
|
+
*/
|
49
|
+
|
50
|
+
exports.true = new exports.Boolean(true);
|
51
|
+
exports.false = new exports.Boolean(false);
|
52
|
+
exports.null = new exports.Null;
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - JSLiteral
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node')
|
13
|
+
, nodes = require('./');
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Initialize a new `JSLiteral` with the given `str`.
|
17
|
+
*
|
18
|
+
* @param {String} str
|
19
|
+
* @api public
|
20
|
+
*/
|
21
|
+
|
22
|
+
var JSLiteral = module.exports = function JSLiteral(str){
|
23
|
+
Node.call(this);
|
24
|
+
this.val = str;
|
25
|
+
this.string = str;
|
26
|
+
};
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Inherit from `Node.prototype`.
|
30
|
+
*/
|
31
|
+
|
32
|
+
JSLiteral.prototype.__proto__ = Node.prototype;
|
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - Keyframes
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node');
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Initialize a new `Keyframes` with the given `name`,
|
16
|
+
* and optional vendor `prefix`.
|
17
|
+
*
|
18
|
+
* @param {String} name
|
19
|
+
* @param {String} prefix
|
20
|
+
* @api public
|
21
|
+
*/
|
22
|
+
|
23
|
+
var Keyframes = module.exports = function Keyframes(name, prefix){
|
24
|
+
Node.call(this);
|
25
|
+
this.name = name;
|
26
|
+
this.frames = [];
|
27
|
+
this.prefix = prefix || 'official';
|
28
|
+
};
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Inherit from `Node.prototype`.
|
32
|
+
*/
|
33
|
+
|
34
|
+
Keyframes.prototype.__proto__ = Node.prototype;
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Push the given `block` at `pos`.
|
38
|
+
*
|
39
|
+
* @param {Unit} pos
|
40
|
+
* @param {Block} block
|
41
|
+
* @api public
|
42
|
+
*/
|
43
|
+
|
44
|
+
Keyframes.prototype.push = function(pos, block){
|
45
|
+
this.frames.push({
|
46
|
+
pos: pos
|
47
|
+
, block: block
|
48
|
+
});
|
49
|
+
};
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Return a clone of this node.
|
53
|
+
*
|
54
|
+
* @return {Node}
|
55
|
+
* @api public
|
56
|
+
*/
|
57
|
+
|
58
|
+
Keyframes.prototype.clone = function(){
|
59
|
+
var clone = new Keyframes(this.name);
|
60
|
+
clone.lineno = this.lineno;
|
61
|
+
clone.prefix = this.prefix;
|
62
|
+
clone.frames = this.frames.map(function(node){
|
63
|
+
node.block = node.block.clone();
|
64
|
+
return node;
|
65
|
+
});
|
66
|
+
return clone;
|
67
|
+
};
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Return `@keyframes name`.
|
71
|
+
*
|
72
|
+
* @return {String}
|
73
|
+
* @api public
|
74
|
+
*/
|
75
|
+
|
76
|
+
Keyframes.prototype.toString = function(){
|
77
|
+
return '@keyframes ' + this.name;
|
78
|
+
};
|
@@ -0,0 +1,92 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - Literal
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node')
|
13
|
+
, nodes = require('./');
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Initialize a new `Literal` with the given `str`.
|
17
|
+
*
|
18
|
+
* @param {String} str
|
19
|
+
* @api public
|
20
|
+
*/
|
21
|
+
|
22
|
+
var Literal = module.exports = function Literal(str){
|
23
|
+
Node.call(this);
|
24
|
+
this.val = str;
|
25
|
+
this.string = str;
|
26
|
+
};
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Inherit from `Node.prototype`.
|
30
|
+
*/
|
31
|
+
|
32
|
+
Literal.prototype.__proto__ = Node.prototype;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Return hash.
|
36
|
+
*
|
37
|
+
* @return {String}
|
38
|
+
* @api public
|
39
|
+
*/
|
40
|
+
|
41
|
+
Literal.prototype.__defineGetter__('hash', function(){
|
42
|
+
return this.val;
|
43
|
+
});
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Return literal value.
|
47
|
+
*
|
48
|
+
* @return {String}
|
49
|
+
* @api public
|
50
|
+
*/
|
51
|
+
|
52
|
+
Literal.prototype.toString = function(){
|
53
|
+
return this.val;
|
54
|
+
};
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Coerce `other` to a literal.
|
58
|
+
*
|
59
|
+
* @param {Node} other
|
60
|
+
* @return {String}
|
61
|
+
* @api public
|
62
|
+
*/
|
63
|
+
|
64
|
+
Literal.prototype.coerce = function(other){
|
65
|
+
switch (other.nodeName) {
|
66
|
+
case 'ident':
|
67
|
+
case 'string':
|
68
|
+
case 'literal':
|
69
|
+
return new Literal(other.string);
|
70
|
+
default:
|
71
|
+
return Node.prototype.coerce.call(this, other);
|
72
|
+
}
|
73
|
+
};
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Operate on `right` with the given `op`.
|
77
|
+
*
|
78
|
+
* @param {String} op
|
79
|
+
* @param {Node} right
|
80
|
+
* @return {Node}
|
81
|
+
* @api public
|
82
|
+
*/
|
83
|
+
|
84
|
+
Literal.prototype.operate = function(op, right){
|
85
|
+
var val = right.first;
|
86
|
+
switch (op) {
|
87
|
+
case '+':
|
88
|
+
return new nodes.Literal(this.string + this.coerce(val).string);
|
89
|
+
default:
|
90
|
+
return Node.prototype.operate.call(this, op, right);
|
91
|
+
}
|
92
|
+
};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - Media
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Node = require('./node')
|
13
|
+
, nodes = require('./');
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Initialize a new `Media` with the given `val`
|
17
|
+
*
|
18
|
+
* @param {String} val
|
19
|
+
* @api public
|
20
|
+
*/
|
21
|
+
|
22
|
+
var Media = module.exports = function Media(val){
|
23
|
+
Node.call(this);
|
24
|
+
this.val = val;
|
25
|
+
};
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Inherit from `Node.prototype`.
|
29
|
+
*/
|
30
|
+
|
31
|
+
Media.prototype.__proto__ = Node.prototype;
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Return @media "val".
|
35
|
+
*
|
36
|
+
* @return {String}
|
37
|
+
* @api public
|
38
|
+
*/
|
39
|
+
|
40
|
+
Media.prototype.toString = function(){
|
41
|
+
return '@media ' + this.val;
|
42
|
+
};
|
@@ -0,0 +1,209 @@
|
|
1
|
+
|
2
|
+
/*!
|
3
|
+
* Stylus - Node
|
4
|
+
* Copyright(c) 2010 LearnBoost <dev@learnboost.com>
|
5
|
+
* MIT Licensed
|
6
|
+
*/
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Module dependencies.
|
10
|
+
*/
|
11
|
+
|
12
|
+
var Evaluator = require('../visitor/evaluator')
|
13
|
+
, utils = require('../utils')
|
14
|
+
, nodes = require('./');
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Node constructor.
|
18
|
+
*
|
19
|
+
* @api public
|
20
|
+
*/
|
21
|
+
|
22
|
+
var Node = module.exports = function Node(){
|
23
|
+
this.lineno = nodes.lineno;
|
24
|
+
Object.defineProperty(this, 'filename', { writable: true, value: nodes.filename });
|
25
|
+
};
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Return this node.
|
29
|
+
*
|
30
|
+
* @return {Node}
|
31
|
+
* @api public
|
32
|
+
*/
|
33
|
+
|
34
|
+
Node.prototype.__defineGetter__('first', function(){
|
35
|
+
return this;
|
36
|
+
});
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Return hash.
|
40
|
+
*
|
41
|
+
* @return {String}
|
42
|
+
* @api public
|
43
|
+
*/
|
44
|
+
|
45
|
+
Node.prototype.__defineGetter__('hash', function(){
|
46
|
+
return this.val;
|
47
|
+
});
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Return node name.
|
51
|
+
*
|
52
|
+
* @return {String}
|
53
|
+
* @api public
|
54
|
+
*/
|
55
|
+
|
56
|
+
Node.prototype.__defineGetter__('nodeName', function(){
|
57
|
+
return this.constructor.name.toLowerCase();
|
58
|
+
});
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Return this node.
|
62
|
+
*
|
63
|
+
* @return {Node}
|
64
|
+
* @api public
|
65
|
+
*/
|
66
|
+
|
67
|
+
Node.prototype.clone = function(){
|
68
|
+
return this;
|
69
|
+
};
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Nodes by default evaluate to themselves.
|
73
|
+
*
|
74
|
+
* @return {Node}
|
75
|
+
* @api public
|
76
|
+
*/
|
77
|
+
|
78
|
+
Node.prototype.eval = function(){
|
79
|
+
return new Evaluator(this).evaluate();
|
80
|
+
};
|
81
|
+
|
82
|
+
/**
|
83
|
+
* Return true.
|
84
|
+
*
|
85
|
+
* @return {Boolean}
|
86
|
+
* @api public
|
87
|
+
*/
|
88
|
+
|
89
|
+
Node.prototype.toBoolean = function(){
|
90
|
+
return nodes.true;
|
91
|
+
};
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Return false if `op` is generally not coerced.
|
95
|
+
*
|
96
|
+
* @param {String} op
|
97
|
+
* @return {Boolean}
|
98
|
+
* @api private
|
99
|
+
*/
|
100
|
+
|
101
|
+
Node.prototype.shouldCoerce = function(op){
|
102
|
+
switch (op) {
|
103
|
+
case 'is a':
|
104
|
+
case 'in':
|
105
|
+
case '||':
|
106
|
+
case '&&':
|
107
|
+
return false;
|
108
|
+
default:
|
109
|
+
return true;
|
110
|
+
}
|
111
|
+
};
|
112
|
+
|
113
|
+
/**
|
114
|
+
* Operate on `right` with the given `op`.
|
115
|
+
*
|
116
|
+
* @param {String} op
|
117
|
+
* @param {Node} right
|
118
|
+
* @return {Node}
|
119
|
+
* @api public
|
120
|
+
*/
|
121
|
+
|
122
|
+
Node.prototype.operate = function(op, right){
|
123
|
+
switch (op) {
|
124
|
+
case 'is a':
|
125
|
+
if ('string' == right.nodeName) {
|
126
|
+
return nodes.Boolean(this.nodeName == right.val);
|
127
|
+
} else {
|
128
|
+
throw new Error('"is a" expects a string, got ' + right.toString());
|
129
|
+
}
|
130
|
+
case '==':
|
131
|
+
return nodes.Boolean(this.hash == right.hash);
|
132
|
+
case '!=':
|
133
|
+
return nodes.Boolean(this.hash != right.hash);
|
134
|
+
case '>=':
|
135
|
+
return nodes.Boolean(this.hash >= right.hash);
|
136
|
+
case '<=':
|
137
|
+
return nodes.Boolean(this.hash <= right.hash);
|
138
|
+
case '>':
|
139
|
+
return nodes.Boolean(this.hash > right.hash);
|
140
|
+
case '<':
|
141
|
+
return nodes.Boolean(this.hash < right.hash);
|
142
|
+
case '||':
|
143
|
+
return this.toBoolean().isTrue
|
144
|
+
? this
|
145
|
+
: right;
|
146
|
+
case 'in':
|
147
|
+
var vals = utils.unwrap(right).nodes
|
148
|
+
, hash = this.hash;
|
149
|
+
if (!vals) throw new Error('"in" given invalid right-hand operand, expecting an expression');
|
150
|
+
for (var i = 0, len = vals.length; i < len; ++i) {
|
151
|
+
if (hash == vals[i].hash) {
|
152
|
+
return nodes.true;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
return nodes.false;
|
156
|
+
case '&&':
|
157
|
+
var a = this.toBoolean()
|
158
|
+
, b = right.toBoolean();
|
159
|
+
return a.isTrue && b.isTrue
|
160
|
+
? right
|
161
|
+
: a.isFalse
|
162
|
+
? this
|
163
|
+
: right;
|
164
|
+
default:
|
165
|
+
if ('[]' == op) {
|
166
|
+
var msg = 'cannot perform '
|
167
|
+
+ this
|
168
|
+
+ '[' + right + ']';
|
169
|
+
} else {
|
170
|
+
var msg = 'cannot perform'
|
171
|
+
+ ' ' + this
|
172
|
+
+ ' ' + op
|
173
|
+
+ ' ' + right;
|
174
|
+
}
|
175
|
+
throw new Error(msg);
|
176
|
+
}
|
177
|
+
};
|
178
|
+
|
179
|
+
/**
|
180
|
+
* Initialize a new `CoercionError` with the given `msg`.
|
181
|
+
*
|
182
|
+
* @param {String} msg
|
183
|
+
* @api private
|
184
|
+
*/
|
185
|
+
|
186
|
+
function CoercionError(msg) {
|
187
|
+
this.name = 'CoercionError'
|
188
|
+
this.message = msg
|
189
|
+
Error.captureStackTrace(this, CoercionError);
|
190
|
+
}
|
191
|
+
|
192
|
+
/**
|
193
|
+
* Inherit from `Error.prototype`.
|
194
|
+
*/
|
195
|
+
|
196
|
+
CoercionError.prototype.__proto__ = Error.prototype;
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Default coercion throws.
|
200
|
+
*
|
201
|
+
* @param {Node} other
|
202
|
+
* @return {Node}
|
203
|
+
* @api public
|
204
|
+
*/
|
205
|
+
|
206
|
+
Node.prototype.coerce = function(other){
|
207
|
+
if (other.nodeName == this.nodeName) return other;
|
208
|
+
throw new CoercionError('cannot coerce ' + other + ' to ' + this.nodeName);
|
209
|
+
};
|