Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / lib / action_view / helpers / tag_helper.rb
blob999cbfb52a286c09988f96ceabf26abfcdbd7787
1 require 'cgi'
2 require 'erb'
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
11       BOOLEAN_ATTRIBUTES = Set.new(%w(disabled readonly multiple))
13       # Returns an empty HTML tag of type +name+ which by default is XHTML 
14       # compliant. Set +open+ to true to create an open tag compatible 
15       # with HTML 4.0 and below. Add HTML attributes by passing an attributes 
16       # hash to +options+. Set +escape+ to false to disable attribute value
17       # escaping.
18       #
19       # ==== Options
20       # The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and 
21       # <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
22       # symbols or strings for the attribute names.
23       #
24       # ==== Examples
25       #   tag("br")
26       #   # => <br />
27       #
28       #   tag("br", nil, true)
29       #   # => <br>
30       #
31       #   tag("input", { :type => 'text', :disabled => true }) 
32       #   # => <input type="text" disabled="disabled" />
33       #
34       #   tag("img", { :src => "open & shut.png" })
35       #   # => <img src="open &amp; shut.png" />
36       #
37       #   tag("img", { :src => "open &amp; shut.png" }, false, false)
38       #   # => <img src="open &amp; shut.png" />
39       def tag(name, options = nil, open = false, escape = true)
40         "<#{name}#{tag_options(options, escape) if options}" + (open ? ">" : " />")
41       end
43       # Returns an HTML block tag of type +name+ surrounding the +content+. Add
44       # HTML attributes by passing an attributes hash to +options+. 
45       # Instead of passing the content as an argument, you can also use a block
46       # in which case, you pass your +options+ as the second parameter.
47       # Set escape to false to disable attribute value escaping.
48       #
49       # ==== Options
50       # The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and 
51       # <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
52       # symbols or strings for the attribute names.
53       #
54       # ==== Examples
55       #   content_tag(:p, "Hello world!")
56       #    # => <p>Hello world!</p>
57       #   content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
58       #    # => <div class="strong"><p>Hello world!</p></div>
59       #   content_tag("select", options, :multiple => true)
60       #    # => <select multiple="multiple">...options...</select>
61       #
62       #   <% content_tag :div, :class => "strong" do -%>
63       #     Hello world!
64       #   <% end -%>
65       #    # => <div class="strong"><p>Hello world!</p></div>
66       def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
67         if block_given?
68           options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
69           content = capture(&block)
70           content_tag = content_tag_string(name, content, options, escape)
71           block_is_within_action_view?(block) ? concat(content_tag, block.binding) : content_tag
72         else
73           content = content_or_options_with_block
74           content_tag_string(name, content, options, escape)
75         end
76       end
78       # Returns a CDATA section with the given +content+.  CDATA sections
79       # are used to escape blocks of text containing characters which would
80       # otherwise be recognized as markup. CDATA sections begin with the string
81       # <tt><![CDATA[</tt> and end with (and may not contain) the string <tt>]]></tt>.
82       #
83       # ==== Examples
84       #   cdata_section("<hello world>")
85       #   # => <![CDATA[<hello world>]]>
86       #
87       #   cdata_section(File.read("hello_world.txt"))
88       #   # => <![CDATA[<hello from a text file]]>
89       def cdata_section(content)
90         "<![CDATA[#{content}]]>"
91       end
93       # Returns an escaped version of +html+ without affecting existing escaped entities.
94       #
95       # ==== Examples
96       #   escape_once("1 > 2 &amp; 3")
97       #   # => "1 &lt; 2 &amp; 3"
98       #
99       #   escape_once("&lt;&lt; Accept & Checkout")
100       #   # => "&lt;&lt; Accept &amp; Checkout"
101       def escape_once(html)
102         html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
103       end
105       private
106         def content_tag_string(name, content, options, escape = true)
107           tag_options = tag_options(options, escape) if options
108           "<#{name}#{tag_options}>#{content}</#{name}>"
109         end
111         def tag_options(options, escape = true)
112           unless options.blank?
113             attrs = []
114             if escape
115               options.each do |key, value|
116                 next unless value
117                 key = key.to_s
118                 value = BOOLEAN_ATTRIBUTES.include?(key) ? key : escape_once(value)
119                 attrs << %(#{key}="#{value}")
120               end
121             else
122               attrs = options.map { |key, value| %(#{key}="#{value}") }
123             end
124             " #{attrs.sort * ' '}" unless attrs.empty?
125           end
126         end
128         def block_is_within_action_view?(block)
129           eval("defined? _erbout", block.binding)
130         end
131     end
132   end