Class: Dommy::NodeList
- Inherits:
-
Array
- Object
- Array
- Dommy::NodeList
- 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.
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#entries ⇒ Object
NodeList ‘entries` returns 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#’s last element).
- #keys ⇒ Object
-
#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.
Instance Method Details
#__js_call__(method, args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dommy/node.rb', line 63 def __js_call__(method, args) case method when "item" item(args[0]) when "forEach" for_each(&args[0]) when "entries" entries when "keys" keys when "values" values end end |
#__js_get__(key) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dommy/node.rb', line 52 def __js_get__(key) case key when "length" length else if key.is_a?(Integer) || key.to_s.match?(/\A\d+\z/) item(key.to_i) 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#’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 |