Class Sass::Tree::RuleNode
In: lib/sass/css.rb
lib/sass/tree/rule_node.rb
Parent: Object
Haml::Util Engine Color SyntaxError UnitConversionError StandardError Node Operation Literal UnaryOperation Funcall Variable Number String Bool EvaluationContext Node\n[lib/sass/css.rb\nlib/sass/tree/node.rb] DebugNode IfNode CommentNode ForNode MixinNode VariableNode ImportNode WhileNode MixinDefNode Repl CSS Environment Lexer Parser PropNode\n[lib/sass/css.rb\nlib/sass/tree/prop_node.rb] DirectiveNode\n[lib/sass/css.rb\nlib/sass/tree/directive_node.rb] RuleNode\n[lib/sass/css.rb\nlib/sass/tree/rule_node.rb] Rack lib/sass/repl.rb lib/sass/css.rb lib/sass/environment.rb lib/sass/error.rb lib/sass/engine.rb lib/sass/script/lexer.rb lib/sass/script/color.rb lib/sass/script/string.rb lib/sass/script/unary_operation.rb lib/sass/script/variable.rb lib/sass/script/funcall.rb lib/sass/script/operation.rb lib/sass/script/bool.rb lib/sass/script/parser.rb lib/sass/script/literal.rb lib/sass/script/node.rb lib/sass/script/number.rb lib/sass/script/functions.rb Functions Script Files lib/sass/tree/while_node.rb lib/sass/tree/if_node.rb lib/sass/tree/mixin_def_node.rb lib/sass/tree/debug_node.rb lib/sass/tree/for_node.rb lib/sass/tree/import_node.rb lib/sass/tree/prop_node.rb lib/sass/tree/node.rb lib/sass/tree/comment_node.rb lib/sass/tree/mixin_node.rb lib/sass/tree/directive_node.rb lib/sass/tree/rule_node.rb lib/sass/tree/variable_node.rb Tree lib/sass/plugin/rack.rb Plugin Sass dot/m_54_0.png

A static node reprenting a CSS rule.

@see Sass::Tree

Methods

Constants

PARENT = '&'   The character used to include the parent selector

Attributes

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>]

Public Class methods

@param rule [String] The first CSS rule. See \{rules}

[Source]

    # File lib/sass/tree/rule_node.rb, line 49
49:     def initialize(rule)
50:       @rules = [rule]
51:       super()
52:     end

Public Instance methods

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

[Source]

    # File lib/sass/tree/rule_node.rb, line 59
59:     def ==(other)
60:       self.class == other.class && rules == other.rules && super
61:     end

Adds another {RuleNode}’s rules to this one‘s.

@param node [RuleNode] The other node

[Source]

    # File lib/sass/tree/rule_node.rb, line 66
66:     def add_rules(node)
67:       @rules += node.rules
68:     end

@return [Boolean] Whether or not this rule is continued on the next line

[Source]

    # 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 `&`

[Source]

     # 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

[Source]

    # 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

Protected Instance methods

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

[Source]

     # 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

Private Instance methods

[Source]

     # 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

[Source]

     # 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

[Validate]