Class: Dommy::DatasetMap

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/element.rb

Overview

‘Element#dataset` proxy. `el.dataset.fooBar` reads / writes `data-foo-bar` per the HTMLOrForeignElement.dataset spec (camelCase ↔ kebab-case round-trip).

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ DatasetMap

Returns a new instance of DatasetMap.



812
813
814
# File 'lib/dommy/element.rb', line 812

def initialize(element)
  @element = element
end

Instance Method Details

#__js_call__(_method, _args) ⇒ Object



831
832
833
# File 'lib/dommy/element.rb', line 831

def __js_call__(_method, _args)
  nil
end

#__js_delete__(key) ⇒ Object

Named deleter (‘delete el.dataset.foo`): removes the data-* attribute.



826
827
828
829
# File 'lib/dommy/element.rb', line 826

def __js_delete__(key)
  @element.remove_attribute(attr_name(key))
  true
end

#__js_get__(key) ⇒ Object



816
817
818
# File 'lib/dommy/element.rb', line 816

def __js_get__(key)
  @element.__dommy_backend_node__[attr_name(key)]
end

#__js_named_props__Object

WebIDL “supported property names” for DOMStringMap: each ‘data-*` attribute’s name with the ‘data-` prefix stripped and `-x` sequences camel-cased (`data-date-of-birth` → `dateOfBirth`, `data-` → “).



838
839
840
841
842
843
844
845
# File 'lib/dommy/element.rb', line 838

def __js_named_props__
  Backend.attribute_nodes(@element.__dommy_backend_node__).filter_map do |a|
    name = Backend.attribute_ns_info(a)[:qualified_name]
    next unless name.start_with?("data-")

    name.sub(/\Adata-/, "").gsub(/-([a-z])/) { ::Regexp.last_match(1).upcase }
  end
end

#__js_set__(key, value) ⇒ Object



820
821
822
823
# File 'lib/dommy/element.rb', line 820

def __js_set__(key, value)
  @element.set_attribute(attr_name(key), value.to_s)
  nil
end