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

This commit is contained in:
Raving Genius 2011-09-08 23:12:11 -04:00
parent 8a2da92c91
commit 3514898ea1
2 changed files with 50 additions and 4 deletions

View File

@ -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

View File

@ -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|