Sha256: 9f491f801730dae61b2cb3efcdaf471b843dfeca098d2e20e2cd60b95f07bb7a

Contents?: true

Size: 1.98 KB

Versions: 14

Compression:

Stored size: 1.98 KB

Contents

require 'hanami/helpers/html_helper/empty_html_node'
require 'hanami/utils/escape'

module Hanami
  module Helpers
    module HtmlHelper
      # HTML node
      #
      # @since 0.1.0
      # @api private
      #
      # @see Hanami::Helpers::HtmlHelper::EmptyHtmlNode
      class HtmlNode < EmptyHtmlNode
        # Initialize a new HTML node
        #
        # @param name [Symbol,String] the name of the tag
        # @param content [String,Proc,Hanami::Helpers::HtmlHelper::HtmlBuilder,NilClass] the optional content
        # @param attributes [Hash,NilClass] the optional tag attributes
        # @param _options [Hash] a optional set of data
        #
        # @return [Hanami::Helpers::HtmlHelper::HtmlNode]
        def initialize(name, content, attributes, _options = {})
          @builder = HtmlBuilder.new
          @name    = name
          @content = case content
                     when Hash
                       @attributes = content
                       nil
                     else
                       @attributes = attributes.to_h if attributes.respond_to?(:to_h)
                       content
                     end
        end

        # Resolve and return the output
        #
        # @return [String] the output
        #
        # @since 0.1.0
        # @api private
        #
        # @see Hanami::Helpers::HtmlHelper::EmptyHtmlNode#to_s
        def to_s
          %(#{super}#{content}</#{@name}>)
        end

        private

        # Resolve the (nested) content
        #
        # @return [String] the content
        #
        # @since 0.1.0
        # @api private
        def content # rubocop:disable Metrics/MethodLength
          case @content
          when Proc
            result = @builder.resolve(&@content)

            if @builder.nested?
              "\n#{@builder}\n"
            else
              "\n#{Utils::Escape.html(result)}\n"
            end
          else
            Utils::Escape.html(@content)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hanami-helpers-1.1.2 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.0 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.0.rc1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.0.beta3 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.0.beta2 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.1.0.beta1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.0.0 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.0.0.rc1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.0.0.beta2 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-1.0.0.beta1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-0.5.1 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-0.5.0 lib/hanami/helpers/html_helper/html_node.rb
hanami-helpers-0.4.0 lib/hanami/helpers/html_helper/html_node.rb