Class: Dommy::NodeList
- Inherits:
-
Array
- Object
- Array
- Dommy::NodeList
- Includes:
- Bridge::Methods
- Defined in:
- lib/dommy/node.rb
Overview
NodeList — Array sub-class that adds the DOM NodeList surface
(item(i) / forEach(cb) / entries / keys / values) on
top of regular Array operations. Returned from
querySelectorAll, getElementsBy*, childNodes, etc.
Live vs. static collections aren't distinguished here — Dommy snapshots tree state at the time of the query, matching what most happy-dom test patterns expect.
Direct Known Subclasses
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#entries ⇒ Object
NodeList
entriesreturns an enumerator of [index, value]. -
#for_each(&block) ⇒ Object
(also: #forEach)
Spec signature:
forEach(callback(value, key, listObj)). -
#item(index) ⇒ Object
Spec-compliant: out-of-range returns nil, not raise (Array#[] is close but we make negative indices fail too — DOM
item(-1)is nil, not Array#[-1]'s last element). - #keys ⇒ Object
-
#values ⇒ Object
valuesis the iterator of the NodeList itself; we returnself.to_a(a plain Array copy) so callers can't mutate the original list.
Methods included from Bridge::Methods
Instance Method Details
#__js_call__(method, args) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/dommy/node.rb', line 74 def __js_call__(method, args) case method when "item" item(args[0]) end end |
#__js_get__(key) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dommy/node.rb', line 52 def __js_get__(key) case key when "length" length else # Indexed getter: out-of-range yields JS `undefined` (item() returns null). if key.is_a?(Integer) || key.to_s.match?(/\A-?\d+\z/) token = item(key.to_i) token.nil? ? Bridge::UNDEFINED : token else Bridge::ABSENT # unknown non-index property end end end |
#entries ⇒ Object
NodeList entries returns an enumerator of [index, value].
37 38 39 |
# File 'lib/dommy/node.rb', line 37 def entries each_with_index.map { |value, index| [index, value] } end |
#for_each(&block) ⇒ Object Also known as: forEach
Spec signature: forEach(callback(value, key, listObj)). The
Ruby each_with_index block-arg order is (value, index), which
we re-yield as (value, index, self) for spec parity.
26 27 28 29 30 31 32 |
# File 'lib/dommy/node.rb', line 26 def for_each(&block) each_with_index do |value, index| block.call(value, index, self) end nil end |
#item(index) ⇒ Object
Spec-compliant: out-of-range returns nil, not raise (Array#[] is
close but we make negative indices fail too — DOM item(-1) is
nil, not Array#[-1]'s last element).
16 17 18 19 20 21 |
# File 'lib/dommy/node.rb', line 16 def item(index) i = index.to_i return nil if i < 0 || i >= length self[i] end |
#keys ⇒ Object
41 42 43 |
# File 'lib/dommy/node.rb', line 41 def keys (0...length).to_a end |
#values ⇒ Object
values is the iterator of the NodeList itself; we return
self.to_a (a plain Array copy) so callers can't mutate
the original list.
48 49 50 |
# File 'lib/dommy/node.rb', line 48 def values to_a end |