Permalink
Browse files

[Sass] Properly parse @imports containing commas in strings and URLs.

  • Loading branch information...
1 parent 973d702 commit cd15061d182d148bb5f7f22510ad1a778ae766f5 @nex3 nex3 committed Jul 28, 2010
Showing with 44 additions and 38 deletions.
  1. +2 −0 doc-src/SASS_CHANGELOG.md
  2. +28 −15 lib/sass/engine.rb
  3. +14 −0 test/sass/engine_test.rb
  4. +0 −23 test/sass/templates/basic.sass
@@ -7,6 +7,8 @@
* Fix a bug where `sass --watch` and `sass --update` were completely broken.
+* Allow `@import`ed values to contain commas.
+
## 3.0.14
[Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.14).
View
@@ -592,25 +592,38 @@ def parse_import(line, value)
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.",
:line => @line + 1) unless line.children.empty?
- if (match = value.match(Sass::SCSS::RX::STRING) || value.match(Sass::SCSS::RX::URI)) &&
- match.offset(0).first == 0 && !match.post_match.strip.empty? &&
- match.post_match.strip[0] != ?,
- # @import "filename" media-type
- return Tree::DirectiveNode.new("@import #{value}")
- end
+ scanner = StringScanner.new(value)
+ values = []
- value.split(/,\s*/).map do |f|
- if f =~ Sass::SCSS::RX::URI
- # All url()s are literal CSS @imports
- next Tree::DirectiveNode.new("@import #{f}")
- elsif f =~ Sass::SCSS::RX::STRING
- f = $1 || $2
+ loop do
+ unless node = parse_import_arg(scanner)
+ raise SyntaxError.new("Invalid @import: expected file to import, was #{scanner.rest.inspect}",
+ :line => @line)
end
+ values << node
+ break unless scanner.scan(/,\s*/)
+ end
+
+ return values
+ end
- # http:// URLs are always literal CSS imports
- next Tree::DirectiveNode.new("@import url(#{f})") if f =~ /^http:\/\//
+ def parse_import_arg(scanner)
+ return if scanner.eos?
+ unless (str = scanner.scan(Sass::SCSS::RX::STRING)) ||
+ (uri = scanner.scan(Sass::SCSS::RX::URI))
+ return Tree::ImportNode.new(scanner.scan(/[^,]+/))
+ end
- Tree::ImportNode.new(f)
+ val = scanner[1] || scanner[2]
+ scanner.scan(/\s*/)
+ if media = scanner.scan(/[^,].*/)
+ Tree::DirectiveNode.new("@import #{str || uri} #{media}")
+ elsif uri
+ Tree::DirectiveNode.new("@import #{uri}")
+ elsif val =~ /^http:\/\//
+ Tree::DirectiveNode.new("@import url(#{val})")
+ else
+ Tree::ImportNode.new(val)
end
end
View
@@ -493,6 +493,12 @@ def test_css_import
def test_media_import
assert_equal("@import \"./fonts.sass\" all;\n",
render("@import \"./fonts.sass\" all"))
+ assert_equal(<<CSS, render(<<SASS))
+@import "./fonts.sass" all;
+@import url(./fonts.scss);
+CSS
+@import "./fonts.sass" all, url(./fonts.scss)
+SASS
end
def test_http_import
@@ -2044,6 +2050,14 @@ def test_mixin_no_arg_error
end
end
+ def test_import_with_commas_in_url
+ assert_equal <<CSS, render(<<SASS)
+@import url(foo.css?bar,baz);
+CSS
+@import url(foo.css?bar,baz)
+SASS
+ end
+
# Encodings
unless Haml::Util.ruby1_8?
@@ -1,23 +0,0 @@
-
-
-body
- :font Arial
- :background blue
-
-#page
- :width 700px
- :height 100
- #header
- :height 300px
- h1
- :font-size 50px
- :color blue
-
-#content.user.show
- #container.top
- #column.left
- :width 100px
- #column.right
- :width 600px
- #container.bottom
- :background brown

0 comments on commit cd15061

Please sign in to comment.