Class: HamlLint::Tree::Node Abstract
- Inherits:
-
Object
- Object
- HamlLint::Tree::Node
- Defined in:
- lib/haml_lint/tree/node.rb
Overview
Decorator class that provides a convenient set of helpers for HAML’s Haml::Parser::ParseNode struct.
The goal is to abstract away the details of the underlying struct and provide a cleaner and more uniform interface for getting information about a node, as there are a number of weird/special cases in the struct returned by the HAML parser.
Direct Known Subclasses
CommentNode, DoctypeNode, FilterNode, HamlCommentNode, PlainNode, RootNode, ScriptNode, SilentScriptNode, TagNode
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#find {|node| ... } ⇒ HamlLint::Tree::Node?
Returns the first node found under the subtree which matches the given block.
-
#initialize(document, parse_node) ⇒ Node
constructor
Creates a node wrapping the given Haml::Parser::ParseNode struct.
- #inspect ⇒ Object
-
#next_node ⇒ HamlLint::Tree::Node?
Returns the next node that appears after this node in the document.
-
#source_code ⇒ String
Source code of all lines this node spans (excluding children).
-
#successor ⇒ HamlLint::Tree::Node?
Returns the node that follows this node, whether it be a sibling or an ancestor’s child, but not a child of this node.
-
#text ⇒ String
Returns the text content of this node.
Constructor Details
#initialize(document, parse_node) ⇒ Node
Creates a node wrapping the given Haml::Parser::ParseNode struct.
19 20 21 22 23 24 |
# File 'lib/haml_lint/tree/node.rb', line 19 def initialize(document, parse_node) @line = parse_node.line @document = document @value = parse_node.value @type = parse_node.type end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
12 13 14 |
# File 'lib/haml_lint/tree/node.rb', line 12 def children @children end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
13 14 15 |
# File 'lib/haml_lint/tree/node.rb', line 13 def line @line end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'lib/haml_lint/tree/node.rb', line 12 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/haml_lint/tree/node.rb', line 13 def type @type end |
Instance Method Details
#find {|node| ... } ⇒ HamlLint::Tree::Node?
Returns the first node found under the subtree which matches the given block.
Returns nil if no node matching the given block was found.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/haml_lint/tree/node.rb', line 34 def find(&block) return self if yield(self) children.each do |child| if result = child.find(&block) return result end end nil # Otherwise no matching node was found end |
#inspect ⇒ Object
62 63 64 |
# File 'lib/haml_lint/tree/node.rb', line 62 def inspect "#<#{self.class.name}>" end |
#next_node ⇒ HamlLint::Tree::Node?
Returns the next node that appears after this node in the document.
Returns nil if there is no next node.
88 89 90 |
# File 'lib/haml_lint/tree/node.rb', line 88 def next_node children.first || successor end |
#source_code ⇒ String
Source code of all lines this node spans (excluding children).
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/haml_lint/tree/node.rb', line 49 def source_code next_node_line = if next_node next_node.line - 1 else @document.source_lines.count + 1 end @document.source_lines[@line - 1...next_node_line] .join("\n") .gsub(/^\s*\z/m, '') # Remove blank lines at the end end |
#successor ⇒ HamlLint::Tree::Node?
Returns the node that follows this node, whether it be a sibling or an ancestor’s child, but not a child of this node.
If you are also willing to return the child, call #next_node.
Returns nil if there is no successor.
74 75 76 77 78 79 80 81 |
# File 'lib/haml_lint/tree/node.rb', line 74 def successor siblings = parent ? parent.children : [self] next_sibling = siblings[siblings.index(self) + 1] if siblings.count > 1 return next_sibling if next_sibling parent.successor if parent end |
#text ⇒ String
Returns the text content of this node.
95 96 97 |
# File 'lib/haml_lint/tree/node.rb', line 95 def text @value[:text].to_s end |