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

Instance Method Summary collapse

Methods inherited from Node

#<<, #last, #perform

Constructor Details

#initialize(rule, options) ⇒ RuleNode

Returns a new instance of RuleNode.



8
9
10
11
# File 'lib/sass/tree/rule_node.rb', line 8

def initialize(rule, options)
  @rule = rule
  super(options)
end

Instance Attribute Details

#ruleObject

Returns the value of attribute rule.



6
7
8
# File 'lib/sass/tree/rule_node.rb', line 6

def rule
  @rule
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
# File 'lib/sass/tree/rule_node.rb', line 13

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

#add_rules(node) ⇒ Object



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

def add_rules(node)
  self.rule = rules
  self.rule += node.rules
end

#continued?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/sass/tree/rule_node.rb', line 26

def continued?
  rule[-1] == ?,
end

#rulesObject



17
18
19
# File 'lib/sass/tree/rule_node.rb', line 17

def rules
  Array(rule)
end

#to_s(tabs, super_rules = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'lib/sass/tree/rule_node.rb', line 30

def to_s(tabs, super_rules = nil)
  attributes = []
  sub_rules = []

  rule_split = /\s*,\s*/
  rule_separator = @style == :compressed ? ',' : ', '
  line_separator = [:nested, :expanded].include?(@style) ? ",\n" : rule_separator
  rule_indent = '  ' * (tabs - 1)
  total_rule = if super_rules
    super_rules.split(",\n").map do |super_line|
      super_line.strip.split(rule_split).map do |super_rule|
        self.rules.map do |line|
          rule_indent + line.gsub(/,$/, '').split(rule_split).map do |rule|
            if rule.include?(PARENT)
              rule.gsub(PARENT, super_rule)
            else
              "#{super_rule} #{rule}"
            end
          end.join(rule_separator)
        end.join(line_separator)
      end.join(rule_separator)
    end.join(line_separator)
  elsif self.rules.any? { |r| r.include?(PARENT) }
    raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'.", line)
  else
    per_rule_indent, total_indent = [:nested, :expanded].include?(@style) ? [rule_indent, ''] : ['', rule_indent]
    total_indent + self.rules.map do |r|
      per_rule_indent + r.gsub(/,$/, '').gsub(rule_split, rule_separator).rstrip
    end.join(line_separator)
  end

  children.each do |child|
    if child.is_a? RuleNode
      sub_rules << child
    else
      attributes << child
    end
  end

  to_return = ''
  if !attributes.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
      attributes = attributes.map { |a| a.to_s(1) }.join(' ')
      to_return << "#{total_rule} { #{attributes} }\n"
    elsif @style == :compressed
      attributes = attributes.map { |a| a.to_s(1) }.join(';')
      to_return << "#{total_rule}{#{attributes}}"
    else
      attributes = attributes.map { |a| a.to_s(tabs + 1) }.join("\n")
      end_attrs = (@style == :expanded ? "\n" + old_spaces : ' ')
      to_return << "#{total_rule} {\n#{attributes}#{end_attrs}}\n"
    end
  end

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

  to_return
end

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



21
22
23
24
25
26
27
28
29
# File 'lib/sass/css.rb', line 21

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

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

  str
end