1 | /*!
|
---|
2 | * Stylus - String
|
---|
3 | * Copyright (c) Automattic <developer.wordpress.com>
|
---|
4 | * MIT Licensed
|
---|
5 | */
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Module dependencies.
|
---|
9 | */
|
---|
10 |
|
---|
11 | var Node = require('./node')
|
---|
12 | , sprintf = require('../functions').s
|
---|
13 | , utils = require('../utils')
|
---|
14 | , nodes = require('./');
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Initialize a new `String` with the given `val`.
|
---|
18 | *
|
---|
19 | * @param {String} val
|
---|
20 | * @param {String} quote
|
---|
21 | * @api public
|
---|
22 | */
|
---|
23 |
|
---|
24 | var String = module.exports = function String(val, quote){
|
---|
25 | Node.call(this);
|
---|
26 | this.val = val;
|
---|
27 | this.string = val;
|
---|
28 | this.prefixed = false;
|
---|
29 | if (typeof quote !== 'string') {
|
---|
30 | this.quote = "'";
|
---|
31 | } else {
|
---|
32 | this.quote = quote;
|
---|
33 | }
|
---|
34 | };
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Inherit from `Node.prototype`.
|
---|
38 | */
|
---|
39 |
|
---|
40 | String.prototype.__proto__ = Node.prototype;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Return quoted string.
|
---|
44 | *
|
---|
45 | * @return {String}
|
---|
46 | * @api public
|
---|
47 | */
|
---|
48 |
|
---|
49 | String.prototype.toString = function(){
|
---|
50 | return this.quote + this.val + this.quote;
|
---|
51 | };
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Return a clone of this node.
|
---|
55 | *
|
---|
56 | * @return {Node}
|
---|
57 | * @api public
|
---|
58 | */
|
---|
59 |
|
---|
60 | String.prototype.clone = function(){
|
---|
61 | var clone = new String(this.val, this.quote);
|
---|
62 | clone.lineno = this.lineno;
|
---|
63 | clone.column = this.column;
|
---|
64 | clone.filename = this.filename;
|
---|
65 | return clone;
|
---|
66 | };
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Return a JSON representation of this node.
|
---|
70 | *
|
---|
71 | * @return {Object}
|
---|
72 | * @api public
|
---|
73 | */
|
---|
74 |
|
---|
75 | String.prototype.toJSON = function(){
|
---|
76 | return {
|
---|
77 | __type: 'String',
|
---|
78 | val: this.val,
|
---|
79 | quote: this.quote,
|
---|
80 | lineno: this.lineno,
|
---|
81 | column: this.column,
|
---|
82 | filename: this.filename
|
---|
83 | };
|
---|
84 | };
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Return Boolean based on the length of this string.
|
---|
88 | *
|
---|
89 | * @return {Boolean}
|
---|
90 | * @api public
|
---|
91 | */
|
---|
92 |
|
---|
93 | String.prototype.toBoolean = function(){
|
---|
94 | return nodes.Boolean(this.val.length);
|
---|
95 | };
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Coerce `other` to a string.
|
---|
99 | *
|
---|
100 | * @param {Node} other
|
---|
101 | * @return {String}
|
---|
102 | * @api public
|
---|
103 | */
|
---|
104 |
|
---|
105 | String.prototype.coerce = function(other){
|
---|
106 | switch (other.nodeName) {
|
---|
107 | case 'string':
|
---|
108 | return other;
|
---|
109 | case 'expression':
|
---|
110 | return new String(other.nodes.map(function(node){
|
---|
111 | return this.coerce(node).val;
|
---|
112 | }, this).join(' '));
|
---|
113 | default:
|
---|
114 | return new String(other.toString());
|
---|
115 | }
|
---|
116 | };
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Operate on `right` with the given `op`.
|
---|
120 | *
|
---|
121 | * @param {String} op
|
---|
122 | * @param {Node} right
|
---|
123 | * @return {Node}
|
---|
124 | * @api public
|
---|
125 | */
|
---|
126 |
|
---|
127 | String.prototype.operate = function(op, right){
|
---|
128 | switch (op) {
|
---|
129 | case '%':
|
---|
130 | var expr = new nodes.Expression;
|
---|
131 | expr.push(this);
|
---|
132 |
|
---|
133 | // constructargs
|
---|
134 | var args = 'expression' == right.nodeName
|
---|
135 | ? utils.unwrap(right).nodes
|
---|
136 | : [right];
|
---|
137 |
|
---|
138 | // apply
|
---|
139 | return sprintf.apply(null, [expr].concat(args));
|
---|
140 | case '+':
|
---|
141 | var expr = new nodes.Expression;
|
---|
142 | expr.push(new String(this.val + this.coerce(right).val));
|
---|
143 | return expr;
|
---|
144 | default:
|
---|
145 | return Node.prototype.operate.call(this, op, right);
|
---|
146 | }
|
---|
147 | };
|
---|