Class: Sass::Tree::Node
- Inherits:
-
Object
- Object
- Sass::Tree::Node
- Includes:
- Enumerable
- Defined in:
- lib/sass/tree/node.rb
Overview
The abstract superclass of all parse-tree nodes.
Direct Known Subclasses
CommentNode, DebugNode, DirectiveNode, ExtendNode, ForNode, IfNode, MixinDefNode, MixinNode, PropNode, RootNode, RuleNode, VariableNode, WarnNode, WhileNode
Constant Summary collapse
- SAVED_OPTIONS =
Names of options that are saved when the node is serialized and cached.
[:importer]
Instance Attribute Summary collapse
-
#children ⇒ Array<Tree::Node>
readonly
The child nodes of this node.
-
#filename ⇒ String
The name of the document on which this node appeared.
-
#has_children ⇒ Boolean
Whether or not this node has child nodes.
-
#line ⇒ Fixnum
The line of the document on which this node appeared.
-
#options ⇒ {Symbol => Object}
The options hash for the node.
Instance Method Summary collapse
-
#<<(child)
Appends a child to the node.
-
#==(other) ⇒ Boolean
Compares this node and another object (only other Nodes will be equal).
-
#_around_dump
Ensures that only SAVED_OPTIONS get saved.
-
#_cssize(extends, parent) ⇒ Tree::Node+
protected
Converts this static Sass node into a static CSS node, returning the new node.
-
#_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) protected
-
#check_child!(child)
Raises an error if the given child node is invalid.
-
#children_to_src(tabs, opts, fmt) ⇒ String
protected
Converts the children of this node to a Sass or SCSS string.
-
#cssize(extends, parent = nil) ⇒ Tree::Node
Converts a static Sass tree (e.g. the output of #perform) into a static CSS tree.
-
#cssize!(extends, parent)
protected
Destructively converts this static Sass node into a static CSS node.
-
#dasherize(s, opts) ⇒ String
protected
Convert any underscores in a string into hyphens, but only if the
:dasherize
option is set. - #do_extend(extends) ⇒ Tree::Node
-
#each {|node| ... }
Iterates through each node in the tree rooted at this node in a pre-order walk.
-
#initialize ⇒ Node
constructor
A new instance of Node.
-
#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. -
#perform(environment) ⇒ Tree::Node
Converts a dynamic tree into a static Sass tree.
-
#perform!(environment)
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.
-
#run_interp(text, environment) ⇒ String
protected
Replaces SassScript in a chunk of text with the resulting value.
-
#selector_to_sass(sel, opts) ⇒ String
protected
Converts a selector to a Sass string.
-
#selector_to_scss(sel, tabs, opts) ⇒ String
protected
Converts a selector to a SCSS string.
-
#selector_to_src(sel, tabs, opts, fmt) ⇒ String
protected
Converts a selector to a Sass or SCSS string.
-
#semi(fmt) ⇒ String
protected
Returns a semicolon if this is SCSS, or an empty string if this is Sass.
-
#style ⇒ Symbol
The output style.
-
#to_s(*args) ⇒ String?
Computes the CSS corresponding to this static CSS tree.
-
#to_sass(tabs = 0, opts = {}) ⇒ String
Converts a node to Sass code that will generate it.
-
#to_scss(tabs = 0, opts = {}) ⇒ String
Converts a node to SCSS code that will generate it.
-
#to_src(tabs, opts, fmt) ⇒ String
protected
Converts a node to Sass or SCSS code that will generate it.
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
59 60 61 |
# File 'lib/sass/tree/node.rb', line 59
def initialize
@children = []
end
|
Instance Attribute Details
#children ⇒ Array<Tree::Node>
The child nodes of this node.
34 35 36 |
# File 'lib/sass/tree/node.rb', line 34
def children
@children
end
|
#filename ⇒ String
The name of the document on which this node appeared.
81 82 83 |
# File 'lib/sass/tree/node.rb', line 81
def filename
@filename || (@options && @options[:filename])
end
|
#has_children ⇒ Boolean
Whether or not this node has child nodes.
This may be true even when #children is empty,
in which case this node has an empty block (e.g. {}
).
41 42 43 |
# File 'lib/sass/tree/node.rb', line 41
def has_children
@has_children
end
|
#line ⇒ Fixnum
The line of the document on which this node appeared.
46 47 48 |
# File 'lib/sass/tree/node.rb', line 46
def line
@line
end
|
#options ⇒ {Symbol => Object}
The options hash for the node. See the Sass options documentation.
57 58 59 |
# File 'lib/sass/tree/node.rb', line 57
def options
@options
end
|
Instance Method Details
#<<(child)
Appends a child to the node.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sass/tree/node.rb', line 90
def <<(child)
return if child.nil?
if child.is_a?(Array)
child.each {|c| self << c}
else
check_child! child
self.has_children = true
@children << child
end
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.
123 124 125 |
# File 'lib/sass/tree/node.rb', line 123
def ==(other)
self.class == other.class && other.children == children
end
|
#_around_dump
Ensures that only SAVED_OPTIONS get saved.
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/sass/tree/node.rb', line 251
def _around_dump
old_options = @options
@options = {}
SAVED_OPTIONS.each do |opt|
@options[opt] = old_options[opt]
end
yield
ensure
options = old_options
end
|
#_cssize(extends, parent) ⇒ Tree::Node+ (protected)
Converts this static Sass node into a static CSS node, returning the new node. This doesn't modify this node or any of its children.
291 292 293 294 295 |
# File 'lib/sass/tree/node.rb', line 291
def _cssize(extends, parent)
node = dup
node.cssize!(extends, parent)
node
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.
318 319 320 321 322 |
# File 'lib/sass/tree/node.rb', line 318
def _perform(environment)
node = dup
node.perform!(environment)
node
end
|
#_to_s ⇒ String? (protected)
Computes the CSS corresponding to this particular Sass node.
This method should never raise SyntaxErrors. Such errors will not be properly annotated with Sass backtrace information. All error conditions should be checked in earlier transformations, such as #cssize and #perform.
275 276 277 |
# File 'lib/sass/tree/node.rb', line 275
def _to_s
Haml::Util.abstract(self)
end
|
#balance(*args) (protected)
363 364 365 366 367 |
# File 'lib/sass/tree/node.rb', line 363
def balance(*args)
res = Haml::Shared.balance(*args)
return res if res
raise Sass::SyntaxError.new("Unbalanced brackets.", :line => line)
end
|
#check_child!(child)
Raises an error if the given child node is invalid.
106 107 108 109 110 |
# File 'lib/sass/tree/node.rb', line 106
def check_child!(child)
if msg = invalid_child?(child)
raise Sass::SyntaxError.new(msg, :line => child.line)
end
end
|
#children_to_src(tabs, opts, fmt) ⇒ String (protected)
Converts the children of this node to a Sass or SCSS string. This will return the trailing newline for the previous line, including brackets if this is SCSS.
412 413 414 415 416 417 418 |
# File 'lib/sass/tree/node.rb', line 412
def children_to_src(tabs, opts, fmt)
return fmt == :sass ? "\n" : " {}\n" if children.empty?
(fmt == :sass ? "\n" : " {\n") +
children.map {|c| c.send("to_#{fmt}", tabs + 1, opts)}.join.rstrip +
(fmt == :sass ? "\n" : " }\n")
end
|
#cssize(extends, parent = nil) ⇒ Tree::Node
192 193 194 195 196 197 |
# File 'lib/sass/tree/node.rb', line 192
def cssize(extends, parent = nil)
_cssize(extends, (parent if parent.class == self.class))
rescue Sass::SyntaxError => e
e.modify_backtrace(:filename => filename, :line => line)
raise e
end
|
#cssize!(extends, parent) (protected)
Destructively converts this static Sass node into a static CSS node. This does modify this node, but will be run non-destructively by #_cssize.
306 307 308 |
# File 'lib/sass/tree/node.rb', line 306
def cssize!(extends, parent)
self.children = children.map {|c| c.cssize(extends, self)}.flatten
end
|
#dasherize(s, opts) ⇒ String (protected)
Convert any underscores in a string into hyphens,
but only if the :dasherize
option is set.
463 464 465 466 467 468 469 |
# File 'lib/sass/tree/node.rb', line 463
def dasherize(s, opts)
if opts[:dasherize]
s.gsub('_', '-')
else
s
end
end
|
#do_extend(extends) ⇒ Tree::Node
170 171 172 173 174 175 176 177 |
# File 'lib/sass/tree/node.rb', line 170
def do_extend(extends)
node = dup
node.children = children.map {|c| c.do_extend(extends)}
node
rescue Sass::SyntaxError => e
e.modify_backtrace(:filename => filename, :line => line)
raise e
end
|
#each {|node| ... }
Iterates through each node in the tree rooted at this node in a pre-order walk.
224 225 226 227 |
# File 'lib/sass/tree/node.rb', line 224
def each(&block)
yield self
children.each {|c| c.each(&block)}
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 except those only allowed under specific nodes (MixinDefNode, ImportNode, ExtendNode) are valid. This is expected to be overriden by subclasses for which some children are invalid.
380 381 382 383 384 385 386 387 388 389 |
# File 'lib/sass/tree/node.rb', line 380
def invalid_child?(child)
case child
when Tree::MixinDefNode
"Mixins may only be defined at the root of a document."
when Tree::ImportNode
"Import directives may only be used at the root of a document."
when Tree::ExtendNode
"Extend directives may only be used within rules."
end
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.
132 |
# File 'lib/sass/tree/node.rb', line 132
def invisible?; false; end
|
#perform(environment) ⇒ Tree::Node
212 213 214 215 216 217 |
# File 'lib/sass/tree/node.rb', line 212
def perform(environment)
_perform(environment)
rescue Sass::SyntaxError => e
e.modify_backtrace(:filename => filename, :line => line)
raise e
end
|
#perform!(environment) (protected)
Destructively runs dynamic Sass code in this particular node. This does modify this node, but will be run non-destructively by #_perform.
331 332 333 |
# File 'lib/sass/tree/node.rb', line 331
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.
340 341 342 |
# File 'lib/sass/tree/node.rb', line 340
def perform_children(environment)
children.map {|c| c.perform(environment)}.flatten
end
|
#run_interp(text, environment) ⇒ String (protected)
Replaces SassScript in a chunk of text with the resulting value.
351 352 353 354 355 356 357 358 359 |
# File 'lib/sass/tree/node.rb', line 351
def run_interp(text, environment)
text.map do |r|
next r if r.is_a?(String)
val = r.perform(environment)
# Interpolated strings should never render with quotes
next val.value if val.is_a?(Sass::Script::String)
val.to_s
end.join.strip
end
|
#selector_to_sass(sel, opts) ⇒ String (protected)
Converts a selector to a Sass string.
436 437 438 439 440 441 442 443 444 |
# File 'lib/sass/tree/node.rb', line 436
def selector_to_sass(sel, opts)
sel.map do |r|
if r.is_a?(String)
r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
else
"\#{#{r.to_sass(opts)}}"
end
end.join
end
|
#selector_to_scss(sel, tabs, opts) ⇒ String (protected)
Converts a selector to a SCSS string.
452 453 454 455 |
# File 'lib/sass/tree/node.rb', line 452
def selector_to_scss(sel, tabs, opts)
sel.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
join.gsub(/^[ \t]*/, ' ' * tabs)
end
|
#selector_to_src(sel, tabs, opts, fmt) ⇒ String (protected)
Converts a selector to a Sass or SCSS string.
427 428 429 |
# File 'lib/sass/tree/node.rb', line 427
def selector_to_src(sel, tabs, opts, fmt)
fmt == :sass ? selector_to_sass(sel, opts) : selector_to_scss(sel, tabs, opts)
end
|
#semi(fmt) ⇒ String (protected)
Returns a semicolon if this is SCSS, or an empty string if this is Sass.
475 476 477 |
# File 'lib/sass/tree/node.rb', line 475
def semi(fmt)
fmt == :sass ? "" : ";"
end
|
#style ⇒ Symbol
The output style. See the Sass options documentation.
137 138 139 |
# File 'lib/sass/tree/node.rb', line 137
def style
@options[:style]
end
|
#to_s(*args) ⇒ String?
Computes the CSS corresponding to this static CSS tree.
#to_s shouldn't be overridden directly; instead, override #_to_s. Only static-node subclasses need to implement #to_s.
This may return nil
, but it will only do so if #invisible? is true.
151 152 153 154 155 156 |
# File 'lib/sass/tree/node.rb', line 151
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.
234 235 236 |
# File 'lib/sass/tree/node.rb', line 234
def to_sass(tabs = 0, opts = {})
to_src(tabs, opts, :sass)
end
|
#to_scss(tabs = 0, opts = {}) ⇒ String
Converts a node to SCSS code that will generate it.
243 244 245 |
# File 'lib/sass/tree/node.rb', line 243
def to_scss(tabs = 0, opts = {})
to_src(tabs, opts, :scss)
end
|
#to_src(tabs, opts, fmt) ⇒ String (protected)
400 401 402 |
# File 'lib/sass/tree/node.rb', line 400
def to_src(tabs, opts, fmt)
Haml::Util.abstract(self)
end
|