Class: Sass::Tree::RuleNode

Inherits:
Node show all
Defined in:
lib/sass/css.rb,
lib/sass/tree/rule_node.rb

Constant Summary collapse

PARENT =

The character used to include the parent selector

'&'

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #filename, #line, #options

Instance Method Summary collapse

Methods inherited from Node

#<<, #_perform, #balance, #interpolate, #invalid_child?, #invisible?, #last, #perform, #perform_children, #render, #style, #to_s

Constructor Details

#initialize(rule) ⇒ RuleNode

Returns a new instance of RuleNode.

Parameters:

  • rule (String)

    The first CSS rule. See #rules



46
47
48
49
# File 'lib/sass/tree/rule_node.rb', line 46

def initialize(rule)
  @rules = [rule]
  super()
end

Instance Attribute Details

#parsed_rulesArray<Array<Array<String|Symbol>>>

The CSS selectors for this rule, parsed for commas and parent-references. It's only set once 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"]]]

Returns:

  • (Array<Array<Array<String|Symbol>>>)


43
44
45
# File 'lib/sass/tree/rule_node.rb', line 43

def parsed_rules
  @parsed_rules
end

#rulesArray<String>

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

Returns:

  • (Array<String>)


21
22
23
# File 'lib/sass/tree/rule_node.rb', line 21

def rules
  @rules
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the contents of two rules.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether or not this node and the other object are the same



56
57
58
# File 'lib/sass/tree/rule_node.rb', line 56

def ==(other)
  self.class == other.class && rules == other.rules && super
end

#_to_s(tabs, super_rules = nil) ⇒ String (protected)

Computes the CSS for the rule.

Parameters:

  • tabs (Fixnum)

    The level of indentation for the CSS

  • super_rules (Array<Array<String>>) (defaults to: nil)

    The rules for the parent node (see #rules), or nil if there are no parents

Returns:

  • (String)

    The resulting CSS

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sass/tree/rule_node.rb', line 81

def _to_s(tabs, super_rules = nil)
  resolved_rules = resolve_parent_refs(super_rules)

  properties = []
  sub_rules = []

  rule_separator = style == :compressed ? ',' : ', '
  line_separator = [:nested, :expanded].include?(style) ? ",\n" : rule_separator
  rule_indent = '  ' * (tabs - 1)
  per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent]

  total_rule = total_indent + resolved_rules.map do |line|
    per_rule_indent + line.join(rule_separator)
  end.join(line_separator)

  children.each do |child|
    next if child.invisible?
    if child.is_a? RuleNode
      sub_rules << child
    else
      properties << child
    end
  end

  to_return = ''
  if !properties.empty?
    old_spaces = '  ' * (tabs - 1)
    spaces = '  ' * tabs
    if @options[:line_comments] && style != :compressed
      to_return << "#{old_spaces}/* line #{line}"

      if filename
        relative_filename = if @options[:css_filename]
          begin
            Pathname.new(filename).relative_path_from(  
              Pathname.new(File.dirname(@options[:css_filename]))).to_s
          rescue ArgumentError
            nil
          end
        end
        relative_filename ||= filename
        to_return << ", #{relative_filename}"
      end

      to_return << " */\n"
    end

    if style == :compact
      properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(' ')
      to_return << "#{total_rule} { #{properties} }\n"
    elsif style == :compressed
      properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(';')
      to_return << "#{total_rule}{#{properties}}"
    else
      properties = properties.map { |a| a.to_s(tabs + 1) }.select{|a| a && a.length > 0}.join("\n")
      end_props = (style == :expanded ? "\n" + old_spaces : ' ')
      to_return << "#{total_rule} {\n#{properties}#{end_props}}\n"
    end
  end

  tabs += 1 unless properties.empty? || style != :nested
  sub_rules.each do |sub|
    to_return << sub.to_s(tabs, resolved_rules)
  end

  to_return
end

#add_rules(node) ⇒ Object

Adds another Sass::Tree::RuleNode's rules to this one's.

Parameters:



63
64
65
# File 'lib/sass/tree/rule_node.rb', line 63

def add_rules(node)
  @rules += node.rules
end

#continued?Boolean

Returns Whether or not this rule is continued on the next line.

Returns:

  • (Boolean)

    Whether or not this rule is continued on the next line



68
69
70
# File 'lib/sass/tree/rule_node.rb', line 68

def continued?
  @rules.last[-1] == ?,
end

#perform!(environment) ⇒ Object (protected)

Runs any SassScript that may be embedded in the rule, and parses the selectors for commas.

Parameters:

  • environment (Sass::Environment)

    The lexical environment containing variable and mixin values



154
155
156
157
# File 'lib/sass/tree/rule_node.rb', line 154

def perform!(environment)
  @parsed_rules = @rules.map {|r| parse_selector(interpolate(r, environment))}
  super
end

#to_sass(tabs, opts = {}) ⇒ Object

See Also:



26
27
28
29
30
31
32
33
34
# File 'lib/sass/css.rb', line 26

def to_sass(tabs, opts = {})
  str = "\n#{'  ' * tabs}#{rules.first}#{children.any? { |c| c.is_a? PropNode } ? "\n" : ''}"

  children.each do |child|
    str << "#{child.to_sass(tabs + 1, opts)}"
  end

  str
end