From 8a2da92c9119bd79250f2e1dcaa88bbd7d6d8388 Mon Sep 17 00:00:00 2001 From: Raving Genius Date: Thu, 8 Sep 2011 20:59:03 -0400 Subject: [PATCH 1/2] Added test coverage for font_files (issue #543) --- test/units/sass_extensions_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/units/sass_extensions_test.rb b/test/units/sass_extensions_test.rb index 372d54e6..5ee96b7d 100644 --- a/test/units/sass_extensions_test.rb +++ b/test/units/sass_extensions_test.rb @@ -98,6 +98,14 @@ class SassExtensionsTest < Test::Unit::TestCase assert_equal "true", evaluate("prefixed(-css2, css2-fallback(css3, css2))") end + 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 + evaluate("font-files('/font/name.woff')") + end + end + %w(stylesheet_url font_url image_url generated_image_url).each do |helper| class_eval %Q{ def test_#{helper}_helper_defers_to_existing_helper From 3514898ea1f69cc7260a7513801ad7c483fb30f0 Mon Sep 17 00:00:00 2001 From: Raving Genius Date: Thu, 8 Sep 2011 23:12:11 -0400 Subject: [PATCH 2/2] Stop requiring font type when type can be guessed from URL (issue #544) --- .../sass_extensions/functions/font_files.rb | 35 +++++++++++++++++-- test/units/sass_extensions_test.rb | 19 +++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) 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|