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

Methods

==   add_rules   continued?   new   perform!   to_s   to_sass  

Constants

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

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 50
50:     def initialize(rule)
51:       @rules = [rule]
52:       super()
53:     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 60
60:     def ==(other)
61:       self.class == other.class && rules == other.rules && super
62:     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 67
67:     def add_rules(node)
68:       @rules += node.rules
69:     end

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

[Source]

    # File lib/sass/tree/rule_node.rb, line 72
72:     def continued?
73:       @rules.last[-1] == ?,
74:     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 83
 83:     def to_s(tabs, super_rules = nil)
 84:       resolved_rules = resolve_parent_refs(super_rules)
 85: 
 86:       properties = []
 87:       sub_rules = []
 88: 
 89:       rule_separator = style == :compressed ? ',' : ', '
 90:       line_separator = [:nested, :expanded].include?(style) ? ",\n" : rule_separator
 91:       rule_indent = '  ' * (tabs - 1)
 92:       per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent]
 93: 
 94:       total_rule = total_indent + resolved_rules.map do |line|
 95:         per_rule_indent + line.join(rule_separator)
 96:       end.join(line_separator)
 97: 
 98:       children.each do |child|
 99:         next if child.invisible?
100:         if child.is_a? RuleNode
101:           sub_rules << child
102:         else
103:           properties << child
104:         end
105:       end
106: 
107:       to_return = ''
108:       if !properties.empty?
109:         old_spaces = '  ' * (tabs - 1)
110:         spaces = '  ' * tabs
111:         if @options[:line_comments] && style != :compressed
112:           to_return << "#{old_spaces}/* line #{line}"
113: 
114:           if filename
115:             relative_filename = if @options[:css_filename]
116:               begin
117:                 Pathname.new(filename).relative_path_from(  
118:                   Pathname.new(File.dirname(@options[:css_filename]))).to_s
119:               rescue ArgumentError
120:                 nil
121:               end
122:             end
123:             relative_filename ||= filename
124:             to_return << ", #{relative_filename}"
125:           end
126: 
127:           to_return << " */\n"
128:         end
129: 
130:         if style == :compact
131:           properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(' ')
132:           to_return << "#{total_rule} { #{properties} }\n"
133:         elsif style == :compressed
134:           properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(';')
135:           to_return << "#{total_rule}{#{properties}}"
136:         else
137:           properties = properties.map { |a| a.to_s(tabs + 1) }.select{|a| a && a.length > 0}.join("\n")
138:           end_props = (style == :expanded ? "\n" + old_spaces : ' ')
139:           to_return << "#{total_rule} {\n#{properties}#{end_props}}\n"
140:         end
141:       end
142: 
143:       tabs += 1 unless properties.empty? || style != :nested
144:       sub_rules.each do |sub|
145:         to_return << sub.to_s(tabs, resolved_rules)
146:       end
147: 
148:       to_return
149:     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 158
158:     def perform!(environment)
159:       @parsed_rules = @rules.map {|r| parse_selector(interpolate(r, environment))}
160:       super
161:     end

[Validate]