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.
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 [Hash<Symbol, Object>] |
Appends a child to the node.
@param child [Tree::Node] The child node @raise [Sass::SyntaxError] if `child` is invalid @see invalid_child?
# 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
# 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]
# File lib/sass/tree/node.rb, line 58 58: def filename 59: @filename || @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]
# 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
# File lib/sass/tree/node.rb, line 79 79: def last 80: children.last 81: 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
# File lib/sass/tree/node.rb, line 154 154: def perform(environment) 155: environment.options = @options if self.class == Tree::Node 156: _perform(environment) 157: rescue Sass::SyntaxError => e; e.add_metadata(filename, line) 158: 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
# 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: raise Sass::SyntaxError.new('Properties aren\'t allowed at the root of a document.', child.line) 127: else 128: next if child.invisible? 129: child_str = child.to_s(1) 130: result << child_str + (style == :compressed ? '' : "\n") 131: end 132: end 133: result.rstrip! 134: return "" if result.empty? 135: return result + "\n" 136: rescue Sass::SyntaxError => e; e.add_metadata(filename, line) 137: 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 [Hash<Symbol, Object>] An options hash (see {Sass::CSS#initialize}) @return [String] The Sass code corresponding to the node
# 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
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
# File lib/sass/tree/node.rb, line 177 177: def _perform(environment) 178: node = dup 179: node.perform!(environment) 180: node 181: end
@see Haml::Shared.balance @raise [Sass::SyntaxError] if the brackets aren‘t balanced
# File lib/sass/tree/node.rb, line 228 228: def balance(*args) 229: res = Haml::Shared.balance(*args) 230: return res if res 231: raise Sass::SyntaxError.new("Unbalanced brackets.", line) 232: 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
# File lib/sass/tree/node.rb, line 210 210: def interpolate(text, environment) 211: res = '' 212: rest = Haml::Shared.handle_interpolation text do |scan| 213: escapes = scan[2].size 214: res << scan.matched[0...-2 - escapes] 215: if escapes % 2 == 1 216: res << "\\" * (escapes - 1) << '#{' 217: else 218: res << "\\" * [0, escapes - 1].max 219: res << Script::Parser.new(scan, line, scan.pos - scan.matched_size, filename). 220: parse_interpolated.perform(environment).to_s 221: end 222: end 223: res + rest 224: 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
# File lib/sass/tree/node.rb, line 244 244: def invalid_child?(child) 245: false 246: 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
# File lib/sass/tree/node.rb, line 190 190: def perform!(environment) 191: self.children = perform_children(Environment.new(environment)) 192: 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
# File lib/sass/tree/node.rb, line 199 199: def perform_children(environment) 200: children.map {|c| c.perform(environment)}.flatten 201: end