Class: Sass::Tree::Node

Inherits:
Object show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



42
43
44
# File 'lib/sass/tree/node.rb', line 42

def initialize
  @children = []
end

Instance Attribute Details

#childrenArray<Tree::Node>

The child nodes of this node.

Returns:



24
25
26
# File 'lib/sass/tree/node.rb', line 24

def children
  @children
end

#filenameString

The name of the document on which this node appeared.

Returns:

  • (String)


58
59
60
# File 'lib/sass/tree/node.rb', line 58

def filename
  @filename || @options[:filename]
end

#lineFixnum

The line of the document on which this node appeared.

Returns:

  • (Fixnum)


29
30
31
# File 'lib/sass/tree/node.rb', line 29

def line
  @line
end

#optionsHash<Symbol, Object>

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

Returns:



40
41
42
# File 'lib/sass/tree/node.rb', line 40

def options
  @options
end

Instance Method Details

#<<(child) ⇒ Object

Appends a child to the node.

Parameters:

Raises:

See Also:



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.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether or not this node and the other object are the same

See Also:



94
95
96
# File 'lib/sass/tree/node.rb', line 94

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.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

See Also:



177
178
179
180
181
# File 'lib/sass/tree/node.rb', line 177

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

#balance(*args) ⇒ Object (protected)

Raises:

See Also:

  • Haml::Shared.balance


228
229
230
231
232
# File 'lib/sass/tree/node.rb', line 228

def balance(*args)
  res = Haml::Shared.balance(*args)
  return res if res
  raise Sass::SyntaxError.new("Unbalanced brackets.", line)
end

#interpolate(text, environment) ⇒ String (protected)

Replaces SassScript in a chunk of text (via #{}) with the resulting value.

Parameters:

  • text (String)

    The text to interpolate

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

  • (String)

    The interpolated text



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/sass/tree/node.rb', line 210

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.

Parameters:

Returns:

  • (Boolean, String)

    Whether or not the child node is valid, as well as the error message to display if it is invalid



244
245
246
# File 'lib/sass/tree/node.rb', line 244

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.

Returns:

  • (Boolean)


111
# File 'lib/sass/tree/node.rb', line 111

def invisible?; false; end

#lastTree::Node

Return the last child node.

We need this because Sass::Tree::Node duck types as an Array for Engine.

Returns:



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 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!.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

  • (Tree::Node)

    The resulting tree of static nodes

Raises:

See Also:



154
155
156
157
158
# File 'lib/sass/tree/node.rb', line 154

def perform(environment)
  environment.options = @options if self.class == Tree::Node
  _perform(environment)
rescue Sass::SyntaxError => e; e.add_metadata(filename, line)
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.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

See Also:



190
191
192
# File 'lib/sass/tree/node.rb', line 190

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.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

  • (Array<Tree::Node>)

    The resulting static nodes



199
200
201
# File 'lib/sass/tree/node.rb', line 199

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

#renderObject

Runs the dynamic Sass code and computes the CSS for the tree.

See Also:



102
103
104
# File 'lib/sass/tree/node.rb', line 102

def render
  perform(Environment.new).to_s
end

#styleSymbol

The output style. See the Sass options documentation.

Returns:

  • (Symbol)


163
164
165
# File 'lib/sass/tree/node.rb', line 163

def style
  @options[:style]
end

#to_sString?

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.

Returns:

  • (String, nil)

    The resulting CSS

Raises:

See Also:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sass/tree/node.rb', line 122

def to_s
  result = String.new
  children.each do |child|
    if child.is_a? PropNode
      raise Sass::SyntaxError.new('Properties 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
  result.rstrip!
  return "" if result.empty?
  return result + "\n"
rescue Sass::SyntaxError => e; e.add_metadata(filename, line)
end

#to_sass(tabs = 0, opts = {}) ⇒ String

Converts a node to Sass code that will generate it.

Parameters:

  • tabs (Fixnum) (defaults to: 0)

    The amount of tabulation to use for the Sass code

  • opts (Hash<Symbol, Object>) (defaults to: {})

    An options hash (see CSS#initialize)

Returns:

  • (String)

    The Sass code corresponding to the node



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