Class | Sass::Tree::RuleNode |
In: |
lib/sass/tree/rule_node.rb
|
Parent: | Node |
A static node reprenting a CSS rule.
@see Sass::Tree
PARENT | = | '&' | The character used to include the parent selector |
group_end | [RW] |
Whether or not this rule is the last rule in a nested group. This is only
set in a CSS tree.
@return [Boolean] |
parsed_rules | [RW] |
The CSS selector for this rule, without any
unresolved interpolation but with parent references still intact.
It‘s only set once {Tree::Node#perform} has been called.
@return [Selector::CommaSequence] |
resolved_rules | [RW] |
The CSS selector for this rule, without any
unresolved interpolation or parent references. It‘s only set once {Tree::Node#cssize} has been called.
@return [Selector::CommaSequence] |
rule | [RW] |
The CSS selector for this rule, interspersed with
{Sass::Script::Node}s representing
`#{}`-interpolation. Any adjacent strings will be merged together.
@return [Array<String, Sass::Script::Node>] |
tabs | [RW] |
How deep this rule is indented relative to a base-level rule. This is only
greater than 0 in the case that:
@return [Fixnum] |
@param rule [Array<String, Sass::Script::Node>]
The CSS rule. See \{#rule}
# File lib/sass/tree/rule_node.rb, line 55 55: def initialize(rule) 56: @rule = Haml::Util.strip_string_array( 57: Haml::Util.merge_adjacent_strings(rule)) 58: @tabs = 0 59: super() 60: end
Compares the contents of two rules.
@param other [Object] The object to compare with @return [Boolean] Whether or not this node and the other object
are the same
# File lib/sass/tree/rule_node.rb, line 67 67: def ==(other) 68: self.class == other.class && rule == other.rule && super 69: end
@return [Boolean] Whether or not this rule is continued on the next line
# File lib/sass/tree/rule_node.rb, line 80 80: def continued? 81: last = @rule.last 82: last.is_a?(String) && last[-1] == ?, 83: end
Extends this Rule‘s selector with the given `extends`.
@see Node#do_extend
# File lib/sass/tree/rule_node.rb, line 108 108: def do_extend(extends) 109: node = dup 110: node.resolved_rules = resolved_rules.do_extend(extends) 111: node 112: end
@see Node#to_sass
# File lib/sass/tree/rule_node.rb, line 86 86: def to_sass(tabs, opts = {}) 87: name = selector_to_sass(rule, opts) 88: name = "\\" + name if name[0] == ?: 89: name.gsub(/^/, ' ' * tabs) + children_to_src(tabs, opts, :sass) 90: end
@see Node#to_scss
# File lib/sass/tree/rule_node.rb, line 93 93: def to_scss(tabs, opts = {}) 94: name = selector_to_scss(rule, tabs, opts) 95: res = name + children_to_src(tabs, opts, :scss) 96: 97: if children.last.is_a?(CommentNode) && children.last.silent 98: res.slice!(-3..-1) 99: res << "\n" << (' ' * tabs) << "}\n" 100: end 101: 102: res 103: end
Converts nested rules into a flat list of rules.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions defined for this tree
@param parent [RuleNode, nil] The parent node of this node,
or nil if the parent isn't a {RuleNode}
# File lib/sass/tree/rule_node.rb, line 197 197: def _cssize(extends, parent) 198: node = super 199: rules = node.children.select {|c| c.is_a?(RuleNode)} 200: props = node.children.reject {|c| c.is_a?(RuleNode) || c.invisible?} 201: 202: unless props.empty? 203: node.children = props 204: rules.each {|r| r.tabs += 1} if style == :nested 205: rules.unshift(node) 206: end 207: 208: rules.last.group_end = true unless parent || rules.empty? 209: 210: rules 211: end
Computes the CSS for the rule.
@param tabs [Fixnum] The level of indentation for the CSS @return [String] The resulting CSS
# File lib/sass/tree/rule_node.rb, line 120 120: def _to_s(tabs) 121: tabs = tabs + self.tabs 122: 123: rule_separator = style == :compressed ? ',' : ', ' 124: line_separator = 125: case style 126: when :nested, :expanded; "\n" 127: when :compressed; "" 128: else; " " 129: end 130: rule_indent = ' ' * (tabs - 1) 131: per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent] 132: 133: total_rule = total_indent + resolved_rules.members. 134: map {|seq| seq.to_a.join}. 135: join(rule_separator).split("\n").map do |line| 136: per_rule_indent + line.strip 137: end.join(line_separator) 138: 139: to_return = '' 140: old_spaces = ' ' * (tabs - 1) 141: spaces = ' ' * tabs 142: if style != :compressed 143: if @options[:debug_info] 144: to_return << debug_info_rule.to_s(tabs) << "\n" 145: elsif @options[:line_comments] 146: to_return << "#{old_spaces}/* line #{line}" 147: 148: if filename 149: relative_filename = if @options[:css_filename] 150: begin 151: Pathname.new(filename).relative_path_from( 152: Pathname.new(File.dirname(@options[:css_filename]))).to_s 153: rescue ArgumentError 154: nil 155: end 156: end 157: relative_filename ||= filename 158: to_return << ", #{relative_filename}" 159: end 160: 161: to_return << " */\n" 162: end 163: end 164: 165: if style == :compact 166: properties = children.map { |a| a.to_s(1) }.join(' ') 167: to_return << "#{total_rule} { #{properties} }#{"\n" if group_end}" 168: elsif style == :compressed 169: properties = children.map { |a| a.to_s(1) }.join(';') 170: to_return << "#{total_rule}{#{properties}}" 171: else 172: properties = children.map { |a| a.to_s(tabs + 1) }.join("\n") 173: end_props = (style == :expanded ? "\n" + old_spaces : ' ') 174: to_return << "#{total_rule} {\n#{properties}#{end_props}}#{"\n" if group_end}" 175: end 176: 177: to_return 178: end
Resolves parent references and nested selectors, and updates the indentation based on the parent‘s indentation.
@param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
The extensions defined for this tree
@param parent [RuleNode, nil] The parent node of this node,
or nil if the parent isn't a {RuleNode}
@raise [Sass::SyntaxError] if the rule has no parents but uses `&`
# File lib/sass/tree/rule_node.rb, line 221 221: def cssize!(extends, parent) 222: self.resolved_rules = @parsed_rules.resolve_parent_refs(parent && parent.resolved_rules) 223: super 224: end
A hash that will be associated with this rule in the CSS document if the {file:SASS_REFERENCE.md#debug_info-option `:debug_info` option} is enabled. This data is used by e.g. [the FireSass Firebug extension](addons.mozilla.org/en-US/firefox/addon/103988).
@return [{to_s => to_s}]
# File lib/sass/tree/rule_node.rb, line 243 243: def debug_info 244: {:filename => filename && ("file://" + URI.escape(File.expand_path(filename))), 245: :line => self.line} 246: end
Returns an error message if the given child node is invalid, and false otherwise.
{ExtendNode}s are valid within {RuleNode}s.
@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/rule_node.rb, line 234 234: def invalid_child?(child) 235: super unless child.is_a?(ExtendNode) 236: end
Runs SassScript interpolation in the selector, and then parses the result into a {Sass::Selector::CommaSequence}.
@param environment [Sass::Environment] The lexical environment containing
variable and mixin values
# File lib/sass/tree/rule_node.rb, line 185 185: def perform!(environment) 186: @parsed_rules = Sass::SCSS::StaticParser.new(run_interp(@rule, environment), self.line). 187: parse_selector(self.filename) 188: super 189: end