Class: ActionView::Helpers::TagHelper::TagBuilder
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | actionview/lib/action_view/helpers/tag_helper.rb |
Class Method Summary
Instance Method Summary
- #attributes(attributes)
- #boolean_tag_option(key)
- #content_tag_string(name, content, options, escape = true)
- #self_closing_tag_string(name, options, escape = true, tag_suffix = " />")
- #tag_option(key, value, escape)
- #tag_options(options, escape = true)
- #tag_string(name, content = nil, options, escape: true, &block)
- #method_missing(called, *args, escape: true, **options, &block) private
- #prefix_tag_option(prefix, key, value, escape) private
- #respond_to_missing?(*args) ⇒ Boolean private
::ActionView::Helpers::OutputSafetyHelper
- Included
#raw | This method outputs without escaping a string. |
#safe_join | This method returns an HTML safe string similar to what |
#to_sentence | Converts the array to a comma-separated sentence where the last element is joined by the connector word. |
::ActionView::Helpers::CaptureHelper
- Included
#capture | The capture method extracts part of a template as a string object. |
#content_for | Calling |
#content_for? |
|
#provide | The same as |
#with_output_buffer | Use an alternate output buffer for the duration of the block. |
Constructor Details
.new(view_context) ⇒ TagBuilder
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 216
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, escape: true, **options, &block) (private)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 324
def method_missing(called, *args, escape: true, **, &block) name = called.name.dasherize TagHelper.ensure_valid_html5_tag_name(name) tag_string(name, *args, , escape: escape, &block) end
Class Method Details
.define_element(name, code_generator:, method_name: name)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 50
def self.define_element(name, code_generator:, method_name: name) return if method_defined?(name) code_generator.class_eval do |batch| batch << "\n" << "def #{method_name}(content = nil, escape: true, **options, &block)" << " tag_string(#{name.inspect}, content, options, escape: escape, &block)" << "end" end end
.define_self_closing_element(name, code_generator:, method_name: name)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 70
def self.define_self_closing_element(name, code_generator:, method_name: name) code_generator.class_eval do |batch| batch << "\n" << "def #{method_name}(content = nil, escape: true, **options, &block)" << " if content || block" << " tag_string(#{name.inspect}, content, options, escape: escape, &block)" << " else" << " self_closing_tag_string(#{name.inspect}, options, escape)" << " end" << "end" end end
.define_void_element(name, code_generator:, method_name: name)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 61
def self.define_void_element(name, code_generator:, method_name: name) code_generator.class_eval do |batch| batch << "\n" << "def #{method_name}(escape: true, **options, &block)" << " self_closing_tag_string(#{name.inspect}, options, escape, '>')" << "end" end end
Instance Method Details
#attributes(attributes)
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 225
def attributes(attributes) (attributes.to_h).to_s.strip.html_safe end
#boolean_tag_option(key)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 290
def boolean_tag_option(key) %(#{key}="#{key}") end
#content_tag_string(name, content, options, escape = true)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 239
def content_tag_string(name, content, , escape = true) = (, escape) if if escape && content.present? content = ERB::Util.unwrapped_html_escape(content) end "<#{name}#{}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe end
#prefix_tag_option(prefix, key, value, escape) (private)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 312
def prefix_tag_option(prefix, key, value, escape) key = "#{prefix}-#{key.to_s.dasherize}" unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(BigDecimal) value = value.to_json end tag_option(key, value, escape) end
#respond_to_missing?(*args) ⇒ Boolean
(private)
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 320
def respond_to_missing?(*args) true end
#self_closing_tag_string(name, options, escape = true, tag_suffix = " />")
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 235
def self_closing_tag_string(name, , escape = true, tag_suffix = " />") "<#{name}#{ (, escape)}#{tag_suffix}".html_safe end
#tag_option(key, value, escape)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 294
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)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 248
def (, escape = true) return if .blank? output = +"" sep = " " .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, escape: true, &block)
[ GitHub ]# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 229
def tag_string(name, content = nil, , escape: true, &block) content = @view_context.capture(self, &block) if block content_tag_string(name, content, , escape) end