开发者

Link Tags in HTML5 should not be self closing, but HAML is closing them

It's my understanding that tags in HTML5 should, by default, not be self closing (although it is permissible to use self closing tags). However I'm using HAML for my application, using the HTML5 doctype (!!! 5) and specifying in application.rb that Haml::Template.options[:format] = :html5

However, when I view my generated markup my link tags are still closed with a self-clos开发者_运维知识库ing tag />

Any idea what's going on here? Is this the default behavior of HAML with the HTML5 doctype, or am I configuring something incorrectly?


If the link tags are generated by Rails helpers such as stylesheet_link_tag, then they will have the closing slash regardless of the Haml format setting, as the string is simply returned from the rails method and included in the output as is, independently of Haml.

So for example this Haml:

!!!
%html
  %head
    = stylesheet_link_tag "foo"
    %link{:rel=>"stylesheet"}

produces the following output with the format set to html5 (which is the default for Rails 3):

<!DOCTYPE html> 
<html> 
  <head> 
    <link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link rel='stylesheet'>

By changing the format to xhtml with Haml::Template.options[:format] = :xhtml in environment.rb, the same Haml produces this instead:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
  <head> 
    <link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link rel='stylesheet' />

The <link> element controlled by Haml (i.e. %link in the Haml source) respects the format, and includes or omits the closing slash as appropriate. The link produced by the Rails helper simply includes the closing slash in both cases.

What !!! produces depends on what format is set, but from the Haml docs:

When the :format option is set to :html5, !!! is always <!DOCTYPE html>.

So you should get <!DOCTYPE html> whatever you put after !!!.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜