Class | Sass::Tree::RuleNode |
In: |
lib/sass/css.rb
lib/sass/tree/rule_node.rb |
Parent: | Object |
A static node reprenting a CSS rule.
@see Sass::Tree
PARENT | = | '&' | The character used to include the parent selector |
parsed_rules | [RW] |
The CSS selectors for this rule, parsed for
commas and parent-references. It‘s only set once {Tree::Node#perform} has been called.
It‘s an array of arrays of arrays. The first level of arrays represents distinct lines in the Sass file; the second level represents comma-separated selectors; the third represents structure within those selectors, currently only parent-refs (represented by `:parent`). For example, &.foo, bar, baz, bip, &.bop, bup would be [[[:parent, "foo"], ["bar"], ["baz"]], [["bip"], [:parent, "bop"], ["bup"]]] @return [Array<Array<Array<String|Symbol>>>] |
rules | [RW] |
The CSS selectors for this rule. Each string is a
selector line, and the lines are meant to be separated by commas. For
example,
foo, bar, baz, bip, bop, bup would be ["foo, bar, baz", "bip, bop, bup"] @return [Array<String>] |
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 59 59: def ==(other) 60: self.class == other.class && rules == other.rules && super 61: end
@return [Boolean] Whether or not this rule is continued on the next line
# File lib/sass/tree/rule_node.rb, line 71 71: def continued? 72: @rules.last[-1] == ?, 73: end
Computes the CSS for the rule.
@param tabs [Fixnum] The level of indentation for the CSS @param super_rules [Array<Array<String>>] The rules for the parent node
(see \{#rules}), or `nil` if there are no parents
@return [String] The resulting CSS @raise [Sass::SyntaxError] if the rule has no parents but uses `&`
# File lib/sass/tree/rule_node.rb, line 82 82: def to_s(tabs, super_rules = nil) 83: resolved_rules = resolve_parent_refs(super_rules) 84: 85: properties = [] 86: sub_rules = [] 87: 88: rule_separator = style == :compressed ? ',' : ', ' 89: line_separator = [:nested, :expanded].include?(style) ? ",\n" : rule_separator 90: rule_indent = ' ' * (tabs - 1) 91: per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent] 92: 93: total_rule = total_indent + resolved_rules.map do |line| 94: per_rule_indent + line.join(rule_separator) 95: end.join(line_separator) 96: 97: children.each do |child| 98: next if child.invisible? 99: if child.is_a? RuleNode 100: sub_rules << child 101: else 102: properties << child 103: end 104: end 105: 106: to_return = '' 107: if !properties.empty? 108: old_spaces = ' ' * (tabs - 1) 109: spaces = ' ' * tabs 110: if @options[:line_comments] && style != :compressed 111: to_return << "#{old_spaces}/* line #{line}" 112: 113: if filename 114: relative_filename = if @options[:css_filename] 115: begin 116: Pathname.new(filename).relative_path_from( 117: Pathname.new(File.dirname(@options[:css_filename]))).to_s 118: rescue ArgumentError 119: nil 120: end 121: end 122: relative_filename ||= filename 123: to_return << ", #{relative_filename}" 124: end 125: 126: to_return << " */\n" 127: end 128: 129: if style == :compact 130: properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(' ') 131: to_return << "#{total_rule} { #{properties} }\n" 132: elsif style == :compressed 133: properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(';') 134: to_return << "#{total_rule}{#{properties}}" 135: else 136: properties = properties.map { |a| a.to_s(tabs + 1) }.select{|a| a && a.length > 0}.join("\n") 137: end_props = (style == :expanded ? "\n" + old_spaces : ' ') 138: to_return << "#{total_rule} {\n#{properties}#{end_props}}\n" 139: end 140: end 141: 142: tabs += 1 unless properties.empty? || style != :nested 143: sub_rules.each do |sub| 144: to_return << sub.to_s(tabs, resolved_rules) 145: end 146: 147: to_return 148: end
@see Node#to_sass
# File lib/sass/css.rb, line 26 26: def to_sass(tabs, opts = {}) 27: name = rules.first 28: name = "\\" + name if name[0] == ?: 29: str = "\n#{' ' * tabs}#{name}#{children.any? { |c| c.is_a? PropNode } ? "\n" : ''}" 30: 31: children.each do |child| 32: str << "#{child.to_sass(tabs + 1, opts)}" 33: end 34: 35: str 36: end
Runs any SassScript that may be embedded in the rule, and parses the selectors for commas.
@param environment [Sass::Environment] The lexical environment containing
variable and mixin values
# File lib/sass/tree/rule_node.rb, line 157 157: def perform!(environment) 158: @parsed_rules = @rules.map {|r| parse_selector(interpolate(r, environment))} 159: super 160: end
# File lib/sass/tree/rule_node.rb, line 197 197: def parse_selector(text) 198: scanner = StringScanner.new(text) 199: rules = [[]] 200: 201: while scanner.rest? 202: rules.last << scanner.scan(/[^",&]*/) 203: case scanner.scan(/./) 204: when '&'; rules.last << :parent 205: when ',' 206: scanner.scan(/\s*/) 207: rules << [] if scanner.rest? 208: when '"' 209: rules.last << '"' << scanner.scan(/([^"\\]|\\.)*/) 210: # We don't want to enforce that strings are closed, 211: # but we do want to consume quotes or trailing backslashes. 212: rules.last << scanner.scan(/./) if scanner.rest? 213: end 214: end 215: 216: rules.map! do |l| 217: Haml::Util.merge_adjacent_strings(l).reject {|r| r.is_a?(String) && r.empty?} 218: end 219: 220: rules 221: end
# File lib/sass/tree/rule_node.rb, line 164 164: def resolve_parent_refs(super_rules) 165: if super_rules.nil? 166: return @parsed_rules.map do |line| 167: line.map do |rule| 168: if rule.include?(:parent) 169: raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'.", self.line) 170: end 171: 172: rule.join 173: end.compact 174: end 175: end 176: 177: new_rules = [] 178: super_rules.each do |super_line| 179: @parsed_rules.each do |line| 180: new_rules << [] 181: 182: super_line.each do |super_rule| 183: line.each do |rule| 184: rule = [:parent, " ", *rule] unless rule.include?(:parent) 185: 186: new_rules.last << rule.map do |segment| 187: next segment unless segment == :parent 188: super_rule 189: end.join 190: end 191: end 192: end 193: end 194: new_rules 195: end