class Browser::Element
Wrap a native DOM element
Public Class Methods
# 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
# File lib/browser/element.rb, line 20 def self.new(native) element = @tags[`(#{native}.tagName || '')`.downcase].allocate element.initialize native element end
@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
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
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
Set the specified attribute to the specified value
# File lib/browser/element.rb, line 148 def []= attribute, value `#@native.setAttribute(#{attribute}, #{value})` end
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
# File lib/browser/element/media.rb, line 4 def buffered TimeRanges.new(`#@native.buffered`) end
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
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
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
# File lib/browser/element/canvas.rb, line 4 def context(type='2d') Context.new(`#@native.getContext(#{type})`) end
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
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
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
# 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
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
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
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
# 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
# File lib/browser/element/media.rb, line 8 def played TimeRanges.new(`#@native.played`) end
# File lib/browser/element.rb, line 159 def query_selector selector result = super Element.new(result) if result end
# File lib/browser/element.rb, line 165 def query_selector_all selector Iterable.new(super).map { |element| Element.new(element) } end
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
# File lib/browser/element/media.rb, line 12 def seekable TimeRanges.new(`#@native.seekable`) end
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
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