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 [{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 && @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 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
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: 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
# 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 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
# 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
# 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
# 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
# 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
# File lib/sass/tree/node.rb, line 201 201: def perform_children(environment) 202: children.map {|c| c.perform(environment)}.flatten 203: end