Permalink
Browse files

Stop requiring font type when type can be guessed from URL (issue #544)

  • Loading branch information...
Raving Genius
Raving Genius committed Sep 9, 2011
1 parent 8a2da92 commit 3514898ea1f69cc7260a7513801ad7c483fb30f0
Showing with 50 additions and 4 deletions.
  1. +32 −3 lib/compass/sass_extensions/functions/font_files.rb
  2. +18 −1 test/units/sass_extensions_test.rb
@@ -1,10 +1,39 @@
module Compass::SassExtensions::Functions::FontFiles
+ FONT_TYPES = {
+ :woff => 'woff',
+ :otf => 'opentype',
+ :opentype => 'opentype',
+ :ttf => 'truetype',
+ :truetype => 'truetype',
+ :svg => 'svg'
+ }
+
def font_files(*args)
- raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0
files = []
- while args.size > 0
- files << "#{font_url(args.shift)} format('#{args.shift}')"
+ args_length = args.length
+ skip_next = false
+
+ args.each_with_index do |arg, index|
+ if skip_next
+ skip_next = false
+ next
+ end
+
+ type = (args_length > (index + 1)) ? args[index + 1].value.to_sym : :wrong
+
+ if FONT_TYPES.key? type
+ skip_next = true
+ else
+ type = arg.to_s.split('.').last.gsub('"', '').to_sym
+ end
+
+ if FONT_TYPES.key? type
+ files << "#{font_url(arg)} format('#{FONT_TYPES[type]}')"
+ else
+ raise Sass::SyntaxError, "Could not determine font type for #{arg}"
+ end
end
+
Sass::Script::String.new(files.join(", "))
end
end
@@ -101,9 +101,26 @@ def test_css2_fallback
def test_font_files
assert_equal '', evaluate('font_files()')
assert_equal "url(/font/name.woff) format('woff'), url(/fonts/name.ttf) format('truetype'), url(/fonts/name.svg#fontpath) format('svg')", evaluate("font-files('/font/name.woff', woff, '/fonts/name.ttf', truetype, '/fonts/name.svg#fontpath', svg)")
- assert_raises Sass::SyntaxError do
+
+ assert_equal "url(/font/with/right_ext.woff) format('woff')", evaluate("font_files('/font/with/right_ext.woff')")
+ assert_equal "url(/font/with/wrong_ext.woff) format('svg')", evaluate("font_files('/font/with/wrong_ext.woff', 'svg')")
+ assert_equal "url(/font/with/no_ext) format('opentype')", evaluate("font_files('/font/with/no_ext', 'otf')")
+ assert_equal "url(/font/with/weird.ext) format('truetype')", evaluate("font_files('/font/with/weird.ext', 'ttf')")
+
+ assert_equal "url(/font/with/right_ext.woff) format('woff'), url(/font/with/right_ext_also.otf) format('opentype')", evaluate("font_files('/font/with/right_ext.woff', '/font/with/right_ext_also.otf')")
+ assert_equal "url(/font/with/wrong_ext.woff) format('truetype'), url(/font/with/right_ext.otf) format('opentype')", evaluate("font_files('/font/with/wrong_ext.woff', 'ttf', '/font/with/right_ext.otf')")
+
+ assert_nothing_raised Sass::SyntaxError do
evaluate("font-files('/font/name.woff')")
end
+
+ assert_raises Sass::SyntaxError do
+ evaluate("font-files('/font/name.ext')")
+ end
+
+ assert_raises Sass::SyntaxError do
+ evaluate("font-files('/font/name.ext', 'nonsense')")
+ end
end
%w(stylesheet_url font_url image_url generated_image_url).each do |helper|

0 comments on commit 3514898

Please sign in to comment.