1 |
|
---|
2 | /*!
|
---|
3 | * Stylus - Group
|
---|
4 | * Copyright (c) Automattic <developer.wordpress.com>
|
---|
5 | * MIT Licensed
|
---|
6 | */
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Module dependencies.
|
---|
10 | */
|
---|
11 |
|
---|
12 | var Node = require('./node');
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Initialize a new `Group`.
|
---|
16 | *
|
---|
17 | * @api public
|
---|
18 | */
|
---|
19 |
|
---|
20 | var Group = module.exports = function Group(){
|
---|
21 | Node.call(this);
|
---|
22 | this.nodes = [];
|
---|
23 | this.extends = [];
|
---|
24 | };
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Inherit from `Node.prototype`.
|
---|
28 | */
|
---|
29 |
|
---|
30 | Group.prototype.__proto__ = Node.prototype;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Push the given `selector` node.
|
---|
34 | *
|
---|
35 | * @param {Selector} selector
|
---|
36 | * @api public
|
---|
37 | */
|
---|
38 |
|
---|
39 | Group.prototype.push = function(selector){
|
---|
40 | this.nodes.push(selector);
|
---|
41 | };
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Return this set's `Block`.
|
---|
45 | */
|
---|
46 |
|
---|
47 | Group.prototype.__defineGetter__('block', function(){
|
---|
48 | return this.nodes[0].block;
|
---|
49 | });
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Assign `block` to each selector in this set.
|
---|
53 | *
|
---|
54 | * @param {Block} block
|
---|
55 | * @api public
|
---|
56 | */
|
---|
57 |
|
---|
58 | Group.prototype.__defineSetter__('block', function(block){
|
---|
59 | for (var i = 0, len = this.nodes.length; i < len; ++i) {
|
---|
60 | this.nodes[i].block = block;
|
---|
61 | }
|
---|
62 | });
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Check if this set has only placeholders.
|
---|
66 | *
|
---|
67 | * @return {Boolean}
|
---|
68 | * @api public
|
---|
69 | */
|
---|
70 |
|
---|
71 | Group.prototype.__defineGetter__('hasOnlyPlaceholders', function(){
|
---|
72 | return this.nodes.every(function(selector) { return selector.isPlaceholder; });
|
---|
73 | });
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Return a clone of this node.
|
---|
77 | *
|
---|
78 | * @return {Node}
|
---|
79 | * @api public
|
---|
80 | */
|
---|
81 |
|
---|
82 | Group.prototype.clone = function(parent){
|
---|
83 | var clone = new Group;
|
---|
84 | clone.lineno = this.lineno;
|
---|
85 | clone.column = this.column;
|
---|
86 | this.nodes.forEach(function(node){
|
---|
87 | clone.push(node.clone(parent, clone));
|
---|
88 | });
|
---|
89 | clone.filename = this.filename;
|
---|
90 | clone.block = this.block.clone(parent, clone);
|
---|
91 | return clone;
|
---|
92 | };
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Return a JSON representation of this node.
|
---|
96 | *
|
---|
97 | * @return {Object}
|
---|
98 | * @api public
|
---|
99 | */
|
---|
100 |
|
---|
101 | Group.prototype.toJSON = function(){
|
---|
102 | return {
|
---|
103 | __type: 'Group',
|
---|
104 | nodes: this.nodes,
|
---|
105 | block: this.block,
|
---|
106 | lineno: this.lineno,
|
---|
107 | column: this.column,
|
---|
108 | filename: this.filename
|
---|
109 | };
|
---|
110 | };
|
---|