Class: Philiprehberger::HtmlBuilder::Node
- Inherits:
-
Object
- Object
- Philiprehberger::HtmlBuilder::Node
- Defined in:
- lib/philiprehberger/html_builder/node.rb
Overview
Represents an HTML element node with tag, attributes, and children
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The element attributes.
-
#children ⇒ Array
readonly
The child nodes.
-
#tag ⇒ Symbol
readonly
The tag name.
Instance Method Summary collapse
-
#add_child(child) ⇒ void
Add a child node.
-
#initialize(tag, attributes: {}) ⇒ Node
constructor
A new instance of Node.
-
#to_html(indent: nil, indent_size: 2) ⇒ String
Render the node to an HTML string.
Constructor Details
#initialize(tag, attributes: {}) ⇒ Node
Returns a new instance of Node.
18 19 20 21 22 |
# File 'lib/philiprehberger/html_builder/node.rb', line 18 def initialize(tag, attributes: {}) @tag = tag @attributes = attributes @children = [] end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns the element attributes.
11 12 13 |
# File 'lib/philiprehberger/html_builder/node.rb', line 11 def attributes @attributes end |
#children ⇒ Array (readonly)
Returns the child nodes.
14 15 16 |
# File 'lib/philiprehberger/html_builder/node.rb', line 14 def children @children end |
#tag ⇒ Symbol (readonly)
Returns the tag name.
8 9 10 |
# File 'lib/philiprehberger/html_builder/node.rb', line 8 def tag @tag end |
Instance Method Details
#add_child(child) ⇒ void
This method returns an undefined value.
Add a child node
28 29 30 |
# File 'lib/philiprehberger/html_builder/node.rb', line 28 def add_child(child) @children << child end |
#to_html(indent: nil, indent_size: 2) ⇒ String
Render the node to an HTML string
37 38 39 40 41 42 43 44 45 |
# File 'lib/philiprehberger/html_builder/node.rb', line 37 def to_html(indent: nil, indent_size: 2) flat_attrs = flatten_attributes(attributes) if indent render_pretty(flat_attrs, indent: indent, indent_size: indent_size) else render_inline(flat_attrs) end end |