UNPKG

1.46 kBJavaScriptView Raw
1
2/*!
3 * Stylus - If
4 * Copyright (c) Automattic <developer.wordpress.com>
5 * MIT Licensed
6 */
7
8/**
9 * Module dependencies.
10 */
11
12var Node = require('./node');
13
14/**
15 * Initialize a new `If` with the given `cond`.
16 *
17 * @param {Expression} cond
18 * @param {Boolean|Block} negate, block
19 * @api public
20 */
21
22var If = module.exports = function If(cond, negate){
23 Node.call(this);
24 this.cond = cond;
25 this.elses = [];
26 if (negate && negate.nodeName) {
27 this.block = negate;
28 } else {
29 this.negate = negate;
30 }
31};
32
33/**
34 * Inherit from `Node.prototype`.
35 */
36
37If.prototype.__proto__ = Node.prototype;
38
39/**
40 * Return a clone of this node.
41 *
42 * @return {Node}
43 * @api public
44 */
45
46If.prototype.clone = function(parent){
47 var clone = new If();
48 clone.cond = this.cond.clone(parent, clone);
49 clone.block = this.block.clone(parent, clone);
50 clone.elses = this.elses.map(function(node){ return node.clone(parent, clone); });
51 clone.negate = this.negate;
52 clone.postfix = this.postfix;
53 clone.lineno = this.lineno;
54 clone.column = this.column;
55 clone.filename = this.filename;
56 return clone;
57};
58
59/**
60 * Return a JSON representation of this node.
61 *
62 * @return {Object}
63 * @api public
64 */
65
66If.prototype.toJSON = function(){
67 return {
68 __type: 'If',
69 cond: this.cond,
70 block: this.block,
71 elses: this.elses,
72 negate: this.negate,
73 postfix: this.postfix,
74 lineno: this.lineno,
75 column: this.column,
76 filename: this.filename
77 };
78};