123456789_123456789_123456789_123456789_123456789_

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

Constant Summary

Class Method Summary

Instance Method Summary

::ActionView::Helpers::OutputSafetyHelper - Included

#raw

This method outputs without escaping a string.

#safe_join

This method returns an HTML safe string similar to what Array#join would return.

#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 stores a block of markup in an identifier for later use.

#content_for?

content_for? checks whether any content has been captured yet using content_for.

#provide

The same as content_for but when used with streaming flushes straight back to the layout.

#with_output_buffer

Use an alternate output buffer for the duration of the block.

Constructor Details

.new(view_context) ⇒ TagBuilder

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 52

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) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 166

def method_missing(called, *args, **options, &block)
  tag_string(called, *args, **options, &block)
end

Instance Method Details

#attributes(attributes)

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">
[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 61

def attributes(attributes)
  tag_options(attributes.to_h).to_s.strip.html_safe
end

#boolean_tag_option(key)

[ GitHub ]

  
# File 'actionview/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)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 79

def (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)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 65

def p(*arguments, **options, &block)
  tag_string(:p, *arguments, **options, &block)
end

#prefix_tag_option(prefix, key, value, escape) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 154

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)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 162

def respond_to_missing?(*args)
  true
end

#tag_option(key, value, escape)

[ GitHub ]

  
# File 'actionview/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('"', "&quot;") if value.include?('"')

  %(#{key}="#{value}")
end

#tag_options(options, escape = true)

[ GitHub ]

  
# File 'actionview/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, escape: true, **options, &block)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 69

def tag_string(name, content = nil, escape: true, **options, &block)
  content = @view_context.capture(self, &block) if block_given?
  self_closing = SVG_SELF_CLOSING_ELEMENTS.include?(name)
  if (HTML_VOID_ELEMENTS.include?(name) || self_closing) && content.nil?
    "<#{name.to_s.dasherize}#{tag_options(options, escape)}#{self_closing ? " />" : ">"}".html_safe
  else
    (name.to_s.dasherize, content || "", options, escape)
  end
end