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

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

Overview

:nodoc:

Class Method Summary collapse

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.



216
217
218
# 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) ⇒ Object (private)



324
325
326
327
328
329
330
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 324

def method_missing(called, *args, escape: true, **options, &block)
  name = called.name.dasherize

  TagHelper.ensure_valid_html5_tag_name(name)

  tag_string(name, *args, options, escape: escape, &block)
end

Class Method Details

.define_element(name, code_generator:, method_name: name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



61
62
63
64
65
66
67
68
# 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) ⇒ 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">


225
226
227
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 225

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

#boolean_tag_option(key) ⇒ Object



290
291
292
# 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) ⇒ Object



239
240
241
242
243
244
245
246
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 239

def (name, content, options, escape = true)
  tag_options = tag_options(options, escape) if options

  if escape && content.present?
    content = ERB::Util.unwrapped_html_escape(content)
  end
  "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end

#self_closing_tag_string(name, options, escape = true, tag_suffix = " />") ⇒ Object



235
236
237
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 235

def self_closing_tag_string(name, options, escape = true, tag_suffix = " />")
  "<#{name}#{tag_options(options, escape)}#{tag_suffix}".html_safe
end

#tag_option(key, value, escape) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# 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('"', "&quot;") if value.include?('"')

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

#tag_options(options, escape = true) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 248

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, escape: true, &block) ⇒ Object



229
230
231
232
233
# File 'actionview/lib/action_view/helpers/tag_helper.rb', line 229

def tag_string(name, content = nil, options, escape: true, &block)
  content = @view_context.capture(self, &block) if block

  (name, content, options, escape)
end