6 module Helpers
#:nodoc:
7 # Provides methods to generate HTML tags programmatically when you can't use
8 # a Builder. By default, they output XHTML compliant tags.
12 BOOLEAN_ATTRIBUTES
= %w(disabled readonly multiple checked
).to_set
13 BOOLEAN_ATTRIBUTES
.merge(BOOLEAN_ATTRIBUTES
.map(&:to_sym))
15 # Returns an empty HTML tag of type +name+ which by default is XHTML
16 # compliant. Set +open+ to true to create an open tag compatible
17 # with HTML 4.0 and below. Add HTML attributes by passing an attributes
18 # hash to +options+. Set +escape+ to false to disable attribute value
22 # The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
23 # <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
24 # symbols or strings for the attribute names.
30 # tag("br", nil, true)
33 # tag("input", { :type => 'text', :disabled => true })
34 # # => <input type="text" disabled="disabled" />
36 # tag("img", { :src => "open & shut.png" })
37 # # => <img src="open & shut.png" />
39 # tag("img", { :src => "open & shut.png" }, false, false)
40 # # => <img src="open & shut.png" />
41 def tag(name
, options
= nil, open
= false, escape
= true)
42 "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}"
45 # Returns an HTML block tag of type +name+ surrounding the +content+. Add
46 # HTML attributes by passing an attributes hash to +options+.
47 # Instead of passing the content as an argument, you can also use a block
48 # in which case, you pass your +options+ as the second parameter.
49 # Set escape to false to disable attribute value escaping.
52 # The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
53 # <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
54 # symbols or strings for the attribute names.
57 # content_tag(:p, "Hello world!")
58 # # => <p>Hello world!</p>
59 # content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
60 # # => <div class="strong"><p>Hello world!</p></div>
61 # content_tag("select", options, :multiple => true)
62 # # => <select multiple="multiple">...options...</select>
64 # <% content_tag :div, :class => "strong" do -%>
67 # # => <div class="strong">Hello world!</div>
68 def content_tag(name
, content_or_options_with_block
= nil, options
= nil, escape
= true, &block
)
70 options
= content_or_options_with_block
if content_or_options_with_block
.is_a
?(Hash
)
71 content_tag
= content_tag_string(name
, capture(&block
), options
, escape
)
73 if block_called_from_erb
?(block
)
79 content_tag_string(name
, content_or_options_with_block
, options
, escape
)
83 # Returns a CDATA section with the given +content+. CDATA sections
84 # are used to escape blocks of text containing characters which would
85 # otherwise be recognized as markup. CDATA sections begin with the string
86 # <tt><![CDATA[</tt> and end with (and may not contain) the string <tt>]]></tt>.
89 # cdata_section("<hello world>")
90 # # => <![CDATA[<hello world>]]>
92 # cdata_section(File.read("hello_world.txt"))
93 # # => <![CDATA[<hello from a text file]]>
94 def cdata_section(content
)
95 "<![CDATA[#{content}]]>"
98 # Returns an escaped version of +html+ without affecting existing escaped entities.
101 # escape_once("1 > 2 & 3")
102 # # => "1 < 2 & 3"
104 # escape_once("<< Accept & Checkout")
105 # # => "<< Accept & Checkout"
106 def escape_once(html
)
107 html
.to_s
.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special
| ERB
::Util::HTML_ESCAPE[special
] }
111 BLOCK_CALLED_FROM_ERB
= 'defined? __in_erb_template'
113 if RUBY_VERSION < '1.9.0'
114 # Check whether we're called from an erb template.
115 # We'd return a string in any other case, but erb <%= ... %>
116 # can't take an <% end %> later on, so we have to use <% ... %>
117 # and implicitly concat.
118 def block_called_from_erb
?(block
)
119 block
&& eval(BLOCK_CALLED_FROM_ERB
, block
)
122 def block_called_from_erb
?(block
)
123 block
&& eval(BLOCK_CALLED_FROM_ERB
, block
.binding
)
127 def content_tag_string(name
, content
, options
, escape
= true)
128 tag_options
= tag_options(options
, escape
) if options
129 "<#{name}#{tag_options}>#{content}</#{name}>"
132 def tag_options(options
, escape
= true)
133 unless options
.blank
?
136 options
.each_pair
do |key
, value
|
137 if BOOLEAN_ATTRIBUTES
.include?(key
)
138 attrs
<< %(#{key}="#{key}") if value
140 attrs << %(#{key}="#{escape_once(value)}") if !value.nil?
144 attrs = options.map { |key, value| %(#{key}="#{value}") }
146 " #{attrs.sort * ' '}" unless attrs.empty?