1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Added multiline selector support to Sass. Thanks to Max Muermann for pointing this out.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@579 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-08-10 05:21:56 +00:00
parent b9880f13d3
commit b83cbfa3b3
4 changed files with 55 additions and 9 deletions

View file

@ -75,6 +75,15 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
# <attribute>
# ...
#
# Like CSS, you can stretch rules over multiple lines.
# However, unlike CSS, you can only do this if each line but the last
# ends with a comma.
# For example:
#
# .users #userTab,
# .posts #postsTab
# <attributes>
#
# === Attributes
#
# There are two different ways to write CSS attrbibutes.

View file

@ -22,17 +22,28 @@ module Sass
children.each do |child|
if child.is_a? AttrNode
raise SyntaxError.new('Attributes aren\'t allowed at the root of a document.', child.line)
end
begin
result += "#{child.to_s(1)}\n"
rescue SyntaxError => e
raise e
elsif child.is_a?(RuleNode) && child.continued?
check_multiline_rule(child)
result << child.to_s(1)
else
begin
result << "#{child.to_s(1)}\n"
rescue SyntaxError => e
raise e
end
end
end
result[0...-1]
end
protected
def check_multiline_rule(rule)
unless rule.children.empty?
raise SyntaxError.new('Rules can\'t end in commas.', rule.line)
end
end
private
# This method should be overridden by subclasses to return an error message

View file

@ -8,10 +8,18 @@ module Sass::Tree
alias_method :rule, :value
alias_method :rule=, :value=
def continued?
rule[-1] == ?,
end
def to_s(tabs, super_rules = nil)
attributes = []
sub_rules = []
# Save this because the comma's removed by the super_rule additions
was_continued = continued?
total_rule = if super_rules
super_rules.split(/,\s*/).collect! do |s|
self.rule.split(/,\s*/).collect do |r|
@ -21,7 +29,7 @@ module Sass::Tree
"#{s} #{r}"
end
end.join(", ")
end.join(", ")
end.join(", ") + (was_continued ? ',' : '')
elsif self.rule.include?(PARENT)
raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'", line)
else
@ -37,7 +45,7 @@ module Sass::Tree
end
to_return = ''
unless attributes.empty?
if !attributes.empty?
if @style == :compact
to_return << "#{total_rule} { #{attributes.join(' ')} }\n"
else
@ -49,10 +57,18 @@ module Sass::Tree
end_attrs = (@style == :expanded ? "\n" : ' ')
to_return << "#{old_spaces}#{total_rule} {\n#{spaces}#{attributes}#{end_attrs}}\n"
end
elsif continued?
to_return << total_rule + (@style == :compact ? ' ' : "\n")
end
tabs += 1 unless attributes.empty?
sub_rules.each { |sub| to_return << sub.to_s(tabs, total_rule) }
sub_rules.each do |sub|
if sub.continued?
check_multiline_rule(sub)
end
to_return << sub.to_s(tabs, total_rule)
end
to_return
end
end

View file

@ -39,6 +39,7 @@ class SassEngineTest < Test::Unit::TestCase
"!a = 1b + 2c" => "Incompatible units: b and c",
"& a\n :b c" => "Base-level rules cannot contain the parent-selector-referencing character '&'",
"a\n :b\n c" => "Illegal nesting: Only attributes may be nested beneath attributes.",
"a,\n :b c" => "Rules can\'t end in commas.",
"!a = b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath constants.",
"@import foo.sass" => "File to import not found or unreadable: foo.sass",
"@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
@ -99,6 +100,15 @@ class SassEngineTest < Test::Unit::TestCase
assert_equal("foo {\n bar: url(foo.png); }\n",
render("foo\n bar = url(foo.png)\n"));
end
def test_multiline_selector
assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n",
render("#foo #bar,\n#baz #boom\n :foo bar"))
assert_equal("#foo #bar,\n#foo #baz {\n foo: bar; }\n",
render("#foo\n #bar,\n #baz\n :foo bar"))
assert_equal("#foo #bar, #baz #boom { foo: bar; }\n",
render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compact))
end
private