Merge pull request #132 from petrblaho/js_css_url

javascript_url and stylesheet_url liquid tag
This commit is contained in:
Didier Lafforgue 2011-07-19 09:05:44 -07:00
commit 294da07565
2 changed files with 64 additions and 7 deletions

View File

@ -3,9 +3,9 @@ module Locomotive
module Filters
module Html
# Write the link to a stylesheet resource
# input: url of the css file
def stylesheet_tag(input, media = 'screen')
# Write the url to a stylesheet resource
# input: name of the css file
def stylesheet_url(input)
return '' if input.nil?
unless input =~ /^(\/|http:)/
@ -14,12 +14,22 @@ module Locomotive
input = "#{input}.css" unless input.ends_with?('.css')
input
end
# Write the link to a stylesheet resource
# input: url of the css file
def stylesheet_tag(input, media = 'screen')
return '' if input.nil?
input = stylesheet_url(input)
%{<link href="#{input}" media="#{media}" rel="stylesheet" type="text/css" />}
end
# Write the link to javascript resource
# input: url of the javascript file
def javascript_tag(input)
# Write the url to javascript resource
# input: name of the javascript file
def javascript_url(input)
return '' if input.nil?
unless input =~ /^(\/|http:)/
@ -28,7 +38,17 @@ module Locomotive
input = "#{input}.js" unless input.ends_with?('.js')
%{<script src="#{input}" type="text/javascript"></script>}
input
end
# Write the link to javascript resource
# input: url of the javascript file
def javascript_tag(input)
return '' if input.nil?
input = javascript_url(input)
%{<script src="#{input}" type="text/javascript"></script>}
end
def theme_image_url(input)

View File

@ -8,6 +8,24 @@ describe Locomotive::Liquid::Filters::Html do
@context = build_context
end
it 'should return a url for a stylesheet file' do
result = "/sites/000000000000000000000042/theme/stylesheets/main.css"
stylesheet_url('main.css').should == result
stylesheet_url('main').should == result
stylesheet_url(nil).should == ''
end
it 'should return a url for a stylesheet file with folder' do
result = "/sites/000000000000000000000042/theme/stylesheets/trash/main.css"
stylesheet_url('trash/main.css').should == result
end
it 'should return a url for a stylesheet file without touching the url' do
result = "/trash/main.css"
stylesheet_url('/trash/main.css').should == result
stylesheet_url('/trash/main').should == result
end
it 'should return a link tag for a stylesheet file' do
result = "<link href=\"/sites/000000000000000000000042/theme/stylesheets/main.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
stylesheet_tag('main.css').should == result
@ -44,6 +62,25 @@ describe Locomotive::Liquid::Filters::Html do
stylesheet_tag('/trash/main','print').should == result
end
it 'should return a url for a javascript file' do
result = "/sites/000000000000000000000042/theme/javascripts/main.js"
javascript_url('main.js').should == result
javascript_url('main').should == result
javascript_url(nil).should == ''
end
it 'should return a url for a javascript file with folder' do
result = "/sites/000000000000000000000042/theme/javascripts/trash/main.js"
javascript_url('trash/main.js').should == result
javascript_url('trash/main').should == result
end
it 'should return a url for a javascript file without touching the url' do
result = "/trash/main.js"
javascript_url('/trash/main.js').should == result
javascript_url('/trash/main').should == result
end
it 'should return a script tag for a javascript file' do
result = %{<script src="/sites/000000000000000000000042/theme/javascripts/main.js" type="text/javascript"></script>}
javascript_tag('main.js').should == result