require 'lotus/utils' # RUBY_VERSION >= '2.2'
require 'lotus/utils/class_attribute'
require 'lotus/utils/escape'
require 'lotus/helpers/html_helper/empty_html_node'
require 'lotus/helpers/html_helper/html_node'
require 'lotus/helpers/html_helper/html_fragment'
require 'lotus/helpers/html_helper/text_node'
module Lotus
module Helpers
module HtmlHelper
# HTML Builder
#
# @since 0.1.0
class HtmlBuilder
# HTML5 content tags
#
# @since 0.1.0
# @api private
#
# @see Lotus::Helpers::HtmlHelper::HtmlNode
# @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element
CONTENT_TAGS = [
'a',
'abbr',
'address',
'article',
'aside',
'audio',
'b',
'bdi',
'bdo',
'blockquote',
'body',
'button',
'canvas',
'caption',
'cite',
'code',
'colgroup',
'data',
'datalist',
'del',
'details',
'dfn',
'div',
'dl',
'dt',
'dd',
'em',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'i',
'iframe',
'ins',
'kbd',
'label',
'legend',
'li',
'link',
'main',
'map',
'mark',
'math',
'menu',
'meter',
'nav',
'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'script',
'section',
'select',
'small',
'span',
'strong',
'style',
'sub',
'summary',
'sup',
'svg',
'table',
'tbody',
'td',
'template',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'u',
'ul',
'video',
].freeze
# HTML5 empty tags
#
# @since 0.1.0
# @api private
#
# @see Lotus::Helpers::HtmlHelper::EmptyHtmlNode
# @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element
EMPTY_TAGS = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr',
].freeze
# New line separator
#
# @since 0.1.0
# @api private
NEWLINE = "\n".freeze
CONTENT_TAGS.each do |tag|
class_eval %{
def #{ tag }(content = nil, attributes = nil, &blk)
@nodes << self.class.html_node.new(:#{ tag }, blk || content, attributes || content, options)
self
end
}
end
EMPTY_TAGS.each do |tag|
class_eval %{
def #{ tag }(attributes = nil)
@nodes << EmptyHtmlNode.new(:#{ tag }, attributes)
self
end
}
end
include Utils::ClassAttribute
class_attribute :html_node
self.html_node = ::Lotus::Helpers::HtmlHelper::HtmlNode
# Initialize a new builder
#
# @return [Lotus::Helpers::HtmlHelper::HtmlBuilder] the builder
#
# @since 0.1.0
# @api private
def initialize
@nodes = []
end
def options
end
# Define a custom tag
#
# @param name [Symbol,String] the name of the tag
# @param content [String,Lotus::Helpers::HtmlHelper::HtmlBuilder,NilClass] the optional content
# @param attributes [Hash,NilClass] the optional tag attributes
# @param blk [Proc] the optional nested content espressed as a block
#
# @return [self]
#
# @since 0.1.0
# @api public
#
# @see Lotus::Helpers::HtmlHelper
#
# @example
# html.tag(:custom) # => hello hello
hello
#lotus
def fragment(&blk) @nodes << HtmlFragment.new(&blk) self end # Defines a custom empty tag # # @param name [Symbol,String] the name of the tag # @param attributes [Hash,NilClass] the optional tag attributes # # @return [self] # # @since 0.1.0 # @api public # # @see Lotus::Helpers::HtmlHelper # # @example # html.empty_tag(:xr) # =>