Class: Rxhp::HtmlElement
- 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:
-
subclass
-
define #tag_name
-
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
Instance Attribute Summary
Attributes inherited from Element
Instance Method Summary collapse
-
#render(options = {}) ⇒ Object
Render the element.
-
#render_children(options = {}) ⇒ Object
Render child elements.
-
#tag_name ⇒ Object
Literal string to include in render output.
-
#validate! ⇒ Object
Check that the element usage does nto have detectable errors.
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 = {} validate! = () open = render_open_tag() inner = render_children() close = render_close_tag() if [:pretty] indent = ' ' * ([:indent] * [: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 = {} = .dup [:depth] += 1 super end |
#tag_name ⇒ Object
Literal string to include in render output.
For example, ‘html’ will lead to ‘<html>…</html>’.
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 |