Class: Opal::RSpec::Element

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/rspec/formatter/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(el, attrs = {}) ⇒ Element

Returns a new instance of Element.



20
21
22
23
24
25
26
27
28
# File 'opal/opal/rspec/formatter/element.rb', line 20

def initialize(el, attrs={})
  if String === el
    @native = `document.createElement(el)`
  else
    @native = el
  end

  attrs.each { |name, val| __send__ "#{name}=", val }
end

Instance Attribute Details

#nativeObject (readonly)

Returns the value of attribute native.



4
5
6
# File 'opal/opal/rspec/formatter/element.rb', line 4

def native
  @native
end

Class Method Details

.from_string(str) ⇒ Object



14
15
16
17
18
# File 'opal/opal/rspec/formatter/element.rb', line 14

def self.from_string(str)
  dummy_div = `document.createElement('div')`
  `#{dummy_div}.innerHTML = #{str}`
  new(`#{dummy_div}.children[0]`)
end

.id(id) ⇒ Object



6
7
8
# File 'opal/opal/rspec/formatter/element.rb', line 6

def self.id(id)
  new(`document.getElementById(id)`)
end

.klass(klass) ⇒ Object



10
11
12
# File 'opal/opal/rspec/formatter/element.rb', line 10

def self.klass(klass)
  new(`document.getElementsByClassName(#{klass})[0]`)
end

Instance Method Details

#append(child) ⇒ Object Also known as: <<



74
75
76
# File 'opal/opal/rspec/formatter/element.rb', line 74

def append(child)
  `#@native.appendChild(#{child.native})`
end

#append_to_headObject



95
96
97
# File 'opal/opal/rspec/formatter/element.rb', line 95

def append_to_head
  `document.getElementsByTagName('head')[0].appendChild(#@native)`
end

#class_nameObject



30
31
32
# File 'opal/opal/rspec/formatter/element.rb', line 30

def class_name
  `#@native.className`
end

#class_name=(name) ⇒ Object



46
47
48
# File 'opal/opal/rspec/formatter/element.rb', line 46

def class_name=(name)
  `#@native.className = #{name}`
end

#css_text=(text) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'opal/opal/rspec/formatter/element.rb', line 80

def css_text=(text)
  %x{
              if (#@native.styleSheet) {
                #@native.styleSheet.cssText = #{text};
              }
              else {
                #@native.appendChild(document.createTextNode(#{text}));
              }
            }
end

#get_child_by_tag_name(tag, index = 0) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'opal/opal/rspec/formatter/element.rb', line 34

def get_child_by_tag_name(tag, index=0)
  elements = `#@native.getElementsByTagName(#{tag})`
  # is an HTMLCollection, not an array
  element_array = []
  %x{
    for (var i=0; i < #{elements}.length; i++) {
      #{element_array}.push(#{elements}[i]);
    }
  }
  Element.new(element_array[index])
end

#html=(html) ⇒ Object



62
63
64
# File 'opal/opal/rspec/formatter/element.rb', line 62

def html=(html)
  `#@native.innerHTML = #{html}`
end

#on_click=(lambda) ⇒ Object



58
59
60
# File 'opal/opal/rspec/formatter/element.rb', line 58

def on_click=(lambda)
  `#@native.onclick = #{lambda}`
end

#outer_htmlObject



54
55
56
# File 'opal/opal/rspec/formatter/element.rb', line 54

def outer_html
  `#@native.outerHTML`
end

#style(name, value) ⇒ Object



91
92
93
# File 'opal/opal/rspec/formatter/element.rb', line 91

def style(name, value)
  `#@native.style[#{name}] = value`
end

#text=(text) ⇒ Object



66
67
68
# File 'opal/opal/rspec/formatter/element.rb', line 66

def text=(text)
  self.html = text.gsub(/</, '&lt').gsub(/>/, '&gt')
end

#type=(type) ⇒ Object



70
71
72
# File 'opal/opal/rspec/formatter/element.rb', line 70

def type=(type)
  `#@native.type = #{type}`
end