string.js 2.7 KB

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