Class: Browserctl::Snapshot::RefDeriver
- Inherits:
-
Object
- Object
- Browserctl::Snapshot::RefDeriver
- Defined in:
- lib/browserctl/snapshot/ref.rb
Overview
Derives a stable element ref from semantic + structural signals.
The same DOM element should produce the same ref across two snapshots of the same page. Inputs to the hash are:
- role (explicit @role, else implicit ARIA role from tag)
- accessible name (aria-label || text || placeholder || alt)
- tag
- parent path (chain of ancestor tag names up to <html>)
Collisions within a single snapshot are disambiguated by the caller via ‘disambiguate(ref, taken)` — the deriver itself is pure.
Constant Summary collapse
- IMPLICIT_ROLE =
{ "a" => "link", "button" => "button", "input" => "textbox", "select" => "combobox", "textarea" => "textbox" }.freeze
- HASH_LEN =
7
Instance Method Summary collapse
- #derive(node) ⇒ Object
-
#disambiguate(ref, taken) ⇒ Object
Given a candidate ref and a set of already-taken refs in the current snapshot, return a unique ref.
Instance Method Details
#derive(node) ⇒ Object
26 27 28 29 |
# File 'lib/browserctl/snapshot/ref.rb', line 26 def derive(node) signal = [role(node), accessible_name(node), node.name, parent_path(node)].join("|") "e#{Digest::SHA256.hexdigest(signal)[0, HASH_LEN]}" end |
#disambiguate(ref, taken) ⇒ Object
Given a candidate ref and a set of already-taken refs in the current snapshot, return a unique ref. Adds ‘-2`, `-3`, … as needed.
33 34 35 36 37 38 39 |
# File 'lib/browserctl/snapshot/ref.rb', line 33 def disambiguate(ref, taken) return ref unless taken.include?(ref) n = 2 n += 1 while taken.include?("#{ref}-#{n}") "#{ref}-#{n}" end |