Class: Marta::ElementInformation::ElementHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/marta/element_information.rb

Overview

Note:

It is believed that no user will use it

We are using helper class which can parse element attributes to our special hash format.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(requestor) ⇒ ElementHelper

Returns a new instance of ElementHelper.



16
17
18
# File 'lib/marta/element_information.rb', line 16

def initialize(requestor)
  @engine = requestor.engine
end

Class Method Details

.method_structure(collection = false) ⇒ Object

That class is also stores an empty special format hash.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/marta/element_information.rb', line 49

def self.method_structure(collection = false)
  return {'options' => {'collection' => collection},
           'positive' => {
             'self' => {
               'text'=>[], 'tag' => [], 'attributes' => {}},
              'pappy' => {
                'text'=>[], 'tag' => [], 'attributes' => {}},
              'granny' => {
                'text'=>[], 'tag' => [], 'attributes' => {}}},
            'negative' => {
              'self' => {
                'text'=>[], 'tag' => [], 'attributes' => {}},
               'pappy' => {
                 'text'=>[], 'tag' => [], 'attributes' => {}},
               'granny' => {
                 'text'=>[], 'tag' => [], 'attributes' => {}}}
              }
end

Instance Method Details

#get_element_info(element, parent_count = 0) ⇒ Object

We can get data of the element or data of any parent.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/marta/element_information.rb', line 21

def get_element_info(element, parent_count = 0)
  parent = ''
  parent_count.times do
    parent = parent + '.parentElement'
  end
  result = Hash.new
  attr_script = %Q[
      var s = {};
      var attrs = arguments[0]#{parent}.attributes;
      for (var l = 0; l < attrs.length; ++l) {
          var a = attrs[l]; s[a.name] = a.value.split(" ");
      } ;
      return s;]
  tag_script = "return arguments[0]#{parent}.tagName"
  text_script = %Q[
  if (arguments[0]#{parent}.textContent == arguments[0]#{parent}.innerHTML)
     {return arguments[0]#{parent}.textContent} else {return ''};]
  result['tag'] = [@engine.execute_script(tag_script, element)]
  txt = @engine.execute_script(text_script, element)
  result['text'] = txt != '' ? [txt] : []
  result['attributes'] = @engine.execute_script(attr_script, element)
  result['attributes'].each_pair do |attribute, value|
    value.uniq!
  end
  return result
end