Module: Phlex::Elements
- Included in:
- HTML, HTML::StandardElements, HTML::VoidElements, SVG::StandardElements
- Defined in:
- lib/phlex/elements.rb
Overview
Extending this module provides the #register_element macro for registering your own custom elements. It’s already extended by HTML and SVG.
Instance Method Summary collapse
-
#register_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) ⇒ Symbol
Register a custom element.
- #register_void_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) ⇒ Object private
- #registered_elements ⇒ Object private
Instance Method Details
#register_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) ⇒ Symbol
31 32 33 34 35 36 37 38 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/phlex/elements.rb', line 31 def register_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) if deprecated deprecation = <<~RUBY Kernel.warn "#{deprecated}" RUBY else deprecation = "" end class_eval(<<-RUBY, __FILE__, __LINE__ + 1) # frozen_string_literal: true def #{method_name}(**attributes, &block) #{deprecation} context = @_context buffer = context.buffer fragment = context.fragments target_found = false if fragment return if fragment.length == 0 # we found all our fragments already id = attributes[:id] if !context.in_target_fragment if fragment[id] context.begin_target(id) target_found = true else yield(self) if block return nil end end end if attributes.length > 0 # with attributes if block # with content block buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">" yield_content(&block) buffer << "</#{tag}>" else # without content block buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>" end else # without attributes if block # with content block buffer << "<#{tag}>" yield_content(&block) buffer << "</#{tag}>" else # without content block buffer << "<#{tag}></#{tag}>" end end #{'flush' if tag == 'head'} context.end_target if target_found nil end alias_method :_#{method_name}, :#{method_name} RUBY registered_elements[method_name] = tag method_name end |
#register_void_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) ⇒ Object
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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/phlex/elements.rb', line 101 def register_void_element(method_name, tag: method_name.name.tr("_", "-"), deprecated: false) if deprecated deprecation = <<~RUBY Kernel.warn "#{deprecated}" RUBY else deprecation = "" end class_eval(<<-RUBY, __FILE__, __LINE__ + 1) # frozen_string_literal: true def #{method_name}(**attributes) #{deprecation} context = @_context buffer = context.buffer fragment = context.fragments if fragment return if fragment.length == 0 # we found all our fragments already id = attributes[:id] if !context.in_target_fragment if fragment[id] context.begin_target(id) target_found = true else return nil end end end if attributes.length > 0 # with attributes buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">" else # without attributes buffer << "<#{tag}>" end context.end_target if target_found nil end alias_method :_#{method_name}, :#{method_name} RUBY registered_elements[method_name] = tag method_name end |
#registered_elements ⇒ Object
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.
20 21 22 |
# File 'lib/phlex/elements.rb', line 20 def registered_elements @registered_elements ||= {} end |