Class Sass::Tree::Node
In: lib/sass/css.rb
lib/sass/tree/node.rb
Parent: Object

This class doubles as the root node of the parse tree and the superclass of all other parse-tree nodes.

Methods

<<   ==   _perform   balance   filename   interpolate   invalid_child?   invisible?   last   new   options=   perform   perform!   perform_children   render   style   to_s   to_sass  

Attributes

children  [RW]  The child nodes of this node.

@return [Array<Tree::Node>]

filename  [W]  The name of the document on which this node appeared.

@return [String]

line  [RW]  The line of the document on which this node appeared.

@return [Fixnum]

options  [R]  The options hash for the node. See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.

@return [{Symbol => Object}]

Public Class methods

[Source]

    # File lib/sass/tree/node.rb, line 42
42:       def initialize
43:         @children = []
44:       end

Public Instance methods

Appends a child to the node.

@param child [Tree::Node] The child node @raise [Sass::SyntaxError] if `child` is invalid @see invalid_child?

[Source]

    # File lib/sass/tree/node.rb, line 67
67:       def <<(child)
68:         if msg = invalid_child?(child)
69:           raise Sass::SyntaxError.new(msg, child.line)
70:         end
71:         @children << child
72:       end

Compares this node and another object (only other {Tree::Node}s 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.

@param other [Object] The object to compare with @return [Boolean] Whether or not this node and the other object

  are the same

@see Sass::Tree

[Source]

    # File lib/sass/tree/node.rb, line 94
94:       def ==(other)
95:         self.class == other.class && other.children == children
96:       end

The name of the document on which this node appeared.

@return [String]

[Source]

    # File lib/sass/tree/node.rb, line 58
58:       def filename
59:         @filename || (@options && @options[:filename])
60:       end

True if \{to_s} will return `nil`; that is, if the node shouldn‘t be rendered. Should only be called in a static tree.

@return [Boolean]

[Source]

     # File lib/sass/tree/node.rb, line 111
111:       def invisible?; false; end

Return the last child node.

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

@return [Tree::Node] The last child node

[Source]

    # File lib/sass/tree/node.rb, line 79
79:       def last
80:         children.last
81:       end

Sets the options hash for the node and all its children.

@param options [{Symbol => Object}] The options @see options

[Source]

    # File lib/sass/tree/node.rb, line 50
50:       def options=(options)
51:         children.each {|c| c.options = options}
52:         @options = options
53:       end

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

@param environment [Sass::Environment] The lexical environment containing

  variable and mixin values

@return [Tree::Node] The resulting tree of static nodes @raise [Sass::SyntaxError] if some element of the tree is invalid @see Sass::Tree

[Source]

     # File lib/sass/tree/node.rb, line 156
156:       def perform(environment)
157:         environment.options = @options if self.class == Tree::Node
158:         _perform(environment)
159:       rescue Sass::SyntaxError => e; e.add_metadata(filename, line)
160:       end

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

@see perform @see to_s

[Source]

     # File lib/sass/tree/node.rb, line 102
102:       def render
103:         perform(Environment.new).to_s
104:       end

The output style. See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.

@return [Symbol]

[Source]

     # File lib/sass/tree/node.rb, line 165
165:       def style
166:         @options[:style]
167:       end

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.

@return [String, nil] The resulting CSS @raise [Sass::SyntaxError] if some element of the tree is invalid @see Sass::Tree

[Source]

     # File lib/sass/tree/node.rb, line 122
122:       def to_s
123:         result = String.new
124:         children.each do |child|
125:           if child.is_a? PropNode
126:             message = "Properties aren't allowed at the root of a document." +
127:               child.pseudo_class_selector_message
128:             raise Sass::SyntaxError.new(message, child.line)
129:           else
130:             next if child.invisible?
131:             child_str = child.to_s(1)
132:             result << child_str + (style == :compressed ? '' : "\n")
133:           end
134:         end
135:         result.rstrip!
136:         return "" if result.empty?
137:         return result + "\n"
138:       rescue Sass::SyntaxError => e; e.add_metadata(filename, line)
139:       end

Converts a node to Sass code that will generate it.

@param tabs [Fixnum] The amount of tabulation to use for the Sass code @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @return [String] The Sass code corresponding to the node

[Source]

    # File lib/sass/css.rb, line 13
13:       def to_sass(tabs = 0, opts = {})
14:         result = ''
15: 
16:         children.each do |child|
17:           result << "#{'  ' * tabs}#{child.to_sass(0, opts)}\n"
18:         end
19: 
20:         result
21:       end

Protected Instance methods

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

@param environment [Sass::Environment] The lexical environment containing

  variable and mixin values

@return [Tree::Node, Array<Tree::Node>] The resulting static nodes @see perform @see Sass::Tree

[Source]

     # File lib/sass/tree/node.rb, line 179
179:       def _perform(environment)
180:         node = dup
181:         node.perform!(environment)
182:         node
183:       end

@see Haml::Shared.balance @raise [Sass::SyntaxError] if the brackets aren‘t balanced

[Source]

     # File lib/sass/tree/node.rb, line 230
230:       def balance(*args)
231:         res = Haml::Shared.balance(*args)
232:         return res if res
233:         raise Sass::SyntaxError.new("Unbalanced brackets.", line)
234:       end

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

@param text [String] The text to interpolate @param environment [Sass::Environment] The lexical environment containing

  variable and mixin values

@return [String] The interpolated text

[Source]

     # File lib/sass/tree/node.rb, line 212
212:       def interpolate(text, environment)
213:         res = ''
214:         rest = Haml::Shared.handle_interpolation text do |scan|
215:           escapes = scan[2].size
216:           res << scan.matched[0...-2 - escapes]
217:           if escapes % 2 == 1
218:             res << "\\" * (escapes - 1) << '#{'
219:           else
220:             res << "\\" * [0, escapes - 1].max
221:             res << Script::Parser.new(scan, line, scan.pos - scan.matched_size, filename).
222:               parse_interpolated.perform(environment).to_s
223:           end
224:         end
225:         res + rest
226:       end

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.

@param child [Tree::Node] A potential child node @return [Boolean, String] Whether or not the child node is valid,

  as well as the error message to display if it is invalid

[Source]

     # File lib/sass/tree/node.rb, line 246
246:       def invalid_child?(child)
247:         false
248:       end

Destructively runs dynamic Sass code in this particular node. This does modify this node, but will be run non-destructively by \{_perform\}.

@param environment [Sass::Environment] The lexical environment containing

  variable and mixin values

@see perform

[Source]

     # File lib/sass/tree/node.rb, line 192
192:       def perform!(environment)
193:         self.children = perform_children(Environment.new(environment))
194:       end

Non-destructively runs \{perform} on all children of the current node.

@param environment [Sass::Environment] The lexical environment containing

  variable and mixin values

@return [Array<Tree::Node>] The resulting static nodes

[Source]

     # File lib/sass/tree/node.rb, line 201
201:       def perform_children(environment)
202:         children.map {|c| c.perform(environment)}.flatten
203:       end

[Validate]