29: def to_s(tabs, super_rules = nil)
30: attributes = []
31: sub_rules = []
32:
33: rule_split = /\s*,\s*/
34: rule_separator = @style == :compressed ? ',' : ', '
35: line_separator = [:nested, :expanded].include?(@style) ? ",\n" : rule_separator
36: rule_indent = ' ' * (tabs - 1)
37: total_rule = if super_rules
38: super_rules.split(",\n").map do |super_line|
39: super_line.strip.split(rule_split).map do |super_rule|
40: self.rules.map do |line|
41: rule_indent + line.gsub(/,$/, '').split(rule_split).map do |rule|
42: if rule.include?(PARENT)
43: rule.gsub(PARENT, super_rule)
44: else
45: "#{super_rule} #{rule}"
46: end
47: end.join(rule_separator)
48: end.join(line_separator)
49: end.join(rule_separator)
50: end.join(line_separator)
51: elsif self.rules.any? { |r| r.include?(PARENT) }
52: raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'.", line)
53: else
54: per_rule_indent, total_indent = [:nested, :expanded].include?(@style) ? [rule_indent, ''] : ['', rule_indent]
55: total_indent + self.rules.map do |r|
56: per_rule_indent + r.gsub(/,$/, '').gsub(rule_split, rule_separator).rstrip
57: end.join(line_separator)
58: end
59:
60: children.each do |child|
61: if child.is_a? RuleNode
62: sub_rules << child
63: else
64: attributes << child
65: end
66: end
67:
68: to_return = ''
69: if !attributes.empty?
70: old_spaces = ' ' * (tabs - 1)
71: spaces = ' ' * tabs
72: if @style == :compact
73: attributes = attributes.map { |a| a.to_s(1) }.join(' ')
74: to_return << "#{total_rule} { #{attributes} }\n"
75: elsif @style == :compressed
76: attributes = attributes.map { |a| a.to_s(1) }.join(';')
77: to_return << "#{total_rule}{#{attributes}}"
78: else
79: attributes = attributes.map { |a| a.to_s(tabs + 1) }.join("\n")
80: end_attrs = (@style == :expanded ? "\n" + old_spaces : ' ')
81: to_return << "#{total_rule} {\n#{attributes}#{end_attrs}}\n"
82: end
83: end
84:
85: tabs += 1 unless attributes.empty? || @style != :nested
86: sub_rules.each do |sub|
87: to_return << sub.to_s(tabs, total_rule)
88: end
89:
90: to_return
91: end