Class: Browser::Element

Inherits:
Object show all
Includes:
DelegateNative, EventTarget
Defined in:
lib/browser/element.rb,
lib/browser/element/media.rb,
lib/browser/element/canvas.rb

Overview

Wrap a native DOM element

Defined Under Namespace

Classes: TimeRange, TimeRanges

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DelegateNative

#method_missing, #property_for_message, #respond_to_missing?

Methods included from EventTarget

#off, #on

Constructor Details

#initialize(native) ⇒ Element

Returns a new instance of Element.

Parameters:

  • native (JS)

    The native DOM element to wrap



27
28
29
# File 'lib/browser/element.rb', line 27

def initialize native
  @native = native
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Browser::DelegateNative

Class Method Details

.element(*tags, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/browser/element.rb', line 12

def self.element *tags, &block
  tags.each do |tag|
    @tags
      .fetch(tag) { @tags[tag] = const_set(tag.capitalize, Class.new(self)) }
      .class_exec(&block)
  end
end

.new(native) ⇒ Object



20
21
22
23
24
# File 'lib/browser/element.rb', line 20

def self.new(native)
  element = @tags[`(#{native}.tagName || '')`.downcase].allocate
  element.initialize native
  element
end

Instance Method Details

#==(other) ⇒ boolean

Determine whether this is the same element

Returns:

  • (boolean)

    true if the element is the same, false otherwise



143
144
145
# File 'lib/browser/element.rb', line 143

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

#[](attribute) ⇒ String

Return the specified attribute

Returns:

  • (String)

    the value for the specified attribute



155
156
157
# File 'lib/browser/element.rb', line 155

def [] attribute
  `#@native.getAttribute(#{attribute})`
end

#[]=(attribute, value) ⇒ Object

Set the specified attribute to the specified value



148
149
150
# File 'lib/browser/element.rb', line 148

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

#append(node) ⇒ Object

Append the specified element as a child element

Parameters:



110
111
112
113
# File 'lib/browser/element.rb', line 110

def append node
  `#@native.appendChild(node['native'] ? node['native'] : node)`
  self
end

#checked?Boolean

A checkbox’s checked status

Returns:

  • (Boolean)

    true if the checkbox is checked, false otherwise



120
121
122
# File 'lib/browser/element.rb', line 120

def checked?
  `!!#@native.checked`
end

#childrenArray<Browser::Element>

This element’s direct child elements

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/browser/element.rb', line 57

def children
  elements = []

  %x{
    var children = #@native.children;
    for(var i = 0; i < children.length; i++) {
      elements[i] = #{Element.new(`children[i]`)};
    }
  }

  elements
end

#clearBrowser::Element

Remove all contents from this element. After this call, ‘empty?` will return `true`.

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/browser/element.rb', line 81

def clear
  if %w(input textarea).include? type
    `#@native.value = null`
  else
    children.each do |child|
      remove_child child
    end
  end

  self
end

#empty?Boolean

Determine whether this element has any contents

Returns:

  • (Boolean)

    true if the element has no children, false otherwise



73
74
75
# File 'lib/browser/element.rb', line 73

def empty?
  `#@native.children.length === 0`
end

#fileBrowser::File

Get the currently selected file for this input. This is only useful for file inputs without the ‘multiple` property set.

Returns:

  • (Browser::File)

    the file selected by the user



128
129
130
# File 'lib/browser/element.rb', line 128

def file
  files.first
end

#filesBrowser::FileList

Get the currently selected files for this input. This is only useful for file inputs with the ‘multiple` property set.

Returns:



136
137
138
# File 'lib/browser/element.rb', line 136

def files
  FileList.new(`#@native.files`)
end

#inner_dom=(element) ⇒ Object

Replace all child elements with the given element

Parameters:

  • element (Browser::Element)

    The Browser element with which to replace this element’s contents



35
36
37
38
# File 'lib/browser/element.rb', line 35

def inner_dom= element
  clear
  append element
end

#inner_htmlString

The contents of this element as an HTML string

Returns:

  • (String)

    the HTML representation of this element’s contents



43
44
45
# File 'lib/browser/element.rb', line 43

def inner_html
  `#@native.innerHTML`
end

#inner_html=(html) ⇒ Object

Use the supplied HTML string to replace this element’s contents

Parameters:

  • html (String)

    the HTML with which to replace this elements contents



50
51
52
# File 'lib/browser/element.rb', line 50

def inner_html= html
  `#@native.innerHTML = html`
end

#query_selector(selector) ⇒ Object



159
160
161
162
163
# File 'lib/browser/element.rb', line 159

def query_selector selector
  result = super

  Element.new(result) if result
end

#query_selector_all(selector) ⇒ Object



165
166
167
# File 'lib/browser/element.rb', line 165

def query_selector_all selector
  Iterable.new(super).map { |element| Element.new(element) }
end

#remove_child(child) ⇒ Object

Remove the specified child element

Parameters:



96
97
98
# File 'lib/browser/element.rb', line 96

def remove_child child
  `#@native.removeChild(child['native'] ? child['native'] : child)`
end

#to_nJS

The native representation of this element.

Returns:

  • (JS)

    the native element wrapped by this object.



172
173
174
# File 'lib/browser/element.rb', line 172

def to_n
  @native
end

#typeString

This element’s type. For example: “div”, “span”, “p”

Returns:

  • (String)

    the HTML tag name for this element



103
104
105
# File 'lib/browser/element.rb', line 103

def type
  `#@native.nodeName`.downcase
end