Sha256: 0d716c50f9ac49224aadb1c7a14ab2ae07321e29a7945dec33fdfca1b3f1a920

Contents?: true

Size: 1.13 KB

Versions: 9

Compression:

Stored size: 1.13 KB

Contents

module Rbexy
  module Nodes
    class HTMLElement < AbstractElement
      KNOWN_VOID_ELEMENTS = ActionView::Helpers::TagHelper::TagBuilder::VOID_ELEMENTS.map(&:to_s).to_set

      def precompile
        nodes = []

        if void? && children.length == 0
          nodes.concat(precompile_open_tag)
        else
          nodes.concat(precompile_open_tag)
          nodes.concat(children.map(&:precompile).flatten)
          nodes << Raw.new("</#{name}>")
        end

        nodes
      end

      private

      def void?
        KNOWN_VOID_ELEMENTS.include?(name)
      end

      def precompile_open_tag
        nodes = [Raw.new("<#{name}")]
        nodes.concat(precompile_members)
        nodes << Raw.new(">")
        nodes
      end

      def precompile_members
        members.map do |node|
          if node.is_a? ExpressionGroup
            ExpressionGroup.new(
              node.members,
              inner_template: "Rbexy::Runtime.splat_attrs(%s)",
              outer_template: ExpressionGroup::OUTPUT_SAFE
            )
          else
            node
          end
        end.map(&:precompile).flatten
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rbexy-2.0.0.beta9 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta8 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta7 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta6 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta5 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta4 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta3 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta2 lib/rbexy/nodes/html_element.rb
rbexy-2.0.0.beta1 lib/rbexy/nodes/html_element.rb