1 |
|
---|
2 | /*!
|
---|
3 | * Stylus - Selector
|
---|
4 | * Copyright (c) Automattic <developer.wordpress.com>
|
---|
5 | * MIT Licensed
|
---|
6 | */
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Module dependencies.
|
---|
10 | */
|
---|
11 |
|
---|
12 | var Block = require('./block')
|
---|
13 | , Node = require('./node');
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Initialize a new `Selector` with the given `segs`.
|
---|
17 | *
|
---|
18 | * @param {Array} segs
|
---|
19 | * @api public
|
---|
20 | */
|
---|
21 |
|
---|
22 | var Selector = module.exports = function Selector(segs){
|
---|
23 | Node.call(this);
|
---|
24 | this.inherits = true;
|
---|
25 | this.segments = segs;
|
---|
26 | this.optional = false;
|
---|
27 | };
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Inherit from `Node.prototype`.
|
---|
31 | */
|
---|
32 |
|
---|
33 | Selector.prototype.__proto__ = Node.prototype;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Return the selector string.
|
---|
37 | *
|
---|
38 | * @return {String}
|
---|
39 | * @api public
|
---|
40 | */
|
---|
41 |
|
---|
42 | Selector.prototype.toString = function(){
|
---|
43 | return this.segments.join('') + (this.optional ? ' !optional' : '');
|
---|
44 | };
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Check if this is placeholder selector.
|
---|
48 | *
|
---|
49 | * @return {Boolean}
|
---|
50 | * @api public
|
---|
51 | */
|
---|
52 |
|
---|
53 | Selector.prototype.__defineGetter__('isPlaceholder', function(){
|
---|
54 | return this.val && ~this.val.substr(0, 2).indexOf('$');
|
---|
55 | });
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Return a clone of this node.
|
---|
59 | *
|
---|
60 | * @return {Node}
|
---|
61 | * @api public
|
---|
62 | */
|
---|
63 |
|
---|
64 | Selector.prototype.clone = function(parent){
|
---|
65 | var clone = new Selector;
|
---|
66 | clone.lineno = this.lineno;
|
---|
67 | clone.column = this.column;
|
---|
68 | clone.filename = this.filename;
|
---|
69 | clone.inherits = this.inherits;
|
---|
70 | clone.val = this.val;
|
---|
71 | clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
|
---|
72 | clone.optional = this.optional;
|
---|
73 | return clone;
|
---|
74 | };
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Return a JSON representation of this node.
|
---|
78 | *
|
---|
79 | * @return {Object}
|
---|
80 | * @api public
|
---|
81 | */
|
---|
82 |
|
---|
83 | Selector.prototype.toJSON = function(){
|
---|
84 | return {
|
---|
85 | __type: 'Selector',
|
---|
86 | inherits: this.inherits,
|
---|
87 | segments: this.segments,
|
---|
88 | optional: this.optional,
|
---|
89 | val: this.val,
|
---|
90 | lineno: this.lineno,
|
---|
91 | column: this.column,
|
---|
92 | filename: this.filename
|
---|
93 | };
|
---|
94 | };
|
---|