Class: Hanami::Helpers::HtmlHelper::EmptyHtmlNode Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/helpers/html_helper/empty_html_node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Empty HTML node

Since:

  • 0.1.0

Direct Known Subclasses

HtmlNode

Constant Summary collapse

BOOLEAN_ATTRIBUTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

List of attributes that get special treatment when rendering.

%w(
  allowfullscreen
  async
  autobuffer
  autofocus
  autoplay
  checked
  compact
  controls
  declare
  default
  defaultchecked
  defaultmuted
  defaultselected
  defer
  disabled
  draggable
  enabled
  formnovalidate
  hidden
  indeterminate
  inert
  ismap
  itemscope
  loop
  multiple
  muted
  nohref
  noresize
  noshade
  novalidate
  nowrap
  open
  pauseonexit
  pubdate
  readonly
  required
  reversed
  scoped
  seamless
  selected
  sortable
  spellcheck
  translate
  truespeed
  typemustmatch
  visible
).freeze
ATTRIBUTES_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Attributes separator

Since:

  • 0.1.0

' '.freeze

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes) ⇒ Hanami::Helpers::HtmlHelper::EmptyHtmlNode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new empty HTML node

Since:

  • 0.1.0


79
80
81
82
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 79

def initialize(name, attributes)
  @name       = name
  @attributes = attributes
end

Instance Method Details

#attribute(attribute_name, value) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


126
127
128
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 126

def attribute(attribute_name, value)
  %(#{ATTRIBUTES_SEPARATOR}#{attribute_name}="#{value}")
end

#attributesString, NilClass (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve the attributes

Since:

  • 0.1.0


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 102

def attributes
  return unless defined?(@attributes) && !@attributes.nil?
  result = ''

  @attributes.each do |attribute_name, value|
    if boolean_attribute?(attribute_name)
      result << boolean_attribute(attribute_name, value) if value
    else
      result << attribute(attribute_name, value)
    end
  end

  result
end

#boolean_attribute(attribute_name, _value) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Do not render boolean attributes when their value is false.

Since:

  • 0.1.0


122
123
124
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 122

def boolean_attribute(attribute_name, _value)
  %(#{ATTRIBUTES_SEPARATOR}#{attribute_name}="#{attribute_name}")
end

#boolean_attribute?(attribute_name) ⇒ Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


117
118
119
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 117

def boolean_attribute?(attribute_name)
  BOOLEAN_ATTRIBUTES.include?(attribute_name.to_s)
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve and return the output

Since:

  • 0.1.0


90
91
92
# File 'lib/hanami/helpers/html_helper/empty_html_node.rb', line 90

def to_s
  %(<#{@name}#{attributes}>)
end