Class: ActionView::Helpers::TagHelper::TagBuilder
Overview
Constant Summary
collapse
- HTML_VOID_ELEMENTS =
%i(area base br col circle embed hr img input keygen link meta param source track wbr).to_set
- SVG_VOID_ELEMENTS =
%i(animate animateMotion animateTransform circle ellipse line path polygon polyline rect set stop use view).to_set
Instance Method Summary
collapse
-
#attributes(attributes) ⇒ Object
Transforms a Hash into HTML Attributes, ready to be interpolated into ERB.
-
#boolean_tag_option(key) ⇒ Object
-
#content_tag_string(name, content, options, escape = true) ⇒ Object
-
#initialize(view_context) ⇒ TagBuilder
constructor
A new instance of TagBuilder.
-
#p(*arguments, **options, &block) ⇒ Object
-
#tag_option(key, value, escape) ⇒ Object
-
#tag_options(options, escape = true) ⇒ Object
-
#tag_string(name, content = nil, **options, &block) ⇒ Object
#raw, #safe_join, #to_sentence
#capture, #content_for, #content_for?, #provide, #with_output_buffer
Constructor Details
#initialize(view_context) ⇒ TagBuilder
Returns a new instance of TagBuilder.
51
52
53
|
# File 'lib/action_view/helpers/tag_helper.rb', line 51
def initialize(view_context)
@view_context = view_context
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(called, *args, **options, &block) ⇒ Object
187
188
189
|
# File 'lib/action_view/helpers/tag_helper.rb', line 187
def method_missing(called, *args, **options, &block)
tag_string(called, *args, **options, &block)
end
|
Instance Method Details
#attributes(attributes) ⇒ Object
Transforms a Hash into HTML Attributes, ready to be interpolated into ERB.
<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %> >
# => <input type="text" aria-label="Search">
60
61
62
|
# File 'lib/action_view/helpers/tag_helper.rb', line 60
def attributes(attributes)
tag_options(attributes.to_h).to_s.strip.html_safe
end
|
#boolean_tag_option(key) ⇒ Object
132
133
134
|
# File 'lib/action_view/helpers/tag_helper.rb', line 132
def boolean_tag_option(key)
%(#{key}="#{key}")
end
|
#content_tag_string(name, content, options, escape = true) ⇒ Object
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/action_view/helpers/tag_helper.rb', line 79
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
if escape
name = ERB::Util.xml_name_escape(name)
content = ERB::Util.unwrapped_html_escape(content)
end
"<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end
|
#p(*arguments, **options, &block) ⇒ Object
64
65
66
|
# File 'lib/action_view/helpers/tag_helper.rb', line 64
def p(*arguments, **options, &block)
tag_string(:p, *arguments, **options, &block)
end
|
#tag_option(key, value, escape) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/action_view/helpers/tag_helper.rb', line 136
def tag_option(key, value, escape)
key = ERB::Util.xml_name_escape(key) if escape
case value
when Array, Hash
value = TagHelper.build_tag_values(value) if key.to_s == "class"
value = escape ? safe_join(value, " ") : value.join(" ")
when Regexp
value = escape ? ERB::Util.unwrapped_html_escape(value.source) : value.source
else
value = escape ? ERB::Util.unwrapped_html_escape(value) : value.to_s
end
value = value.gsub('"', """) if value.include?('"')
%(#{key}="#{value}")
end
|
#tag_options(options, escape = true) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/action_view/helpers/tag_helper.rb', line 90
def tag_options(options, escape = true)
return if options.blank?
output = +""
sep = " "
options.each_pair do |key, value|
type = TAG_TYPES[key]
if type == :data && value.is_a?(Hash)
value.each_pair do |k, v|
next if v.nil?
output << sep
output << prefix_tag_option(key, k, v, escape)
end
elsif type == :aria && value.is_a?(Hash)
value.each_pair do |k, v|
next if v.nil?
case v
when Array, Hash
tokens = TagHelper.build_tag_values(v)
next if tokens.none?
v = safe_join(tokens, " ")
else
v = v.to_s
end
output << sep
output << prefix_tag_option(key, k, v, escape)
end
elsif type == :boolean
if value
output << sep
output << boolean_tag_option(key)
end
elsif !value.nil?
output << sep
output << tag_option(key, value, escape)
end
end
output unless output.empty?
end
|
#tag_string(name, content = nil, **options, &block) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/action_view/helpers/tag_helper.rb', line 68
def tag_string(name, content = nil, **options, &block)
escape = handle_deprecated_escape_options(options)
content = @view_context.capture(self, &block) if block_given?
if (HTML_VOID_ELEMENTS.include?(name) || SVG_VOID_ELEMENTS.include?(name)) && content.nil?
"<#{name.to_s.dasherize}#{tag_options(options, escape)}>".html_safe
else
content_tag_string(name.to_s.dasherize, content || "", options, escape)
end
end
|