UNPKG

1.9 kBJavaScriptView Raw
1
2/*!
3 * Stylus - Property
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 `Property` with the given `segs` and optional `expr`.
16 *
17 * @param {Array} segs
18 * @param {Expression} expr
19 * @api public
20 */
21
22var Property = module.exports = function Property(segs, expr){
23 Node.call(this);
24 this.segments = segs;
25 this.expr = expr;
26};
27
28/**
29 * Inherit from `Node.prototype`.
30 */
31
32Property.prototype.__proto__ = Node.prototype;
33
34/**
35 * Return a clone of this node.
36 *
37 * @return {Node}
38 * @api public
39 */
40
41Property.prototype.clone = function(parent){
42 var clone = new Property(this.segments);
43 clone.name = this.name;
44 if (this.literal) clone.literal = this.literal;
45 clone.lineno = this.lineno;
46 clone.column = this.column;
47 clone.filename = this.filename;
48 clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
49 if (this.expr) clone.expr = this.expr.clone(parent, clone);
50 return clone;
51};
52
53/**
54 * Return a JSON representation of this node.
55 *
56 * @return {Object}
57 * @api public
58 */
59
60Property.prototype.toJSON = function(){
61 var json = {
62 __type: 'Property',
63 segments: this.segments,
64 name: this.name,
65 lineno: this.lineno,
66 column: this.column,
67 filename: this.filename
68 };
69 if (this.expr) json.expr = this.expr;
70 if (this.literal) json.literal = this.literal;
71 return json;
72};
73
74/**
75 * Return string representation of this node.
76 *
77 * @return {String}
78 * @api public
79 */
80
81Property.prototype.toString = function(){
82 return 'property(' + this.segments.join('') + ', ' + this.expr + ')';
83};
84
85/**
86 * Operate on the property expression.
87 *
88 * @param {String} op
89 * @param {Node} right
90 * @return {Node}
91 * @api public
92 */
93
94Property.prototype.operate = function(op, right, val){
95 return this.expr.operate(op, right, val);
96};