if.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. * Stylus - If
  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 `If` with the given `cond`.
  12. *
  13. * @param {Expression} cond
  14. * @param {Boolean|Block} negate, block
  15. * @api public
  16. */
  17. var If = module.exports = function If(cond, negate){
  18. Node.call(this);
  19. this.cond = cond;
  20. this.elses = [];
  21. if (negate && negate.nodeName) {
  22. this.block = negate;
  23. } else {
  24. this.negate = negate;
  25. }
  26. };
  27. /**
  28. * Inherit from `Node.prototype`.
  29. */
  30. If.prototype.__proto__ = Node.prototype;
  31. /**
  32. * Return a clone of this node.
  33. *
  34. * @return {Node}
  35. * @api public
  36. */
  37. If.prototype.clone = function(parent){
  38. var clone = new If();
  39. clone.cond = this.cond.clone(parent, clone);
  40. clone.block = this.block.clone(parent, clone);
  41. clone.elses = this.elses.map(function(node){ return node.clone(parent, clone); });
  42. clone.negate = this.negate;
  43. clone.postfix = this.postfix;
  44. clone.lineno = this.lineno;
  45. clone.column = this.column;
  46. clone.filename = this.filename;
  47. return clone;
  48. };
  49. /**
  50. * Return a JSON representation of this node.
  51. *
  52. * @return {Object}
  53. * @api public
  54. */
  55. If.prototype.toJSON = function(){
  56. return {
  57. __type: 'If',
  58. cond: this.cond,
  59. block: this.block,
  60. elses: this.elses,
  61. negate: this.negate,
  62. postfix: this.postfix,
  63. lineno: this.lineno,
  64. column: this.column,
  65. filename: this.filename
  66. };
  67. };