Class: Escapement::Tag

Inherits:
Object
  • Object
show all
Includes:
PrettyNames, Traversal
Defined in:
lib/escapement/tag.rb

Overview

A tag represents an entity that may or may not have child elements. Once we extract the data about this DOM node, we recursively continue the traversal until we reach the leaf text node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PrettyNames

#node_to_type

Methods included from Traversal

#process_children

Constructor Details

#initialize(node, start_position) ⇒ Tag

Returns a new instance of Tag.



11
12
13
14
15
# File 'lib/escapement/tag.rb', line 11

def initialize(node, start_position)
  @node = node
  @start_position = @current_position = start_position
  @entities = []
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



9
10
11
# File 'lib/escapement/tag.rb', line 9

def entities
  @entities
end

#nodeObject (readonly)

Returns the value of attribute node.



9
10
11
# File 'lib/escapement/tag.rb', line 9

def node
  @node
end

Instance Method Details

#processObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/escapement/tag.rb', line 17

def process
  @entities << {
    type: node_to_type,
    html_tag: node.name,
    position: [@current_position, @current_position + node.text.length],
    attributes: Hash[filtered_attributes.map { |k, v| [k, v.value] }]
  }

  process_children
end