Class: Sass::Tree::Node

Inherits:
Object show all
Includes:
Enumerable
Defined in:
/var/www/haml-pages/.haml/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

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Node) initialize

A new instance of Node



59
60
61
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 59

def initialize
  @children = []
end

Instance Attribute Details

- (Array<Tree::Node>) children

The child nodes of this node.

Returns:



34
35
36
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 34

def children
  @children
end

- (String) filename

The name of the document on which this node appeared.

Returns:

  • (String)


81
82
83
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 81

def filename
  @filename || (@options && @options[:filename])
end

- (Boolean) has_children

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. {}).

Returns:

  • (Boolean)


41
42
43
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 41

def has_children
  @has_children
end

- (Fixnum) line

The line of the document on which this node appeared.

Returns:

  • (Fixnum)


46
47
48
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 46

def line
  @line
end

- ({Symbol => Object}) options

The options hash for the node. See the Sass options documentation.

Returns:



57
58
59
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 57

def options
  @options
end

Instance Method Details

- <<(child)

Appends a child to the node.

Parameters:

Raises:

See Also:



90
91
92
93
94
95
96
97
98
99
# File '/var/www/haml-pages/.haml/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

- (Boolean) ==(other)

Compares this node and another object (only other 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.

Parameters:

  • (Object) other — The object to compare with

Returns:

  • (Boolean) — Whether or not this node and the other object are the same

See Also:



123
124
125
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 123

def ==(other)
  self.class == other.class && other.children == children
end

- (Tree::Node+) _cssize(extends, parent) (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.

Parameters:

Returns:

Raises:

See Also:



276
277
278
279
280
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 276

def _cssize(extends, parent)
  node = dup
  node.cssize!(extends, parent)
  node
end

- (Tree::Node+) _perform(environment) (protected)

Runs any dynamic Sass code in this particular node. This doesn’t modify this node or any of its children.

Parameters:

  • (Sass::Environment) environment — The lexical environment containing variable and mixin values

Returns:

See Also:



303
304
305
306
307
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 303

def _perform(environment)
  node = dup
  node.perform!(environment)
  node
end

- (String?) _to_s (protected)

Computes the CSS corresponding to this particular Sass node.

This method should never raise Sass::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.

Parameters:

  • (Array) args — ignored

Returns:

  • (String, nil) — The resulting CSS

Raises:

  • (NotImplementedError)

See Also:



260
261
262
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 260

def _to_s
  raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #_to_s or #to_s.")
end

- balance(*args) (protected)

Raises:

See Also:

  • Haml::Shared.balance


348
349
350
351
352
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 348

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.

Parameters:

Raises:

See Also:



106
107
108
109
110
# File '/var/www/haml-pages/.haml/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

- (String) children_to_src(tabs, opts, fmt) (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.

Parameters:

  • (Fixnum) tabs — The amount of tabulation to use for the Sass code
  • ({Symbol => Object}) opts — An options hash (see Sass::CSS#initialize)
  • (Symbol) fmt:sass or :scss

Returns:

  • (String) — The Sass or SCSS code corresponding to the children


397
398
399
400
401
402
403
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 397

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

- (Tree::Node) cssize(extends, parent = nil)

Converts a static Sass tree (e.g. the output of #perform) into a static CSS tree.

#cssize shouldn’t be overridden directly; instead, override #_cssize or #cssize!.

Parameters:

Returns:

  • (Tree::Node) — The resulting tree of static nodes

Raises:

See Also:



192
193
194
195
196
197
# File '/var/www/haml-pages/.haml/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.

Parameters:

See Also:



291
292
293
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 291

def cssize!(extends, parent)
  self.children = children.map {|c| c.cssize(extends, self)}.flatten
end

- (String) dasherize(s, opts) (protected)

Convert any underscores in a string into hyphens, but only if the :dasherize option is set.

Parameters:

  • (String) s — The string to convert
  • ({Symbol => Object}) opts — The options hash

Returns:

  • (String) — The converted string


448
449
450
451
452
453
454
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 448

def dasherize(s, opts)
  if opts[:dasherize]
    s.gsub('_', '-')
  else
    s
  end
end

- (Tree::Node) do_extend(extends)

TODO: Link this to the reference documentation on @extend when such a thing exists.

Converts a static CSS tree (e.g. the output of #cssize) into another static CSS tree, with the given extensions applied to all relevant RuleNodes.

Parameters:

Returns:

  • (Tree::Node) — The resulting tree of static CSS nodes.

Raises:

  • (Sass::SyntaxError) — Only if there’s a programmer error and this is not a static CSS tree


170
171
172
173
174
175
176
177
# File '/var/www/haml-pages/.haml/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(&block) {|node| ... }

Iterates through each node in the tree rooted at this node in a pre-order walk.

Yields:

  • node

Yield Parameters:

  • (Node) node — a node in the tree


224
225
226
227
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 224

def each(&block)
  yield self
  children.each {|c| c.each(&block)}
end

- (Boolean, String) invalid_child?(child) (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 (Tree::MixinDefNode, Tree::ImportNode, Tree::ExtendNode) are valid. This is expected to be overriden by subclasses for which some children are invalid.

Parameters:

Returns:

  • (Boolean, String) — Whether or not the child node is valid, as well as the error message to display if it is invalid


365
366
367
368
369
370
371
372
373
374
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 365

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

- (Boolean) invisible?

True if #to_s will return nil; that is, if the node shouldn’t be rendered. Should only be called in a static tree.

Returns:

  • (Boolean)


132
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 132

def invisible?; false end

- (Tree::Node) perform(environment)

Converts a dynamic tree into a static Sass tree. That is, 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; instead, override #_perform or #perform!.

Parameters:

  • (Sass::Environment) environment — The lexical environment containing variable and mixin values

Returns:

  • (Tree::Node) — The resulting tree of static nodes

Raises:

See Also:



212
213
214
215
216
217
# File '/var/www/haml-pages/.haml/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.

Parameters:

  • (Sass::Environment) environment — The lexical environment containing variable and mixin values

See Also:



316
317
318
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 316

def perform!(environment)
  self.children = perform_children(Environment.new(environment))
end

- (Array<Tree::Node>) perform_children(environment) (protected)

Non-destructively runs #perform on all children of the current node.

Parameters:

  • (Sass::Environment) environment — The lexical environment containing variable and mixin values

Returns:

  • (Array<Tree::Node>) — The resulting static nodes


325
326
327
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 325

def perform_children(environment)
  children.map {|c| c.perform(environment)}.flatten
end

- (String) run_interp(text, environment) (protected)

Replaces SassScript in a chunk of text with the resulting value.

Parameters:

Returns:

  • (String) — The interpolated text


336
337
338
339
340
341
342
343
344
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 336

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

- (String) selector_to_sass(sel, opts) (protected)

Converts a selector to a Sass string.

Parameters:

Returns:

  • (String) — The Sass code corresponding to the selector


421
422
423
424
425
426
427
428
429
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 421

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

- (String) selector_to_scss(sel, tabs, opts) (protected)

Converts a selector to a SCSS string.

Parameters:

Returns:

  • (String) — The SCSS code corresponding to the selector


437
438
439
440
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 437

def selector_to_scss(sel, tabs, opts)
  sel.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
    join.gsub(/^[ \t]*/, '  ' * tabs)
end

- (String) selector_to_src(sel, tabs, opts, fmt) (protected)

Converts a selector to a Sass or SCSS string.

Parameters:

  • (Array<String, Sass::Script::Node>) sel — The selector to convert
  • (Fixnum) tabs — The indentation of the selector
  • ({Symbol => Object}) opts — An options hash (see Sass::CSS#initialize)
  • (Symbol) fmt:sass or :scss

Returns:

  • (String) — The Sass or SCSS code corresponding to the selector


412
413
414
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 412

def selector_to_src(sel, tabs, opts, fmt)
  fmt == :sass ? selector_to_sass(sel, opts) : selector_to_scss(sel, tabs, opts)
end

- (String) semi(fmt) (protected)

Returns a semicolon if this is SCSS, or an empty string if this is Sass.

Parameters:

  • (Symbol) fmt:sass or :scss

Returns:

  • (String) — A semicolon or the empty string


460
461
462
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 460

def semi(fmt)
  fmt == :sass ? "" : ";"
end

- (Symbol) style

The output style. See the Sass options documentation.

Returns:

  • (Symbol)


137
138
139
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 137

def style
  @options[:style]
end

- (String?) to_s(*args)

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.

Parameters:

  • (Array) args — Passed on to #_to_s

Returns:

  • (String, nil) — The resulting CSS

See Also:



151
152
153
154
155
156
# File '/var/www/haml-pages/.haml/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

- (String) to_sass(tabs = 0, opts = {})

Converts a node to Sass code that will generate it.

Parameters:

  • (Fixnum) tabs (defaults to: 0) — The amount of tabulation to use for the Sass code
  • ({Symbol => Object}) opts (defaults to: {}) — An options hash (see Sass::CSS#initialize)

Returns:

  • (String) — The Sass code corresponding to the node


234
235
236
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 234

def to_sass(tabs = 0, opts = {})
  to_src(tabs, opts, :sass)
end

- (String) to_scss(tabs = 0, opts = {})

Converts a node to SCSS code that will generate it.

Parameters:

  • (Fixnum) tabs (defaults to: 0) — The amount of tabulation to use for the SCSS code
  • ({Symbol => Object}) opts (defaults to: {}) — An options hash (see Sass::CSS#initialize)

Returns:

  • (String) — The Sass code corresponding to the node


243
244
245
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 243

def to_scss(tabs = 0, opts = {})
  to_src(tabs, opts, :scss)
end

- (String) to_src(tabs, opts, fmt) (protected)

Converts a node to Sass or SCSS code that will generate it.

This method is called by the default #to_sass and #to_scss methods, so that the same code can be used for both with minor variations.

Parameters:

  • (Fixnum) tabs — The amount of tabulation to use for the SCSS code
  • ({Symbol => Object}) opts — An options hash (see Sass::CSS#initialize)
  • (Symbol) fmt:sass or :scss

Returns:

  • (String) — The Sass or SCSS code corresponding to the node

Raises:

  • (NotImplementedError)


385
386
387
# File '/var/www/haml-pages/.haml/lib/sass/tree/node.rb', line 385

def to_src(tabs, opts, fmt)
  raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_#{fmt}.")
end