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



67
68
69
70
71
72
73
74
# File 'lib/hecks/forms/html.rb', line 67

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



76
77
78
79
# File 'lib/hecks/forms/html.rb', line 76

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



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

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