Class: Sass::Tree::Node

Inherits:
Object show all
Defined in:
lib/sass/tree/node.rb,
lib/sass/css.rb

Overview

The abstract superclass of all parse-tree nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



50
51
52
# File 'lib/sass/tree/node.rb', line 50

def initialize
  @children = []
end

Instance Attribute Details

#childrenArray<Tree::Node>

The child nodes of this node.

Returns:



32
33
34
# File 'lib/sass/tree/node.rb', line 32

def children
  @children
end

#filenameString

The name of the document on which this node appeared.

Returns:

  • (String)


66
67
68
# File 'lib/sass/tree/node.rb', line 66

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

#lineFixnum

The line of the document on which this node appeared.

Returns:

  • (Fixnum)


37
38
39
# File 'lib/sass/tree/node.rb', line 37

def line
  @line
end

#options{Symbol => Object}

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

Returns:



48
49
50
# File 'lib/sass/tree/node.rb', line 48

def options
  @options
end

Instance Method Details

#<<(child)

Appends a child to the node.

Parameters:

Raises:

See Also:



75
76
77
78
79
80
# File 'lib/sass/tree/node.rb', line 75

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.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

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

See Also:



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

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

#_cssize(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.

Parameters:

  • parent (Node, nil)

    The parent node of this node. This should only be non-nil if the parent is the same class as this node

Returns:

Raises:

See Also:



210
211
212
213
214
# File 'lib/sass/tree/node.rb', line 210

def _cssize(parent)
  node = dup
  node.cssize!(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.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

See Also:



235
236
237
238
239
# File 'lib/sass/tree/node.rb', line 235

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

#_to_sString? (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.

Parameters:

  • args (Array)

    ignored

Returns:

  • (String, nil)

    The resulting CSS

Raises:

  • (NotImplementedError)

See Also:



196
197
198
# File 'lib/sass/tree/node.rb', line 196

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


286
287
288
289
290
# File 'lib/sass/tree/node.rb', line 286

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

#cssize(parent = nil) ⇒ Tree::Node

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:

  • parent (Node, nil) (defaults to: nil)

    The parent node of this node. This should only be non-nil if the parent is the same class as this node

Returns:

  • (Tree::Node)

    The resulting tree of static nodes

Raises:

See Also:



156
157
158
159
160
161
# File 'lib/sass/tree/node.rb', line 156

def cssize(parent = nil)
  _cssize((parent if parent.class == self.class))
rescue Sass::SyntaxError => e
  e.modify_backtrace(:filename => filename, :line => line)
  raise e
end

#cssize!(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:

  • parent (Node, nil)

    The parent node of this node. This should only be non-nil if the parent is the same class as this node

See Also:



223
224
225
# File 'lib/sass/tree/node.rb', line 223

def cssize!(parent)
  self.children = children.map {|c| c.cssize(self)}.flatten
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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/sass/tree/node.rb', line 268

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



302
303
304
# File 'lib/sass/tree/node.rb', line 302

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)


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

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:



87
88
89
# File 'lib/sass/tree/node.rb', line 87

def last
  children.last
end

#perform(environment) ⇒ Tree::Node

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:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

Returns:

  • (Tree::Node)

    The resulting tree of static nodes

Raises:

See Also:



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

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:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values

See Also:



248
249
250
# File 'lib/sass/tree/node.rb', line 248

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



257
258
259
# File 'lib/sass/tree/node.rb', line 257

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

#render

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

See Also:



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

def render
  perform(Environment.new).cssize.to_s
end

#styleSymbol

The output style. See the Sass options documentation.

Returns:

  • (Symbol)


124
125
126
# File 'lib/sass/tree/node.rb', line 124

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.

Parameters:

  • args (Array)

    Passed on to #_to_s

Returns:

  • (String, nil)

    The resulting CSS

See Also:



138
139
140
141
142
143
# File 'lib/sass/tree/node.rb', line 138

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.

Parameters:

  • tabs (Fixnum) (defaults to: 0)

    The amount of tabulation to use for the Sass code

  • opts ({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