class Browser::Element

Wrap a native DOM element

Public Class Methods

element(*tags, &block) click to toggle source
# 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) click to toggle source
# File lib/browser/element.rb, line 20
def self.new(native)
  element = @tags[`(#{native}.tagName || '')`.downcase].allocate
  element.initialize native
  element
end
new(native) click to toggle source

@param native [JS] The native DOM element to wrap

# File lib/browser/element.rb, line 27
def initialize native
  @native = native
end

Public Instance Methods

==(other) click to toggle source

Determine whether this is the same element

@return [boolean] true if the element is the same, false otherwise

# File lib/browser/element.rb, line 143
def ==(other)
  `#@native === #{other.to_n}`
end
[](attribute) click to toggle source

Return the specified attribute

@return [String] the value for the specified attribute

# File lib/browser/element.rb, line 155
def [] attribute
  `#@native.getAttribute(#{attribute})`
end
[]=(attribute, value) click to toggle source

Set the specified attribute to the specified value

# File lib/browser/element.rb, line 148
def []= attribute, value
  `#@native.setAttribute(#{attribute}, #{value})`
end
append(node) click to toggle source

Append the specified element as a child element

@param element [Browser::Element, JS] the element to insert

# File lib/browser/element.rb, line 110
def append node
  `#@native.appendChild(node['native'] ? node['native'] : node)`
  self
end
buffered() click to toggle source
# File lib/browser/element/media.rb, line 4
def buffered
  TimeRanges.new(`#@native.buffered`)
end
checked?() click to toggle source

A checkbox's checked status

@return [Boolean] true if the checkbox is checked, false otherwise

# File lib/browser/element.rb, line 120
def checked?
  `!!#@native.checked`
end
children() click to toggle source

This element's direct child elements

@return [Array<Browser::Element>] list of this element's children

# 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
clear() click to toggle source

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

@return [Browser::Element] self

# 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
context(type='2d') click to toggle source
# File lib/browser/element/canvas.rb, line 4
def context(type='2d')
  Context.new(`#@native.getContext(#{type})`)
end
empty?() click to toggle source

Determine whether this element has any contents

@return [Boolean] true if the element has no children, false otherwise

# File lib/browser/element.rb, line 73
def empty?
  `#@native.children.length === 0`
end
file() click to toggle source

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

@return [Browser::File] the file selected by the user

# File lib/browser/element.rb, line 128
def file
  files.first
end
files() click to toggle source

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

@return [Browser::FileList] the currently selected files for this input

# File lib/browser/element.rb, line 136
def files
  FileList.new(`#@native.files`)
end
fullscreen() click to toggle source
Calls superclass method
# File lib/browser/element/media.rb, line 27
def fullscreen
  fullscreen = %w(
    requestFullScreen
    requestFullscreen
    webkitRequestFullScreen
    webkitRequestFullscreen
    mozRequestFullScreen
    msRequestFullscreen
  ).find { |prop| `!!#@native[prop]` }

  if fullscreen
    `#@native[fullscreen]()`
  else
    warn "[#{self.class}] Cannot determine the method to full-screen a video"
    super
  end
end
Also aliased as: request_fullscreen
inner_dom=(element) click to toggle source

Replace all child elements with the given element

@param element [Browser::Element] The Browser element with which to replace

this element's contents
# File lib/browser/element.rb, line 35
def inner_dom= element
  clear
  append element
end
inner_html() click to toggle source

The contents of this element as an HTML string

@return [String] the HTML representation of this element's contents

# File lib/browser/element.rb, line 43
def inner_html
  `#@native.innerHTML`
end
inner_html=(html) click to toggle source

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

@param html [String] the HTML with which to replace this elements contents

# File lib/browser/element.rb, line 50
def inner_html= html
  `#@native.innerHTML = html`
end
network_state() click to toggle source
# File lib/browser/element/media.rb, line 16
def network_state
  case `#@native.networkState`
  when `HTMLMediaElement.NETWORK_EMPTY` then :no_data
  when `HTMLMediaElement.NETWORK_IDLE` then :idle
  when `HTMLMediaElement.NETWORK_LOADING` then :loading
  when `HTMLMediaElement.NETWORK_NO_SOURCE` then :no_source
  end
end
played() click to toggle source
# File lib/browser/element/media.rb, line 8
def played
  TimeRanges.new(`#@native.played`)
end
query_selector(selector) click to toggle source
Calls superclass method
# File lib/browser/element.rb, line 159
def query_selector selector
  result = super

  Element.new(result) if result
end
query_selector_all(selector) click to toggle source
Calls superclass method
# File lib/browser/element.rb, line 165
def query_selector_all selector
  Iterable.new(super).map { |element| Element.new(element) }
end
remove_child(child) click to toggle source

Remove the specified child element

@param child [Browser::Element] the child element to remove

# File lib/browser/element.rb, line 96
def remove_child child
  `#@native.removeChild(child['native'] ? child['native'] : child)`
end
request_fullscreen()
Alias for: fullscreen
seekable() click to toggle source
# File lib/browser/element/media.rb, line 12
def seekable
  TimeRanges.new(`#@native.seekable`)
end
to_n() click to toggle source

The native representation of this element.

@return [JS] the native element wrapped by this object.

# File lib/browser/element.rb, line 172
def to_n
  @native
end
type() click to toggle source

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

@return [String] the HTML tag name for this element

# File lib/browser/element.rb, line 103
def type
  `#@native.nodeName`.downcase
end