property.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. * Stylus - Property
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node');
  10. /**
  11. * Initialize a new `Property` with the given `segs` and optional `expr`.
  12. *
  13. * @param {Array} segs
  14. * @param {Expression} expr
  15. * @api public
  16. */
  17. var Property = module.exports = function Property(segs, expr){
  18. Node.call(this);
  19. this.segments = segs;
  20. this.expr = expr;
  21. };
  22. /**
  23. * Inherit from `Node.prototype`.
  24. */
  25. Property.prototype.__proto__ = Node.prototype;
  26. /**
  27. * Return a clone of this node.
  28. *
  29. * @return {Node}
  30. * @api public
  31. */
  32. Property.prototype.clone = function(parent){
  33. var clone = new Property(this.segments);
  34. clone.name = this.name;
  35. if (this.literal) clone.literal = this.literal;
  36. clone.lineno = this.lineno;
  37. clone.column = this.column;
  38. clone.filename = this.filename;
  39. clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
  40. if (this.expr) clone.expr = this.expr.clone(parent, clone);
  41. return clone;
  42. };
  43. /**
  44. * Return a JSON representation of this node.
  45. *
  46. * @return {Object}
  47. * @api public
  48. */
  49. Property.prototype.toJSON = function(){
  50. var json = {
  51. __type: 'Property',
  52. segments: this.segments,
  53. name: this.name,
  54. lineno: this.lineno,
  55. column: this.column,
  56. filename: this.filename
  57. };
  58. if (this.expr) json.expr = this.expr;
  59. if (this.literal) json.literal = this.literal;
  60. return json;
  61. };
  62. /**
  63. * Return string representation of this node.
  64. *
  65. * @return {String}
  66. * @api public
  67. */
  68. Property.prototype.toString = function(){
  69. return 'property(' + this.segments.join('') + ', ' + this.expr + ')';
  70. };
  71. /**
  72. * Operate on the property expression.
  73. *
  74. * @param {String} op
  75. * @param {Node} right
  76. * @return {Node}
  77. * @api public
  78. */
  79. Property.prototype.operate = function(op, right, val){
  80. return this.expr.operate(op, right, val);
  81. };