| Class | Sass::Tree::Node |
| In: |
lib/sass/tree/node.rb
|
| Parent: | Object |
The abstract superclass of all 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] |
| has_children | [RW] |
Whether or not this node has child nodes. This may be true even when
\{children} is empty, in which case this node has an empty block (e.g.
`{}`).
@return [Boolean] |
| 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, Array<Tree::Node>] The child node or nodes @raise [Sass::SyntaxError] if `child` is invalid @see invalid_child?
# File lib/sass/tree/node.rb, line 90
90: def <<(child)
91: return if child.nil?
92: if child.is_a?(Array)
93: child.each {|c| self << c}
94: else
95: check_child! child
96: self.has_children = true
97: @children << child
98: end
99: 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 123
123: def ==(other)
124: self.class == other.class && other.children == children
125: end
Raises an error if the given child node is invalid.
@param child [Tree::Node] The child node @raise [Sass::SyntaxError] if `child` is invalid @see invalid_child?
# File lib/sass/tree/node.rb, line 106
106: def check_child!(child)
107: if msg = invalid_child?(child)
108: raise Sass::SyntaxError.new(msg, :line => child.line)
109: end
110: end
@private
# File lib/sass/tree/node.rb, line 73
73: def children=(children)
74: self.has_children ||= !children.empty?
75: @children = children
76: end
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!}.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions defined for this tree
@param 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
@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 192
192: def cssize(extends, parent = nil)
193: _cssize(extends, (parent if parent.class == self.class))
194: rescue Sass::SyntaxError => e
195: e.modify_backtrace(:filename => filename, :line => line)
196: raise e
197: end
Converts a static CSS tree (e.g. the output of \{cssize}) into another static CSS tree, with the given extensions applied to all relevant {RuleNode}s.
@todo Link this to the reference documentation on `@extend`
when such a thing exists.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions to perform on this tree
@return [Tree::Node] The resulting tree of static CSS nodes. @raise [Sass::SyntaxError] Only if there‘s a programmer error
and this is not a static CSS tree
# File lib/sass/tree/node.rb, line 170
170: def do_extend(extends)
171: node = dup
172: node.children = children.map {|c| c.do_extend(extends)}
173: node
174: rescue Sass::SyntaxError => e
175: e.modify_backtrace(:filename => filename, :line => line)
176: raise e
177: end
The name of the document on which this node appeared.
@return [String]
# File lib/sass/tree/node.rb, line 81
81: def filename
82: @filename || (@options && @options[:filename])
83: 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 132
132: def invisible?; false; end
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!}.
@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 212
212: def perform(environment)
213: _perform(environment)
214: rescue Sass::SyntaxError => e
215: e.modify_backtrace(:filename => filename, :line => line)
216: raise e
217: end
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.
@param args [Array] Passed on to \{_to_s} @return [String, nil] The resulting CSS @see Sass::Tree
# File lib/sass/tree/node.rb, line 151
151: def to_s(*args)
152: _to_s(*args)
153: rescue Sass::SyntaxError => e
154: e.modify_backtrace(:filename => filename, :line => line)
155: raise e
156: 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/tree/node.rb, line 234
234: def to_sass(tabs = 0, opts = {})
235: to_src(tabs, opts, :sass)
236: end
Converts a node to SCSS code that will generate it.
@param tabs [Fixnum] The amount of tabulation to use for the SCSS code @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @return [String] The Sass code corresponding to the node
# File lib/sass/tree/node.rb, line 243
243: def to_scss(tabs = 0, opts = {})
244: to_src(tabs, opts, :scss)
245: end
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.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions defined for this tree
@param 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
@return [Tree::Node, Array<Tree::Node>] The resulting static CSS nodes @raise [Sass::SyntaxError] if some element of the tree is invalid @see cssize @see Sass::Tree
# File lib/sass/tree/node.rb, line 276
276: def _cssize(extends, parent)
277: node = dup
278: node.cssize!(extends, parent)
279: node
280: 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 303
303: def _perform(environment)
304: node = dup
305: node.perform!(environment)
306: node
307: end
Computes the CSS corresponding to this particular Sass node.
This method should never raise {Sass::SyntaxError}s. 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}.
@param args [Array] ignored @return [String, nil] The resulting CSS @see to_s @see Sass::Tree
# File lib/sass/tree/node.rb, line 260
260: def _to_s
261: raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #_to_s or #to_s.")
262: end
@see Haml::Shared.balance @raise [Sass::SyntaxError] if the brackets aren‘t balanced
# File lib/sass/tree/node.rb, line 349
349: def balance(*args)
350: res = Haml::Shared.balance(*args)
351: return res if res
352: raise Sass::SyntaxError.new("Unbalanced brackets.", :line => line)
353: end
Converts the children of this node to a Sass or SCSS string. This will return the trailing newline for the previous line, including brackets if this is SCSS.
@param tabs [Fixnum] The amount of tabulation to use for the Sass code @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @param fmt [Symbol] `:sass` or `:scss` @return [String] The Sass or SCSS code corresponding to the children
# File lib/sass/tree/node.rb, line 396
396: def children_to_src(tabs, opts, fmt)
397: return fmt == :sass ? "\n" : " {}\n" if children.empty?
398:
399: (fmt == :sass ? "\n" : " {\n") +
400: children.map {|c| c.send("to_#{fmt}", tabs + 1, opts)}.join.rstrip +
401: (fmt == :sass ? "\n" : " }\n")
402: end
Destructively converts this static Sass node into a static CSS node. This does modify this node, but will be run non-destructively by \{_cssize\}.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions defined for this tree
@param 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 cssize
# File lib/sass/tree/node.rb, line 291
291: def cssize!(extends, parent)
292: self.children = children.map {|c| c.cssize(extends, self)}.flatten
293: end
Convert any underscores in a string into hyphens, but only if the `:dasherize` option is set.
@param s [String] The string to convert @param opts [{Symbol => Object}] The options hash @return [String] The converted string
# File lib/sass/tree/node.rb, line 447
447: def dasherize(s, opts)
448: if opts[:dasherize]
449: s.gsub('_', '-')
450: else
451: s
452: end
453: end
Returns an error message if the given child node is invalid, and false otherwise.
By default, all child nodes except those only allowed under specific nodes ({Tree::MixinDefNode}, {Tree::ImportNode}, {Tree::ExtendNode}) 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 366
366: def invalid_child?(child)
367: case child
368: when Tree::MixinDefNode
369: "Mixins may only be defined at the root of a document."
370: when Tree::ImportNode
371: "Import directives may only be used at the root of a document."
372: end
373: 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 316
316: def perform!(environment)
317: self.children = perform_children(Environment.new(environment))
318: self.children.each {|c| check_child! c}
319: 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 326
326: def perform_children(environment)
327: children.map {|c| c.perform(environment)}.flatten
328: end
Replaces SassScript in a chunk of text with the resulting value.
@param text [Array<String, Sass::Script::Node>] 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 337
337: def run_interp(text, environment)
338: text.map do |r|
339: next r if r.is_a?(String)
340: val = r.perform(environment)
341: # Interpolated strings should never render with quotes
342: next val.value if val.is_a?(Sass::Script::String)
343: val.to_s
344: end.join.strip
345: end
Converts a selector to a Sass string.
@param sel [Array<String, Sass::Script::Node>] The selector to convert @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @return [String] The Sass code corresponding to the selector
# File lib/sass/tree/node.rb, line 420
420: def selector_to_sass(sel, opts)
421: sel.map do |r|
422: if r.is_a?(String)
423: r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
424: else
425: "\#{#{r.to_sass(opts)}}"
426: end
427: end.join
428: end
Converts a selector to a SCSS string.
@param sel [Array<String, Sass::Script::Node>] The selector to convert @param tabs [Fixnum] The indentation of the selector @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @return [String] The SCSS code corresponding to the selector
# File lib/sass/tree/node.rb, line 436
436: def selector_to_scss(sel, tabs, opts)
437: sel.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
438: join.gsub(/^[ \t]*/, ' ' * tabs)
439: end
Converts a selector to a Sass or SCSS string.
@param sel [Array<String, Sass::Script::Node>] The selector to convert @param tabs [Fixnum] The indentation of the selector @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @param fmt [Symbol] `:sass` or `:scss` @return [String] The Sass or SCSS code corresponding to the selector
# File lib/sass/tree/node.rb, line 411
411: def selector_to_src(sel, tabs, opts, fmt)
412: fmt == :sass ? selector_to_sass(sel, opts) : selector_to_scss(sel, tabs, opts)
413: end
Converts a node to Sass or SCSS code that will generate it.
This method is called by the default \{to_sass} and \{to_scss} methods, so that the same code can be used for both with minor variations.
@param tabs [Fixnum] The amount of tabulation to use for the SCSS code @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) @param fmt [Symbol] `:sass` or `:scss` @return [String] The Sass or SCSS code corresponding to the node
# File lib/sass/tree/node.rb, line 384
384: def to_src(tabs, opts, fmt)
385: raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_#{fmt}.")
386: end