Permalink
Browse files

Don't self-close img, or link when using Rails helpers.

  • Loading branch information...
1 parent c305af4 commit 5820bcc4f230371e21dc250a237aa4d93a115362 @danielmorrison danielmorrison committed Apr 4, 2009
Showing with 36 additions and 4 deletions.
  1. +5 −1 README
  2. +2 −2 init.rb
  3. +29 −1 lib/html5.rb
View
6 README
@@ -1,13 +1,17 @@
Html5 on Rails
=====
+by Daniel Morrison, Collective Idea
+
This plugin is a bit of a playground to see what html goodies we can do. I'm implementing things as I desire them, and not in any normal order.
Current Features
=======
Since the type attributes for CSS and javascript tags have sensible defaults, we can omit them unless we need to override them.
<script src="/javascripts/all.js"></script>
-<link href="/stylesheets/boilerplate.css?1238073247" media="all" rel="stylesheet" />
+<link href="/stylesheets/main.css" media="all" rel="stylesheet">
+
+Don't self-close tags. This allows us to have <img src="foo.png"> instead of <img src="foo.png" />. Of course you'll need that if you're serving your pages as application/xhtml+xml, but if you know what that means, you'll probably know how to override it.
Copyright (c) 2009 Collective Idea, released under the MIT license
View
@@ -1,2 +1,2 @@
-# Include hook code here
-require 'html5'
+::SKIP_SELF_CLOSE_TAGS = true
+require 'html5'
View
@@ -14,6 +14,34 @@ def javascript_src_tag_with_empty_type(source, options)
# type attribute defaults to text/css so it can be omitted.
# http://www.whatwg.org/specs/web-apps/current-work/#script
def stylesheet_tag(source, options)
- tag("link", { "rel" => "stylesheet", "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
+ tag("link", { "rel" => "stylesheet", "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), ::SKIP_SELF_CLOSE_TAGS, false)
+ end
+
+ def image_tag(source, options = {})
+ options.symbolize_keys!
+
+ options[:src] = path_to_image(source)
+ options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
+
+ if size = options.delete(:size)
+ options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
+ end
+
+ if mouseover = options.delete(:mouseover)
+ options[:onmouseover] = "this.src='#{image_path(mouseover)}'"
+ options[:onmouseout] = "this.src='#{image_path(options[:src])}'"
+ end
+
+ tag("img", options, ::SKIP_SELF_CLOSE_TAGS)
+ end
+
+ def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
+ tag(
+ "link",
+ {"rel" => tag_options[:rel] || "alternate",
+ "type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s,
+ "title" => tag_options[:title] || type.to_s.upcase,
+ "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options}, ::SKIP_SELF_CLOSE_TAGS
+ )
end
end

0 comments on commit 5820bcc

Please sign in to comment.