Class: Sass::Tree::Node
- Defined in:
- lib/sass/tree/node.rb,
lib/sass/css.rb
Overview
This class doubles as the root node of the parse tree and the superclass of all other parse-tree nodes.
Direct Known Subclasses
AttrNode, CommentNode, DebugNode, DirectiveNode, FileNode, ForNode, IfNode, MixinDefNode, MixinNode, RuleNode, VariableNode, WhileNode
Instance Attribute Summary collapse
-
#children ⇒ Array<Tree::Node>
The child nodes of this node.
-
#filename ⇒ String
The name of the document on which this node appeared.
-
#line ⇒ Fixnum
The line of the document on which this node appeared.
-
#options ⇒ Hash<Symbol, Object>
The options hash for the node.
Instance Method Summary collapse
-
#<<(child) ⇒ Object
Appends a child to the node.
-
#==(other) ⇒ Boolean
Compares this node and another object (only other Nodes will be equal).
-
#initialize ⇒ Node
constructor
A new instance of Node.
-
#invisible? ⇒ Boolean
True if #to_s will return ‘nil`; that is, if the node shouldn’t be rendered.
-
#last ⇒ Tree::Node
Return the last child node.
-
#perform(environment) ⇒ Tree::Node
Runs the dynamic Sass code: mixins, variables, control structures, and so forth.
-
#render ⇒ Object
Runs the dynamic Sass code and computes the CSS for the tree.
-
#style ⇒ Symbol
The output style.
-
#to_s ⇒ String?
Computes the CSS corresponding to this Sass tree.
-
#to_sass(tabs = 0, opts = {}) ⇒ String
Converts a node to Sass code that will generate it.
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
42 43 44 |
# File 'lib/sass/tree/node.rb', line 42 def initialize @children = [] end |
Instance Attribute Details
#children ⇒ Array<Tree::Node>
The child nodes of this node.
24 25 26 |
# File 'lib/sass/tree/node.rb', line 24 def children @children end |
#filename ⇒ String
The name of the document on which this node appeared.
58 59 60 |
# File 'lib/sass/tree/node.rb', line 58 def filename @filename || @options[:filename] end |
#line ⇒ Fixnum
The line of the document on which this node appeared.
29 30 31 |
# File 'lib/sass/tree/node.rb', line 29 def line @line end |
#options ⇒ Hash<Symbol, Object>
The options hash for the node. See [the Sass options documentation](../../Sass.html#sass_options).
40 41 42 |
# File 'lib/sass/tree/node.rb', line 40 def @options end |
Instance Method Details
#<<(child) ⇒ Object
Appends a child to the node.
67 68 69 70 71 72 |
# File 'lib/sass/tree/node.rb', line 67 def <<(child) if msg = invalid_child?(child) raise Sass::SyntaxError.new(msg, child.line) end @children << child end |
#==(other) ⇒ Boolean
Compares this node and another object (only other Sass::Tree::Nodes will be equal). This does a structural comparison; if the contents of the nodes and all the child nodes are equivalent, then the nodes are as well.
Only static nodes need to override this.
94 95 96 |
# File 'lib/sass/tree/node.rb', line 94 def ==(other) self.class == other.class && other.children == children end |
#invisible? ⇒ Boolean
True if #to_s will return ‘nil`; that is, if the node shouldn’t be rendered. Should only be called in a static tree.
111 |
# File 'lib/sass/tree/node.rb', line 111 def invisible?; false; end |
#last ⇒ Tree::Node
Return the last child node.
We need this because Sass::Tree::Node duck types as an Array for Engine.
79 80 81 |
# File 'lib/sass/tree/node.rb', line 79 def last children.last end |
#perform(environment) ⇒ Tree::Node
Runs the dynamic Sass code: mixins, variables, control structures, and so forth. This doesn’t modify this node or any of its children.
#perform shouldn’t be overridden directly; if you want to return a new node (or list of nodes), override #_perform; if you want to destructively modify this node, override #perform!.
152 153 154 155 156 |
# File 'lib/sass/tree/node.rb', line 152 def perform(environment) environment. = @options if self.class == Tree::Node _perform(environment) rescue Sass::SyntaxError => e; e.(filename, line) end |
#render ⇒ Object
Runs the dynamic Sass code and computes the CSS for the tree.
102 103 104 |
# File 'lib/sass/tree/node.rb', line 102 def render perform(Environment.new).to_s end |
#style ⇒ Symbol
The output style. See [the Sass options documentation](../../Sass.html#output_style).
161 162 163 |
# File 'lib/sass/tree/node.rb', line 161 def style @options[:style] end |
#to_s ⇒ String?
Computes the CSS corresponding to this Sass tree.
Only static-node subclasses need to implement #to_s.
This may return ‘nil`, but it will only do so if #invisible? is true.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/sass/tree/node.rb', line 122 def to_s result = String.new children.each do |child| if child.is_a? AttrNode raise Sass::SyntaxError.new('Attributes aren\'t allowed at the root of a document.', child.line) else next if child.invisible? child_str = child.to_s(1) result << child_str + (style == :compressed ? '' : "\n") end end style == :compressed ? result+"\n" : result[0...-1] rescue Sass::SyntaxError => e; e.(filename, line) end |
#to_sass(tabs = 0, opts = {}) ⇒ String
Converts a node to Sass code that will generate it.
13 14 15 16 17 18 19 20 21 |
# File 'lib/sass/css.rb', line 13 def to_sass(tabs = 0, opts = {}) result = '' children.each do |child| result << "#{' ' * tabs}#{child.to_sass(0, opts)}\n" end result end |