lib/hot_module.rb in hot_module-1.0.0.alpha12 vs lib/hot_module.rb in hot_module-1.0.0.alpha13

- old
+ new

@@ -148,18 +148,29 @@ # Override in component # # @return [Hash] def attributes = {} + def rendering_mode = @_rendering_mode || :node + + def rendering_mode=(mode) + @_rendering_mode = case mode + when :node, :string + mode + end + end + # @param attributes [Hash] # @param content [String, Nokolexbor::Element] - # @param return_node [Boolean] - def render_element(attributes: self.attributes, content: self.content, return_node: false) # rubocop:disable Metrics + def render_element(attributes: self.attributes, content: self.content) # rubocop:disable Metrics doc = self.class.doc.clone @_content = content - tmpl_el = doc.css("> template").find { _1.attributes.empty? } + tmpl_el = doc.css("> template").find do |node| + node.attributes.empty? || + (node.attributes.count == 1 && node.attributes.any? { |k| k[0].start_with?("data-") }) + end unless tmpl_el tmpl_el = doc.document.create_element("template") immediate_children = doc.css("> :not(style):not(script)") tmpl_el.children[0] << immediate_children @@ -170,11 +181,11 @@ process_fragment(tmpl_el) HoTModuLe.registered_elements.each do |component| tmpl_el.children[0].css(component.tag_name).reverse.each do |node| if node["hmod:ignore"] - node.attribute("hmod:ignore").remove + node.remove_attribute("hmod:ignore") next end attrs = node.attributes.transform_values(&:value) attrs.reject! { |k| k.start_with?("hmod:") } @@ -190,11 +201,11 @@ attrs.transform_keys!(&:to_sym) new_node = node.replace( component.new(**attrs).render_element(content: node.children) ) - new_node.first.attribute("hmod:ignore")&.remove + new_node.remove_attribute("hmod:ignore") end end # Set attributes on the custom element attributes.each { |k, v| doc[k.to_s.tr("_", "-")] = value_to_attribute(v) if v } @@ -245,12 +256,11 @@ doc << node end tmpl_el.remove end - # And that is that. - return_node ? doc : doc.to_html + rendering_mode == :node ? doc : doc.to_html end def call(...) = render_element(...) def inspect = "#<#{self.class.name} #{attributes}>" @@ -378,12 +388,13 @@ if defined?(Bridgetown) Bridgetown.initializer :hot_module do |config| Bridgetown::Component.extend ActiveSupport::DescendantsTracker HoTModuLe.module_eval do - def render_in(*) + def render_in(view_context, rendering_mode = :string, &block) @attributes&.[]=("hmod:ignore", "") - super + self.rendering_mode = rendering_mode + super(view_context, &block) end end # Eager load all components config.hook :site, :after_reset do |site|