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.
Constant Summary collapse
- JS_METHOD_NAMES =
Methods routed through js_call (keep in sync with its when-arms).
%w[item forEach entries keys values].freeze
Instance Method Summary collapse
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_method_names__ ⇒ 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dommy/node.rb', line 69 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dommy/node.rb', line 58 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 |
#__js_method_names__ ⇒ Object
15 16 17 |
# File 'lib/dommy/node.rb', line 15 def __js_method_names__ JS_METHOD_NAMES end |
#entries ⇒ Object
NodeList ‘entries` returns an enumerator of [index, value].
43 44 45 |
# File 'lib/dommy/node.rb', line 43 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.
32 33 34 35 36 37 38 |
# File 'lib/dommy/node.rb', line 32 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).
22 23 24 25 26 27 |
# File 'lib/dommy/node.rb', line 22 def item(index) i = index.to_i return nil if i < 0 || i >= length self[i] end |
#keys ⇒ Object
47 48 49 |
# File 'lib/dommy/node.rb', line 47 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.
54 55 56 |
# File 'lib/dommy/node.rb', line 54 def values to_a end |