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

[Sass] [SCSS] Support empty directives and rules.

This commit is contained in:
Nathan Weizenbaum 2010-02-02 16:05:52 -08:00
parent 70c2f5b817
commit 6f6ec9c4f4
4 changed files with 63 additions and 42 deletions

View file

@ -83,6 +83,7 @@ module Sass
node = node(Sass::Tree::DirectiveNode.new("@#{name} #{val}".strip))
if tok(/\{/)
node.has_children = true
block_contents(node, :directive)
tok!(/\}/)
end
@ -232,6 +233,7 @@ module Sass
end
def block(node, context)
node.has_children = true
tok!(/\{/)
block_contents(node, context)
tok!(/\}/)
@ -242,7 +244,7 @@ module Sass
def block_contents(node, context)
block_given? ? yield : ss_comments(node)
node << (child = block_child(context))
while tok(/;/) || (child && !child.children.empty?)
while tok(/;/) || (child && child.has_children)
block_given? ? yield : ss_comments(node)
node << (child = block_child(context))
end

View file

@ -27,44 +27,42 @@ module Sass::Tree
# @param tabs [Fixnum] The level of indentation for the CSS
# @return [String] The resulting CSS
def _to_s(tabs)
if children.empty?
value + ";"
else
result = if style == :compressed
"#{value}{"
else
"#{' ' * (tabs - 1)}#{value} {" + (style == :compact ? ' ' : "\n")
end
was_prop = false
first = true
children.each do |child|
next if child.invisible?
if style == :compact
if child.is_a?(PropNode)
result << "#{child.to_s(first || was_prop ? 1 : tabs + 1)} "
else
if was_prop
result[-1] = "\n"
end
rendered = child.to_s(tabs + 1).dup
rendered = rendered.lstrip if first
result << rendered.rstrip + "\n"
end
was_prop = child.is_a?(PropNode)
first = false
elsif style == :compressed
result << (was_prop ? ";#{child.to_s(1)}" : child.to_s(1))
was_prop = child.is_a?(PropNode)
return value + ";" unless has_children
return value + " {}" if children.empty?
result = if style == :compressed
"#{value}{"
else
"#{' ' * (tabs - 1)}#{value} {" + (style == :compact ? ' ' : "\n")
end
was_prop = false
first = true
children.each do |child|
next if child.invisible?
if style == :compact
if child.is_a?(PropNode)
result << "#{child.to_s(first || was_prop ? 1 : tabs + 1)} "
else
result << child.to_s(tabs + 1) + "\n"
if was_prop
result[-1] = "\n"
end
rendered = child.to_s(tabs + 1).dup
rendered = rendered.lstrip if first
result << rendered.rstrip + "\n"
end
was_prop = child.is_a?(PropNode)
first = false
elsif style == :compressed
result << (was_prop ? ";#{child.to_s(1)}" : child.to_s(1))
was_prop = child.is_a?(PropNode)
else
result << child.to_s(tabs + 1) + "\n"
end
result.rstrip + if style == :compressed
"}"
else
(style == :expanded ? "\n" : " ") + "}\n"
end
end
result.rstrip + if style == :compressed
"}"
else
(style == :expanded ? "\n" : " ") + "}\n"
end
end
end
end

View file

@ -33,6 +33,13 @@ module Sass
# @return [Array<Tree::Node>]
attr_accessor :children
# Whether or not this node has child nodes.
# This may be true even when \{#children} is empty,
# in which case this node has an empty block (e.g. `{}`).
#
# @return [Boolean]
attr_accessor :has_children
# The line of the document on which this node appeared.
#
# @return [Fixnum]
@ -62,6 +69,12 @@ module Sass
@options = options
end
# @private
def children=(children)
self.has_children ||= !children.empty?
@children = children
end
# The name of the document on which this node appeared.
#
# @return [String]

View file

@ -21,6 +21,14 @@ sel {
CSS
end
def test_empty_rule
assert_equal "", render("#foo .bar {}")
assert_equal "", render(<<SCSS)
#foo .bar {
}
SCSS
end
def test_cdo_and_cdc_ignored_at_toplevel
assert_equal <<CSS, render(<<SCSS)
foo {
@ -396,13 +404,13 @@ SCSS
SCSS
end
# TODO: Make this work.
# Currently we check whether a directive has children
# to determine whether to use {} or ;.
#
# def test_empty_block_directive
# assert_parses "@foo {}"
# end
def test_empty_block_directive
assert_parses "@foo {}"
assert_equal "@foo {}\n", render(<<SCSS)
@foo {
}
SCSS
end
def test_multiple_block_directives
assert_parses <<SCSS