UNPKG

1.86 kBJavaScriptView Raw
1
2/*!
3 * Stylus - Literal
4 * Copyright (c) Automattic <developer.wordpress.com>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
12var 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
22var Literal = module.exports = function Literal(str){
23 Node.call(this);
24 this.val = str;
25 this.string = str;
26 this.prefixed = false;
27};
28
29/**
30 * Inherit from `Node.prototype`.
31 */
32
33Literal.prototype.__proto__ = Node.prototype;
34
35/**
36 * Return hash.
37 *
38 * @return {String}
39 * @api public
40 */
41
42Literal.prototype.__defineGetter__('hash', function(){
43 return this.val;
44});
45
46/**
47 * Return literal value.
48 *
49 * @return {String}
50 * @api public
51 */
52
53Literal.prototype.toString = function(){
54 return this.val;
55};
56
57/**
58 * Coerce `other` to a literal.
59 *
60 * @param {Node} other
61 * @return {String}
62 * @api public
63 */
64
65Literal.prototype.coerce = function(other){
66 switch (other.nodeName) {
67 case 'ident':
68 case 'string':
69 case 'literal':
70 return new Literal(other.string);
71 default:
72 return Node.prototype.coerce.call(this, other);
73 }
74};
75
76/**
77 * Operate on `right` with the given `op`.
78 *
79 * @param {String} op
80 * @param {Node} right
81 * @return {Node}
82 * @api public
83 */
84
85Literal.prototype.operate = function(op, right){
86 var val = right.first;
87 switch (op) {
88 case '+':
89 return new nodes.Literal(this.string + this.coerce(val).string);
90 default:
91 return Node.prototype.operate.call(this, op, right);
92 }
93};
94
95/**
96 * Return a JSON representation of this node.
97 *
98 * @return {Object}
99 * @api public
100 */
101
102Literal.prototype.toJSON = function(){
103 return {
104 __type: 'Literal',
105 val: this.val,
106 string: this.string,
107 prefixed: this.prefixed,
108 lineno: this.lineno,
109 column: this.column,
110 filename: this.filename
111 };
112};