1 |
|
---|
2 | /*!
|
---|
3 | * Stylus - Ident
|
---|
4 | * Copyright (c) Automattic <developer.wordpress.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 `Ident` by `name` with the given `val` node.
|
---|
17 | *
|
---|
18 | * @param {String} name
|
---|
19 | * @param {Node} val
|
---|
20 | * @api public
|
---|
21 | */
|
---|
22 |
|
---|
23 | var Ident = module.exports = function Ident(name, val, mixin){
|
---|
24 | Node.call(this);
|
---|
25 | this.name = name;
|
---|
26 | this.string = name;
|
---|
27 | this.val = val || nodes.null;
|
---|
28 | this.mixin = !!mixin;
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Check if the variable has a value.
|
---|
33 | *
|
---|
34 | * @return {Boolean}
|
---|
35 | * @api public
|
---|
36 | */
|
---|
37 |
|
---|
38 | Ident.prototype.__defineGetter__('isEmpty', function(){
|
---|
39 | return undefined == this.val;
|
---|
40 | });
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Return hash.
|
---|
44 | *
|
---|
45 | * @return {String}
|
---|
46 | * @api public
|
---|
47 | */
|
---|
48 |
|
---|
49 | Ident.prototype.__defineGetter__('hash', function(){
|
---|
50 | return this.name;
|
---|
51 | });
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Inherit from `Node.prototype`.
|
---|
55 | */
|
---|
56 |
|
---|
57 | Ident.prototype.__proto__ = Node.prototype;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Return a clone of this node.
|
---|
61 | *
|
---|
62 | * @return {Node}
|
---|
63 | * @api public
|
---|
64 | */
|
---|
65 |
|
---|
66 | Ident.prototype.clone = function(parent){
|
---|
67 | var clone = new Ident(this.name);
|
---|
68 | clone.val = this.val.clone(parent, clone);
|
---|
69 | clone.mixin = this.mixin;
|
---|
70 | clone.lineno = this.lineno;
|
---|
71 | clone.column = this.column;
|
---|
72 | clone.filename = this.filename;
|
---|
73 | clone.property = this.property;
|
---|
74 | clone.rest = this.rest;
|
---|
75 | return clone;
|
---|
76 | };
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Return a JSON representation of this node.
|
---|
80 | *
|
---|
81 | * @return {Object}
|
---|
82 | * @api public
|
---|
83 | */
|
---|
84 |
|
---|
85 | Ident.prototype.toJSON = function(){
|
---|
86 | return {
|
---|
87 | __type: 'Ident',
|
---|
88 | name: this.name,
|
---|
89 | val: this.val,
|
---|
90 | mixin: this.mixin,
|
---|
91 | property: this.property,
|
---|
92 | rest: this.rest,
|
---|
93 | lineno: this.lineno,
|
---|
94 | column: this.column,
|
---|
95 | filename: this.filename
|
---|
96 | };
|
---|
97 | };
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Return <name>.
|
---|
101 | *
|
---|
102 | * @return {String}
|
---|
103 | * @api public
|
---|
104 | */
|
---|
105 |
|
---|
106 | Ident.prototype.toString = function(){
|
---|
107 | return this.name;
|
---|
108 | };
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Coerce `other` to an ident.
|
---|
112 | *
|
---|
113 | * @param {Node} other
|
---|
114 | * @return {String}
|
---|
115 | * @api public
|
---|
116 | */
|
---|
117 |
|
---|
118 | Ident.prototype.coerce = function(other){
|
---|
119 | switch (other.nodeName) {
|
---|
120 | case 'ident':
|
---|
121 | case 'string':
|
---|
122 | case 'literal':
|
---|
123 | return new Ident(other.string);
|
---|
124 | case 'unit':
|
---|
125 | return new Ident(other.toString());
|
---|
126 | default:
|
---|
127 | return Node.prototype.coerce.call(this, other);
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Operate on `right` with the given `op`.
|
---|
133 | *
|
---|
134 | * @param {String} op
|
---|
135 | * @param {Node} right
|
---|
136 | * @return {Node}
|
---|
137 | * @api public
|
---|
138 | */
|
---|
139 |
|
---|
140 | Ident.prototype.operate = function(op, right){
|
---|
141 | var val = right.first;
|
---|
142 | switch (op) {
|
---|
143 | case '-':
|
---|
144 | if ('unit' == val.nodeName) {
|
---|
145 | var expr = new nodes.Expression;
|
---|
146 | val = val.clone();
|
---|
147 | val.val = -val.val;
|
---|
148 | expr.push(this);
|
---|
149 | expr.push(val);
|
---|
150 | return expr;
|
---|
151 | }
|
---|
152 | case '+':
|
---|
153 | return new nodes.Ident(this.string + this.coerce(val).string);
|
---|
154 | }
|
---|
155 | return Node.prototype.operate.call(this, op, right);
|
---|
156 | };
|
---|