Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 6024 Bytes
1 | require 'active_support/multibyte' |
2 | require 'active_support/core_ext/string/output_safety' |
3 | require 'set' |
4 | |
5 | module ActionView |
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. |
9 | module TagHelper |
10 | include ERB::Util |
11 | |
12 | BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked).to_set |
13 | BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym)) |
14 | |
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 |
19 | # escaping. |
20 | # |
21 | # ==== Options |
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. |
25 | # |
26 | # ==== Examples |
27 | # tag("br") |
28 | # # => <br /> |
29 | # |
30 | # tag("br", nil, true) |
31 | # # => <br> |
32 | # |
33 | # tag("input", { :type => 'text', :disabled => true }) |
34 | # # => <input type="text" disabled="disabled" /> |
35 | # |
36 | # tag("img", { :src => "open & shut.png" }) |
37 | # # => <img src="open & shut.png" /> |
38 | # |
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 ? ">" : " />"}".html_safe |
43 | end |
44 | |
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. |
50 | # |
51 | # ==== Options |
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. |
55 | # |
56 | # ==== Examples |
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> |
63 | # |
64 | # <% content_tag :div, :class => "strong" do -%> |
65 | # Hello world! |
66 | # <% end -%> |
67 | # # => <div class="strong">Hello world!</div> |
68 | def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) |
69 | if block_given? |
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) |
72 | |
73 | if block_called_from_erb?(block) |
74 | concat(content_tag) |
75 | else |
76 | content_tag |
77 | end |
78 | else |
79 | content_tag_string(name, content_or_options_with_block, options, escape) |
80 | end |
81 | end |
82 | |
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>. |
87 | # |
88 | # ==== Examples |
89 | # cdata_section("<hello world>") |
90 | # # => <![CDATA[<hello world>]]> |
91 | # |
92 | # cdata_section(File.read("hello_world.txt")) |
93 | # # => <![CDATA[<hello from a text file]]> |
94 | def cdata_section(content) |
95 | "<![CDATA[#{content}]]>".html_safe |
96 | end |
97 | |
98 | # Returns an escaped version of +html+ without affecting existing escaped entities. |
99 | # |
100 | # ==== Examples |
101 | # escape_once("1 < 2 & 3") |
102 | # # => "1 < 2 & 3" |
103 | # |
104 | # escape_once("<< Accept & Checkout") |
105 | # # => "<< Accept & Checkout" |
106 | def escape_once(html) |
107 | ActiveSupport::Multibyte.clean(html.to_s).gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] } |
108 | end |
109 | |
110 | private |
111 | BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template' |
112 | |
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) |
120 | end |
121 | else |
122 | def block_called_from_erb?(block) |
123 | block && eval(BLOCK_CALLED_FROM_ERB, block.binding) |
124 | end |
125 | end |
126 | |
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}>".html_safe |
130 | end |
131 | |
132 | def tag_options(options, escape = true) |
133 | unless options.blank? |
134 | attrs = [] |
135 | if escape |
136 | options.each_pair do |key, value| |
137 | if BOOLEAN_ATTRIBUTES.include?(key) |
138 | attrs << %(#{key}="#{key}") if value |
139 | else |
140 | attrs << %(#{key}="#{escape_once(value)}") if !value.nil? |
141 | end |
142 | end |
143 | else |
144 | attrs = options.map { |key, value| %(#{key}="#{value}") } |
145 | end |
146 | " #{attrs.sort * ' '}".html_safe unless attrs.empty? |
147 | end |
148 | end |
149 | end |
150 | end |
151 | end |