Class: Rxhp::HtmlElement

Inherits:
Element show all
Includes:
AttributeValidator
Defined in:
lib/rxhp/html_element.rb

Overview

Base class for ‘real’ elements that actually end up in the markup.

For example, <span> is a subclass of this.

To use:

  1. subclass

  2. define #tag_name

  3. call Rxhp::Scope.define_element(‘foo’, Foo, MyModule) to register the class

… or just add a define_tag line to html.rb.

There’s another two base classes for special types of html elements:

HtmlSelfClosingElement:

elements where in HTML, the closing tag is optional - for example, <p> and <li>

HtmlSingletonElement:

not only is the closing tag optional, but child elements are forbidden - for example,
and <img>

These can also be defined via Rxhp::Html#define_tag

If you’re making a custom element that’s purely server-side (i.e. is just composed of HTML elements), you want to subclass ComposableElement instead.

Direct Known Subclasses

HtmlSelfClosingElement, HtmlSingletonElement

Instance Attribute Summary

Attributes inherited from Element

#attributes, #children

Instance Method Summary collapse

Methods included from AttributeValidator

match?, #valid_attributes?, #validate_attributes!

Methods inherited from Element

#children?, #fill_options, #initialize, #valid?

Methods included from Scope

current, define_element, #fragment, with_parent

Constructor Details

This class inherits a constructor from Rxhp::Element

Instance Method Details

#render(options = {}) ⇒ Object

Render the element.

Pays attention to the formatter type, doctype, pretty print options, etc. See Element#render for options.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rxhp/html_element.rb', line 54

def render options = {}
  validate!
  options = fill_options(options)

  open = render_open_tag(options)
  inner = render_children(options)
  close = render_close_tag(options)

  if options[:pretty]
    indent = ' ' * (options[:indent] * options[:depth])
    out = indent.dup
    out << open << "\n"
    out << inner if inner
    out << indent << close << "\n" if close && !close.empty?
    out
  else
    out = open
    out << inner if inner
    out << close if close
    out
  end
end

#render_children(options = {}) ⇒ Object

Render child elements.

Increases the depth count for the sake of pretty printing.



80
81
82
83
84
# File 'lib/rxhp/html_element.rb', line 80

def render_children options = {}
  child_options = options.dup
  child_options[:depth] += 1
  super child_options
end

#tag_nameObject

Literal string to include in render output.

For example, ‘html’ will lead to ‘<html>…</html>’.

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/rxhp/html_element.rb', line 38

def tag_name
  raise NotImplementedError.new
end

#validate!Object

Check that the element usage does nto have detectable errors.

At the moment, this just checks for attribute correctness.



45
46
47
48
# File 'lib/rxhp/html_element.rb', line 45

def validate!
  super
  validate_attributes!
end