2006-12-04 08:41:09 +00:00
|
|
|
require 'sass/tree/node'
|
|
|
|
require 'sass/tree/attr_node'
|
|
|
|
|
|
|
|
module Sass::Tree
|
|
|
|
class RuleNode < ValueNode
|
2007-03-17 04:17:01 +00:00
|
|
|
# The character used to include the parent selector
|
|
|
|
PARENT = '&'
|
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
alias_method :rule, :value
|
|
|
|
alias_method :rule=, :value=
|
2007-08-10 05:21:56 +00:00
|
|
|
|
|
|
|
def continued?
|
|
|
|
rule[-1] == ?,
|
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
|
2007-02-06 03:34:49 +00:00
|
|
|
def to_s(tabs, super_rules = nil)
|
2006-12-04 08:41:09 +00:00
|
|
|
attributes = []
|
|
|
|
sub_rules = []
|
2007-08-10 05:21:56 +00:00
|
|
|
|
|
|
|
# Save this because the comma's removed by the super_rule additions
|
|
|
|
was_continued = continued?
|
|
|
|
|
2007-01-17 16:32:15 +00:00
|
|
|
total_rule = if super_rules
|
2007-02-06 03:34:49 +00:00
|
|
|
super_rules.split(/,\s*/).collect! do |s|
|
2007-06-22 20:03:56 +00:00
|
|
|
self.rule.split(/,\s*/).collect do |r|
|
|
|
|
if r.include?(PARENT)
|
|
|
|
r.gsub(PARENT, s)
|
|
|
|
else
|
|
|
|
"#{s} #{r}"
|
|
|
|
end
|
|
|
|
end.join(", ")
|
2007-08-10 05:21:56 +00:00
|
|
|
end.join(", ") + (was_continued ? ',' : '')
|
2007-06-22 20:03:56 +00:00
|
|
|
elsif self.rule.include?(PARENT)
|
2007-03-17 04:17:01 +00:00
|
|
|
raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'", line)
|
2007-01-18 09:30:30 +00:00
|
|
|
else
|
|
|
|
self.rule
|
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
|
|
|
|
children.each do |child|
|
2007-03-17 23:34:26 +00:00
|
|
|
if child.is_a? RuleNode
|
2006-12-04 08:41:09 +00:00
|
|
|
sub_rules << child
|
2007-03-17 23:34:26 +00:00
|
|
|
else
|
|
|
|
attributes << child
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
to_return = ''
|
2007-08-10 05:21:56 +00:00
|
|
|
if !attributes.empty?
|
2007-08-12 11:00:41 +00:00
|
|
|
old_spaces = ' ' * (tabs - 1)
|
|
|
|
spaces = ' ' * tabs
|
2007-02-06 03:34:49 +00:00
|
|
|
if @style == :compact
|
2007-08-12 10:54:35 +00:00
|
|
|
attributes = attributes.map { |a| a.to_s(1) }.join(' ')
|
|
|
|
to_return << "#{old_spaces}#{total_rule} { #{attributes} }\n"
|
2007-02-06 03:34:49 +00:00
|
|
|
else
|
2007-08-12 10:54:35 +00:00
|
|
|
attributes = attributes.map { |a| a.to_s(tabs + 1) }.join("\n")
|
|
|
|
end_attrs = (@style == :expanded ? "\n" + old_spaces : ' ')
|
|
|
|
to_return << "#{old_spaces}#{total_rule} {\n#{attributes}#{end_attrs}}\n"
|
2007-02-06 01:58:23 +00:00
|
|
|
end
|
2007-08-10 05:21:56 +00:00
|
|
|
elsif continued?
|
2007-08-12 10:54:35 +00:00
|
|
|
to_return << (' ' * (tabs - 1)) + total_rule + (@style == :compact ? ' ' : "\n")
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
|
2007-08-12 10:54:35 +00:00
|
|
|
tabs += 1 unless attributes.empty? || @style != :nested
|
2007-08-10 05:21:56 +00:00
|
|
|
sub_rules.each do |sub|
|
|
|
|
if sub.continued?
|
|
|
|
check_multiline_rule(sub)
|
|
|
|
end
|
|
|
|
|
|
|
|
to_return << sub.to_s(tabs, total_rule)
|
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
to_return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|