Class: Lotus::Helpers::HtmlHelper::HtmlBuilder

Inherits:
Object
  • Object
show all
Includes:
Utils::ClassAttribute
Defined in:
lib/lotus/helpers/html_helper/html_builder.rb

Overview

HTML Builder

Since:

  • 0.1.0

Direct Known Subclasses

FormHelper::FormBuilder

Constant Summary collapse

CONTENT_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 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
EMPTY_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 empty tags

[
  'area',
  'base',
  'br',
  'col',
  'embed',
  'hr',
  'img',
  'input',
  'keygen',
  'link',
  'menuitem',
  'meta',
  'param',
  'source',
  'track',
  'wbr',
].freeze
NEWLINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

New line separator

Since:

  • 0.1.0

"\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeLotus::Helpers::HtmlHelper::HtmlBuilder

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.

Initialize a new builder

Since:

  • 0.1.0



182
183
184
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 182

def initialize
  @nodes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ 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.

Forward missing methods to the current context. This allows to access views local variables from nested content blocks.

Since:

  • 0.1.0



370
371
372
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 370

def method_missing(m, *args, &blk)
  @context.__send__(m, *args, &blk)
end

Instance Method Details

#empty_tag(name, attributes = nil) ⇒ self

Defines a custom empty tag

Examples:

html.empty_tag(:xr) # => <xr>

html.empty_tag(:xr, id: 'foo') # => <xr id="foo">

html.empty_tag(:xr, id: 'foo', 'data-xyz': 'bar') # => <xr id="foo" data-xyz="bar">

Parameters:

  • name (Symbol, String)

    the name of the tag

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



280
281
282
283
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 280

def empty_tag(name, attributes = nil)
  @nodes << EmptyHtmlNode.new(name, attributes)
  self
end

#encode(encoding) ⇒ String

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.

Encode the content with the given character encoding

Parameters:

  • encoding (Encoding, String)

    the encoding or its string representation

Returns:

  • (String)

    the encoded string

Since:

  • 0.2.5



335
336
337
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 335

def encode(encoding)
  to_s.encode(encoding)
end

#fragment(&blk) ⇒ self

Define a HTML fragment

Examples:

html.fragment('Lotus') # => Lotus

html do
  p 'hello'
  p 'lotus'
end
# =>
  <p>hello</p>
  <p>lotus</p>

Parameters:

  • blk (Proc)

    the optional nested content espressed as a block

Returns:

  • (self)

See Also:

Since:

  • 0.2.6



257
258
259
260
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 257

def fragment(&blk)
  @nodes << HtmlFragment.new(&blk)
  self
end

#nested?TrueClass, FalseClass

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.

Check if there are nested nodes

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



345
346
347
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 345

def nested?
  @nodes.any?
end

#optionsObject

Since:

  • 0.1.0



186
187
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 186

def options
end

#resolve(&blk) ⇒ Object

Since:

  • 0.1.0



354
355
356
357
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 354

def resolve(&blk)
  @context = blk.binding.receiver
  instance_exec(&blk)
end

#tag(name, content = nil, attributes = nil, &blk) ⇒ self

Define a custom tag

Examples:

html.tag(:custom) # => <custom></custom>

html.tag(:custom, 'foo') # => <custom>foo</custom>

html.tag(:custom, html.p('hello')) # => <custom><p>hello</p></custom>

html.tag(:custom) { 'foo' }
# =>
#<custom>
#  foo
#</custom>

html.tag(:custom) do
  p 'hello'
end
# =>
#<custom>
#  <p>hello</p>
#</custom>

html.tag(:custom, 'hello', id: 'foo', 'data-xyz': 'bar') # => <custom id="foo" data-xyz="bar">hello</custom>

html.tag(:custom, id: 'foo') { 'hello' }
# =>
#<custom id="foo">
#  hello
#</custom>

Parameters:

  • name (Symbol, String)

    the name of the tag

  • content (String, Lotus::Helpers::HtmlHelper::HtmlBuilder, NilClass) (defaults to: nil)

    the optional content

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

  • blk (Proc)

    the optional nested content espressed as a block

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



231
232
233
234
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 231

def tag(name, content = nil, attributes = nil, &blk)
  @nodes << HtmlNode.new(name, blk || content, attributes || content, options)
  self
end

#text(content) ⇒ self Also known as: +

Defines a plain string of text. This particularly useful when you want to build more complex HTML.

Examples:


html.label do
  text "Option 1"
  radio_button :option, 1
end

# <label>
#   Option 1
#   <input type="radio" name="option" value="1" />
# </label>

Parameters:

  • content (String)

    the text to be rendered.

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



306
307
308
309
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 306

def text(content)
  @nodes << TextNode.new(content)
  self
end

#to_sLotus::Utils::Escape::SafeString

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.

Resolves all the nodes and generates the markup

Returns:

  • (Lotus::Utils::Escape::SafeString)

    the output

See Also:

Since:

  • 0.1.0



323
324
325
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 323

def to_s
  Utils::Escape::SafeString.new(@nodes.map(&:to_s).join(NEWLINE))
end