Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_view / helpers / tag_helper.rb
1 require 'action_view/erb/util'
2 require 'set'
3
4 module ActionView
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.
8 module TagHelper
9 include ERB::Util
10
11 BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked).to_set
12 BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym))
13
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
18 # escaping.
19 #
20 # ==== Options
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.
24 #
25 # ==== Examples
26 # tag("br")
27 # # => <br />
28 #
29 # tag("br", nil, true)
30 # # => <br>
31 #
32 # tag("input", { :type => 'text', :disabled => true })
33 # # => <input type="text" disabled="disabled" />
34 #
35 # tag("img", { :src => "open & shut.png" })
36 # # => <img src="open &amp; shut.png" />
37 #
38 # tag("img", { :src => "open &amp; shut.png" }, false, false)
39 # # => <img src="open &amp; shut.png" />
40 def tag(name, options = nil, open = false, escape = true)
41 "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}"
42 end
43
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.
49 #
50 # ==== Options
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.
54 #
55 # ==== Examples
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>
62 #
63 # <% content_tag :div, :class => "strong" do -%>
64 # Hello world!
65 # <% end -%>
66 # # => <div class="strong">Hello world!</div>
67 def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
68 if block_given?
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)
71
72 if block_called_from_erb?(block)
73 concat(content_tag)
74 else
75 content_tag
76 end
77 else
78 content_tag_string(name, content_or_options_with_block, options, escape)
79 end
80 end
81
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>.
86 #
87 # ==== Examples
88 # cdata_section("<hello world>")
89 # # => <![CDATA[<hello world>]]>
90 #
91 # cdata_section(File.read("hello_world.txt"))
92 # # => <![CDATA[<hello from a text file]]>
93 def cdata_section(content)
94 "<![CDATA[#{content}]]>"
95 end
96
97 # Returns an escaped version of +html+ without affecting existing escaped entities.
98 #
99 # ==== Examples
100 # escape_once("1 < 2 &amp; 3")
101 # # => "1 &lt; 2 &amp; 3"
102 #
103 # escape_once("&lt;&lt; Accept & Checkout")
104 # # => "&lt;&lt; Accept &amp; Checkout"
105 def escape_once(html)
106 html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
107 end
108
109 private
110 BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
111
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)
119 end
120 else
121 def block_called_from_erb?(block)
122 block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
123 end
124 end
125
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}>"
129 end
130
131 def tag_options(options, escape = true)
132 unless options.blank?
133 attrs = []
134 if escape
135 options.each_pair do |key, value|
136 if BOOLEAN_ATTRIBUTES.include?(key)
137 attrs << %(#{key}="#{key}") if value
138 else
139 attrs << %(#{key}="#{escape_once(value)}") if !value.nil?
140 end
141 end
142 else
143 attrs = options.map { |key, value| %(#{key}="#{value}") }
144 end
145 " #{attrs.sort * ' '}" unless attrs.empty?
146 end
147 end
148 end
149 end
150 end