Module: Phlex::SGML::Elements

Defined in:
lib/phlex/opal_compat.rb

Instance Method Summary collapse

Instance Method Details

#__register_void_element__(method_name, tag: method_name.name.tr("_", "-")) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/phlex/opal_compat.rb', line 82

def __register_void_element__(method_name, tag: method_name.name.tr("_", "-"))
  define_method(method_name) do |**attributes|
    state = @_state
    return unless state.should_render?

    buffer = state.buffer
    if attributes.length > 0
      buffer << "<#{tag}"
      __homura_normalize_comma_separated_tokens__(method_name, attributes)
      buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
      buffer << ">"
    else
      buffer << "<#{tag}>"
    end

    nil
  end

  __registered_elements__[method_name] = tag
  method_name
end

#register_element(method_name, tag: method_name.name.tr("_", "-")) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/phlex/opal_compat.rb', line 39

def register_element(method_name, tag: method_name.name.tr("_", "-"))
  define_method(method_name) do |**attributes, &content|
    state = @_state
    buffer = state.buffer
    has_content = !content.nil?

    unless state.should_render?
      content.call(self) if has_content
      return nil
    end

    if attributes.length > 0
      buffer << "<#{tag}"
      __homura_normalize_comma_separated_tokens__(method_name, attributes)
      buffer << (Phlex::ATTRIBUTE_CACHE[attributes] ||= Phlex::SGML::Attributes.generate_attributes(attributes))
      buffer << ">"

      if has_content
        original_length = buffer.bytesize
        rendered_content = content.call(self)
        __implicit_output__(rendered_content) if original_length == buffer.bytesize
        buffer << "</#{tag}>"
      else
        buffer << "</#{tag}>"
      end
    elsif has_content
      buffer << "<#{tag}>"
      original_length = buffer.bytesize
      rendered_content = content.call(self)
      __implicit_output__(rendered_content) if original_length == buffer.bytesize
      buffer << "</#{tag}>"
    else
      buffer << "<#{tag}></#{tag}>"
    end

    flush if tag == "head"
    nil
  end

  __registered_elements__[method_name] = tag
  method_name
end