From bd53f6ae340f6f1a6791f184a0358b2f7d93168b Mon Sep 17 00:00:00 2001 From: Lennart Melzer Date: Sun, 22 Nov 2009 10:09:32 +0100 Subject: [PATCH] Replaced 'closed' flag through simple block handling: `tag(name, options) { some_content }` becomes `content` `tag((name, options)` becomes `` This will only produce XHTML! --- .gitignore | 2 ++ lib/sinatra/static_assets.rb | 33 ++++++++++++++++++------------ test/sinatra_static_assets_test.rb | 8 ++++---- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index e5d0274..05b74f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.gem *~ +*.swp +*.swo \#* .\#* diff --git a/lib/sinatra/static_assets.rb b/lib/sinatra/static_assets.rb index ee31f40..165703f 100644 --- a/lib/sinatra/static_assets.rb +++ b/lib/sinatra/static_assets.rb @@ -13,15 +13,13 @@ module Sinatra # The default value of +closed+ option is +false+. # def image_tag(source, options = {}) - closed = options.delete(:closed) options[:src] = url_for(source) - tag("img", options, closed) + tag("img", options) end def stylesheet_link_tag(*sources) list, options = extract_options(sources) - closed = options.delete(:closed) - list.collect { |source| stylesheet_tag(source, options, closed) }.join("\n") + list.collect { |source| stylesheet_tag(source, options) }.join("\n") end def javascript_script_tag(*sources) @@ -29,33 +27,42 @@ module Sinatra list.collect { |source| javascript_tag(source, options) }.join("\n") end - def link_to(desc, url) - "#{desc}" + def link_to(desc, url, options = {}) + tag("a", options.merge(:href => url_for(url))) do + desc + end end private - def tag(name, options = {}, closed = false) - "<#{name}#{tag_options(options) if options}#{closed ? " />" : ">"}" + def tag(name, options = {}) + start_tag = "<#{name}#{tag_options(options) if options}" + if block_given? + content = yield + "#{start_tag}>#{content}" + else + "#{start_tag}/>" + end end def tag_options(options) unless options.empty? attrs = [] - attrs = options.map { |key, value| %(#{key}="#{value}") } + attrs = options.map { |key, value| %(#{key}="#{Rack::Utils.escape_html(value)}") } " #{attrs.sort * ' '}" unless attrs.empty? end end - def stylesheet_tag(source, options, closed = false) + def stylesheet_tag(source, options = {}) tag("link", { :type => "text/css", :charset => "utf-8", :media => "screen", :rel => "stylesheet", - :href => url_for(source) }.merge(options), closed) + :href => url_for(source) }.merge(options)) end - def javascript_tag(source, options) + def javascript_tag(source, options = {}) tag("script", { :type => "text/javascript", :charset => "utf-8", - :src => url_for(source) }.merge(options), false) + "" + :src => url_for(source) }.merge(options)) do + end end def extract_options(a) diff --git a/test/sinatra_static_assets_test.rb b/test/sinatra_static_assets_test.rb index 9e10c23..53cbaf3 100644 --- a/test/sinatra_static_assets_test.rb +++ b/test/sinatra_static_assets_test.rb @@ -25,7 +25,7 @@ EOD get '/image_tag', {}, 'SCRIPT_NAME' => '/bar' assert last_response.ok? assert_equal last_response.body, < +[foo image] EOD end @@ -33,8 +33,8 @@ EOD get '/stylesheet_link_tag', {}, 'SCRIPT_NAME' => '/bar' assert last_response.ok? assert_equal last_response.body, < - + + EOD end @@ -50,7 +50,7 @@ EOD get '/link_to_tag', {}, 'SCRIPT_NAME' => '/bar' assert last_response.ok? assert_equal last_response.body, <Tatry Mountains Rescue Team +Tatry Mountains Rescue Team EOD end