Class: Eskimo::HTML::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/eskimo/html/component.rb

Overview

Base HTMLElement component which takes a #tag_name, a set of #attributes, and optionally a set of #children and turns them into a beautiful – believe it or not – HTML tag:

<x-foo <!-- attributes -->>
  <!-- children -->
</x-foo>

See ASCII::Component for how this works under the hood since it’s similar.

Direct Known Subclasses

Html

Constant Summary collapse

ATTRIBUTE_REWRITES =

Mapping of Ruby attribute name to HTML attribute name; some words like “class” are reserved and are problematic when passed as attributes so this hash supports a method to rewrite those.

{
  'className' => 'class'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, &children) ⇒ Component

Returns a new instance of Component.



50
51
52
53
# File 'lib/eskimo/html/component.rb', line 50

def initialize(attributes = nil, &children)
  @attributes = attributes || BLANK_ATTRIBUTES
  @children = children
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/eskimo/html/component.rb', line 23

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



24
25
26
# File 'lib/eskimo/html/component.rb', line 24

def children
  @children
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



25
26
27
# File 'lib/eskimo/html/component.rb', line 25

def tag_name
  @tag_name
end

#voidObject (readonly)

Returns the value of attribute void.



26
27
28
# File 'lib/eskimo/html/component.rb', line 26

def void
  @void
end

Class Method Details

.define!(tag_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eskimo/html/component.rb', line 35

def self.define!(tag_name)
  eval "    class Eskimo::HTML::\#{tag_name.capitalize} < Eskimo::HTML::Component\n      def tag_name\n        \"\#{tag_name}\".freeze\n      end\n\n      def void\n        @void = Eskimo::HTML::VOID_TAGS.include?(tag_name) if @void.nil?\n        @void\n      end\n    end\n  RUBY\nend\n"

.tag(tag_name, attrs, &children) ⇒ Object



28
29
30
31
32
33
# File 'lib/eskimo/html/component.rb', line 28

def self.tag(tag_name, attrs, &children)
  new(attrs, &children).tap do |component|
    component.instance_variable_set(:@tag_name, tag_name)
    component.instance_variable_set(:@void, Eskimo::HTML::VOID_TAGS.include?(tag_name))
  end
end

Instance Method Details

#render(render:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/eskimo/html/component.rb', line 55

def render(render:, **)
  safe_tag_name = Eskimo::HTML::Util.xml_name_escape(tag_name)
  tag_with_attributes = "#{safe_tag_name} #{serialize_attributes}".strip

  if void
    "<#{tag_with_attributes} />"
  else
    "<#{tag_with_attributes}>#{render[@children]}</#{safe_tag_name}>"
  end
end