Replaced 'closed' flag through simple block handling:
`tag(name, options) { some_content }` becomes `<name opt=...>content</name>` `tag((name, options)` becomes `<tag opt=... />` This will only produce XHTML!
This commit is contained in:
parent
121f43f702
commit
bd53f6ae34
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
|||||||
*.gem
|
*.gem
|
||||||
*~
|
*~
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
\#*
|
\#*
|
||||||
.\#*
|
.\#*
|
||||||
|
@ -13,15 +13,13 @@ module Sinatra
|
|||||||
# The default value of +closed+ option is +false+.
|
# The default value of +closed+ option is +false+.
|
||||||
#
|
#
|
||||||
def image_tag(source, options = {})
|
def image_tag(source, options = {})
|
||||||
closed = options.delete(:closed)
|
|
||||||
options[:src] = url_for(source)
|
options[:src] = url_for(source)
|
||||||
tag("img", options, closed)
|
tag("img", options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def stylesheet_link_tag(*sources)
|
def stylesheet_link_tag(*sources)
|
||||||
list, options = extract_options(sources)
|
list, options = extract_options(sources)
|
||||||
closed = options.delete(:closed)
|
list.collect { |source| stylesheet_tag(source, options) }.join("\n")
|
||||||
list.collect { |source| stylesheet_tag(source, options, closed) }.join("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def javascript_script_tag(*sources)
|
def javascript_script_tag(*sources)
|
||||||
@ -29,33 +27,42 @@ module Sinatra
|
|||||||
list.collect { |source| javascript_tag(source, options) }.join("\n")
|
list.collect { |source| javascript_tag(source, options) }.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_to(desc, url)
|
def link_to(desc, url, options = {})
|
||||||
"<a href='#{url_for(url)}'>#{desc}</a>"
|
tag("a", options.merge(:href => url_for(url))) do
|
||||||
|
desc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def tag(name, options = {}, closed = false)
|
def tag(name, options = {})
|
||||||
"<#{name}#{tag_options(options) if options}#{closed ? " />" : ">"}"
|
start_tag = "<#{name}#{tag_options(options) if options}"
|
||||||
|
if block_given?
|
||||||
|
content = yield
|
||||||
|
"#{start_tag}>#{content}</#{name}>"
|
||||||
|
else
|
||||||
|
"#{start_tag}/>"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_options(options)
|
def tag_options(options)
|
||||||
unless options.empty?
|
unless options.empty?
|
||||||
attrs = []
|
attrs = []
|
||||||
attrs = options.map { |key, value| %(#{key}="#{value}") }
|
attrs = options.map { |key, value| %(#{key}="#{Rack::Utils.escape_html(value)}") }
|
||||||
" #{attrs.sort * ' '}" unless attrs.empty?
|
" #{attrs.sort * ' '}" unless attrs.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def stylesheet_tag(source, options, closed = false)
|
def stylesheet_tag(source, options = {})
|
||||||
tag("link", { :type => "text/css",
|
tag("link", { :type => "text/css",
|
||||||
:charset => "utf-8", :media => "screen", :rel => "stylesheet",
|
:charset => "utf-8", :media => "screen", :rel => "stylesheet",
|
||||||
:href => url_for(source) }.merge(options), closed)
|
:href => url_for(source) }.merge(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
def javascript_tag(source, options)
|
def javascript_tag(source, options = {})
|
||||||
tag("script", { :type => "text/javascript", :charset => "utf-8",
|
tag("script", { :type => "text/javascript", :charset => "utf-8",
|
||||||
:src => url_for(source) }.merge(options), false) + "</script>"
|
:src => url_for(source) }.merge(options)) do
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_options(a)
|
def extract_options(a)
|
||||||
|
@ -25,7 +25,7 @@ EOD
|
|||||||
get '/image_tag', {}, 'SCRIPT_NAME' => '/bar'
|
get '/image_tag', {}, 'SCRIPT_NAME' => '/bar'
|
||||||
assert last_response.ok?
|
assert last_response.ok?
|
||||||
assert_equal last_response.body, <<EOD
|
assert_equal last_response.body, <<EOD
|
||||||
<img alt="[foo image]" src="/bar/images/foo.jpg">
|
<img alt="[foo image]" src="/bar/images/foo.jpg"/>
|
||||||
EOD
|
EOD
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -33,8 +33,8 @@ EOD
|
|||||||
get '/stylesheet_link_tag', {}, 'SCRIPT_NAME' => '/bar'
|
get '/stylesheet_link_tag', {}, 'SCRIPT_NAME' => '/bar'
|
||||||
assert last_response.ok?
|
assert last_response.ok?
|
||||||
assert_equal last_response.body, <<EOD
|
assert_equal last_response.body, <<EOD
|
||||||
<link charset="utf-8" href="/bar/stylesheets/winter.css" media="projection" rel="stylesheet" type="text/css">
|
<link charset="utf-8" href="/bar/stylesheets/winter.css" media="projection" rel="stylesheet" type="text/css"/>
|
||||||
<link charset="utf-8" href="/bar/stylesheets/summer.css" media="projection" rel="stylesheet" type="text/css">
|
<link charset="utf-8" href="/bar/stylesheets/summer.css" media="projection" rel="stylesheet" type="text/css"/>
|
||||||
EOD
|
EOD
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ EOD
|
|||||||
get '/link_to_tag', {}, 'SCRIPT_NAME' => '/bar'
|
get '/link_to_tag', {}, 'SCRIPT_NAME' => '/bar'
|
||||||
assert last_response.ok?
|
assert last_response.ok?
|
||||||
assert_equal last_response.body, <<EOD
|
assert_equal last_response.body, <<EOD
|
||||||
<a href='/bar/topr'>Tatry Mountains Rescue Team</a>
|
<a href="/bar/topr">Tatry Mountains Rescue Team</a>
|
||||||
EOD
|
EOD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user