Class: Markbridge::AST::Element
- Defined in:
- lib/markbridge/ast/element.rb
Overview
Base class for all AST elements that can contain children. Elements form the structural nodes of the AST tree, while Text nodes are leaves.
Direct Known Subclasses
Align, Bold, Code, Color, Document, Email, Heading, Image, Italic, List, ListItem, Paragraph, Quote, Size, Spoiler, Strikethrough, Subscript, Superscript, Table, TableCell, TableRow, Underline, Url
Instance Attribute Summary collapse
-
#children ⇒ Array<Node>
readonly
The child nodes of this element.
Instance Method Summary collapse
-
#<<(child) ⇒ Element
Add a child node to this element.
-
#initialize ⇒ Element
constructor
A new instance of Element.
Constructor Details
#initialize ⇒ Element
Returns a new instance of Element.
17 18 19 |
# File 'lib/markbridge/ast/element.rb', line 17 def initialize @children = [] end |
Instance Attribute Details
#children ⇒ Array<Node> (readonly)
Returns the child nodes of this element.
15 16 17 |
# File 'lib/markbridge/ast/element.rb', line 15 def children @children end |
Instance Method Details
#<<(child) ⇒ Element
Add a child node to this element. Consecutive Text nodes are automatically merged for optimization.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/markbridge/ast/element.rb', line 31 def <<(child) unless child.is_a?(Node) actual = child.nil? ? "nil" : child.class raise TypeError, "child must be a #{Markbridge::AST::Node} (got #{actual})" end if child.is_a?(Text) && children.last.is_a?(Text) @children.last.merge(child) else @children << child end self end |