Class: Sass::Tree::Node
- Defined in:
- lib/sass/tree/node.rb,
lib/sass/css.rb
Overview
The abstract superclass of all parse-tree nodes.
Direct Known Subclasses
CommentNode, DebugNode, DirectiveNode, ForNode, IfNode, MixinDefNode, MixinNode, PropNode, RootNode, 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).
-
#_perform(environment) ⇒ Tree::Node+
protected
Runs any dynamic Sass code in this particular node.
-
#_to_s ⇒ String?
protected
Computes the CSS corresponding to this particular Sass node.
- #balance(*args) ⇒ Object protected
-
#initialize ⇒ Node
constructor
A new instance of Node.
-
#interpolate(text, environment) ⇒ String
protected
Replaces SassScript in a chunk of text (via
#{}
) with the resulting value. -
#invalid_child?(child) ⇒ Boolean, String
protected
Returns an error message if the given child node is invalid, and false otherwise.
-
#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 directives, and so forth.
-
#perform!(environment) ⇒ Object
protected
Destructively runs dynamic Sass code in this particular node.
-
#perform_children(environment) ⇒ Array<Tree::Node>
protected
Non-destructively runs #perform on all children of the current node.
-
#render ⇒ Object
Runs the dynamic Sass code and computes the CSS for the tree.
-
#style ⇒ Symbol
The output style.
-
#to_s(*args) ⇒ 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.
41 42 43 |
# File 'lib/sass/tree/node.rb', line 41
def initialize
@children = []
end
|
Instance Attribute Details
#children ⇒ Array<Tree::Node>
The child nodes of this node.
23 24 25 |
# File 'lib/sass/tree/node.rb', line 23
def children
@children
end
|
#filename ⇒ String
The name of the document on which this node appeared.
57 58 59 |
# File 'lib/sass/tree/node.rb', line 57
def filename
@filename || @options[:filename]
end
|
#line ⇒ Fixnum
The line of the document on which this node appeared.
28 29 30 |
# File 'lib/sass/tree/node.rb', line 28
def line
@line
end
|
#options ⇒ Hash<Symbol, Object>
The options hash for the node. See the Sass options documentation.
39 40 41 |
# File 'lib/sass/tree/node.rb', line 39
def options
@options
end
|
Instance Method Details
#<<(child) ⇒ Object
Appends a child to the node.
66 67 68 69 70 71 |
# File 'lib/sass/tree/node.rb', line 66
def <<(child)
if msg = invalid_child?(child)
raise Sass::SyntaxError.new(msg, :line => 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.
93 94 95 |
# File 'lib/sass/tree/node.rb', line 93
def ==(other)
self.class == other.class && other.children == children
end
|
#_perform(environment) ⇒ Tree::Node+ (protected)
Runs any dynamic Sass code in this particular node. This doesn't modify this node or any of its children.
180 181 182 183 184 |
# File 'lib/sass/tree/node.rb', line 180
def _perform(environment)
node = dup
node.perform!(environment)
node
end
|
#_to_s ⇒ String? (protected)
Computes the CSS corresponding to this particular Sass node.
168 169 170 |
# File 'lib/sass/tree/node.rb', line 168
def _to_s
raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #_to_s or #to_s.")
end
|
#balance(*args) ⇒ Object (protected)
231 232 233 234 235 |
# File 'lib/sass/tree/node.rb', line 231
def balance(*args)
res = Haml::Shared.balance(*args)
return res if res
raise Sass::SyntaxError.new("Unbalanced brackets.", :line => line)
end
|
#interpolate(text, environment) ⇒ String (protected)
Replaces SassScript in a chunk of text (via #{}
)
with the resulting value.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/sass/tree/node.rb', line 213
def interpolate(text, environment)
res = ''
rest = Haml::Shared.handle_interpolation text do |scan|
escapes = scan[2].size
res << scan.matched[0...-2 - escapes]
if escapes % 2 == 1
res << "\\" * (escapes - 1) << '#{'
else
res << "\\" * [0, escapes - 1].max
res << Script::Parser.new(scan, line, scan.pos - scan.matched_size, filename).
parse_interpolated.perform(environment).to_s
end
end
res + rest
end
|
#invalid_child?(child) ⇒ Boolean, String (protected)
Returns an error message if the given child node is invalid, and false otherwise.
By default, all child nodes are valid. This is expected to be overriden by subclasses for which some children are invalid.
247 248 249 |
# File 'lib/sass/tree/node.rb', line 247
def invalid_child?(child)
false
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.
110 |
# File 'lib/sass/tree/node.rb', line 110
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.
78 79 80 |
# File 'lib/sass/tree/node.rb', line 78
def last
children.last
end
|
#perform(environment) ⇒ Tree::Node
Runs the dynamic Sass code: mixins, variables, control directives, 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!.
151 152 153 154 155 156 157 |
# File 'lib/sass/tree/node.rb', line 151
def perform(environment)
environment.options = @options if self.class == Tree::Node
_perform(environment)
rescue Sass::SyntaxError => e
e.modify_backtrace(:filename => filename, :line => line)
raise e
end
|
#perform!(environment) ⇒ Object (protected)
Destructively runs dynamic Sass code in this particular node. This does modify this node, but will be run non-destructively by #_perform.
193 194 195 |
# File 'lib/sass/tree/node.rb', line 193
def perform!(environment)
self.children = perform_children(Environment.new(environment))
end
|
#perform_children(environment) ⇒ Array<Tree::Node> (protected)
Non-destructively runs #perform on all children of the current node.
202 203 204 |
# File 'lib/sass/tree/node.rb', line 202
def perform_children(environment)
children.map {|c| c.perform(environment)}.flatten
end
|
#render ⇒ Object
Runs the dynamic Sass code and computes the CSS for the tree.
101 102 103 |
# File 'lib/sass/tree/node.rb', line 101
def render
perform(Environment.new).to_s
end
|
#style ⇒ Symbol
The output style. See the Sass options documentation.
115 116 117 |
# File 'lib/sass/tree/node.rb', line 115
def style
@options[:style]
end
|
#to_s(*args) ⇒ 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.
129 130 131 132 133 134 |
# File 'lib/sass/tree/node.rb', line 129
def to_s(*args)
_to_s(*args)
rescue Sass::SyntaxError => e
e.modify_backtrace(:filename => filename, :line => line)
raise e
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
|