| 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] The child node @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: check_child! child
93: self.has_children = true
94: @children << child
95: 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 119
119: def ==(other)
120: self.class == other.class && other.children == children
121: 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 102
102: def check_child!(child)
103: if msg = invalid_child?(child)
104: raise Sass::SyntaxError.new(msg, :line => child.line)
105: end
106: 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 188
188: def cssize(extends, parent = nil)
189: _cssize(extends, (parent if parent.class == self.class))
190: rescue Sass::SyntaxError => e
191: e.modify_backtrace(:filename => filename, :line => line)
192: raise e
193: 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 166
166: def do_extend(extends)
167: node = dup
168: node.children = children.map {|c| c.do_extend(extends)}
169: node
170: rescue Sass::SyntaxError => e
171: e.modify_backtrace(:filename => filename, :line => line)
172: raise e
173: 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 128
128: 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 208
208: def perform(environment)
209: _perform(environment)
210: rescue Sass::SyntaxError => e
211: e.modify_backtrace(:filename => filename, :line => line)
212: raise e
213: 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 147
147: def to_s(*args)
148: _to_s(*args)
149: rescue Sass::SyntaxError => e
150: e.modify_backtrace(:filename => filename, :line => line)
151: raise e
152: 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 230
230: def to_sass(tabs = 0, opts = {})
231: to_src(tabs, opts, :sass)
232: 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 239
239: def to_scss(tabs = 0, opts = {})
240: to_src(tabs, opts, :scss)
241: 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 272
272: def _cssize(extends, parent)
273: node = dup
274: node.cssize!(extends, parent)
275: node
276: 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 299
299: def _perform(environment)
300: node = dup
301: node.perform!(environment)
302: node
303: 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 256
256: def _to_s
257: raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #_to_s or #to_s.")
258: end
@see Haml::Shared.balance @raise [Sass::SyntaxError] if the brackets aren‘t balanced
# File lib/sass/tree/node.rb, line 344
344: def balance(*args)
345: res = Haml::Shared.balance(*args)
346: return res if res
347: raise Sass::SyntaxError.new("Unbalanced brackets.", :line => line)
348: 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 393
393: def children_to_src(tabs, opts, fmt)
394: return fmt == :sass ? "\n" : " {}\n" if children.empty?
395:
396: (fmt == :sass ? "\n" : " {\n") +
397: children.map {|c| c.send("to_#{fmt}", tabs + 1, opts)}.join.rstrip +
398: (fmt == :sass ? "\n" : " }\n")
399: 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 287
287: def cssize!(extends, parent)
288: self.children = children.map {|c| c.cssize(extends, self)}.flatten
289: 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 444
444: def dasherize(s, opts)
445: if opts[:dasherize]
446: s.gsub('_', '-')
447: else
448: s
449: end
450: 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 361
361: def invalid_child?(child)
362: case child
363: when Tree::MixinDefNode
364: "Mixins may only be defined at the root of a document."
365: when Tree::ImportNode
366: "Import directives may only be used at the root of a document."
367: when Tree::ExtendNode
368: "Extend directives may only be used within rules."
369: end
370: 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 312
312: def perform!(environment)
313: self.children = perform_children(Environment.new(environment))
314: 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 321
321: def perform_children(environment)
322: children.map {|c| c.perform(environment)}.flatten
323: 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 332
332: def run_interp(text, environment)
333: text.map do |r|
334: next r if r.is_a?(String)
335: val = r.perform(environment)
336: # Interpolated strings should never render with quotes
337: next val.value if val.is_a?(Sass::Script::String)
338: val.to_s
339: end.join.strip
340: 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 417
417: def selector_to_sass(sel, opts)
418: sel.map do |r|
419: if r.is_a?(String)
420: r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
421: else
422: "\#{#{r.to_sass(opts)}}"
423: end
424: end.join
425: 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 433
433: def selector_to_scss(sel, tabs, opts)
434: sel.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
435: join.gsub(/^[ \t]*/, ' ' * tabs)
436: 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 408
408: def selector_to_src(sel, tabs, opts, fmt)
409: fmt == :sass ? selector_to_sass(sel, opts) : selector_to_scss(sel, tabs, opts)
410: 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 381
381: def to_src(tabs, opts, fmt)
382: raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_#{fmt}.")
383: end