diff --git a/lib/compass/sass_extensions/functions/font_files.rb b/lib/compass/sass_extensions/functions/font_files.rb index c18ce05a..f7f72d27 100644 --- a/lib/compass/sass_extensions/functions/font_files.rb +++ b/lib/compass/sass_extensions/functions/font_files.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 diff --git a/test/units/sass_extensions_test.rb b/test/units/sass_extensions_test.rb index 5ee96b7d..d965dcc9 100644 --- a/test/units/sass_extensions_test.rb +++ b/test/units/sass_extensions_test.rb @@ -101,9 +101,26 @@ class SassExtensionsTest < Test::Unit::TestCase 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|