Skip to content

Commit

Permalink
Disallow @import within mixins and control directives.
Browse files Browse the repository at this point in the history
It didn't really work how you'd expect anyway.
  • Loading branch information
nex3 committed Dec 17, 2010
1 parent 81613d0 commit 178c0a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
20 changes: 17 additions & 3 deletions lib/sass/tree/visitors/check_nesting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ class Sass::Tree::Visitors::CheckNesting < Sass::Tree::Visitors::Base
protected

def visit(node)
if error = @parent && (
try_send("invalid_#{node_name @parent}_child?", @parent, node) ||
try_send("invalid_#{node_name node}_parent?", @parent, node))
if error = (@parent && (
try_send("invalid_#{node_name @parent}_child?", @parent, node) ||
try_send("invalid_#{node_name node}_parent?", @parent, node))) ||
(@real_parent && (
try_send("invalid_#{node_name @real_parent}_real_child?", @real_parent, node) ||
try_send("invalid_#{node_name node}_real_parent?", @real_parent, node)))
raise Sass::SyntaxError.new(error)
end
super
Expand All @@ -19,9 +22,11 @@ def visit_children(parent)
@parent = parent unless is_any_of?(parent,
Sass::Tree::EachNode, Sass::Tree::ForNode, Sass::Tree::IfNode,
Sass::Tree::ImportNode, Sass::Tree::MixinNode, Sass::Tree::WhileNode)
old_real_parent, @real_parent = @real_parent, parent
super
ensure
@parent = old_parent
@real_parent = old_real_parent
end

def visit_root(node)
Expand Down Expand Up @@ -63,6 +68,10 @@ def invalid_function_child?(parent, child)
end

def invalid_import_parent?(parent, child)
if is_any_of?(@real_parent, Sass::Tree::IfNode, Sass::Tree::ForNode, Sass::Tree::WhileNode,
Sass::Tree::EachNode, Sass::Tree::MixinDefNode)
return "Import directives may not be used within control directives or mixins."
end
return if parent.is_a?(Sass::Tree::RootNode)
return "CSS import directives may only be used at the root of a document." if child.css_import?
# If this is a nested @import, we need to make sure it doesn't have anything
Expand All @@ -75,6 +84,10 @@ def invalid_import_parent?(parent, child)
raise e
end

def invalid_import_real_parent?(parent, child)

end

def invalid_mixindef_parent?(parent, child)
"Mixins may only be defined at the root of a document." unless parent.is_a?(Sass::Tree::RootNode)
end
Expand Down Expand Up @@ -108,3 +121,4 @@ def try_send(method, *args, &block)
send(method, *args, &block)
end
end

28 changes: 2 additions & 26 deletions test/sass/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class SassEngineTest < Test::Unit::TestCase
MSG
"@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
"foo\n @import foo.css" => "CSS import directives may only be used at the root of a document.",
"@if true\n @import foo" => "Import directives may not be used within control directives or mixins.",
"@mixin foo\n @import foo" => "Import directives may not be used within control directives or mixins.",
'$foo: "bar" "baz" !' => %Q{Invalid CSS after ""bar" "baz" ": expected expression (e.g. 1px, bold), was "!"},
"=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
"=foo\n :color red\n.bar\n +bang_bop" => "Undefined mixin 'bang_bop'.",
Expand Down Expand Up @@ -607,32 +609,6 @@ def test_import_in_rule
SASS
end

def test_import_in_mixin
assert_equal(<<CSS, render(<<SASS, :load_paths => [File.dirname(__FILE__) + '/templates/']))
.foo #foo {
background-color: #bbaaff; }
CSS
@mixin import
@import partial
.foo
@include import
SASS
end

def test_import_in_loop
assert_equal(<<CSS, render(<<SASS, :load_paths => [File.dirname(__FILE__) + '/templates/']))
#foo {
background-color: #bbaaff; }
#foo {
background-color: #bbaaff; }
CSS
@for $i from 1 through 2
@import partial
SASS
end

def test_nested_import_with_toplevel_constructs
Sass::Engine.new(".foo\n @import importee", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
rescue Sass::SyntaxError => err
Expand Down

0 comments on commit 178c0a7

Please sign in to comment.