Class: Browser::Element
- 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
-
#==(other) ⇒ boolean
Determine whether this is the same element.
-
#[](attribute) ⇒ String
Return the specified attribute.
-
#[]=(attribute, value) ⇒ Object
Set the specified attribute to the specified value.
-
#append(node) ⇒ Object
Append the specified element as a child element.
-
#checked? ⇒ Boolean
A checkbox’s checked status.
-
#children ⇒ Array<Browser::Element>
This element’s direct child elements.
-
#clear ⇒ Browser::Element
Remove all contents from this element.
-
#empty? ⇒ Boolean
Determine whether this element has any contents.
-
#file ⇒ Browser::File
Get the currently selected file for this input.
-
#files ⇒ Browser::FileList
Get the currently selected files for this input.
-
#initialize(native) ⇒ Element
constructor
A new instance of Element.
-
#inner_dom=(element) ⇒ Object
Replace all child elements with the given element.
-
#inner_html ⇒ String
The contents of this element as an HTML string.
-
#inner_html=(html) ⇒ Object
Use the supplied HTML string to replace this element’s contents.
- #query_selector(selector) ⇒ Object
- #query_selector_all(selector) ⇒ Object
-
#remove_child(child) ⇒ Object
Remove the specified child element.
-
#to_n ⇒ JS
The native representation of this element.
-
#type ⇒ String
This element’s type.
Methods included from DelegateNative
#method_missing, #property_for_message, #respond_to_missing?
Methods included from EventTarget
Constructor Details
#initialize(native) ⇒ Element
Returns a new instance of Element.
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 *, &block .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
143 144 145 |
# File 'lib/browser/element.rb', line 143 def ==(other) `#@native === #{other.to_n}` end |
#[](attribute) ⇒ String
Return 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
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
120 121 122 |
# File 'lib/browser/element.rb', line 120 def checked? `!!#@native.checked` end |
#children ⇒ Array<Browser::Element>
This element’s direct child elements
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 |
#clear ⇒ Browser::Element
Remove all contents from this element. After this call, ‘empty?` will return `true`.
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
73 74 75 |
# File 'lib/browser/element.rb', line 73 def empty? `#@native.children.length === 0` end |
#file ⇒ Browser::File
Get the currently selected file for this input. This is only useful for file inputs without the ‘multiple` property set.
128 129 130 |
# File 'lib/browser/element.rb', line 128 def file files.first end |
#files ⇒ Browser::FileList
Get the currently selected files for this input. This is only useful for file inputs with the ‘multiple` property set.
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
35 36 37 38 |
# File 'lib/browser/element.rb', line 35 def inner_dom= element clear append element end |
#inner_html ⇒ String
The contents of this element as an HTML string
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
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
96 97 98 |
# File 'lib/browser/element.rb', line 96 def remove_child child `#@native.removeChild(child['native'] ? child['native'] : child)` end |
#to_n ⇒ JS
The native representation of this element.
172 173 174 |
# File 'lib/browser/element.rb', line 172 def to_n @native end |
#type ⇒ String
This element’s type. For example: “div”, “span”, “p”
103 104 105 |
# File 'lib/browser/element.rb', line 103 def type `#@native.nodeName`.downcase end |