Module: Hecks::Forms::Tag

Defined in:
lib/hecks/forms/html.rb

Overview

A tiny attribute-hash -> string helper, shared by every renderer in this directory so <input ...> doesn't get hand-assembled five different ways with five different escaping bugs waiting in each. true renders as a bare boolean attribute (required, not required="true"); nil/false are dropped entirely.

Class Method Summary collapse

Class Method Details

.attrs(pairs) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/hecks/forms/html.rb', line 36

def self.attrs(pairs)
  pairs.filter_map do |name, value|
    next if value.nil? || value == false
    next name.to_s.tr("_", "-") if value == true

    %(#{name.to_s.tr("_", "-")}="#{Escape.attr(value)}")
  end.join(" ")
end

.open(name, **pairs) ⇒ Object



45
46
47
48
# File 'lib/hecks/forms/html.rb', line 45

def self.open(name, **pairs)
  rendered = attrs(pairs)
  rendered.empty? ? "<#{name}>" : "<#{name} #{rendered}>"
end

.void(name, **pairs) ⇒ Object

self-closing tags read the same; HTML5 needs no slash



50
# File 'lib/hecks/forms/html.rb', line 50

def self.void(name, **pairs) = open(name, **pairs) # self-closing tags read the same; HTML5 needs no slash