1
require 'action_view/erb/util'
5
module Helpers #:nodoc:
6
# Provides methods to generate HTML tags programmatically when you can't use
7
# a Builder. By default, they output XHTML compliant tags.
11
BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked).to_set
12
BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym))
14
# Returns an empty HTML tag of type +name+ which by default is XHTML
15
# compliant. Set +open+ to true to create an open tag compatible
16
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
17
# hash to +options+. Set +escape+ to false to disable attribute value
21
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
22
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
23
# symbols or strings for the attribute names.
29
# tag("br", nil, true)
32
# tag("input", { :type => 'text', :disabled => true })
33
# # => <input type="text" disabled="disabled" />
35
# tag("img", { :src => "open & shut.png" })
36
# # => <img src="open & shut.png" />
38
# tag("img", { :src => "open & shut.png" }, false, false)
39
# # => <img src="open & shut.png" />
40
def tag(name, options = nil, open = false, escape = true)
41
"<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe!
44
# Returns an HTML block tag of type +name+ surrounding the +content+. Add
45
# HTML attributes by passing an attributes hash to +options+.
46
# Instead of passing the content as an argument, you can also use a block
47
# in which case, you pass your +options+ as the second parameter.
48
# Set escape to false to disable attribute value escaping.
51
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
52
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
53
# symbols or strings for the attribute names.
56
# content_tag(:p, "Hello world!")
57
# # => <p>Hello world!</p>
58
# content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
59
# # => <div class="strong"><p>Hello world!</p></div>
60
# content_tag("select", options, :multiple => true)
61
# # => <select multiple="multiple">...options...</select>
63
# <% content_tag :div, :class => "strong" do -%>
66
# # => <div class="strong">Hello world!</div>
67
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
69
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
70
content_tag = content_tag_string(name, capture(&block), options, escape)
72
if block_called_from_erb?(block)
78
content_tag_string(name, content_or_options_with_block, options, escape)
82
# Returns a CDATA section with the given +content+. CDATA sections
83
# are used to escape blocks of text containing characters which would
84
# otherwise be recognized as markup. CDATA sections begin with the string
85
# <tt><![CDATA[</tt> and end with (and may not contain) the string <tt>]]></tt>.
88
# cdata_section("<hello world>")
89
# # => <![CDATA[<hello world>]]>
91
# cdata_section(File.read("hello_world.txt"))
92
# # => <![CDATA[<hello from a text file]]>
93
def cdata_section(content)
94
"<![CDATA[#{content}]]>".html_safe!
97
# Returns an escaped version of +html+ without affecting existing escaped entities.
100
# escape_once("1 < 2 & 3")
101
# # => "1 < 2 & 3"
103
# escape_once("<< Accept & Checkout")
104
# # => "<< Accept & Checkout"
105
def escape_once(html)
106
ActiveSupport::Multibyte.clean(html.to_s).gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
110
BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
112
if RUBY_VERSION < '1.9.0'
113
# Check whether we're called from an erb template.
114
# We'd return a string in any other case, but erb <%= ... %>
115
# can't take an <% end %> later on, so we have to use <% ... %>
116
# and implicitly concat.
117
def block_called_from_erb?(block)
118
block && eval(BLOCK_CALLED_FROM_ERB, block)
121
def block_called_from_erb?(block)
122
block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
126
def content_tag_string(name, content, options, escape = true)
127
tag_options = tag_options(options, escape) if options
128
"<#{name}#{tag_options}>#{content}</#{name}>".html_safe!
131
def tag_options(options, escape = true)
132
unless options.blank?
135
options.each_pair do |key, value|
136
if BOOLEAN_ATTRIBUTES.include?(key)
137
attrs << %(#{key}="#{key}") if value
139
attrs << %(#{key}="#{escape_once(value)}") if !value.nil?
143
attrs = options.map { |key, value| %(#{key}="#{value}") }
145
" #{attrs.sort * ' '}".html_safe! unless attrs.empty?