Class: ActionView::Helpers::TagHelper::TagBuilder

Inherits:
Object
  • Object
show all
Includes:
CaptureHelper, OutputSafetyHelper
Defined in:
lib/action_view/helpers/tag_helper.rb

Overview

:nodoc:

Constant Summary collapse

VOID_ELEMENTS =
%i(area base br col embed hr img input keygen link meta param source track wbr).to_set

Instance Method Summary collapse

Methods included from OutputSafetyHelper

#raw, #safe_join, #to_sentence

Methods included from CaptureHelper

#capture, #content_for, #content_for?, #provide, #with_output_buffer

Constructor Details

#initialize(view_context) ⇒ TagBuilder

Returns a new instance of TagBuilder.



40
41
42
# File 'lib/action_view/helpers/tag_helper.rb', line 40

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, &block) ⇒ Object (private)



109
110
111
# File 'lib/action_view/helpers/tag_helper.rb', line 109

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

Instance Method Details

#boolean_tag_option(key) ⇒ Object



83
84
85
# File 'lib/action_view/helpers/tag_helper.rb', line 83

def boolean_tag_option(key)
  %(#{key}="#{key}")
end

#content_tag_string(name, content, options, escape = true) ⇒ Object



53
54
55
56
57
# File 'lib/action_view/helpers/tag_helper.rb', line 53

def (name, content, options, escape = true)
  tag_options = tag_options(options, escape) if options
  content     = ERB::Util.unwrapped_html_escape(content) if escape
  "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end

#tag_option(key, value, escape) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/action_view/helpers/tag_helper.rb', line 87

def tag_option(key, value, escape)
  if value.is_a?(Array)
    value = escape ? safe_join(value, " ".freeze) : value.join(" ".freeze)
  else
    value = escape ? ERB::Util.unwrapped_html_escape(value) : value.to_s
  end
  %(#{key}="#{value.gsub('"'.freeze, '&quot;'.freeze)}")
end

#tag_options(options, escape = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/action_view/helpers/tag_helper.rb', line 59

def tag_options(options, escape = true)
  return if options.blank?
  output = "".dup
  sep    = " "
  options.each_pair do |key, value|
    if TAG_PREFIXES.include?(key) && 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 BOOLEAN_ATTRIBUTES.include?(key)
      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_attributes: true, **options, &block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/action_view/helpers/tag_helper.rb', line 44

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